Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.DataAccess/3.4/HiveDao.cs @ 5779

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

#1233

  • implemented pause, stop for single jobs
  • introduced Command property for jobs (to distinguish between state and command (abort vs. aborted))
  • improved behaviour of ItemTreeView (double click opens new window, selected item stays marked)
  • fixed bugs in StateLogGanttChartListView and HiveJobView
  • fixed cloning of client-side dtos
File size: 24.0 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 System.Linq.Expressions;
26
27namespace HeuristicLab.Services.Hive.DataAccess {
28  using HeuristicLab.Services.Hive.Common.DataTransfer;
29  using HeuristicLab.Services.Hive.DataAccess.Properties;
30  using DT = HeuristicLab.Services.Hive.Common.DataTransfer;
31
32  public class HiveDao : IHiveDao {
33    public static HiveDataContext CreateContext() {
34      return new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString);
35    }
36
37    public HiveDao() {
38    }
39
40    #region Job Methods
41    public DT.Job GetJob(Guid id) {
42      using (var db = CreateContext()) {
43        return Convert.ToDto(db.Jobs.SingleOrDefault(x => x.JobId == id));
44      }
45    }
46
47    public IEnumerable<DT.Job> GetJobs(Expression<Func<Job, bool>> predicate) {
48      using (var db = CreateContext()) {
49        return db.Jobs.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
50      }
51    }
52
53    public Guid AddJob(DT.Job dto) {
54      using (var db = CreateContext()) {
55        var entity = Convert.ToEntity(dto);
56        db.Jobs.InsertOnSubmit(entity);
57        db.SubmitChanges();
58        foreach (Guid pluginId in dto.PluginsNeededIds) {
59          db.RequiredPlugins.InsertOnSubmit(new RequiredPlugin() { JobId = entity.JobId, PluginId = pluginId });
60        }
61        db.SubmitChanges();
62        return entity.JobId;
63      }
64    }
65
66    public void UpdateJob(DT.Job dto) {
67      using (var db = CreateContext()) {
68        var entity = db.Jobs.FirstOrDefault(x => x.JobId == dto.Id);
69        if (entity == null) db.Jobs.InsertOnSubmit(Convert.ToEntity(dto));
70        else Convert.ToEntity(dto, entity);
71        // todo: update required plugins
72        db.SubmitChanges();
73      }
74    }
75
76    public void DeleteJob(Guid id) {
77      using (var db = CreateContext()) {
78        var entity = db.Jobs.FirstOrDefault(x => x.JobId == id);
79        if (entity != null) db.Jobs.DeleteOnSubmit(entity);
80        db.SubmitChanges(); // JobData and child jobs are deleted by db-trigger
81      }
82    }
83
84    /// <summary>
85    /// returns all parent jobs which are waiting for their child jobs to finish
86    /// </summary>
87    /// <param name="resourceIds">list of resourceids which for which the jobs should be valid</param>
88    /// <param name="count">maximum number of jobs to return</param>
89    /// <param name="finished">if true, all parent jobs which have FinishWhenChildJobsFinished=true are returned, otherwise only FinishWhenChildJobsFinished=false are returned</param>
90    /// <returns></returns>
91    public IEnumerable<DT.Job> GetParentJobs(IEnumerable<Guid> resourceIds, int count, bool finished) {
92      using (var db = CreateContext()) {
93        var query = from ar in db.AssignedResources
94                    where resourceIds.Contains(ar.ResourceId)
95                       && ar.Job.State == JobState.Waiting
96                       && ar.Job.IsParentJob
97                       && (finished ? ar.Job.FinishWhenChildJobsFinished : !ar.Job.FinishWhenChildJobsFinished)
98                       && (from child in db.Jobs
99                           where child.ParentJobId == ar.Job.JobId
100                           select child.State == JobState.Finished
101                               || child.State == JobState.Aborted
102                               || child.State == JobState.Failed).All(x => x)
103                       && (from child in db.Jobs // avoid returning WaitForChildJobs jobs where no child-jobs exist (yet)
104                           where child.ParentJobId == ar.Job.JobId
105                           select child).Count() > 0
106                    orderby ar.Job.Priority descending
107                    select Convert.ToDto(ar.Job);
108        return count == 0 ? query.ToArray() : query.Take(count).ToArray();
109      }
110    }
111
112    public IEnumerable<DT.Job> GetWaitingJobs(DT.Slave slave, int count) {
113      using (var db = CreateContext()) {
114        var resourceIds = GetParentResources(slave.Id).Select(r => r.Id);
115        var waitingParentJobs = GetParentJobs(resourceIds, count, false);
116        if (count > 0 && waitingParentJobs.Count() >= count) return waitingParentJobs.Take(count).ToArray();
117
118        var query = from ar in db.AssignedResources
119                    where resourceIds.Contains(ar.ResourceId)
120                       && !(ar.Job.IsParentJob && ar.Job.FinishWhenChildJobsFinished)
121                       && ar.Job.State == JobState.Waiting
122                       && ar.Job.CoresNeeded <= slave.FreeCores
123                       && ar.Job.MemoryNeeded <= slave.FreeMemory
124                    orderby ar.Job.Priority descending
125                    select Convert.ToDto(ar.Job);
126        var waitingJobs = (count == 0 ? query : query.Take(count)).ToArray();
127        return waitingJobs.Union(waitingParentJobs).OrderByDescending(x => x.Priority);
128      }
129    }
130
131    public DT.Job UpdateJobState(Guid jobId, JobState jobState, Guid? slaveId, Guid? userId, string exception) {
132      using (var db = CreateContext()) {
133        var job = db.Jobs.SingleOrDefault(x => x.JobId == jobId);
134        job.State = jobState;
135        db.StateLogs.InsertOnSubmit(new StateLog {
136          JobId = jobId,
137          State = jobState,
138          SlaveId = slaveId,
139          UserId = userId,
140          Exception = exception,
141          DateTime = DateTime.Now
142        });
143        db.SubmitChanges();
144        job = db.Jobs.SingleOrDefault(x => x.JobId == jobId);
145        return Convert.ToDto(job);
146      }
147    }
148    #endregion
149
150    #region JobData Methods
151
152    public DT.JobData GetJobData(Guid id) {
153      using (var db = CreateContext()) {
154        return Convert.ToDto(db.JobDatas.SingleOrDefault(x => x.JobId == id));
155      }
156    }
157
158    public IEnumerable<DT.JobData> GetJobDatas(Expression<Func<JobData, bool>> predicate) {
159      using (var db = CreateContext()) {
160        return db.JobDatas.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
161      }
162    }
163
164    public Guid AddJobData(DT.JobData dto) {
165      using (var db = CreateContext()) {
166        var entity = Convert.ToEntity(dto);
167        db.JobDatas.InsertOnSubmit(entity);
168        db.SubmitChanges();
169        return entity.JobId;
170      }
171    }
172
173    public void UpdateJobData(DT.JobData dto) {
174      using (var db = CreateContext()) {
175        var entity = db.JobDatas.FirstOrDefault(x => x.JobId == dto.JobId);
176        if (entity == null) db.JobDatas.InsertOnSubmit(Convert.ToEntity(dto));
177        else Convert.ToEntity(dto, entity);
178        db.SubmitChanges();
179      }
180    }
181
182    public void DeleteJobData(Guid id) {
183      using (var db = CreateContext()) {
184        var entity = db.JobDatas.FirstOrDefault(x => x.JobId == id); // check if all the byte[] is loaded into memory here. otherwise work around to delete without loading it
185        if (entity != null) db.JobDatas.DeleteOnSubmit(entity);
186        db.SubmitChanges();
187      }
188    }
189    #endregion
190
191    #region StateLog Methods
192
193    public DT.StateLog GetStateLog(Guid id) {
194      using (var db = CreateContext()) {
195        return Convert.ToDto(db.StateLogs.SingleOrDefault(x => x.StateLogId == id));
196      }
197    }
198
199    public IEnumerable<DT.StateLog> GetStateLogs(Expression<Func<StateLog, bool>> predicate) {
200      using (var db = CreateContext()) {
201        return db.StateLogs.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
202      }
203    }
204
205    public Guid AddStateLog(DT.StateLog dto) {
206      using (var db = CreateContext()) {
207        var entity = Convert.ToEntity(dto);
208        db.StateLogs.InsertOnSubmit(entity);
209        db.SubmitChanges();
210        return entity.StateLogId;
211      }
212    }
213
214    public void UpdateStateLog(DT.StateLog dto) {
215      using (var db = CreateContext()) {
216        var entity = db.StateLogs.FirstOrDefault(x => x.StateLogId == dto.Id);
217        if (entity == null) db.StateLogs.InsertOnSubmit(Convert.ToEntity(dto));
218        else Convert.ToEntity(dto, entity);
219        db.SubmitChanges();
220      }
221    }
222
223    public void DeleteStateLog(Guid id) {
224      using (var db = CreateContext()) {
225        var entity = db.StateLogs.FirstOrDefault(x => x.StateLogId == id);
226        if (entity != null) db.StateLogs.DeleteOnSubmit(entity);
227        db.SubmitChanges();
228      }
229    }
230    #endregion
231
232    #region HiveExperiment Methods
233    public DT.HiveExperiment GetHiveExperiment(Guid id) {
234      using (var db = CreateContext()) {
235        return Convert.ToDto(db.HiveExperiments.SingleOrDefault(x => x.HiveExperimentId == id));
236      }
237    }
238
239    public IEnumerable<DT.HiveExperiment> GetHiveExperiments(Expression<Func<HiveExperiment, bool>> predicate) {
240      using (var db = CreateContext()) {
241        return db.HiveExperiments.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
242      }
243    }
244
245    public Guid AddHiveExperiment(DT.HiveExperiment dto) {
246      using (var db = CreateContext()) {
247        var entity = Convert.ToEntity(dto);
248        db.HiveExperiments.InsertOnSubmit(entity);
249        db.SubmitChanges();
250        return entity.HiveExperimentId;
251      }
252    }
253
254    public void UpdateHiveExperiment(DT.HiveExperiment dto) {
255      using (var db = CreateContext()) {
256        var entity = db.HiveExperiments.FirstOrDefault(x => x.HiveExperimentId == dto.Id);
257        if (entity == null) db.HiveExperiments.InsertOnSubmit(Convert.ToEntity(dto));
258        else Convert.ToEntity(dto, entity);
259        db.SubmitChanges();
260      }
261    }
262
263    public void DeleteHiveExperiment(Guid id) {
264      using (var db = CreateContext()) {
265        var entity = db.HiveExperiments.FirstOrDefault(x => x.HiveExperimentId == id);
266        if (entity != null) db.HiveExperiments.DeleteOnSubmit(entity);
267        db.SubmitChanges();
268      }
269    }
270    #endregion
271
272    #region HiveExperimentPermission Methods
273
274    public DT.HiveExperimentPermission GetHiveExperimentPermission(Guid hiveExperimentId, Guid grantedUserId) {
275      using (var db = CreateContext()) {
276        return Convert.ToDto(db.HiveExperimentPermissions.SingleOrDefault(x => x.HiveExperimentId == hiveExperimentId && x.GrantedUserId == grantedUserId));
277      }
278    }
279
280    public IEnumerable<DT.HiveExperimentPermission> GetHiveExperimentPermissions(Expression<Func<HiveExperimentPermission, bool>> predicate) {
281      using (var db = CreateContext()) {
282        return db.HiveExperimentPermissions.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
283      }
284    }
285
286    public void AddHiveExperimentPermission(DT.HiveExperimentPermission dto) {
287      using (var db = CreateContext()) {
288        var entity = Convert.ToEntity(dto);
289        db.HiveExperimentPermissions.InsertOnSubmit(entity);
290        db.SubmitChanges();
291      }
292    }
293
294    public void UpdateHiveExperimentPermission(DT.HiveExperimentPermission dto) {
295      using (var db = CreateContext()) {
296        var entity = db.HiveExperimentPermissions.FirstOrDefault(x => x.HiveExperimentId == dto.HiveExperimentId && x.GrantedUserId == dto.GrantedUserId);
297        if (entity == null) db.HiveExperimentPermissions.InsertOnSubmit(Convert.ToEntity(dto));
298        else Convert.ToEntity(dto, entity);
299        db.SubmitChanges();
300      }
301    }
302
303    public void DeleteHiveExperimentPermission(Guid hiveExperimentId, Guid grantedUserId) {
304      using (var db = CreateContext()) {
305        var entity = db.HiveExperimentPermissions.FirstOrDefault(x => x.HiveExperimentId == hiveExperimentId && x.GrantedUserId == grantedUserId);
306        if (entity != null) db.HiveExperimentPermissions.DeleteOnSubmit(entity);
307        db.SubmitChanges();
308      }
309    }
310
311    #endregion
312
313    #region Plugin Methods
314    public DT.Plugin GetPlugin(Guid id) {
315      using (var db = CreateContext()) {
316        return Convert.ToDto(db.Plugins.SingleOrDefault(x => x.PluginId == id));
317      }
318    }
319
320    public IEnumerable<DT.Plugin> GetPlugins(Expression<Func<Plugin, bool>> predicate) {
321      using (var db = CreateContext()) {
322        return db.Plugins.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
323      }
324    }
325
326    public Guid AddPlugin(DT.Plugin dto) {
327      using (var db = CreateContext()) {
328        var entity = Convert.ToEntity(dto);
329        db.Plugins.InsertOnSubmit(entity);
330        db.SubmitChanges();
331        return entity.PluginId;
332      }
333    }
334
335    public void UpdatePlugin(DT.Plugin dto) {
336      using (var db = CreateContext()) {
337        var entity = db.Plugins.FirstOrDefault(x => x.PluginId == dto.Id);
338        if (entity == null) db.Plugins.InsertOnSubmit(Convert.ToEntity(dto));
339        else Convert.ToEntity(dto, entity);
340        db.SubmitChanges();
341      }
342    }
343
344    public void DeletePlugin(Guid id) {
345      using (var db = CreateContext()) {
346        var entity = db.Plugins.FirstOrDefault(x => x.PluginId == id);
347        if (entity != null) db.Plugins.DeleteOnSubmit(entity);
348        db.SubmitChanges();
349      }
350    }
351    #endregion
352
353    #region PluginData Methods
354
355    public DT.PluginData GetPluginData(Guid id) {
356      using (var db = CreateContext()) {
357        return Convert.ToDto(db.PluginDatas.SingleOrDefault(x => x.PluginDataId == id));
358      }
359    }
360
361    public IEnumerable<DT.PluginData> GetPluginDatas(Expression<Func<PluginData, bool>> predicate) {
362      using (var db = CreateContext()) {
363        return db.PluginDatas.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
364      }
365    }
366
367    public Guid AddPluginData(DT.PluginData dto) {
368      using (var db = CreateContext()) {
369        var entity = Convert.ToEntity(dto);
370        db.PluginDatas.InsertOnSubmit(entity);
371        db.SubmitChanges();
372        return entity.PluginDataId;
373      }
374    }
375
376    public void UpdatePluginData(DT.PluginData dto) {
377      using (var db = CreateContext()) {
378        var entity = db.PluginDatas.FirstOrDefault(x => x.PluginId == dto.PluginId);
379        if (entity == null) db.PluginDatas.InsertOnSubmit(Convert.ToEntity(dto));
380        else Convert.ToEntity(dto, entity);
381        db.SubmitChanges();
382      }
383    }
384
385    public void DeletePluginData(Guid id) {
386      using (var db = CreateContext()) {
387        var entity = db.PluginDatas.FirstOrDefault(x => x.PluginDataId == id); // todo: check if all the byte[] is loaded into memory here. otherwise work around to delete without loading it
388        if (entity != null) db.PluginDatas.DeleteOnSubmit(entity);
389        db.SubmitChanges();
390      }
391    }
392    #endregion
393
394    #region Slave Methods
395    public DT.Slave GetSlave(Guid id) {
396      using (var db = CreateContext()) {
397        return Convert.ToDto(db.Resources.OfType<Slave>().SingleOrDefault(x => x.ResourceId == id));
398      }
399    }
400
401    public IEnumerable<DT.Slave> GetSlaves(Expression<Func<Slave, bool>> predicate) {
402      using (var db = CreateContext()) {
403        return db.Resources.OfType<Slave>().Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
404      }
405    }
406
407    public Guid AddSlave(DT.Slave dto) {
408      using (var db = CreateContext()) {
409        var entity = Convert.ToEntity(dto);
410        db.Resources.InsertOnSubmit(entity);
411        db.SubmitChanges();
412        return entity.ResourceId;
413      }
414    }
415
416    public void UpdateSlave(DT.Slave dto) {
417      using (var db = CreateContext()) {
418        var entity = db.Resources.OfType<Slave>().FirstOrDefault(x => x.ResourceId == dto.Id);
419        if (entity == null) db.Resources.InsertOnSubmit(Convert.ToEntity(dto));
420        else Convert.ToEntity(dto, entity);
421        db.SubmitChanges();
422      }
423    }
424
425    public void DeleteSlave(Guid id) {
426      using (var db = CreateContext()) {
427        var entity = db.Resources.OfType<Slave>().FirstOrDefault(x => x.ResourceId == id);
428        if (entity != null) db.Resources.DeleteOnSubmit(entity);
429        db.SubmitChanges();
430      }
431    }
432    #endregion
433
434    #region SlaveGroup Methods
435    public DT.SlaveGroup GetSlaveGroup(Guid id) {
436      using (var db = CreateContext()) {
437        return Convert.ToDto(db.Resources.OfType<SlaveGroup>().SingleOrDefault(x => x.ResourceId == id));
438      }
439    }
440
441    public IEnumerable<DT.SlaveGroup> GetSlaveGroups(Expression<Func<SlaveGroup, bool>> predicate) {
442      using (var db = CreateContext()) {
443        return db.Resources.OfType<SlaveGroup>().Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
444      }
445    }
446
447    public Guid AddSlaveGroup(DT.SlaveGroup dto) {
448      using (var db = CreateContext()) {
449        if (dto.Id == Guid.Empty)
450          dto.Id = Guid.NewGuid();
451        var entity = Convert.ToEntity(dto);
452        db.Resources.InsertOnSubmit(entity);
453        db.SubmitChanges();
454        return entity.ResourceId;
455      }
456    }
457
458    public void UpdateSlaveGroup(DT.SlaveGroup dto) {
459      using (var db = CreateContext()) {
460        var entity = db.Resources.OfType<SlaveGroup>().FirstOrDefault(x => x.ResourceId == dto.Id);
461        if (entity == null) db.Resources.InsertOnSubmit(Convert.ToEntity(dto));
462        else Convert.ToEntity(dto, entity);
463        db.SubmitChanges();
464      }
465    }
466
467    public void DeleteSlaveGroup(Guid id) {
468      using (var db = CreateContext()) {
469        var entity = db.Resources.OfType<SlaveGroup>().FirstOrDefault(x => x.ResourceId == id);
470        if (entity != null) {
471          if (db.Resources.Where(r => r.ParentResourceId == id).Count() > 0) {
472            throw new DaoException("Cannot delete SlaveGroup as long as there are Slaves in the group");
473          }
474          db.Resources.DeleteOnSubmit(entity);
475        }
476        db.SubmitChanges();
477      }
478    }
479    #endregion
480
481    #region Resource Methods
482    public DT.Resource GetResource(Guid id) {
483      using (var db = CreateContext()) {
484        return Convert.ToDto(db.Resources.SingleOrDefault(x => x.ResourceId == id));
485      }
486    }
487
488    public IEnumerable<DT.Resource> GetResources(Expression<Func<Resource, bool>> predicate) {
489      using (var db = CreateContext()) {
490        return db.Resources.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
491      }
492    }
493
494    public Guid AddResource(DT.Resource dto) {
495      using (var db = CreateContext()) {
496        var entity = Convert.ToEntity(dto);
497        db.Resources.InsertOnSubmit(entity);
498        db.SubmitChanges();
499        return entity.ResourceId;
500      }
501    }
502
503    public void UpdateResource(DT.Resource dto) {
504      using (var db = CreateContext()) {
505        var entity = db.Resources.FirstOrDefault(x => x.ResourceId == dto.Id);
506        if (entity == null) db.Resources.InsertOnSubmit(Convert.ToEntity(dto));
507        else Convert.ToEntity(dto, entity);
508        db.SubmitChanges();
509      }
510    }
511
512    public void DeleteResource(Guid id) {
513      using (var db = CreateContext()) {
514        var entity = db.Resources.FirstOrDefault(x => x.ResourceId == id);
515        if (entity != null) db.Resources.DeleteOnSubmit(entity);
516        db.SubmitChanges();
517      }
518    }
519
520    public void AssignJobToResource(Guid jobId, Guid resourceId) {
521      using (var db = CreateContext()) {
522        var job = db.Jobs.Where(x => x.JobId == jobId).Single();
523        job.AssignedResources.Add(new AssignedResource() { JobId = jobId, ResourceId = resourceId });
524        db.SubmitChanges();
525      }
526    }
527
528    public IEnumerable<DT.Resource> GetAssignedResources(Guid jobId) {
529      using (var db = CreateContext()) {
530        var job = db.Jobs.Where(x => x.JobId == jobId).Single();
531        return job.AssignedResources.Select(x => Convert.ToDto(x.Resource)).ToArray();
532      }
533    }
534
535    /// <summary>
536    /// Returns all parent resources of a resource (the given resource is also added)
537    /// </summary>
538    private IEnumerable<DT.Resource> GetParentResources(Guid resourceId) {
539      using (var db = CreateContext()) {
540        var resources = new List<Resource>();
541        CollectParentResources(resources, db.Resources.Where(r => r.ResourceId == resourceId).Single());
542        return resources.Select(r => Convert.ToDto(r)).ToArray();
543      }
544    }
545
546    private void CollectParentResources(List<Resource> resources, Resource resource) {
547      if (resource == null) return;
548      resources.Add(resource);
549      CollectParentResources(resources, resource.ParentResource);
550    }
551    #endregion
552
553    #region Authorization Methods
554    public Permission GetPermissionForJob(Guid jobId, Guid userId) {
555      using (var db = CreateContext()) {
556        return GetPermissionForExperiment(GetExperimentForJob(jobId), userId);
557      }
558    }
559
560    public Permission GetPermissionForExperiment(Guid experimentId, Guid userId) {
561      using (var db = CreateContext()) {
562        HiveExperimentPermission permission = db.HiveExperimentPermissions.SingleOrDefault(p => p.HiveExperimentId == experimentId && p.GrantedUserId == userId);
563        return permission != null ? permission.Permission : Permission.NotAllowed;
564      }
565    }
566
567    public Guid GetExperimentForJob(Guid jobId) {
568      using (var db = CreateContext()) {
569        var job = db.Jobs.SingleOrDefault(j => j.JobId == jobId);
570        if (job.ParentJobId.HasValue) {
571          return GetExperimentForJob(job.ParentJobId.Value);
572        } else {
573          return db.HiveExperiments.SingleOrDefault(he => he.RootJobId == jobId).HiveExperimentId;
574        }
575      }
576    }
577
578    #endregion
579
580    #region Lifecycle Methods
581    public DateTime GetLastCleanup() {
582      using (var db = CreateContext()) {
583        var entity = db.Lifecycles.SingleOrDefault();
584        return entity != null ? entity.LastCleanup : DateTime.MinValue;
585      }
586    }
587
588    public void SetLastCleanup(DateTime datetime) {
589      using (var db = CreateContext()) {
590        var entity = db.Lifecycles.SingleOrDefault();
591        if (entity != null) {
592          entity.LastCleanup = datetime;
593        } else {
594          entity = new Lifecycle();
595          entity.LifecycleId = 0; // always only one entry with ID:0
596          entity.LastCleanup = datetime;
597          db.Lifecycles.InsertOnSubmit(entity);
598        }
599        db.SubmitChanges();
600      }
601    }
602    #endregion
603
604    #region AppointmentMethods
605    public Appointment GetAppointment(Guid id) {
606      using (var db = CreateContext()) {
607        return Convert.ToDto(db.UptimeCalendars.SingleOrDefault(x => x.UptimeCalendarId == id));
608      }
609    }
610
611    public IEnumerable<Appointment> GetAppointments(Expression<Func<UptimeCalendar, bool>> predicate) {
612      using (var db = CreateContext()) {
613        return db.UptimeCalendars.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
614      }
615    }
616
617    public Guid AddAppointment(Appointment dto) {
618      using (var db = CreateContext()) {
619        var entity = Convert.ToEntity(dto);
620        db.UptimeCalendars.InsertOnSubmit(entity);
621        db.SubmitChanges();
622        return entity.UptimeCalendarId;
623      }
624    }
625
626    public void UpdateAppointment(Appointment dto) {
627      using (var db = CreateContext()) {
628        var entity = db.UptimeCalendars.FirstOrDefault(x => x.UptimeCalendarId == dto.Id);
629        if (entity == null) db.UptimeCalendars.InsertOnSubmit(Convert.ToEntity(dto));
630        else Convert.ToEntity(dto, entity);
631        db.SubmitChanges();
632      }
633    }
634
635    public void DeleteAppointment(Guid id) {
636      using (var db = CreateContext()) {
637        var entity = db.UptimeCalendars.FirstOrDefault(x => x.UptimeCalendarId == id);
638        if (entity != null) db.UptimeCalendars.DeleteOnSubmit(entity);
639        db.SubmitChanges();
640      }
641    }
642    #endregion
643  }
644}
Note: See TracBrowser for help on using the repository browser.