Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CEDMA-Refactoring-Ticket419/HeuristicLab.Hive.Server.ADODataAccess/ResourceAdapter.cs @ 978

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

Added user adapter to the service locator (#372)

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