Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.3/sources/HeuristicLab.Hive/HeuristicLab.Hive.Server.LINQDataAccess/3.3/UptimeCalendarDao.cs @ 5048

Last change on this file since 5048 was 4424, checked in by cneumuel, 14 years ago
  • Added and updated License Information in every file
  • Sort and remove usings in every file
  • Deleted obsolete DataAccess.ADOHelper
  • Deleted some obsolete files
File size: 6.6 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Hive.Contracts.BusinessObjects;
26using HeuristicLab.Hive.Server.DataAccess;
27
28namespace HeuristicLab.Hive.Server.LINQDataAccess {
29  public class UptimeCalendarDao: BaseDao<AppointmentDto, UptimeCalendar>, IUptimeCalendarDao {
30    public override UptimeCalendar DtoToEntity(AppointmentDto source, UptimeCalendar target) {
31      if(source == null)
32        return null;
33      if (target == null)
34        target = new UptimeCalendar();
35
36      target.AllDayEvent = source.AllDayEvent;
37      target.EndDate = source.EndDate;
38      target.StartDate = source.StartDate;
39      target.Recurring = source.Recurring;
40      target.RecurringId = source.RecurringId;
41
42      target.ResourceId = source.ResourceId;
43
44      return target;
45     
46    }
47
48    public override AppointmentDto EntityToDto(UptimeCalendar source, AppointmentDto target) {
49      if (source == null)
50        return null;
51      if (target == null)
52        target = new AppointmentDto();
53     
54      target.AllDayEvent = source.AllDayEvent;
55      target.EndDate = source.EndDate;
56      target.StartDate = source.StartDate;
57      target.Recurring = source.Recurring;
58      target.RecurringId = source.RecurringId;
59
60      target.ResourceId = source.ResourceId;
61
62      return target;
63
64    }
65
66    #region IGenericDao<AppointmentDto> Members
67
68    public AppointmentDto FindById(Guid id) {
69      return (from app in Context.UptimeCalendars
70              where app.UptimeCalendarId.Equals(id)
71              select EntityToDto(app, null)).SingleOrDefault();
72    }
73
74    public IEnumerable<AppointmentDto> FindAll() {
75      return (from app in Context.UptimeCalendars             
76              select EntityToDto(app, null)).ToList();
77    }
78
79    public AppointmentDto Insert(AppointmentDto bObj) {
80      UptimeCalendar uc = DtoToEntity(bObj, null);
81      Context.UptimeCalendars.InsertOnSubmit(uc);
82      CommitChanges();
83      bObj.Id = uc.UptimeCalendarId;
84      return bObj;
85    }
86
87    public void Delete(Guid id) {
88      Context.UptimeCalendars.DeleteOnSubmit(Context.UptimeCalendars.SingleOrDefault(uc => uc.UptimeCalendarId.Equals(id)));
89      CommitChanges();
90    }
91
92    public void Update(AppointmentDto bObj) {
93      UptimeCalendar cc = Context.UptimeCalendars.SingleOrDefault(c => c.UptimeCalendarId.Equals(bObj.Id));
94      DtoToEntity(bObj, cc);
95      CommitChanges();
96    }
97
98    public IEnumerable<AppointmentDto> GetUptimeCalendarForResource(Guid resourceId) {
99      return (from uc in Context.UptimeCalendars
100              where uc.ResourceId.Equals(resourceId)
101              select EntityToDto(uc, null)).ToList();
102    }
103
104    public void SetUptimeCalendarForResource(Guid resourceId, IEnumerable<AppointmentDto> appointments) {
105      var q = (from uc in Context.UptimeCalendars
106               where uc.ResourceId.Equals(resourceId)
107               select uc);
108     
109      Context.UptimeCalendars.DeleteAllOnSubmit(q);
110
111      foreach (AppointmentDto appdto in appointments) {     
112        UptimeCalendar uc = DtoToEntity(appdto, null);
113        uc.ResourceId = resourceId;
114        Context.UptimeCalendars.InsertOnSubmit(uc);
115      }
116
117      CommitChanges();           
118    }   
119
120    public IEnumerable<AppointmentDto> GetCalendarForSlave(SlaveDto slave) {
121      Slave dbc = Context.Resources.OfType<Slave>().SingleOrDefault(c => c.ResourceId.Equals(slave.Id));
122      IList<AppointmentDto> appointments = new List<AppointmentDto>();
123      if (dbc != null) {
124        SlaveGroup cg =
125          Context.Resources.OfType<SlaveGroup>().SingleOrDefault(cgroup => cgroup.ResourceId.Equals(dbc.UseCalendarFromResourceId));
126        //in case no plan has been set
127        if (cg == null)
128          if (dbc.SlaveGroup_Resources_Parents.FirstOrDefault() != null)
129            cg = dbc.SlaveGroup_Resources_Parents.FirstOrDefault().SlaveGroup;
130
131        if (cg == null)
132          return appointments;
133
134        while (cg.UptimeCalendars.Count == 0) {
135          if (cg.SlaveGroup_Resources_Parents.FirstOrDefault() != null)
136            cg = cg.SlaveGroup_Resources_Parents.FirstOrDefault().SlaveGroup;
137          else {
138            break;
139          }
140        }
141
142        foreach (UptimeCalendar appointment in cg.UptimeCalendars) {
143          appointments.Add(EntityToDto(appointment,null)); 
144        }
145
146      }
147      return appointments;
148    }
149
150    public void NotifySlavesOfNewCalendar(Guid groupId, bool forcePush) {
151     
152      //Get the current SlaveGroup
153      SlaveGroup cg = Context.Resources.OfType<SlaveGroup>().SingleOrDefault(cgroup => cgroup.ResourceId.Equals(groupId));
154      if(cg == null)
155        return;
156
157      //Get all the affected slaves
158      List<Slave> slaves = Context.Resources.OfType<Slave>().Where(c => c.UseCalendarFromResourceId.Equals(cg.ResourceId)).ToList();
159     
160      //Set new state
161      foreach (Slave slave in slaves) {
162        slave.CalendarSyncStatus = (forcePush ? Enum.GetName(typeof(CalendarState), CalendarState.ForceFetch) : Enum.GetName(typeof(CalendarState), CalendarState.Fetch));     
163      }
164     
165      CommitChanges();
166
167      //Get all Subgroups
168      List<SlaveGroup> groups = (from cg1 in Context.Resources.OfType<SlaveGroup>()
169                                  where cg1.SlaveGroup_Resources_Parents.Any(
170                                cgr => cgr.SlaveGroupId.Equals(groupId))
171                              select cg1).ToList();
172
173      //If they have their own calendar - stop propagation
174      //otherweise - propagate
175      foreach (SlaveGroup cgroup in groups) {
176        if(cgroup.UptimeCalendars.Count == 0)
177          NotifySlavesOfNewCalendar(cgroup.ResourceId, forcePush);       
178      }
179    }
180
181    #endregion
182  }
183}
Note: See TracBrowser for help on using the repository browser.