[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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Text;
|
---|
| 26 | using HeuristicLab.Hive.Server.Core.InternalInterfaces.DataAccess;
|
---|
| 27 | using HeuristicLab.Hive.Contracts.BusinessObjects;
|
---|
[925] | 28 | using System.Runtime.CompilerServices;
|
---|
[826] | 29 |
|
---|
| 30 | namespace 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 |
|
---|
| 114 | public ICollection<Resource> GetAllResources() {
|
---|
[925] | 115 | IList<Resource> allResources =
|
---|
[845] | 116 | new List<Resource>();
|
---|
[925] | 117 |
|
---|
[845] | 118 | foreach (dsHiveServer.ResourceRow row in data) {
|
---|
[899] | 119 | Resource resource = new Resource();
|
---|
| 120 | Convert(row, resource);
|
---|
| 121 | allResources.Add(resource);
|
---|
[845] | 122 | }
|
---|
| 123 |
|
---|
| 124 | return allResources;
|
---|
[826] | 125 | }
|
---|
| 126 |
|
---|
[925] | 127 | [MethodImpl(MethodImplOptions.Synchronized)]
|
---|
[826] | 128 | public bool DeleteResource(Resource resource) {
|
---|
[845] | 129 | if(resource != null) {
|
---|
[925] | 130 | dsHiveServer.ResourceRow row =
|
---|
| 131 | data.FindByResourceId(resource.ResourceId);
|
---|
[826] | 132 |
|
---|
[925] | 133 | if (row != null) {
|
---|
| 134 | data.RemoveResourceRow(row);
|
---|
[826] | 135 |
|
---|
[925] | 136 | return true;
|
---|
[845] | 137 | }
|
---|
[826] | 138 | }
|
---|
[845] | 139 |
|
---|
| 140 | return false;
|
---|
[826] | 141 | }
|
---|
| 142 |
|
---|
| 143 | #endregion
|
---|
| 144 | }
|
---|
| 145 | }
|
---|