Changeset 3203 for trunk/sources/HeuristicLab.Hive.Client.Core/3.2/ConfigurationManager/UptimeManager.cs
- Timestamp:
- 03/23/10 10:50:26 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Hive.Client.Core/3.2/ConfigurationManager/UptimeManager.cs
r2025 r3203 7 7 using System.IO; 8 8 using HeuristicLab.Hive.Client.Common; 9 using HeuristicLab.Hive.Contracts.BusinessObjects; 10 using HeuristicLab.Hive.Contracts; 9 11 10 12 namespace HeuristicLab.Hive.Client.Core.ConfigurationManager { 11 13 public class UptimeManager { 12 14 13 private List<Appointment> appointments= null;14 public List<Appointment> Appointments{15 private AppointmentContainer _appContainer = null; 16 public AppointmentContainer AppContainer { 15 17 get { 16 if ( appointments== null)18 if (_appContainer == null) 17 19 RestoreFromHDD(); 18 return appointments;20 return _appContainer; 19 21 } 20 set { 21 appointments = value; 22 PersistToHDD(); 23 } 24 } 25 private static String path = System.IO.Directory.GetCurrentDirectory()+"\\plugins\\Hive.Client.Jobs\\"; 22 } 23 public bool CalendarAvailable { get; set; } 24 25 private static String path = System.IO.Directory.GetCurrentDirectory() + "\\plugins\\Hive.Client.Jobs\\"; 26 26 27 27 private static UptimeManager instance = null; … … 36 36 37 37 private void PersistToHDD() { 38 XmlSerializer s = new XmlSerializer(typeof( List<Appointment>));38 XmlSerializer s = new XmlSerializer(typeof(AppointmentContainer)); 39 39 if (!Directory.Exists(path)) 40 40 Directory.CreateDirectory(path); … … 42 42 try { 43 43 w = new StreamWriter(path + "calendar.xml"); 44 s.Serialize(w, Appointments); 45 } catch(Exception e) { 44 s.Serialize(w, AppContainer); 45 } 46 catch (Exception e) { 46 47 Logging.Instance.Error(this.ToString(), "Persistance of the Calendar failed!", e); 47 } finally { 48 if(w!=null) 48 } 49 finally { 50 if (w != null) 49 51 w.Close(); 50 52 } … … 52 54 53 55 private void RestoreFromHDD() { 54 XmlSerializer s = new XmlSerializer(typeof( List<Appointment>));55 if (File.Exists(Path.Combine(path, "calendar.xml"))) {56 XmlSerializer s = new XmlSerializer(typeof(AppointmentContainer)); 57 if (File.Exists(Path.Combine(path, "calendar.xml"))) { 56 58 TextReader r = null; 57 59 58 60 try { 59 61 r = new StreamReader(path + "calendar.xml"); 60 Appointments = (List<Appointment>)s.Deserialize(r); 61 } catch (Exception e) { 62 _appContainer = (AppointmentContainer)s.Deserialize(r); 63 CalendarAvailable = true; 64 } 65 catch (Exception e) { 62 66 Logging.Instance.Error(this.ToString(), "Deserialization of Calendar failed", e); 63 67 Logging.Instance.Info(this.ToString(), "Starting with a new one"); 64 appointments = new List<Appointment>(); 65 } finally { 66 if(r!=null) 67 r.Close(); 68 _appContainer = new AppointmentContainer(); 69 CalendarAvailable = false; 70 } 71 finally { 72 if (r != null) 73 r.Close(); 68 74 } 69 75 } else { 70 Appointments = new List<Appointment>();76 _appContainer = new AppointmentContainer(); 71 77 } 72 78 } 73 79 74 public bool isOnline() { 75 foreach (Appointment app in Appointments) 76 if ((DateTime.Now >= app.StartDate) && 77 (DateTime.Now <= app.EndDate)) 78 return true; 79 return false; 80 public bool IsOnline() { 81 return AppContainer.Appointments.Any(app => (DateTime.Now >= app.StartDate) && (DateTime.Now <= app.EndDate)); 82 } 83 84 public bool SetAppointments(bool isLocal, bool isForced, IEnumerable<Appointment> appointments) { 85 if (!isForced && !isLocal && AppContainer.IsLocal) 86 return false; 87 88 AppContainer.Appointments = new List<Appointment>(appointments); 89 AppContainer.IsLocal = isLocal; 90 AppContainer.Updated = DateTime.Now; 91 CalendarAvailable = true; 92 93 PersistToHDD(); 94 95 return true; 96 } 97 98 internal bool SetAppointments(bool isLocal, ResponseCalendar response) { 99 IList<Appointment> app = new List<Appointment>(); 100 foreach (AppointmentDto appointmentDto in response.Appointments) { 101 app.Add(new Appointment { 102 AllDayEvent = appointmentDto.AllDayEvent, 103 EndDate = appointmentDto.EndDate, 104 StartDate = appointmentDto.StartDate, 105 Recurring = appointmentDto.Recurring, 106 107 RecurringId = appointmentDto.RecurringId, 108 Locked = true, 109 Subject = "Online", 110 }); 111 } 112 return SetAppointments(isLocal, response.ForceFetch, app); 80 113 } 81 114 }
Note: See TracChangeset
for help on using the changeset viewer.