Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Server.Console/3.2/CgCalendar.cs @ 3580

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

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

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