1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.IO;
|
---|
25 | using System.Linq;
|
---|
26 | using System.Xml.Serialization;
|
---|
27 | using HeuristicLab.Calendar;
|
---|
28 | using HeuristicLab.Hive.Contracts.BusinessObjects;
|
---|
29 | using HeuristicLab.Hive.Contracts.ResponseObjects;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Hive.Slave.Core.ConfigurationManager {
|
---|
32 | public class UptimeManager {
|
---|
33 |
|
---|
34 | private AppointmentContainer appContainer = null;
|
---|
35 | public AppointmentContainer AppContainer {
|
---|
36 | get {
|
---|
37 | if (appContainer == null)
|
---|
38 | RestoreFromHDD();
|
---|
39 | return appContainer;
|
---|
40 | }
|
---|
41 | }
|
---|
42 | public bool CalendarAvailable { get; set; }
|
---|
43 |
|
---|
44 | private static String path = System.IO.Directory.GetCurrentDirectory() + "\\plugins\\Hive.Slave.Jobs\\";
|
---|
45 |
|
---|
46 | private static UptimeManager instance = null;
|
---|
47 | public static UptimeManager Instance {
|
---|
48 | get {
|
---|
49 | if (instance == null) {
|
---|
50 | instance = new UptimeManager();
|
---|
51 | }
|
---|
52 | return instance;
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | public UptimeManager() {
|
---|
57 | RestoreFromHDD();
|
---|
58 | }
|
---|
59 |
|
---|
60 | private void PersistToHDD() {
|
---|
61 | XmlSerializer s = new XmlSerializer(typeof(AppointmentContainer));
|
---|
62 | if (!Directory.Exists(path))
|
---|
63 | Directory.CreateDirectory(path);
|
---|
64 | TextWriter w = null;
|
---|
65 | try {
|
---|
66 | w = new StreamWriter(path + "calendar.xml");
|
---|
67 | s.Serialize(w, AppContainer);
|
---|
68 | }
|
---|
69 | catch (Exception e) {
|
---|
70 | Logger.Error("Persistance of the Calendar failed!", e);
|
---|
71 | }
|
---|
72 | finally {
|
---|
73 | if (w != null)
|
---|
74 | w.Close();
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | private void RestoreFromHDD() {
|
---|
79 | XmlSerializer s = new XmlSerializer(typeof(AppointmentContainer));
|
---|
80 | if (File.Exists(Path.Combine(path, "calendar.xml"))) {
|
---|
81 | TextReader r = null;
|
---|
82 |
|
---|
83 | try {
|
---|
84 | r = new StreamReader(path + "calendar.xml");
|
---|
85 | appContainer = (AppointmentContainer)s.Deserialize(r);
|
---|
86 | CalendarAvailable = true;
|
---|
87 | }
|
---|
88 | catch (Exception e) {
|
---|
89 | Logger.Error("Deserialization of Calendar failed", e);
|
---|
90 | Logger.Info("Starting with a new one");
|
---|
91 | appContainer = new AppointmentContainer();
|
---|
92 | CalendarAvailable = false;
|
---|
93 | }
|
---|
94 | finally {
|
---|
95 | if (r != null)
|
---|
96 | r.Close();
|
---|
97 | }
|
---|
98 | } else {
|
---|
99 | appContainer = new AppointmentContainer();
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | public bool IsAllowedToCalculate() {
|
---|
104 | return AppContainer.Appointments.Any(app => (DateTime.Now >= app.StartDate) && (DateTime.Now <= app.EndDate));
|
---|
105 | }
|
---|
106 |
|
---|
107 | public bool SetAppointments(bool isLocal, bool isForced, IEnumerable<Appointment> appointments) {
|
---|
108 | if (!isForced && !isLocal && AppContainer.IsLocal)
|
---|
109 | return false;
|
---|
110 |
|
---|
111 | AppContainer.Appointments = new List<Appointment>(appointments);
|
---|
112 | AppContainer.IsLocal = isLocal;
|
---|
113 | AppContainer.Updated = DateTime.Now;
|
---|
114 | CalendarAvailable = true;
|
---|
115 |
|
---|
116 | PersistToHDD();
|
---|
117 |
|
---|
118 | return true;
|
---|
119 | }
|
---|
120 |
|
---|
121 | internal bool SetAppointments(bool isLocal, ResponseCalendar response) {
|
---|
122 | IList<Appointment> app = new List<Appointment>();
|
---|
123 | foreach (AppointmentDto appointmentDto in response.Appointments) {
|
---|
124 | app.Add(new Appointment {
|
---|
125 | AllDayEvent = appointmentDto.AllDayEvent,
|
---|
126 | EndDate = appointmentDto.EndDate,
|
---|
127 | StartDate = appointmentDto.StartDate,
|
---|
128 | Recurring = appointmentDto.Recurring,
|
---|
129 |
|
---|
130 | RecurringId = appointmentDto.RecurringId,
|
---|
131 | Locked = true,
|
---|
132 | Subject = "Online",
|
---|
133 | });
|
---|
134 | }
|
---|
135 | return SetAppointments(isLocal, response.ForceFetch, app);
|
---|
136 | }
|
---|
137 | }
|
---|
138 | }
|
---|