Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ClientManagement/HeuristicLab.Services.Authentication/HeuristicLab.Services.Authentication.AdminMethods/ClientAdmin.cs @ 4728

Last change on this file since 4728 was 4695, checked in by fruehrli, 14 years ago

#1197
commit completed

File size: 7.2 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Collections;
4using System.Linq;
5using System.Data.Linq;
6using System.Data.Linq.Mapping;
7using System.Text;
8
9using HeuristicLab.Services.Authentication.DataAccess;
10using HeuristicLab.Services.Authentication.DataTransfer;
11using HeuristicLab.Services.Authentication;
12using DA = HeuristicLab.Services.Authentication.DataAccess;
13using DT = HeuristicLab.Services.Authentication.DataTransfer;
14using HLSA = HeuristicLab.Services.Authentication;
15
16namespace HeuristicLab.Services.Authentication.AdminMethods {
17  public class ClientAdmin : ResourceAdmin {
18
19    private void TransferData(DT.Client c, Hashtable values) {
20      c.ResourceID = new Guid((string)values[eResource.ResourceID]);
21      c.Name = (string)values[eResource.Name];
22      c.Description = (string)values[eResource.Description];
23      c.ResourceType = "Client";
24      c.ProcessorType = (string)values[eResource.ProcessorType];
25      c.NumberOfProcessors = (string)values[eResource.NumberOfProcessors];
26      c.NumberOfThreads = (string)values[eResource.NumberOfThreads];
27      c.MemorySize = (string)values[eResource.MemorySize];
28      c.IPAdress = (string)values[eResource.IPAdress];
29      c.OperatingSystem = (string)values[eResource.OperatingSystem];
30    }
31
32    public override HashSet<Hashtable> Get(Hashtable values) {
33      values[eResource.ResourceType] = "Client";
34      String QueryString = "SELECT * FROM Resource";
35      List<String> lcv = new List<String>();
36      String QueryStringExtension = " WHERE ";
37      CreateQueryStringExtension(lcv, ref QueryStringExtension, values);
38      if (QueryStringExtension.CompareTo(" WHERE ") > 0) {
39        QueryString += QueryStringExtension;
40      } // else select all
41      //string val1 = "%" + ((string)tmpHt[eClient.Name]) + "%";
42      //QueryString += eClient.Name + " LIKE {0}";
43      object[] compValues = lcv.ToArray();
44      using (ClientManagmentDataContext cmdc = new ClientManagmentDataContext()) {
45        if (cmdc.DatabaseExists()) {
46          Table<DA.Resource> clTable = cmdc.GetTable<DA.Resource>();
47          IEnumerable<DA.Resource> clients = cmdc.ExecuteQuery<DA.Resource>(QueryString, compValues);
48          HashSet<Hashtable> resultSet = new HashSet<Hashtable>();
49          var selectedClients = from c in clients orderby c.Name select HLSA.Convert.ToDto((DA.Client)c);
50          foreach (DT.Client cl in selectedClients) {
51            Hashtable ht = new Hashtable();
52            ht.Add(eResource.ResourceID, cl.ResourceID);
53            ht.Add(eResource.Name, cl.Name);
54            ht.Add(eResource.Description, cl.Description);
55            ht.Add(eResource.ResourceType, cl.ResourceType);
56            ht.Add(eResource.ProcessorType, cl.ProcessorType);
57            ht.Add(eResource.NumberOfProcessors, cl.NumberOfProcessors);
58            ht.Add(eResource.NumberOfThreads, cl.NumberOfThreads);
59            ht.Add(eResource.IPAdress, cl.IPAdress);
60            ht.Add(eResource.MemorySize, cl.MemorySize);
61            ht.Add(eResource.OperatingSystem, cl.OperatingSystem);
62            // look for group entries in ResourceResourcGroup 
63            //Guid groupID = new Guid();  /// ??
64            String groupID = "";
65            String groupName = "";
66            GetGroupIDandName(ref groupID, ref groupName, cmdc, cl.ResourceID);
67            ht.Add(eResource.ResourceIDGroup, groupID);
68            ht.Add(eResource.ResourceNameGroup, groupName);
69
70            resultSet.Add(ht);
71          }
72          return resultSet;
73        }
74        return null;
75      }
76    }
77   
78    public override bool Create(Hashtable values) {
79      if (((string)values[eResource.ResourceIDGroup] == "") ||
80          ((string)values[eResource.ResourceID] != "")) {
81        return false;
82      }
83      DT.Client c = new DT.Client();
84      values[eResource.ResourceID] = Guid.NewGuid().ToString();
85      values[eResource.ResourceType] = "Client";
86      TransferData(c, values);
87      Guid groupID = new Guid((string)values[eResource.ResourceIDGroup]);
88      using (ClientManagmentDataContext cmdc = new ClientManagmentDataContext()) {
89        if (cmdc.DatabaseExists()) {
90          // Client
91          DA.Client clEntity = HLSA.Convert.ToEntity(c);
92          cmdc.GetTable<DA.Resource>().InsertOnSubmit(clEntity);
93
94          Table<DA.Resource> clTable = cmdc.GetTable<DA.Resource>();
95          //Table<ResourceResourceGroup> rrgTable = cmdc.GetTable<ResourceResourceGroup>();
96          DA.Resource groupEntity = cmdc.GetTable<DA.Resource>().FirstOrDefault(x => x.ResourceID == groupID);
97          if (groupEntity == null)
98            return false;
99
100          cmdc.SubmitChanges();
101
102          DA.Resource clientEntity = cmdc.GetTable<DA.Resource>().FirstOrDefault(x => x.ResourceID == c.ResourceID);
103          if (clientEntity == null)
104           return false;
105
106          //ResourceResourceGroup
107          ResourceResourceGroup rrgEntity = new ResourceResourceGroup();
108          rrgEntity.ResourceGroupID = groupEntity.ResourceID;
109          rrgEntity.ResourceID = clientEntity.ResourceID;
110          cmdc.GetTable<ResourceResourceGroup>().InsertOnSubmit(rrgEntity);
111
112          cmdc.SubmitChanges();
113          return true;
114        }
115        return false;
116      }
117    }
118
119    public override bool Update(Hashtable values) {
120      string groupIDString = (string)values[eResource.ResourceIDGroup];
121      string IDString = (string)values[eResource.ResourceID];
122
123      DT.Client uc = new DT.Client();
124      bool groupModified = false;
125      bool success = true;
126      bool validGroupID = (IDString != "") && (IsIDString(groupIDString));
127      bool validID = (IDString != "") && (IsIDString(IDString));
128
129      if (!validGroupID || !validID || (IDString.CompareTo(groupIDString) == 0))
130        return false;
131
132      Guid resID = new Guid(IDString);
133      TransferData(uc, values);
134
135      using (ClientManagmentDataContext cmdc = new ClientManagmentDataContext()) {
136
137        if (cmdc.DatabaseExists()) {
138            Guid currentGroupID = new Guid(groupIDString);
139            Guid oldGroupID = new Guid();
140
141            ResourceResourceGroup rrgEntity = cmdc.GetTable<ResourceResourceGroup>().FirstOrDefault(
142                                 x => x.ResourceID == resID);
143          if (rrgEntity != null) {
144            oldGroupID = rrgEntity.ResourceGroupID;
145          }
146          // check if groupname was modified
147          groupModified = (oldGroupID != currentGroupID);
148
149          DA.Resource clientEntity = cmdc.GetTable<DA.Resource>().FirstOrDefault(x => x.ResourceID == uc.ResourceID);
150          // update client
151          if ((clientEntity != null) && success) {
152            uc.ResourceType = clientEntity.ResourceType; // dont allow change of ResourceTyp
153            HLSA.Convert.ToEntity(uc, (DA.Client)clientEntity);
154            if (groupModified) {
155              // remove entry in ResourceResourcGroup if group modiied
156              RemoveRRGEntry(cmdc, resID);
157            }
158            cmdc.SubmitChanges();
159          }
160
161          if (groupModified && success) {
162            CreateRRGEntry(cmdc, resID, currentGroupID);
163            cmdc.SubmitChanges();
164          }
165        }
166      }
167      return success;
168    }   
169
170  }
171}
Note: See TracBrowser for help on using the repository browser.