Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Server.ADODataAccess/ResourceAdapter.cs @ 976

Last change on this file since 976 was 976, checked in by svonolfe, 16 years ago

Fixed selecting by name (#372)

File size: 4.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Hive.Server.Core.InternalInterfaces.DataAccess;
27using HeuristicLab.Hive.Contracts.BusinessObjects;
28using System.Runtime.CompilerServices;
29
30namespace HeuristicLab.Hive.Server.ADODataAccess {
31  class ResourceAdapter: DataAdapterBase, IResourceAdapter {
32    private dsHiveServerTableAdapters.ResourceTableAdapter adapter =
33        new dsHiveServerTableAdapters.ResourceTableAdapter();
34
35    private dsHiveServer.ResourceDataTable data =
36        new dsHiveServer.ResourceDataTable();
37
38    public ResourceAdapter() {
39      adapter.Fill(data);
40    }
41
42    protected override void Update() {
43      this.adapter.Update(this.data);
44    }
45
46    private Resource Convert(dsHiveServer.ResourceRow row,
47      Resource resource) {
48      if (row != null && resource != null) {
49        resource.ResourceId = row.ResourceId;
50        if (!row.IsNameNull())
51          resource.Name = row.Name;
52        else
53          resource.Name = String.Empty;
54
55        return resource;
56      } else
57        return null;
58    }
59
60    private dsHiveServer.ResourceRow Convert(Resource resource,
61      dsHiveServer.ResourceRow row) {
62      if (resource != null && row != null) {
63        row.Name = resource.Name;
64
65        return row;
66      } else
67        return null;
68    }
69
70    #region IResourceAdapter Members
71    [MethodImpl(MethodImplOptions.Synchronized)]
72    public void UpdateResource(Resource resource) {
73      if (resource != null) {
74        dsHiveServer.ResourceRow row =
75          data.FindByResourceId(resource.ResourceId);
76
77        if (row == null) {
78          row = data.NewResourceRow();
79          data.AddResourceRow(row);
80
81          //write row to db to get primary key
82          adapter.Update(row);
83        }
84
85        Convert(resource, row);
86        resource.ResourceId = row.ResourceId;
87      }
88    }
89
90    public bool GetResourceById(Resource resource) {
91      if (resource != null) {
92        dsHiveServer.ResourceRow row =
93          data.FindByResourceId(resource.ResourceId);
94        if (row != null) {
95          Convert(row, resource);
96
97          return true;
98        }
99      }
100
101      return false;
102    }
103
104    public Resource GetResourceById(long resourceId) {
105      Resource resource = new Resource();
106      resource.ResourceId = resourceId;
107
108      if(GetResourceById(resource))
109        return resource;
110      else
111        return null;
112    }
113
114    public Resource GetResourceByName(string name) {
115      dsHiveServer.ResourceRow row = null;
116
117      IEnumerable<dsHiveServer.ResourceRow> permOwners =
118        from r in
119          data.AsEnumerable<dsHiveServer.ResourceRow>()
120        where !r.IsNameNull() && r.Name == name
121        select r;
122
123      if (permOwners.Count<dsHiveServer.ResourceRow>() == 1)
124        row = permOwners.First<dsHiveServer.ResourceRow>();
125
126      if (row != null) {
127        Resource res = new Resource();
128        Convert(row, res);
129
130        return res;
131      } else {
132        return null;
133      }
134    }
135
136    public ICollection<Resource> GetAllResources() {
137      IList<Resource> allResources =
138        new List<Resource>();
139     
140      foreach (dsHiveServer.ResourceRow row in data) {
141        Resource resource = new Resource();
142        Convert(row, resource);
143        allResources.Add(resource);
144      }
145
146      return allResources;
147    }
148
149    [MethodImpl(MethodImplOptions.Synchronized)]
150    public bool DeleteResource(Resource resource) {
151      if(resource != null) {
152        dsHiveServer.ResourceRow row =
153          data.FindByResourceId(resource.ResourceId);
154
155        if (row != null) {
156          row.Delete();
157          adapter.Update(row);
158
159          return true;
160        }
161      }
162       
163      return false;
164    }
165
166    #endregion
167  }
168}
Note: See TracBrowser for help on using the repository browser.