1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Hive.Contracts.BusinessObjects;
|
---|
6 | using HeuristicLab.Hive.Server.DataAccess;
|
---|
7 | using System.Threading;
|
---|
8 |
|
---|
9 | namespace 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 ClientDto Insert(ClientDto info) {
|
---|
42 | Client c = DtoToEntity(info, null);
|
---|
43 | Context.Clients.InsertOnSubmit(c);
|
---|
44 | Context.SubmitChanges();
|
---|
45 | info.Id = c.ResourceId;
|
---|
46 | return info;
|
---|
47 | }
|
---|
48 |
|
---|
49 | //Cascading delete takes care of the rest
|
---|
50 | public void Delete(ClientDto info) {
|
---|
51 | Resource res = Context.Resources.SingleOrDefault(c => c.ResourceId.Equals(info.Id));
|
---|
52 | Context.Resources.DeleteOnSubmit(res);
|
---|
53 | Context.SubmitChanges();
|
---|
54 | }
|
---|
55 |
|
---|
56 | public void Update(ClientDto info) {
|
---|
57 | Client client = Context.Clients.SingleOrDefault(c => c.ResourceId.Equals(info.Id));
|
---|
58 | DtoToEntity(info, client);
|
---|
59 | try {
|
---|
60 | Console.WriteLine("Sending from thread: " + Thread.CurrentThread.ManagedThreadId);
|
---|
61 | Context.SubmitChanges();
|
---|
62 | } catch (System.Data.Linq.ChangeConflictException cce) {
|
---|
63 | Console.WriteLine(cce);
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | public override Client DtoToEntity(ClientDto source, Client target) {
|
---|
68 | if (source == null)
|
---|
69 | return null;
|
---|
70 | if (target == null)
|
---|
71 | target = new Client();
|
---|
72 |
|
---|
73 | target.CPUSpeed = source.CpuSpeedPerCore;
|
---|
74 |
|
---|
75 | if(target.Resource == null)
|
---|
76 | target.Resource = new Resource();
|
---|
77 |
|
---|
78 | target.FreeMemory = source.FreeMemory;
|
---|
79 | target.Resource.Name = source.Name;
|
---|
80 | target.Resource.ResourceId = source.Id;
|
---|
81 |
|
---|
82 | target.Login = source.Login;
|
---|
83 | target.Memory = source.Memory;
|
---|
84 | target.NumberOfCores = source.NrOfCores;
|
---|
85 | target.NumberOfFreeCores = source.NrOfFreeCores;
|
---|
86 | target.Status = Enum.GetName(typeof(State), source.State);
|
---|
87 | return target;
|
---|
88 | }
|
---|
89 |
|
---|
90 | public override ClientDto EntityToDto(Client source, ClientDto target) {
|
---|
91 | if (source == null)
|
---|
92 | return null;
|
---|
93 | if(target == null)
|
---|
94 | target = new ClientDto();
|
---|
95 | target.CpuSpeedPerCore = source.CPUSpeed;
|
---|
96 | target.FreeMemory = source.FreeMemory;
|
---|
97 | target.Id = source.ResourceId;
|
---|
98 | target.Login = source.Login;
|
---|
99 | target.Memory = source.Memory;
|
---|
100 | target.Name = source.Resource.Name;
|
---|
101 | target.NrOfCores = source.NumberOfCores;
|
---|
102 | target.NrOfFreeCores = source.NumberOfFreeCores;
|
---|
103 | target.State = (State) Enum.Parse(typeof (State), source.Status);
|
---|
104 | return target;
|
---|
105 | }
|
---|
106 | }
|
---|
107 | }
|
---|