Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.2/sources/HeuristicLab.Hive.Server.LINQDataAccess/3.2/ClientDao.cs @ 3931

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

added minor speedups and better transaction handling to the server (#828)

File size: 3.9 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Hive.Contracts.BusinessObjects;
6using HeuristicLab.Hive.Server.DataAccess;
7using System.Threading;
8
9namespace HeuristicLab.Hive.Server.LINQDataAccess {
10  public class ClientDao: BaseDao<ClientDto, Client>, IClientDao {
11
12    public ClientDao() {     
13    }
14
15    public ClientDto FindById(Guid id) {
16      return (from client in Context.Resources.OfType<Client>()                         
17              where client.ResourceId.Equals(id)
18              select EntityToDto(client, null)
19            ).SingleOrDefault();     
20    }
21
22    public IEnumerable<ClientDto> FindAll() {
23      return (from client in Context.Resources.OfType<Client>()
24              select
25                EntityToDto(client, null)
26             ).ToList();
27    }
28
29    public IEnumerable<ClientDto> FindAllClientsWithoutGroup() {
30      return (from client in Context.Resources.OfType<Client>()
31              where client.ClientGroup_Resources_Parents.Count == 0
32              select EntityToDto(client, null)).ToList();
33    }
34
35    public ClientDto GetClientForJob(Guid jobId) {
36      return (from job in Context.Jobs
37              where job.JobId.Equals(jobId)
38              select EntityToDto(job.Client, null)).SingleOrDefault();
39    }
40
41    public void SetServerSideCalendar(ClientDto client, Guid clientGroupId) {
42      Client dbclient = Context.Resources.OfType<Client>().SingleOrDefault(c => c.ResourceId.Equals(client.Id));
43      dbclient.UseCalendarFromResourceId = clientGroupId;
44      dbclient.CalendarSyncStatus = Enum.GetName(typeof(CalendarState), CalendarState.Fetch);
45      CommitChanges();
46    }
47
48    public ClientDto Insert(ClientDto info) {
49      Client c = DtoToEntity(info, null);     
50      Context.Resources.InsertOnSubmit(c);
51      CommitChanges();
52      info.Id = c.ResourceId;
53      return info;
54    }
55
56    //Cascading delete takes care of the rest
57    public void Delete(ClientDto info) {
58      Resource res = Context.Resources.SingleOrDefault(c => c.ResourceId.Equals(info.Id));           
59      Context.Resources.DeleteOnSubmit(res);
60      CommitChanges();
61    }
62
63    public void Update(ClientDto info) {
64      Client client = Context.Resources.OfType<Client>().SingleOrDefault(c => c.ResourceId.Equals(info.Id));
65      DtoToEntity(info, client);
66      CommitChanges();     
67    }
68
69    public override Client DtoToEntity(ClientDto source, Client target) {
70      if (source == null)
71        return null;
72      if (target == null)
73        target = new Client();
74     
75      target.CPUSpeed = source.CpuSpeedPerCore;
76     
77      target.FreeMemory = source.FreeMemory;
78      target.Name = source.Name;
79      target.ResourceId = source.Id;
80      target.CalendarSyncStatus = Enum.GetName(typeof(CalendarState), source.CalendarSyncStatus);
81      target.Login = source.Login;
82      target.Memory = source.Memory;
83      target.NumberOfCores = source.NrOfCores;
84      target.NumberOfFreeCores = source.NrOfFreeCores;
85      target.Status = Enum.GetName(typeof(State), source.State);
86      return target;
87    }
88
89    public override ClientDto EntityToDto(Client source, ClientDto target) {
90      if (source == null)
91        return null;
92      if(target == null)
93        target = new ClientDto();
94      target.CpuSpeedPerCore = source.CPUSpeed;
95      target.FreeMemory = source.FreeMemory ?? 0;
96      target.Id = source.ResourceId;
97      target.CalendarSyncStatus = (CalendarState) Enum.Parse(typeof (CalendarState), source.CalendarSyncStatus);
98      target.Login = source.Login;
99      target.Memory = source.Memory ?? 0;
100      target.Name = source.Name;
101      target.NrOfCores = source.NumberOfCores ?? 0;
102      target.NrOfFreeCores = source.NumberOfFreeCores ?? 0;
103      target.State = (State) Enum.Parse(typeof (State), source.Status);
104      return target;
105    }
106  }
107}
Note: See TracBrowser for help on using the repository browser.