Free cookie consent management tool by TermsFeed Policy Generator

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

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

Fixed deletion (#372)

File size: 4.4 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      Resource resource = new Resource();
116
117      dsHiveServer.ResourceRow row =
118        data.Single<dsHiveServer.ResourceRow>(
119          r => !r.IsNameNull() && r.Name == name);
120
121      if (row != null) {
122        Convert(row, resource);
123
124        return resource;
125      } else {
126        return null;
127      }
128    }
129
130    public ICollection<Resource> GetAllResources() {
131      IList<Resource> allResources =
132        new List<Resource>();
133     
134      foreach (dsHiveServer.ResourceRow row in data) {
135        Resource resource = new Resource();
136        Convert(row, resource);
137        allResources.Add(resource);
138      }
139
140      return allResources;
141    }
142
143    [MethodImpl(MethodImplOptions.Synchronized)]
144    public bool DeleteResource(Resource resource) {
145      if(resource != null) {
146        dsHiveServer.ResourceRow row =
147          data.FindByResourceId(resource.ResourceId);
148
149        if (row != null) {
150          row.Delete();
151          adapter.Update(row);
152
153          return true;
154        }
155      }
156       
157      return false;
158    }
159
160    #endregion
161  }
162}
Note: See TracBrowser for help on using the repository browser.