Free cookie consent management tool by TermsFeed Policy Generator

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

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

Refactored DAL (now using GUIDs as IDs instead of longs) (#527)

File size: 4.9 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.DataAccess;
27using HeuristicLab.Hive.Contracts.BusinessObjects;
28using HeuristicLab.DataAccess.ADOHelper;
29
30namespace HeuristicLab.Hive.Server.ADODataAccess {
31  class ResourceAdapter:
32    CachedDataAdapter<
33      dsHiveServerTableAdapters.ResourceTableAdapter,
34      Resource,
35      dsHiveServer.ResourceRow,
36      dsHiveServer.ResourceDataTable>, 
37    IResourceAdapter {
38    public ResourceAdapter():
39      base(ServiceLocator.GetDBSynchronizer()) {
40    }
41
42    #region Fields
43    private IClientAdapter clientAdapter = null;
44
45    private IClientAdapter ClientAdapter {
46      get {
47        if (clientAdapter == null)
48          clientAdapter = ServiceLocator.GetClientAdapter();
49       
50        return clientAdapter;
51      }
52    }
53    #endregion
54
55    #region Overrides
56    protected override Resource ConvertRow(dsHiveServer.ResourceRow row,
57      Resource resource) {
58      if (row != null && resource != null) {
59        resource.Id = row.ResourceId;
60        if (!row.IsNameNull())
61          resource.Name = row.Name;
62        else
63          resource.Name = String.Empty;
64
65        return resource;
66      } else
67        return null;
68    }
69
70    protected override dsHiveServer.ResourceRow ConvertObj(Resource resource,
71      dsHiveServer.ResourceRow row) {
72      if (resource != null && row != null) {
73        row.ResourceId = resource.Id;
74        row.Name = resource.Name;
75
76        return row;
77      } else
78        return null;
79    }
80
81    protected override void UpdateRow(dsHiveServer.ResourceRow row) {
82      Adapter.Update(row);
83    }
84
85    protected override dsHiveServer.ResourceRow
86      InsertNewRow(Resource resource) {
87      dsHiveServer.ResourceDataTable data =
88        new dsHiveServer.ResourceDataTable();
89
90      dsHiveServer.ResourceRow row = data.NewResourceRow();
91      row.ResourceId = resource.Id;
92      data.AddResourceRow(row);
93
94      return row;
95    }
96
97    protected override dsHiveServer.ResourceRow
98      InsertNewRowInCache(Resource resource) {
99      dsHiveServer.ResourceRow row = cache.NewResourceRow();
100      row.ResourceId = resource.Id;
101      cache.AddResourceRow(row);
102
103      return row;
104    }
105
106    protected override void FillCache() {
107      Adapter.FillByActive(cache);
108    }
109
110    protected override void SynchronizeWithDb() {
111      Adapter.Update(cache);
112    }
113
114    protected override bool PutInCache(Resource obj) {
115      return (obj is ClientInfo &&
116        (obj as ClientInfo).State != State.offline);
117    }
118
119    protected override IEnumerable<dsHiveServer.ResourceRow>
120      FindById(Guid id) {
121      return Adapter.GetDataById(id);
122    }
123
124    protected override dsHiveServer.ResourceRow
125      FindCachedById(Guid id) {
126      return cache.FindByResourceId(id);
127    }
128
129    protected override IEnumerable<dsHiveServer.ResourceRow>
130      FindAll() {
131      return FindMultipleRows(
132        new Selector(Adapter.GetData),
133        new Selector(cache.AsEnumerable<dsHiveServer.ResourceRow>));
134    }
135
136    #endregion
137
138    #region IResourceAdapter Members
139    public bool GetById(Resource resource) {
140      if (resource != null) {
141        dsHiveServer.ResourceRow row =
142          GetRowById(resource.Id);
143
144        if (row != null) {
145          Convert(row, resource);
146
147          return true;
148        }
149      }
150
151      return false;
152    }
153
154    public Resource GetByName(string name) {
155      dsHiveServer.ResourceRow row =
156        base.FindSingleRow(
157          delegate() {
158            return Adapter.GetDataByName(name);
159          },
160          delegate() {
161            return from r in
162                     cache.AsEnumerable<dsHiveServer.ResourceRow>()
163                   where !r.IsNameNull() &&
164                          r.Name == name
165                   select r;
166          });
167
168      if (row != null) {
169        Resource res = new Resource();
170        res = Convert(row, res);
171
172        return res;
173      } else {
174        return null;
175      }
176    }
177
178    #endregion
179  }
180}
Note: See TracBrowser for help on using the repository browser.