Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3494 was 3203, checked in by kgrading, 15 years ago

implemented the server on the client, using push & force push, added refresh buttons, added auto calender methods that traverse the tree... (#908)

File size: 4.1 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      Context.SubmitChanges();
46    }
47
48    public ClientDto Insert(ClientDto info) {
49      Client c = DtoToEntity(info, null);     
50      Context.Clients.InsertOnSubmit(c);
51      Context.SubmitChanges();
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      Context.SubmitChanges();
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      try {
67        Console.WriteLine("Sending from thread: " + Thread.CurrentThread.ManagedThreadId);
68        Context.SubmitChanges();
69      } catch (System.Data.Linq.ChangeConflictException cce) {
70        Console.WriteLine(cce);       
71      }
72    }
73
74    public override Client DtoToEntity(ClientDto source, Client target) {
75      if (source == null)
76        return null;
77      if (target == null)
78        target = new Client();
79     
80      target.CPUSpeed = source.CpuSpeedPerCore;
81     
82      if(target.Resource == null)
83        target.Resource = new Resource();
84
85      target.FreeMemory = source.FreeMemory;
86      target.Resource.Name = source.Name;
87      target.Resource.ResourceId = source.Id;
88      target.CalendarSyncStatus = Enum.GetName(typeof(CalendarState), source.CalendarSyncStatus);
89      target.Login = source.Login;
90      target.Memory = source.Memory;
91      target.NumberOfCores = source.NrOfCores;
92      target.NumberOfFreeCores = source.NrOfFreeCores;
93      target.Status = Enum.GetName(typeof(State), source.State);
94      return target;
95    }
96
97    public override ClientDto EntityToDto(Client source, ClientDto target) {
98      if (source == null)
99        return null;
100      if(target == null)
101        target = new ClientDto();
102      target.CpuSpeedPerCore = source.CPUSpeed;
103      target.FreeMemory = source.FreeMemory;
104      target.Id = source.ResourceId;
105      target.CalendarSyncStatus = (CalendarState) Enum.Parse(typeof (CalendarState), source.CalendarSyncStatus);
106      target.Login = source.Login;
107      target.Memory = source.Memory;
108      target.Name = source.Resource.Name;
109      target.NrOfCores = source.NumberOfCores;
110      target.NrOfFreeCores = source.NumberOfFreeCores;
111      target.State = (State) Enum.Parse(typeof (State), source.Status);
112      return target;
113    }
114  }
115}
Note: See TracBrowser for help on using the repository browser.