Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Server.LINQDataAccess/3.2/UptimeCalendarDao.cs @ 3203

Last change on this file since 3203 was 3203, checked in by kgrading, 14 years ago

implemented the server on the client, using push & force push, added refresh buttons, added auto calender methods that traverse the tree... (#908)

File size: 5.8 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Hive.Contracts.BusinessObjects;
6using HeuristicLab.Hive.Server.DataAccess;
7
8namespace HeuristicLab.Hive.Server.LINQDataAccess {
9  public class UptimeCalendarDao: BaseDao<AppointmentDto, UptimeCalendar>, IUptimeCalendarDao {
10    public override UptimeCalendar DtoToEntity(AppointmentDto source, UptimeCalendar target) {
11      if(source == null)
12        return null;
13      if (target == null)
14        target = new UptimeCalendar();
15
16      target.AllDayEvent = source.AllDayEvent;
17      target.EndDate = source.EndDate;
18      target.StartDate = source.StartDate;
19      target.Recurring = source.Recurring;
20      target.RecurringId = source.RecurringId;
21
22      target.ResourceId = source.ResourceId;
23
24      return target;
25     
26    }
27
28    public override AppointmentDto EntityToDto(UptimeCalendar source, AppointmentDto target) {
29      if (source == null)
30        return null;
31      if (target == null)
32        target = new AppointmentDto();
33     
34      target.AllDayEvent = source.AllDayEvent;
35      target.EndDate = source.EndDate;
36      target.StartDate = source.StartDate;
37      target.Recurring = source.Recurring;
38      target.RecurringId = source.RecurringId;
39
40      target.ResourceId = source.ResourceId;
41
42      return target;
43
44    }
45
46    #region IGenericDao<AppointmentDto> Members
47
48    public AppointmentDto FindById(Guid id) {
49      return (from app in Context.UptimeCalendars
50              where app.UptimeCalendarId.Equals(id)
51              select EntityToDto(app, null)).SingleOrDefault();
52    }
53
54    public IEnumerable<AppointmentDto> FindAll() {
55      return (from app in Context.UptimeCalendars             
56              select EntityToDto(app, null)).ToList();
57    }
58
59    public AppointmentDto Insert(AppointmentDto bObj) {
60      UptimeCalendar uc = DtoToEntity(bObj, null);
61      Context.UptimeCalendars.InsertOnSubmit(uc);
62      Context.SubmitChanges();
63      bObj.Id = uc.UptimeCalendarId;
64      return bObj;
65    }
66
67    public void Delete(AppointmentDto bObj) {
68      Context.UptimeCalendars.DeleteOnSubmit(Context.UptimeCalendars.SingleOrDefault(uc => uc.UptimeCalendarId.Equals(bObj.Id)));
69      Context.SubmitChanges();
70    }
71
72    public void Update(AppointmentDto bObj) {
73      UptimeCalendar cc = Context.UptimeCalendars.SingleOrDefault(c => c.UptimeCalendarId.Equals(bObj.Id));
74      DtoToEntity(bObj, cc);
75      Context.SubmitChanges();
76    }
77
78    public IEnumerable<AppointmentDto> GetUptimeCalendarForResource(Guid resourceId) {
79      return (from uc in Context.UptimeCalendars
80              where uc.ResourceId.Equals(resourceId)
81              select EntityToDto(uc, null)).ToList();
82    }
83
84    public void SetUptimeCalendarForResource(Guid resourceId, IEnumerable<AppointmentDto> appointments) {
85      var q = (from uc in Context.UptimeCalendars
86               where uc.ResourceId.Equals(resourceId)
87               select uc);
88     
89      Context.UptimeCalendars.DeleteAllOnSubmit(q);
90
91      foreach (AppointmentDto appdto in appointments) {     
92        UptimeCalendar uc = DtoToEntity(appdto, null);
93        uc.ResourceId = resourceId;
94        Context.UptimeCalendars.InsertOnSubmit(uc);
95      }
96
97      Context.SubmitChanges();           
98    }   
99
100    public IEnumerable<AppointmentDto> GetCalendarForClient(ClientDto client) {
101      Client dbc = Context.Clients.SingleOrDefault(c => c.ResourceId.Equals(client.Id));
102      IList<AppointmentDto> appointments = new List<AppointmentDto>();
103      if (dbc != null) {
104        ClientGroup cg =
105          Context.ClientGroups.SingleOrDefault(cgroup => cgroup.ResourceId.Equals(dbc.UseCalendarFromResourceId));
106        //in case no plan has been set
107        if (cg == null)
108          if (dbc.Resource.ClientGroup_Resources.FirstOrDefault() != null)
109            cg = dbc.Resource.ClientGroup_Resources.FirstOrDefault().ClientGroup;
110
111        if (cg == null)
112          return appointments;
113
114        while (cg.Resource.UptimeCalendars.Count == 0) {
115          if (cg.Resource.ClientGroup_Resources.FirstOrDefault() != null)
116            cg = cg.Resource.ClientGroup_Resources.FirstOrDefault().ClientGroup;
117          else {
118            break;
119          }
120        }
121
122        foreach (UptimeCalendar appointment in cg.Resource.UptimeCalendars) {
123          appointments.Add(EntityToDto(appointment,null)); 
124        }
125
126      }
127      return appointments;
128    }
129
130    public void NotifyClientsOfNewCalendar(Guid groupId, bool forcePush) {
131     
132      //Get the current ClientGroup
133      ClientGroup cg = Context.ClientGroups.SingleOrDefault(cgroup => cgroup.ResourceId.Equals(groupId));
134      if(cg == null)
135        return;
136
137      //Get all the affected clients
138      List<Client> clients = Context.Clients.Where(c => c.UseCalendarFromResourceId.Equals(cg.ResourceId)).ToList();
139     
140      //Set new state
141      foreach (Client client in clients) {
142        client.CalendarSyncStatus = (forcePush ? Enum.GetName(typeof(CalendarState), CalendarState.ForceFetch) : Enum.GetName(typeof(CalendarState), CalendarState.Fetch));     
143      }
144     
145      Context.SubmitChanges();
146
147      //Get all Subgroups
148      List<ClientGroup> groups = (from cg1 in Context.ClientGroups
149                                  where cg1.Resource.ClientGroup_Resources.Any(
150                                cgr => cgr.ClientGroupId.Equals(groupId))
151                              select cg1).ToList();
152
153      //If they have their own calendar - stop propagation
154      //otherweise - propagate
155      foreach (ClientGroup cgroup in groups) {
156        if(cgroup.Resource.UptimeCalendars.Count == 0)
157          NotifyClientsOfNewCalendar(groupId, forcePush);       
158      }
159    }
160
161    #endregion
162  }
163}
Note: See TracBrowser for help on using the repository browser.