Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Server.ADODataAccess/PermissionOwnerAdapter.cs @ 978

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

Fixed selecting by name (#372)

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