Free cookie consent management tool by TermsFeed Policy Generator

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

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

Added caching, thread safety to DataAccess layer (#372)

File size: 4.0 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 ICollection<Resource> GetAllResources() {
115      IList<Resource> allResources =
116        new List<Resource>();
117     
118      foreach (dsHiveServer.ResourceRow row in data) {
119        Resource resource = new Resource();
120        Convert(row, resource);
121        allResources.Add(resource);
122      }
123
124      return allResources;
125    }
126
127    [MethodImpl(MethodImplOptions.Synchronized)]
128    public bool DeleteResource(Resource resource) {
129      if(resource != null) {
130        dsHiveServer.ResourceRow row =
131          data.FindByResourceId(resource.ResourceId);
132
133        if (row != null) {
134          data.RemoveResourceRow(row);
135
136          return true;
137        }
138      }
139       
140      return false;
141    }
142
143    #endregion
144  }
145}
Note: See TracBrowser for help on using the repository browser.