Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6006 was 6006, checked in by cneumuel, 13 years ago

#1233

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