Free cookie consent management tool by TermsFeed Policy Generator

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

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

Refactored DAL, Improved Caching (#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;
28using System.Runtime.CompilerServices;
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    #region Fields
39    dsHiveServer.ResourceDataTable data =
40        new dsHiveServer.ResourceDataTable();
41
42    private IClientAdapter clientAdapter = null;
43
44    private IClientAdapter ClientAdapter {
45      get {
46        if (clientAdapter == null)
47          clientAdapter = ServiceLocator.GetClientAdapter();
48       
49        return clientAdapter;
50      }
51    }
52    #endregion
53
54    #region Overrides
55    protected override Resource Convert(dsHiveServer.ResourceRow row,
56      Resource resource) {
57      if (row != null && resource != null) {
58        resource.Id = row.ResourceId;
59        if (!row.IsNameNull())
60          resource.Name = row.Name;
61        else
62          resource.Name = String.Empty;
63
64        return resource;
65      } else
66        return null;
67    }
68
69    protected override dsHiveServer.ResourceRow Convert(Resource resource,
70      dsHiveServer.ResourceRow row) {
71      if (resource != null && row != null) {
72        row.Name = resource.Name;
73
74        return row;
75      } else
76        return null;
77    }
78
79    protected override void UpdateRow(dsHiveServer.ResourceRow row) {
80      adapter.Update(row);
81    }
82
83    protected override dsHiveServer.ResourceRow
84      InsertNewRow(Resource resource) {
85      dsHiveServer.ResourceRow row = data.NewResourceRow();
86      data.AddResourceRow(row);
87
88      return row;
89    }
90
91    protected override dsHiveServer.ResourceRow
92      InsertNewRowInCache(Resource resource) {
93      dsHiveServer.ResourceRow row = cache.NewResourceRow();
94      cache.AddResourceRow(row);
95
96      return row;
97    }
98
99    protected override void FillCache() {
100      cache = adapter.GetDataByActive();
101    }
102
103    public override void SyncWithDb() {
104      adapter.Update(cache);
105    }
106
107    protected override bool PutInCache(Resource obj) {
108      return (obj is ClientInfo &&
109        (obj as ClientInfo).State != State.offline);
110    }
111
112    protected override IEnumerable<dsHiveServer.ResourceRow>
113      FindById(long id) {
114      return adapter.GetDataById(id);
115    }
116
117    protected override dsHiveServer.ResourceRow
118      FindCachedById(long id) {
119      return cache.FindByResourceId(id);
120    }
121
122    protected override IEnumerable<dsHiveServer.ResourceRow>
123      FindAll() {
124      return FindMultipleRows(
125        new Selector(adapter.GetData),
126        new Selector(cache.AsEnumerable<dsHiveServer.ResourceRow>));
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        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.