Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Operator Architecture Refactoring/HeuristicLab.Hive.Server.ADODataAccess/ResourceAdapter.cs @ 1877

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

Added user adapter (#372).

File size: 3.4 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: IResourceAdapter {
31    #region IResourceAdapter Members
32    private dsHiveServerTableAdapters.ResourceTableAdapter adapter =
33      new dsHiveServerTableAdapters.ResourceTableAdapter();
34
35    private Resource Convert(dsHiveServer.ResourceRow row) {
36      if (row != null) {
37        Resource resource = new Resource();
38
39        resource.ResourceId = row.ResourceId;
40        resource.Name = row.Name;
41
42        return resource;
43      } else
44        return null;
45    }
46
47    private dsHiveServer.ResourceRow Convert(Resource resource,
48      dsHiveServer.ResourceRow row) {
49      if (resource != null && row != null) {
50        row.Name = resource.Name;
51      }
52
53      return row;
54    }
55
56    public void UpdateResource(Resource resource) {
57      if (resource != null) {
58        dsHiveServer.ResourceDataTable data =
59          adapter.GetDataById(resource.ResourceId);
60
61        dsHiveServer.ResourceRow row;
62        if (data.Count == 0) {
63          row = data.NewResourceRow();
64          data.AddResourceRow(row);
65        } else {
66          row = data[0];
67        }
68
69        Convert(resource, row);
70
71        adapter.Update(data);
72
73        resource.ResourceId = row.ResourceId;
74      }
75    }
76
77    public Resource GetResourceById(long resourceId) {
78      dsHiveServer.ResourceDataTable data =
79          adapter.GetDataById(resourceId);
80      if (data.Count == 1) {
81        dsHiveServer.ResourceRow row =
82          data[0];
83        return Convert(row);
84      } else {
85        return null;
86      }
87    }
88
89    public ICollection<Resource> GetAllResources() {
90      ICollection<Resource> allResources =
91        new List<Resource>();
92
93      dsHiveServer.ResourceDataTable data =
94          adapter.GetData();
95
96      foreach (dsHiveServer.ResourceRow row in data) {
97        allResources.Add(Convert(row));
98      }
99
100      return allResources;
101    }
102
103    public bool DeleteResource(Resource resource) {
104      if(resource != null) {
105
106        dsHiveServer.ResourceDataTable data =
107           adapter.GetDataById(resource.ResourceId);
108
109        if (data.Count == 1) {
110          dsHiveServer.ResourceRow row = data[0];
111
112          row.Delete();
113          return adapter.Update(data) > 0;
114        }
115      }
116       
117      return false;
118    }
119
120    #endregion
121  }
122}
Note: See TracBrowser for help on using the repository browser.