#region License Information
/* HeuristicLab
* Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HeuristicLab.Hive.Server.Core.InternalInterfaces.DataAccess;
using HeuristicLab.Hive.Contracts.BusinessObjects;
using System.Runtime.CompilerServices;
using System.Data;
namespace HeuristicLab.Hive.Server.ADODataAccess {
class ClientGroupAdapter :
DataAdapterBase,
IClientGroupAdapter {
#region Fields
dsHiveServer.ClientGroupDataTable data =
new dsHiveServer.ClientGroupDataTable();
private dsHiveServerTableAdapters.ClientGroup_ResourceTableAdapter resourceClientGroupAdapter =
new dsHiveServerTableAdapters.ClientGroup_ResourceTableAdapter();
private IResourceAdapter resourceAdapter = null;
private IResourceAdapter ResAdapter {
get {
if (resourceAdapter == null)
resourceAdapter = ServiceLocator.GetResourceAdapter();
return resourceAdapter;
}
}
private IClientAdapter clientAdapter = null;
private IClientAdapter ClientAdapter {
get {
if (clientAdapter == null)
clientAdapter = ServiceLocator.GetClientAdapter();
return clientAdapter;
}
}
#endregion
#region Overrides
protected override ClientGroup ConvertRow(dsHiveServer.ClientGroupRow row,
ClientGroup clientGroup) {
if (row != null && clientGroup != null) {
/*Parent - Permission Owner*/
clientGroup.Id = row.ResourceId;
ResAdapter.GetById(clientGroup);
//first check for created references
dsHiveServer.ClientGroup_ResourceDataTable clientGroupRows =
resourceClientGroupAdapter.GetDataByClientGroupId(clientGroup.Id);
foreach (dsHiveServer.ClientGroup_ResourceRow resourceClientGroupRow in
clientGroupRows) {
Resource resource = null;
IEnumerable resources =
from p in
clientGroup.Resources
where p.Id == resourceClientGroupRow.ResourceId
select p;
if (resources.Count() == 1)
resource = resources.First();
if (resource == null) {
Resource res =
ClientAdapter.GetById(resourceClientGroupRow.ResourceId);
if (res == null) {
//is a client group
res =
GetById(resourceClientGroupRow.ResourceId);
}
if (res != null)
clientGroup.Resources.Add(res);
}
}
//secondly check for deleted references
ICollection deleted =
new List();
foreach (Resource resource in clientGroup.Resources) {
dsHiveServer.ClientGroup_ResourceDataTable found =
resourceClientGroupAdapter.GetDataByClientGroupResourceId(
clientGroup.Id,
resource.Id);
if (found.Count != 1) {
deleted.Add(resource);
}
}
foreach (Resource resource in deleted) {
clientGroup.Resources.Remove(resource);
}
return clientGroup;
} else
return null;
}
protected override dsHiveServer.ClientGroupRow ConvertObj(ClientGroup clientGroup,
dsHiveServer.ClientGroupRow row) {
if (clientGroup != null && row != null) {
row.ResourceId = clientGroup.Id;
//update references
foreach (Resource resource in clientGroup.Resources) {
//first update the member to make sure it exists in the DB
if (resource is ClientInfo) {
ClientAdapter.Update(resource as ClientInfo);
} else if (resource is ClientGroup) {
Update(resource as ClientGroup);
}
//secondly check for created references
dsHiveServer.ClientGroup_ResourceRow resourceClientGroupRow =
null;
dsHiveServer.ClientGroup_ResourceDataTable found =
resourceClientGroupAdapter.GetDataByClientGroupResourceId(
clientGroup.Id,
resource.Id);
if (found.Count == 1)
resourceClientGroupRow = found[0];
if (resourceClientGroupRow == null) {
resourceClientGroupRow =
found.NewClientGroup_ResourceRow();
resourceClientGroupRow.ResourceId =
resource.Id;
resourceClientGroupRow.ClientGroupId =
clientGroup.Id;
found.AddClientGroup_ResourceRow(resourceClientGroupRow);
resourceClientGroupAdapter.Update(
resourceClientGroupRow);
}
}
//thirdly check for deleted references
dsHiveServer.ClientGroup_ResourceDataTable clientGroupRows =
resourceClientGroupAdapter.GetDataByClientGroupId(clientGroup.Id);
ICollection deleted =
new List();
foreach (dsHiveServer.ClientGroup_ResourceRow resourceClientGroupRow in
clientGroupRows) {
Resource resource = null;
IEnumerable resources =
from r in
clientGroup.Resources
where r.Id == resourceClientGroupRow.ResourceId
select r;
if (resources.Count() == 1)
resource = resources.First();
if (resource == null) {
deleted.Add(resourceClientGroupRow);
}
}
foreach (dsHiveServer.ClientGroup_ResourceRow resourceClientGroupRow in deleted) {
resourceClientGroupRow.Delete();
resourceClientGroupAdapter.Update(resourceClientGroupRow);
}
}
return row;
}
protected override dsHiveServer.ClientGroupRow
InsertNewRow(ClientGroup group) {
dsHiveServer.ClientGroupRow row =
data.NewClientGroupRow();
row.ResourceId = group.Id;
data.AddClientGroupRow(row);
Adapter.Update(row);
return row;
}
protected override void
UpdateRow(dsHiveServer.ClientGroupRow row) {
Adapter.Update(row);
}
protected override IEnumerable
FindById(long id) {
return Adapter.GetDataById(id);
}
protected override IEnumerable
FindAll() {
return Adapter.GetData();
}
#endregion
#region IClientGroupAdapter Members
[MethodImpl(MethodImplOptions.Synchronized)]
public override void Update(ClientGroup group) {
if (group != null) {
ResAdapter.Update(group);
base.Update(group);
}
}
public ClientGroup GetByName(string name) {
ClientGroup group = new ClientGroup();
Resource res =
ResAdapter.GetByName(name);
if (res != null) {
return GetById(res.Id);
}
return null;
}
public ICollection MemberOf(Resource resource) {
ICollection clientGroups =
new List();
if (resource != null) {
IEnumerable clientGroupRows =
resourceClientGroupAdapter.GetDataByResourceId(resource.Id);
foreach (dsHiveServer.ClientGroup_ResourceRow clientGroupRow in
clientGroupRows) {
ClientGroup clientGroup =
GetById(clientGroupRow.ClientGroupId);
clientGroups.Add(clientGroup);
}
}
return clientGroups;
}
[MethodImpl(MethodImplOptions.Synchronized)]
public override bool Delete(ClientGroup group) {
if (group != null) {
return base.Delete(group) &&
ResAdapter.Delete(group);
}
return false;
}
#endregion
}
}