Free cookie consent management tool by TermsFeed Policy Generator

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

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

implemented the server on the client, using push & force push, added refresh buttons, added auto calender methods that traverse the tree... (#908)

File size: 3.6 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    private void PersistToHDD() {
38      XmlSerializer s = new XmlSerializer(typeof(AppointmentContainer));
39      if (!Directory.Exists(path))
40        Directory.CreateDirectory(path);
41      TextWriter w = null;
42      try {
43        w = new StreamWriter(path + "calendar.xml");
44        s.Serialize(w, AppContainer);
45      }
46      catch (Exception e) {
47        Logging.Instance.Error(this.ToString(), "Persistance of the Calendar failed!", e);
48      }
49      finally {
50        if (w != null)
51          w.Close();
52      }
53    }
54
55    private void RestoreFromHDD() {
56      XmlSerializer s = new XmlSerializer(typeof(AppointmentContainer));
57      if (File.Exists(Path.Combine(path, "calendar.xml"))) {
58        TextReader r = null;
59
60        try {
61          r = new StreamReader(path + "calendar.xml");
62          _appContainer = (AppointmentContainer)s.Deserialize(r);
63          CalendarAvailable = true;
64        }
65        catch (Exception e) {
66          Logging.Instance.Error(this.ToString(), "Deserialization of Calendar failed", e);
67          Logging.Instance.Info(this.ToString(), "Starting with a new one");
68          _appContainer = new AppointmentContainer();
69          CalendarAvailable = false;
70        }
71        finally {
72          if (r != null)
73            r.Close();
74        }
75      } else {
76        _appContainer = new AppointmentContainer();
77      }
78    }
79
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);
113    }
114  }
115}
Note: See TracBrowser for help on using the repository browser.