Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Client.Core/3.2/ConfigurationManager/UptimeManager.cs @ 3220

Last change on this file since 3220 was 3220, checked in by kgrading, 14 years ago

improved the DAL further, changed minor details for the presentation (#830)

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