[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 {
|
---|
[995] | 31 | class ResourceAdapter:
|
---|
| 32 | CachedDataAdapter<
|
---|
| 33 | dsHiveServerTableAdapters.ResourceTableAdapter,
|
---|
| 34 | Resource,
|
---|
| 35 | dsHiveServer.ResourceRow,
|
---|
| 36 | dsHiveServer.ResourceDataTable>,
|
---|
| 37 | IResourceAdapter {
|
---|
| 38 | #region Fields
|
---|
| 39 | dsHiveServer.ResourceDataTable data =
|
---|
[925] | 40 | new dsHiveServer.ResourceDataTable();
|
---|
| 41 |
|
---|
[995] | 42 | private IClientAdapter clientAdapter = null;
|
---|
[925] | 43 |
|
---|
[995] | 44 | private IClientAdapter ClientAdapter {
|
---|
| 45 | get {
|
---|
| 46 | if (clientAdapter == null)
|
---|
| 47 | clientAdapter = ServiceLocator.GetClientAdapter();
|
---|
| 48 |
|
---|
| 49 | return clientAdapter;
|
---|
| 50 | }
|
---|
[925] | 51 | }
|
---|
[995] | 52 | #endregion
|
---|
[925] | 53 |
|
---|
[995] | 54 | #region Overrides
|
---|
| 55 | protected override Resource Convert(dsHiveServer.ResourceRow row,
|
---|
[899] | 56 | Resource resource) {
|
---|
| 57 | if (row != null && resource != null) {
|
---|
[995] | 58 | resource.Id = row.ResourceId;
|
---|
[899] | 59 | if (!row.IsNameNull())
|
---|
| 60 | resource.Name = row.Name;
|
---|
| 61 | else
|
---|
| 62 | resource.Name = String.Empty;
|
---|
[845] | 63 |
|
---|
| 64 | return resource;
|
---|
[925] | 65 | } else
|
---|
[845] | 66 | return null;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[995] | 69 | protected override dsHiveServer.ResourceRow Convert(Resource resource,
|
---|
[845] | 70 | dsHiveServer.ResourceRow row) {
|
---|
| 71 | if (resource != null && row != null) {
|
---|
| 72 | row.Name = resource.Name;
|
---|
| 73 |
|
---|
[899] | 74 | return row;
|
---|
| 75 | } else
|
---|
| 76 | return null;
|
---|
[845] | 77 | }
|
---|
| 78 |
|
---|
[995] | 79 | protected override void UpdateRow(dsHiveServer.ResourceRow row) {
|
---|
| 80 | adapter.Update(row);
|
---|
| 81 | }
|
---|
[826] | 82 |
|
---|
[995] | 83 | protected override dsHiveServer.ResourceRow
|
---|
| 84 | InsertNewRow(Resource resource) {
|
---|
| 85 | dsHiveServer.ResourceRow row = data.NewResourceRow();
|
---|
| 86 | data.AddResourceRow(row);
|
---|
[826] | 87 |
|
---|
[995] | 88 | return row;
|
---|
| 89 | }
|
---|
[925] | 90 |
|
---|
[995] | 91 | protected override dsHiveServer.ResourceRow
|
---|
| 92 | InsertNewRowInCache(Resource resource) {
|
---|
| 93 | dsHiveServer.ResourceRow row = cache.NewResourceRow();
|
---|
| 94 | cache.AddResourceRow(row);
|
---|
| 95 |
|
---|
| 96 | return row;
|
---|
[826] | 97 | }
|
---|
| 98 |
|
---|
[995] | 99 | protected override void FillCache() {
|
---|
| 100 | cache = adapter.GetDataByActive();
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | public override void SyncWithDb() {
|
---|
| 104 | adapter.Update(cache);
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | protected override bool PutInCache(Resource obj) {
|
---|
| 108 | return (obj is ClientInfo &&
|
---|
| 109 | (obj as ClientInfo).State != State.offline);
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | protected override IEnumerable<dsHiveServer.ResourceRow>
|
---|
| 113 | FindById(long id) {
|
---|
| 114 | return adapter.GetDataById(id);
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | protected override dsHiveServer.ResourceRow
|
---|
| 118 | FindCachedById(long id) {
|
---|
| 119 | return cache.FindByResourceId(id);
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | protected override IEnumerable<dsHiveServer.ResourceRow>
|
---|
| 123 | FindAll() {
|
---|
| 124 | return FindMultipleRows(
|
---|
| 125 | new Selector(adapter.GetData),
|
---|
| 126 | new Selector(cache.AsEnumerable<dsHiveServer.ResourceRow>));
|
---|
| 127 | }
|
---|
| 128 | #endregion
|
---|
| 129 |
|
---|
| 130 | #region IResourceAdapter Members
|
---|
| 131 | public bool GetById(Resource resource) {
|
---|
[899] | 132 | if (resource != null) {
|
---|
[925] | 133 | dsHiveServer.ResourceRow row =
|
---|
[995] | 134 | GetRowById(resource.Id);
|
---|
| 135 |
|
---|
[925] | 136 | if (row != null) {
|
---|
[899] | 137 | Convert(row, resource);
|
---|
| 138 |
|
---|
| 139 | return true;
|
---|
| 140 | }
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | return false;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
[995] | 146 | public Resource GetByName(string name) {
|
---|
| 147 | dsHiveServer.ResourceRow row =
|
---|
| 148 | base.FindSingleRow(
|
---|
| 149 | delegate() {
|
---|
| 150 | return adapter.GetDataByName(name);
|
---|
| 151 | },
|
---|
| 152 | delegate() {
|
---|
| 153 | return from r in
|
---|
| 154 | cache.AsEnumerable<dsHiveServer.ResourceRow>()
|
---|
| 155 | where !r.IsNameNull() &&
|
---|
| 156 | r.Name == name
|
---|
| 157 | select r;
|
---|
| 158 | });
|
---|
[899] | 159 |
|
---|
[971] | 160 | if (row != null) {
|
---|
[976] | 161 | Resource res = new Resource();
|
---|
| 162 | Convert(row, res);
|
---|
[971] | 163 |
|
---|
[976] | 164 | return res;
|
---|
[971] | 165 | } else {
|
---|
| 166 | return null;
|
---|
| 167 | }
|
---|
| 168 | }
|
---|
| 169 |
|
---|
[826] | 170 | #endregion
|
---|
| 171 | }
|
---|
| 172 | }
|
---|