Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3195 was 3022, checked in by kgrading, 15 years ago

added the calendar in the dal and the server console. Works on every Resource (Group / Client) (#908)

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