Free cookie consent management tool by TermsFeed Policy Generator

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

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

Fixed selecting by name (#372)

File size: 4.6 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;
26using HeuristicLab.Hive.Server.Core.InternalInterfaces.DataAccess;
27using HeuristicLab.Hive.Contracts.BusinessObjects;
[925]28using System.Runtime.CompilerServices;
[826]29
30namespace HeuristicLab.Hive.Server.ADODataAccess {
[925]31  class ResourceAdapter: DataAdapterBase, IResourceAdapter {
[826]32    private dsHiveServerTableAdapters.ResourceTableAdapter adapter =
[925]33        new dsHiveServerTableAdapters.ResourceTableAdapter();
[826]34
[925]35    private dsHiveServer.ResourceDataTable data =
36        new dsHiveServer.ResourceDataTable();
37
38    public ResourceAdapter() {
39      adapter.Fill(data);
40    }
41
42    protected override void Update() {
43      this.adapter.Update(this.data);
44    }
45
46    private Resource Convert(dsHiveServer.ResourceRow row,
[899]47      Resource resource) {
48      if (row != null && resource != null) {
[845]49        resource.ResourceId = row.ResourceId;
[899]50        if (!row.IsNameNull())
51          resource.Name = row.Name;
52        else
53          resource.Name = String.Empty;
[845]54
55        return resource;
[925]56      } else
[845]57        return null;
58    }
59
60    private dsHiveServer.ResourceRow Convert(Resource resource,
61      dsHiveServer.ResourceRow row) {
62      if (resource != null && row != null) {
63        row.Name = resource.Name;
64
[899]65        return row;
66      } else
67        return null;
[845]68    }
69
[925]70    #region IResourceAdapter Members
71    [MethodImpl(MethodImplOptions.Synchronized)]
[826]72    public void UpdateResource(Resource resource) {
73      if (resource != null) {
[925]74        dsHiveServer.ResourceRow row =
75          data.FindByResourceId(resource.ResourceId);
[826]76
[925]77        if (row == null) {
[826]78          row = data.NewResourceRow();
79          data.AddResourceRow(row);
80
[925]81          //write row to db to get primary key
82          adapter.Update(row);
83        }
84
[845]85        Convert(resource, row);
[826]86        resource.ResourceId = row.ResourceId;
87      }
88    }
89
[925]90    public bool GetResourceById(Resource resource) {
[899]91      if (resource != null) {
[925]92        dsHiveServer.ResourceRow row =
93          data.FindByResourceId(resource.ResourceId);
94        if (row != null) {
[899]95          Convert(row, resource);
96
97          return true;
98        }
99      }
100
101      return false;
102    }
103
[845]104    public Resource GetResourceById(long resourceId) {
[899]105      Resource resource = new Resource();
106      resource.ResourceId = resourceId;
107
[925]108      if(GetResourceById(resource))
[899]109        return resource;
110      else
[845]111        return null;
[826]112    }
113
[971]114    public Resource GetResourceByName(string name) {
[976]115      dsHiveServer.ResourceRow row = null;
[971]116
[976]117      IEnumerable<dsHiveServer.ResourceRow> permOwners =
118        from r in
119          data.AsEnumerable<dsHiveServer.ResourceRow>()
120        where !r.IsNameNull() && r.Name == name
121        select r;
[971]122
[976]123      if (permOwners.Count<dsHiveServer.ResourceRow>() == 1)
124        row = permOwners.First<dsHiveServer.ResourceRow>();
125
[971]126      if (row != null) {
[976]127        Resource res = new Resource();
128        Convert(row, res);
[971]129
[976]130        return res;
[971]131      } else {
132        return null;
133      }
134    }
135
[826]136    public ICollection<Resource> GetAllResources() {
[925]137      IList<Resource> allResources =
[845]138        new List<Resource>();
[925]139     
[845]140      foreach (dsHiveServer.ResourceRow row in data) {
[899]141        Resource resource = new Resource();
142        Convert(row, resource);
143        allResources.Add(resource);
[845]144      }
145
146      return allResources;
[826]147    }
148
[925]149    [MethodImpl(MethodImplOptions.Synchronized)]
[826]150    public bool DeleteResource(Resource resource) {
[845]151      if(resource != null) {
[925]152        dsHiveServer.ResourceRow row =
153          data.FindByResourceId(resource.ResourceId);
[826]154
[925]155        if (row != null) {
[972]156          row.Delete();
157          adapter.Update(row);
[826]158
[925]159          return true;
[845]160        }
[826]161      }
[845]162       
163      return false;
[826]164    }
165
166    #endregion
167  }
168}
Note: See TracBrowser for help on using the repository browser.