Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1396 was 1377, checked in by svonolfe, 15 years ago

Created Heuristiclab DB Core (refactoring) #527

File size: 4.8 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.Name = resource.Name;
74
75        return row;
76      } else
77        return null;
78    }
79
80    protected override void UpdateRow(dsHiveServer.ResourceRow row) {
81      Adapter.Update(row);
82    }
83
84    protected override dsHiveServer.ResourceRow
85      InsertNewRow(Resource resource) {
86      dsHiveServer.ResourceDataTable data =
87        new dsHiveServer.ResourceDataTable();
88
89      dsHiveServer.ResourceRow row = data.NewResourceRow();
90      data.AddResourceRow(row);
91
92      return row;
93    }
94
95    protected override dsHiveServer.ResourceRow
96      InsertNewRowInCache(Resource resource) {
97      dsHiveServer.ResourceRow row = cache.NewResourceRow();
98      cache.AddResourceRow(row);
99
100      return row;
101    }
102
103    protected override void FillCache() {
104      Adapter.FillByActive(cache);
105    }
106
107    protected override void SynchronizeWithDb() {
108      Adapter.Update(cache);
109    }
110
111    protected override bool PutInCache(Resource obj) {
112      return (obj is ClientInfo &&
113        (obj as ClientInfo).State != State.offline);
114    }
115
116    protected override IEnumerable<dsHiveServer.ResourceRow>
117      FindById(long id) {
118      return Adapter.GetDataById(id);
119    }
120
121    protected override dsHiveServer.ResourceRow
122      FindCachedById(long id) {
123      return cache.FindByResourceId(id);
124    }
125
126    protected override IEnumerable<dsHiveServer.ResourceRow>
127      FindAll() {
128      return FindMultipleRows(
129        new Selector(Adapter.GetData),
130        new Selector(cache.AsEnumerable<dsHiveServer.ResourceRow>));
131    }
132
133    #endregion
134
135    #region IResourceAdapter Members
136    public bool GetById(Resource resource) {
137      if (resource != null) {
138        dsHiveServer.ResourceRow row =
139          GetRowById(resource.Id);
140
141        if (row != null) {
142          Convert(row, resource);
143
144          return true;
145        }
146      }
147
148      return false;
149    }
150
151    public Resource GetByName(string name) {
152      dsHiveServer.ResourceRow row =
153        base.FindSingleRow(
154          delegate() {
155            return Adapter.GetDataByName(name);
156          },
157          delegate() {
158            return from r in
159                     cache.AsEnumerable<dsHiveServer.ResourceRow>()
160                   where !r.IsNameNull() &&
161                          r.Name == name
162                   select r;
163          });
164
165      if (row != null) {
166        Resource res = new Resource();
167        res = Convert(row, res);
168
169        return res;
170      } else {
171        return null;
172      }
173    }
174
175    #endregion
176  }
177}
Note: See TracBrowser for help on using the repository browser.