Index: trunk/sources/HeuristicLab.Hive.Server.ADODataAccess/UserGroupAdapter.cs
===================================================================
--- trunk/sources/HeuristicLab.Hive.Server.ADODataAccess/UserGroupAdapter.cs	(revision 972)
+++ trunk/sources/HeuristicLab.Hive.Server.ADODataAccess/UserGroupAdapter.cs	(revision 995)
@@ -31,16 +31,17 @@
 
 namespace HeuristicLab.Hive.Server.ADODataAccess {
-  class UserGroupAdapter: DataAdapterBase, IUserGroupAdapter {
-    private dsHiveServerTableAdapters.UserGroupTableAdapter adapter =
-        new dsHiveServerTableAdapters.UserGroupTableAdapter();
-
-    private dsHiveServer.UserGroupDataTable data =
-      new dsHiveServer.UserGroupDataTable();
+  class UserGroupAdapter: 
+    DataAdapterBase<
+      dsHiveServerTableAdapters.UserGroupTableAdapter, 
+      UserGroup, 
+      dsHiveServer.UserGroupRow>,
+    IUserGroupAdapter {
+
+    #region Fields
+    dsHiveServer.UserGroupDataTable data =
+        new dsHiveServer.UserGroupDataTable();
 
     private dsHiveServerTableAdapters.PermissionOwner_UserGroupTableAdapter permOwnerUserGroupAdapter =
       new dsHiveServerTableAdapters.PermissionOwner_UserGroupTableAdapter();
-
-    private dsHiveServer.PermissionOwner_UserGroupDataTable permOwnerUserGroupData =
-      new dsHiveServer.PermissionOwner_UserGroupDataTable();
 
     private IPermissionOwnerAdapter permOwnerAdapter = null;
@@ -65,28 +66,17 @@
       }
     }
-
-    public UserGroupAdapter() {
-      adapter.Fill(data);
-      permOwnerUserGroupAdapter.Fill(permOwnerUserGroupData);
-    }
-
-    protected override void Update() {
-      this.adapter.Update(this.data);
-      this.permOwnerUserGroupAdapter.Update(permOwnerUserGroupData);
-    }
-
-    private UserGroup Convert(dsHiveServer.UserGroupRow row,
+    #endregion
+
+    #region Overrides
+    protected override UserGroup Convert(dsHiveServer.UserGroupRow row,
       UserGroup userGroup) {
       if (row != null && userGroup != null) {
         /*Parent - Permission Owner*/
-        userGroup.PermissionOwnerId = row.PermissionOwnerId;
-        PermOwnerAdapter.GetPermissionOwnerById(userGroup);
+        userGroup.Id = row.PermissionOwnerId;
+        PermOwnerAdapter.GetById(userGroup);
 
         //first check for created references
-        IEnumerable<dsHiveServer.PermissionOwner_UserGroupRow> userGroupRows =
-          from permOwner in
-            permOwnerUserGroupData.AsEnumerable<dsHiveServer.PermissionOwner_UserGroupRow>()
-          where permOwner.UserGroupId == userGroup.PermissionOwnerId
-          select permOwner;
+        dsHiveServer.PermissionOwner_UserGroupDataTable userGroupRows =
+          permOwnerUserGroupAdapter.GetDataByUserGroupId(userGroup.Id);
 
         foreach (dsHiveServer.PermissionOwner_UserGroupRow permOwnerUserGroupRow in
@@ -97,5 +87,5 @@
             from p in
               userGroup.Members
-            where p.PermissionOwnerId == permOwnerUserGroupRow.PermissionOwnerId
+            where p.Id == permOwnerUserGroupRow.PermissionOwnerId
             select p;
           if (permOwners.Count<PermissionOwner>() == 1)
@@ -104,10 +94,10 @@
           if (permOwner == null) {
             PermissionOwner permissionOwner = 
-              UserAdapter.GetUserById(permOwnerUserGroupRow.PermissionOwnerId);
+              UserAdapter.GetById(permOwnerUserGroupRow.PermissionOwnerId);
 
             if (permissionOwner == null) {
               //is a user group
               permissionOwner =
-                GetUserGroupById(permOwnerUserGroupRow.PermissionOwnerId);
+                GetById(permOwnerUserGroupRow.PermissionOwnerId);
             }
 
@@ -122,10 +112,9 @@
 
         foreach (PermissionOwner permOwner in userGroup.Members) {
-          dsHiveServer.PermissionOwner_UserGroupRow permOwnerUserGroupRow =
-            permOwnerUserGroupData.FindByPermissionOwnerIdUserGroupId(
-              permOwner.PermissionOwnerId,
-              userGroup.PermissionOwnerId);
-
-          if (permOwnerUserGroupRow == null) {
+          dsHiveServer.PermissionOwner_UserGroupDataTable found =
+            permOwnerUserGroupAdapter.GetDataByPermownerUsergroupId(
+              permOwner.Id,
+              userGroup.Id);
+          if (found.Count != 1) {
             deleted.Add(permOwner);
           }
@@ -141,8 +130,8 @@
     }
 
-    private dsHiveServer.UserGroupRow Convert(UserGroup userGroup,
+    protected override dsHiveServer.UserGroupRow Convert(UserGroup userGroup,
       dsHiveServer.UserGroupRow row) {
       if (userGroup != null && row != null) {
-        row.PermissionOwnerId = userGroup.PermissionOwnerId;
+        row.PermissionOwnerId = userGroup.Id;
       
         //update references
@@ -150,35 +139,37 @@
           //first update the member to make sure it exists in the DB
           if (permOwner is User) {
-            UserAdapter.UpdateUser(permOwner as User);
+            UserAdapter.Update(permOwner as User);
           } else if (permOwner is UserGroup) {
-            UpdateUserGroup(permOwner as UserGroup);
+            Update(permOwner as UserGroup);
           }
 
           //secondly check for created references
           dsHiveServer.PermissionOwner_UserGroupRow permOwnerUserGroupRow =
-            permOwnerUserGroupData.FindByPermissionOwnerIdUserGroupId(
-              permOwner.PermissionOwnerId,
-              userGroup.PermissionOwnerId);
+            null;
+          dsHiveServer.PermissionOwner_UserGroupDataTable found = 
+            permOwnerUserGroupAdapter.GetDataByPermownerUsergroupId(
+              permOwner.Id,
+              userGroup.Id);
+          if (found.Count == 1)
+            permOwnerUserGroupRow = found[0];
 
           if (permOwnerUserGroupRow == null) {
             permOwnerUserGroupRow = 
-              permOwnerUserGroupData.NewPermissionOwner_UserGroupRow();
+              found.NewPermissionOwner_UserGroupRow();
 
             permOwnerUserGroupRow.PermissionOwnerId =
-              permOwner.PermissionOwnerId;
+              permOwner.Id;
             permOwnerUserGroupRow.UserGroupId =
-              userGroup.PermissionOwnerId;
-
-            permOwnerUserGroupData.AddPermissionOwner_UserGroupRow(
-              permOwnerUserGroupRow);
+              userGroup.Id;
+
+            found.AddPermissionOwner_UserGroupRow(permOwnerUserGroupRow);
+
+            permOwnerUserGroupAdapter.Update(permOwnerUserGroupRow);
           }
         }
 
         //thirdly check for deleted references
-        IEnumerable<dsHiveServer.PermissionOwner_UserGroupRow> userGroupRows = 
-          from permOwner in 
-            permOwnerUserGroupData.AsEnumerable<dsHiveServer.PermissionOwner_UserGroupRow>()
-          where permOwner.UserGroupId == userGroup.PermissionOwnerId  
-          select permOwner;
+        dsHiveServer.PermissionOwner_UserGroupDataTable userGroupRows =
+            permOwnerUserGroupAdapter.GetDataByUserGroupId(userGroup.Id);
 
         ICollection<dsHiveServer.PermissionOwner_UserGroupRow> deleted =
@@ -192,5 +183,5 @@
             from p in
               userGroup.Members
-            where p.PermissionOwnerId == permOwnerUserGroupRow.PermissionOwnerId
+            where p.Id == permOwnerUserGroupRow.PermissionOwnerId
             select p;
 
@@ -203,81 +194,62 @@
         }
 
-        foreach (dsHiveServer.PermissionOwner_UserGroupRow permOwnerUserGroupRow in
-          deleted) {
-          permOwnerUserGroupData.RemovePermissionOwner_UserGroupRow(
-            permOwnerUserGroupRow);
-        }
-
+        foreach (dsHiveServer.PermissionOwner_UserGroupRow 
+          permOwnerUserGroupRow in deleted) {
+          permOwnerUserGroupRow.Delete();
+          permOwnerUserGroupAdapter.Update(permOwnerUserGroupRow);
+        }
       }
 
       return row;
     }
+
+    protected override dsHiveServer.UserGroupRow
+      InsertNewRow(UserGroup group) {
+      dsHiveServer.UserGroupRow row =
+        data.NewUserGroupRow();
+
+      row.PermissionOwnerId = group.Id;
+
+      data.AddUserGroupRow(row);
+      adapter.Update(row);
+
+      return row;
+    }
+
+    protected override void
+     UpdateRow(dsHiveServer.UserGroupRow row) {
+      adapter.Update(row);
+    }
+
+    protected override IEnumerable<dsHiveServer.UserGroupRow>
+      FindById(long id) {
+      return adapter.GetDataById(id);
+    }
+
+    protected override IEnumerable<dsHiveServer.UserGroupRow>
+      FindAll() {
+      return adapter.GetData();
+    }
+    #endregion
 
     #region IUserGroupAdapter Members
     [MethodImpl(MethodImplOptions.Synchronized)]
-    public void UpdateUserGroup(UserGroup group) {
+    public override void Update(UserGroup group) {
       if (group != null) {
-        PermOwnerAdapter.UpdatePermissionOwner(group);
-
-        dsHiveServer.UserGroupRow row =
-          data.FindByPermissionOwnerId(group.PermissionOwnerId);
-
-        if (row == null) {
-          row = data.NewUserGroupRow();
-          row.PermissionOwnerId = group.PermissionOwnerId;
-          data.AddUserGroupRow(row);
-        }
-
-        Convert(group, row);
-      }
-    }
-
-    public UserGroup GetUserGroupById(long userGroupId) {
-      UserGroup userGroup = new UserGroup();
-
-      dsHiveServer.UserGroupRow row =
-        data.FindByPermissionOwnerId(userGroupId);
-
-      if (row != null) {
-        Convert(row, userGroup);
-
-        return userGroup;
-      } else {
-        return null;
-      }
-    }
-
-    public UserGroup GetUserGroupByName(string name) {
-      UserGroup group = new UserGroup();
-
-      PermissionOwner permOwner =
-        PermOwnerAdapter.GetPermissionOwnerByName(name);
-
-      if (permOwner != null) {
-        dsHiveServer.UserGroupRow row =
-          data.FindByPermissionOwnerId(permOwner.PermissionOwnerId);
-
-        if (row != null) {
-          Convert(row, group);
-
-          return group;
-        }
+        PermOwnerAdapter.Update(group);
+
+        base.Update(group);
+      }
+    }
+
+    public UserGroup GetByName(String name) {
+      if (name != null) {
+        return base.FindSingle(
+          delegate() {
+            return adapter.GetDataByName(name);
+          });
       }
 
       return null;
-    }
-
-    public ICollection<UserGroup> GetAllUserGroups() {
-      ICollection<UserGroup> allUserGroups =
-        new List<UserGroup>();
-
-      foreach (dsHiveServer.UserGroupRow row in data) {
-        UserGroup userGroup = new UserGroup();
-
-        Convert(row, userGroup);
-        allUserGroups.Add(userGroup);
-      }
-
-      return allUserGroups;
     }
 
@@ -287,14 +259,11 @@
       
       if (permOwner != null) {
-        IEnumerable<dsHiveServer.PermissionOwner_UserGroupRow> userGroupRows =
-         from userGroup in
-           permOwnerUserGroupData.AsEnumerable<dsHiveServer.PermissionOwner_UserGroupRow>()
-         where userGroup.PermissionOwnerId == permOwner.PermissionOwnerId
-         select userGroup;
+        dsHiveServer.PermissionOwner_UserGroupDataTable userGroupRows =
+          permOwnerUserGroupAdapter.GetDataByPermissionOwnerId(permOwner.Id);
 
         foreach (dsHiveServer.PermissionOwner_UserGroupRow userGroupRow in
           userGroupRows) {
           UserGroup userGroup = 
-            GetUserGroupById(userGroupRow.UserGroupId);
+            GetById(userGroupRow.UserGroupId);
           userGroups.Add(userGroup);
         }
@@ -305,31 +274,8 @@
 
     [MethodImpl(MethodImplOptions.Synchronized)]
-    public bool DeleteUserGroup(UserGroup group) {
+    public override bool Delete(UserGroup group) {
       if (group != null) {
-        dsHiveServer.UserGroupRow row =
-          data.FindByPermissionOwnerId(group.PermissionOwnerId);
-
-        if (row != null) {
-          ICollection<dsHiveServer.PermissionOwner_UserGroupRow> deleted =
-            new List<dsHiveServer.PermissionOwner_UserGroupRow>();
-          
-          foreach (dsHiveServer.PermissionOwner_UserGroupRow permOwnerUserGroupRow in
-            permOwnerUserGroupData) {
-            if (permOwnerUserGroupRow.UserGroupId == group.PermissionOwnerId || 
-              permOwnerUserGroupRow.PermissionOwnerId == group.PermissionOwnerId) {
-              deleted.Add(permOwnerUserGroupRow);
-            }
-          }
-
-          foreach (dsHiveServer.PermissionOwner_UserGroupRow permOwnerUserGroupRow in
-            deleted) {
-            permOwnerUserGroupData.RemovePermissionOwner_UserGroupRow(
-              permOwnerUserGroupRow);
-          }
-
-          row.Delete();
-          adapter.Update(row);
-          return PermOwnerAdapter.DeletePermissionOwner(group);
-        }
+        return base.Delete(group) && 
+          PermOwnerAdapter.Delete(group);
       }
 
