Free cookie consent management tool by TermsFeed Policy Generator

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

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

Refactored DAL, avoided possible race conditions (#372)

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