Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 971 was 939, checked in by svonolfe, 15 years ago

Fixed referential integrity between user and user group (#372)

File size: 4.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;
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      PermissionOwner permOwner = new PermissionOwner();
119
120      dsHiveServer.PermissionOwnerRow row =
121        data.Single<dsHiveServer.PermissionOwnerRow>(
122          r => !r.IsNameNull() && r.Name == name);
123
124      if (row != null) {
125        Convert(row, permOwner);
126
127        return permOwner;
128      } else {
129        return null;
130      }
131    }
132
133    public ICollection<PermissionOwner> GetAllPermissionOwners() {
134      ICollection<PermissionOwner> allPermissionOwners =
135        new List<PermissionOwner>();
136
137      foreach (dsHiveServer.PermissionOwnerRow row in data) {
138        PermissionOwner permOwner = new PermissionOwner();
139        Convert(row, permOwner);
140        allPermissionOwners.Add(permOwner);
141      }
142
143      return allPermissionOwners;
144    }
145
146    [MethodImpl(MethodImplOptions.Synchronized)]
147    public bool DeletePermissionOwner(PermissionOwner permOwner) {
148      if (permOwner != null) {
149          dsHiveServer.PermissionOwnerRow row =
150            data.FindByPermissionOwnerId(permOwner.PermissionOwnerId);
151
152          if(row != null) {
153            data.RemovePermissionOwnerRow(row);
154         
155            return true;
156          }
157        }
158
159      return false;
160    }
161
162    #endregion
163  }
164}
Note: See TracBrowser for help on using the repository browser.