Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1197
commit completed

File size: 4.6 KB
RevLine 
[4695]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
18  public enum eResource {
19    ResourceID = 1, Name = 2, Description = 3, ResourceType = 4,
20    ProcessorType = 5, NumberOfProcessors = 6, NumberOfThreads = 7,
21    MemorySize = 8, IPAdress = 9, OperatingSystem = 10, ResourceNameGroup, ResourceIDGroup,
22    GroupGroupID, GroupGroupName
23  };
24
25  public abstract class ResourceAdmin {   
26
27    public abstract HashSet<Hashtable> Get(Hashtable values);
28    public abstract bool Create(Hashtable values);   
29    public abstract bool Update(Hashtable values);   
30   
31    public bool RemoveRRGEntry(ClientManagmentDataContext cmdc, Guid resID) {
32      ResourceResourceGroup rrgEntity = cmdc.GetTable<ResourceResourceGroup>().FirstOrDefault(
33                      x => x.ResourceID == resID);
34      //ResourceResourceGroup rrgEntityGroup = cmdc.GetTable<ResourceResourceGroup>().FirstOrDefault(
35      //                x => x.ResourceGroupID == resID);
36      if (rrgEntity != null) { // && (rrgEntityGroup == null)) {
37        cmdc.GetTable<ResourceResourceGroup>().DeleteOnSubmit(rrgEntity);
38        return true;
39      }
40      return false;
41    }
42   
43    public void CreateRRGEntry(ClientManagmentDataContext cmdc, Guid clientID, Guid groupID) {
44      ResourceResourceGroup rrgEntity = new ResourceResourceGroup();
45      rrgEntity.ResourceGroupID = groupID;
46      rrgEntity.ResourceID = clientID;
47      cmdc.GetTable<ResourceResourceGroup>().InsertOnSubmit(rrgEntity);
48    }
49   
50    public void GetGroupIDandName(ref String groupID, ref String groupName, ClientManagmentDataContext cmdc, Guid clientID) {
51      ResourceResourceGroup rrgEntity = cmdc.GetTable<ResourceResourceGroup>().FirstOrDefault(
52                               x => x.ResourceID == clientID);
53      if (rrgEntity != null) {
54        groupID = (rrgEntity.ResourceGroupID).ToString();
55        DA.Resource groupEntity = cmdc.GetTable<DA.Resource>().FirstOrDefault(
56                  x => x.ResourceID == rrgEntity.ResourceGroupID);
57        groupName = groupEntity.Name;
58      }
59    }
60   
61    public void CreateQueryStringExtension(List<String> compareValues, ref String QueryStringExtension, Hashtable ht) {
62      int i = 0;
63      foreach (DictionaryEntry kvp in ht) {
64        if (((string)kvp.Value != "") &&
65          (kvp.Key.Equals(eResource.ResourceNameGroup) == false) &&
66          (kvp.Key.Equals(eResource.ResourceIDGroup) == false)) {
67          compareValues.Add(String.Format("%" + (ht[kvp.Key]).ToString() + "%"));
68          if (i == 0)
69            QueryStringExtension += (kvp.Key.ToString() + " LIKE {" + i + "}");
70          else
71            QueryStringExtension += " AND " + (kvp.Key.ToString() + " LIKE {" + i + "}");
72          i++;
73        }
74      }
75    }
76   
77    public bool Delete(Hashtable values) {
78      string idStr = (string)values[eResource.ResourceID];
79      if ((idStr == "") || (!IsIDString(idStr))) {
80        return false;
81      }
82
83      using (ClientManagmentDataContext cmdc = new ClientManagmentDataContext()) {
84        Guid clientID = new Guid(idStr);
85
86        if (cmdc.DatabaseExists()) {
87          // remove entry in ResourceResourcGroup 
88          //if (!(RemoveRRGEntry(cmdc, clientID)))
89          //  return false;
90          RemoveRRGEntry(cmdc, clientID);
91          // check if resource is member of a group
92          DA.Resource Entity = cmdc.GetTable<DA.Resource>().FirstOrDefault(x => x.ResourceID == new Guid(idStr));
93         
94          // only allow removing if group is not still in use
95          DA.ResourceResourceGroup rrgEntity = cmdc.GetTable<DA.ResourceResourceGroup>()
96                              .FirstOrDefault(x => x.ResourceGroupID == new Guid(idStr));
97          if ((Entity != null) && (rrgEntity == null)) {
98            cmdc.GetTable<DA.Resource>().DeleteOnSubmit(Entity);
99            cmdc.SubmitChanges();
100            return true;
101          }
102        }
103      } return false;
104    }
105
106    public bool IsIDString (string idString){
107      try{
108        Guid gid = new Guid(idString);
109        return true;
110      }
111      catch (Exception e) {return false;}
112    }
113
114  }
115}
Note: See TracBrowser for help on using the repository browser.