Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.Hive.Client.Core/3.2/ConfigurationManager/UptimeManager.cs @ 4498

Last change on this file since 4498 was 2025, checked in by kgrading, 15 years ago

added calendar behavior in the whole application (#669)

File size: 2.4 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;
9
10namespace HeuristicLab.Hive.Client.Core.ConfigurationManager {
11  public class UptimeManager {
12
13    private List<Appointment> appointments = null;
14    public List<Appointment> Appointments {
15      get {
16        if (appointments == null)
17          RestoreFromHDD();
18        return appointments;
19      }
20      set {
21        appointments = value;
22        PersistToHDD();
23      }
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(List<Appointment>));
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, Appointments); 
45      } catch(Exception e) {
46        Logging.Instance.Error(this.ToString(), "Persistance of the Calendar failed!", e);
47      } finally {
48        if(w!=null)
49          w.Close();
50      }
51    }
52
53    private void RestoreFromHDD() {
54      XmlSerializer s = new XmlSerializer(typeof(List<Appointment>));
55      if(File.Exists(Path.Combine(path, "calendar.xml"))) {
56        TextReader r = null;
57       
58        try {
59          r = new StreamReader(path + "calendar.xml");
60          Appointments = (List<Appointment>)s.Deserialize(r);       
61        } catch (Exception e) {
62          Logging.Instance.Error(this.ToString(), "Deserialization of Calendar failed", e);
63          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        }
69      } else {
70        Appointments = new List<Appointment>();
71      }
72    }
73
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    }
81  }
82}
Note: See TracBrowser for help on using the repository browser.