Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.3-HiveMigration/sources/HeuristicLab.Hive/HeuristicLab.Hive.Server.Console/3.3/CgCalendar.cs @ 4263

Last change on this file since 4263 was 4263, checked in by cneumuel, 14 years ago

consolidated Response objects to use only StatusMessage with enums instead of strings.
removed Success property from Response. success is now represented by StatusMessage alone. (#1159)

File size: 12.2 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Linq;
7using System.Text;
8using System.Windows.Forms;
9using System.Xml.Serialization;
10using HeuristicLab.Calendar;
11using HeuristicLab.Hive.Slave.Console;
12using HeuristicLab.Hive.Contracts.BusinessObjects;
13using HeuristicLab.Hive.Contracts;
14using System.Collections;
15using HeuristicLab.Hive.Contracts.ResponseObjects;
16
17namespace HeuristicLab.Hive.Server.ServerConsole {
18
19  //delegate to write text in the textbox from another process
20  public delegate void AppendTextDelegate(String message);
21
22  //delegate to remove text in the textbox from another process
23  public delegate void RemoveTextDelegate(int newLength, int maxChars);
24
25  //delegate fired, if a dialog is being closed
26  public delegate void OnDialogClosedDelegate(RecurrentEvent e);
27
28  public partial class CgCalendar : Form {
29
30    public Guid ResourceId { get; set; }
31    public string Name { get; set; }
32
33    [XmlArray("Appointments")]
34    [XmlArrayItem("Appointment", typeof(Appointment))]
35    public List<Appointment> onlineTimes = new List<Appointment>();
36
37    public CgCalendar(Guid resourceId, String name) {     
38      Name = name;
39      ResourceId = resourceId;
40      this.Text = Name + "(" + ResourceId + ")";
41      InitializeComponent();
42      InitCalender();
43      UpdateCalendar();
44    }
45
46    private void InitCalender() {
47      dvOnline.StartDate = DateTime.Now;
48      dvOnline.OnNewAppointment += new EventHandler<NewAppointmentEventArgs>(dvOnline_OnNewAppointment);
49      dvOnline.OnResolveAppointments += new EventHandler<ResolveAppointmentsEventArgs>(dvOnline_OnResolveAppointments);
50
51      //get calender from client
52     
53    }
54
55    private void UpdateCalendar() {
56      onlineTimes.Clear();
57      ResponseList<AppointmentDto> response = ServiceLocator.GetClientManager().GetUptimeCalendarForResource(ResourceId);
58      if(response.StatusMessage == ResponseStatus.Ok) {
59        foreach (AppointmentDto appointmentDto in response.List) {
60          onlineTimes.Add(new Appointment {
61                                            AllDayEvent = appointmentDto.AllDayEvent,
62                                            EndDate = appointmentDto.EndDate,
63                                            StartDate = appointmentDto.StartDate,
64                                            Recurring = appointmentDto.Recurring,
65                                           
66                                            RecurringId = appointmentDto.RecurringId,
67                                            BorderColor = Color.Red,
68                                            Locked = true,
69                                            Subject = "Online",
70                                          });
71        }
72        dvOnline.Invalidate();
73      }   
74    }
75
76    private bool CreateAppointment() {
77      DateTime from, to;
78
79      if (!string.IsNullOrEmpty(dtpFrom.Text) && !string.IsNullOrEmpty(dtpTo.Text)) {
80        if (chbade.Checked) {
81          //whole day appointment, only dates are visible
82          if (DateTime.TryParse(dtpFrom.Text, out from) && DateTime.TryParse(dtpTo.Text, out to) && from <= to)
83            onlineTimes.Add(CreateAppointment(from, to.AddDays(1), true));
84          else
85            MessageBox.Show("Incorrect date format", "Schedule Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
86        } else if (!string.IsNullOrEmpty(txttimeFrom.Text) && !string.IsNullOrEmpty(txttimeTo.Text)) {
87          //Timeframe appointment
88          if (DateTime.TryParse(dtpFrom.Text + " " + txttimeFrom.Text, out from) && DateTime.TryParse(dtpTo.Text + " " + txttimeTo.Text, out to) && from < to) {
89            if (from.Date == to.Date)
90              onlineTimes.Add(CreateAppointment(from, to, false));
91            else {
92              //more than 1 day selected
93              while (from.Date != to.Date) {
94                onlineTimes.Add(CreateAppointment(from, new DateTime(from.Year, from.Month, from.Day, to.Hour, to.Minute, 0, 0), false));
95                from = from.AddDays(1);
96              }
97              onlineTimes.Add(CreateAppointment(from, new DateTime(from.Year, from.Month, from.Day, to.Hour, to.Minute, 0, 0), false));
98            }
99          } else
100            MessageBox.Show("Incorrect date format", "Schedule Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
101        }
102        dvOnline.Invalidate();
103        return true;
104      } else {
105        MessageBox.Show("Error in create appointment, please fill out all textboxes!", "Schedule Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
106        return false;
107      }
108    }
109
110    private Appointment CreateAppointment(DateTime startDate, DateTime endDate, bool allDay) {
111      Appointment App = new Appointment();
112      App.StartDate = startDate;
113      App.EndDate = endDate;
114      App.AllDayEvent = allDay;
115      App.BorderColor = Color.Red;
116      App.Locked = true;
117      App.Subject = "Online";
118      App.Recurring = false;
119      return App;
120    }
121
122    private Appointment CreateAppointment(DateTime startDate, DateTime endDate, bool allDay, bool recurring, Guid recurringId) {
123      Appointment App = new Appointment();
124      App.StartDate = startDate;
125      App.EndDate = endDate;
126      App.AllDayEvent = allDay;
127      App.BorderColor = Color.Red;
128      App.Locked = true;
129      App.Subject = "Online";
130      App.Recurring = recurring;
131      App.RecurringId = recurringId;
132      return App;
133    }
134
135    private void DeleteAppointment() {
136      onlineTimes.Remove(dvOnline.SelectedAppointment);
137    }
138
139    private void DeleteRecurringAppointment(Guid recurringId) {
140      onlineTimes.RemoveAll(a => a.RecurringId.ToString() == dvOnline.SelectedAppointment.RecurringId.ToString());
141    }
142
143    private void ChangeRecurrenceAppointment(Guid recurringId) {
144      int hourfrom = int.Parse(txttimeFrom.Text.Substring(0, txttimeFrom.Text.IndexOf(':')));
145      int hourTo = int.Parse(txttimeTo.Text.Substring(0, txttimeTo.Text.IndexOf(':')));
146      List<Appointment> recurringAppointments = onlineTimes.Where(appointment => appointment.RecurringId == recurringId).ToList();
147      recurringAppointments.ForEach(appointment => appointment.StartDate = new DateTime(appointment.StartDate.Year, appointment.StartDate.Month, appointment.StartDate.Day, hourfrom, 0, 0));
148      recurringAppointments.ForEach(appointment => appointment.EndDate = new DateTime(appointment.EndDate.Year, appointment.EndDate.Month, appointment.EndDate.Day, hourTo, 0, 0));
149
150      DeleteRecurringAppointment(recurringId);
151      onlineTimes.AddRange(recurringAppointments);
152    }
153
154    public void DialogClosed(RecurrentEvent e) {
155      CreateDailyRecurrenceAppointments(e.DateFrom, e.DateTo, e.AllDay, e.IncWeeks, e.WeekDays);
156    }
157
158    private void CreateDailyRecurrenceAppointments(DateTime dateFrom, DateTime dateTo, bool allDay, int incWeek, HashSet<DayOfWeek> daysOfWeek) {
159      DateTime incDate = dateFrom;
160      Guid guid = Guid.NewGuid();
161
162      while (incDate.Date <= dateTo.Date) {
163        if (daysOfWeek.Contains(incDate.Date.DayOfWeek))
164          onlineTimes.Add(CreateAppointment(incDate, new DateTime(incDate.Year, incDate.Month, incDate.Day, dateTo.Hour, dateTo.Minute, 0), allDay, true, guid));
165        incDate = incDate.AddDays(1);
166      }
167
168      dvOnline.Invalidate();
169    }
170
171    private void btbDelete_Click(object sender, EventArgs e) {
172      Appointment selectedAppointment = dvOnline.SelectedAppointment;
173      if (dvOnline.SelectedAppointment != null) {
174        if (!selectedAppointment.Recurring)
175          DeleteAppointment();
176        else {
177          DialogResult res = MessageBox.Show("Delete all events in this series?", "Delete recurrences", MessageBoxButtons.YesNo);
178          if (res != DialogResult.Yes)
179            DeleteAppointment();
180          else
181            DeleteRecurringAppointment(selectedAppointment.RecurringId);
182        }
183      }
184      dvOnline.Invalidate();
185    }
186
187    private void chbade_CheckedChanged(object sender, EventArgs e) {
188      txttimeFrom.Visible = !chbade.Checked;
189      txttimeTo.Visible = !chbade.Checked;
190    }
191
192    private void dvOnline_OnSelectionChanged(object sender, EventArgs e) {
193      //btCreate.Enabled = true;
194      if (dvOnline.Selection == SelectionType.DateRange) {
195        dtpFrom.Text = dvOnline.SelectionStart.ToShortDateString();
196        dtpTo.Text = dvOnline.SelectionEnd.Date.ToShortDateString();
197        txttimeFrom.Text = dvOnline.SelectionStart.ToShortTimeString();
198        txttimeTo.Text = dvOnline.SelectionEnd.ToShortTimeString();
199
200        btCreate.Text = "Save";
201      }
202
203      if (dvOnline.Selection == SelectionType.Appointment) {
204
205        dtpFrom.Text = dvOnline.SelectedAppointment.StartDate.ToShortDateString();
206        dtpTo.Text = dvOnline.SelectedAppointment.EndDate.ToShortDateString();
207        txttimeFrom.Text = dvOnline.SelectedAppointment.StartDate.ToShortTimeString();
208        txttimeTo.Text = dvOnline.SelectedAppointment.EndDate.ToShortTimeString();
209
210        if (dvOnline.SelectedAppointment.Recurring)
211          //btCreate.Enabled = false;
212          //also change the caption of the save button
213          btCreate.Text = "Save changes";
214      }
215
216      if (dvOnline.Selection == SelectionType.None) {
217        //also change the caption of the save button
218        btCreate.Text = "Save";
219      }
220
221    }
222
223    private void mcOnline_DateChanged(object sender, DateRangeEventArgs e) {
224      dvOnline.StartDate = mcOnline.SelectionStart;
225    }
226
227    private void btCreate_Click(object sender, EventArgs e) {
228      if (dvOnline.Selection != SelectionType.Appointment) {
229        CreateAppointment();
230      } else {
231        //now we want to change an existing appointment
232        if (!dvOnline.SelectedAppointment.Recurring) {
233          if (CreateAppointment())
234            DeleteAppointment();
235        } else {
236          //change recurring appointment
237          //check, if only selected appointment has to change or whole recurrence
238          DialogResult res = MessageBox.Show("Change all events in this series?", "Change recurrences", MessageBoxButtons.YesNo);
239          if (res != DialogResult.Yes) {
240            if (CreateAppointment())
241              DeleteAppointment();
242          } else
243            ChangeRecurrenceAppointment(dvOnline.SelectedAppointment.RecurringId);
244        }
245      }
246      dvOnline.Invalidate();
247    }
248
249    private void btnRecurrence_Click(object sender, EventArgs e) {
250      Recurrence recurrence = new Recurrence();
251      recurrence.dialogClosedDelegate = new OnDialogClosedDelegate(this.DialogClosed);
252      recurrence.Show();
253    }
254
255    private void btnSaveCal_Click(object sender, EventArgs e) {
256      List<AppointmentDto> appointments = new List<AppointmentDto>();
257      foreach(Appointment app in onlineTimes) {
258        AppointmentDto apdto = new AppointmentDto {
259                                                    AllDayEvent = app.AllDayEvent,
260                                                    EndDate = app.EndDate,
261                                                    Recurring = app.Recurring,
262                                                    RecurringId = app.RecurringId,
263                                                    StartDate = app.StartDate
264                                                  };
265        appointments.Add(apdto);
266      }
267      ServiceLocator.GetClientManager().SetUptimeCalendarForResource(ResourceId, appointments, cbx_forcePush.Checked);     
268    }
269
270    private void dvOnline_OnResolveAppointments(object sender, ResolveAppointmentsEventArgs e) {
271      List<Appointment> Apps = new List<Appointment>();
272
273      foreach (Appointment m_App in onlineTimes)
274        if ((m_App.StartDate >= e.StartDate) &&
275            (m_App.StartDate <= e.EndDate))
276          Apps.Add(m_App);
277      e.Appointments = Apps;
278    }
279
280    private void dvOnline_OnNewAppointment(object sender, NewAppointmentEventArgs e) {
281      Appointment Appointment = new Appointment();
282
283      Appointment.StartDate = e.StartDate;
284      Appointment.EndDate = e.EndDate;
285
286      onlineTimes.Add(Appointment);
287    }
288
289    private void btnClearCal_Click(object sender, EventArgs e) {
290      onlineTimes.Clear();
291    }
292  }
293}
294   
295 
296   
297
Note: See TracBrowser for help on using the repository browser.