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