Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Server.LINQDataAccess/3.2/ClientDao.cs @ 3578

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

Removed References to HiveLogging and updated the default logging mechanism (#991)

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.Clients
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.Clients
24              select
25                EntityToDto(client, null)
26             ).ToList();
27    }
28
29    public IEnumerable<ClientDto> FindAllClientsWithoutGroup() {
30      return (from client in Context.Clients
31              where client.Resource.ClientGroup_Resources.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.Clients.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.Clients.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.Clients.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      if(target.Resource == null)
78        target.Resource = new Resource();
79
80      target.FreeMemory = source.FreeMemory;
81      target.Resource.Name = source.Name;
82      target.Resource.ResourceId = source.Id;
83      target.CalendarSyncStatus = Enum.GetName(typeof(CalendarState), source.CalendarSyncStatus);
84      target.Login = source.Login;
85      target.Memory = source.Memory;
86      target.NumberOfCores = source.NrOfCores;
87      target.NumberOfFreeCores = source.NrOfFreeCores;
88      target.Status = Enum.GetName(typeof(State), source.State);
89      return target;
90    }
91
92    public override ClientDto EntityToDto(Client source, ClientDto target) {
93      if (source == null)
94        return null;
95      if(target == null)
96        target = new ClientDto();
97      target.CpuSpeedPerCore = source.CPUSpeed;
98      target.FreeMemory = source.FreeMemory;
99      target.Id = source.ResourceId;
100      target.CalendarSyncStatus = (CalendarState) Enum.Parse(typeof (CalendarState), source.CalendarSyncStatus);
101      target.Login = source.Login;
102      target.Memory = source.Memory;
103      target.Name = source.Resource.Name;
104      target.NrOfCores = source.NumberOfCores;
105      target.NrOfFreeCores = source.NumberOfFreeCores;
106      target.State = (State) Enum.Parse(typeof (State), source.Status);
107      return target;
108    }
109  }
110}
Note: See TracBrowser for help on using the repository browser.