#region License Information
/* HeuristicLab
* Copyright (C) 2002-2010 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 HeuristicLab.Hive.Contracts.BusinessObjects;
using HeuristicLab.Hive.Server.DataAccess;
namespace HeuristicLab.Hive.Server.LINQDataAccess {
public class SlaveGroupDao : BaseDao, ISlaveGroupDao {
#region IGenericDao Members
public SlaveGroupDto FindById(Guid id) {
return (from cc in Context.Resources.OfType()
where cc.ResourceId.Equals(id)
select EntityToDto(cc, null)).SingleOrDefault();
}
public IEnumerable FindAll() {
return (from cc in Context.Resources.OfType()
select EntityToDto(cc, null)).ToList();
}
public SlaveGroupDto Insert(SlaveGroupDto bObj) {
//Auto GUID is disabled for Ressource... thx to the slaves... grml
if(bObj.Id == Guid.Empty)
bObj.Id = Guid.NewGuid();
SlaveGroup cc = DtoToEntity(bObj, null);
Context.Resources.InsertOnSubmit(cc);
CommitChanges();
bObj.Id = cc.ResourceId;
return bObj;
}
public void Delete(Guid id) {
//Deleting all references
Context.SlaveGroup_Resources.DeleteAllOnSubmit(Context.SlaveGroup_Resources.Where(cg => cg.SlaveGroupId.Equals(id)));
Resource res = Context.Resources.SingleOrDefault(c => c.ResourceId.Equals(id));
Context.Resources.DeleteOnSubmit(res);
CommitChanges();
}
public void Update(SlaveGroupDto bObj) {
SlaveGroup slave = Context.Resources.OfType().SingleOrDefault(c => c.ResourceId.Equals(bObj.Id));
DtoToEntity(bObj, slave);
CommitChanges();
}
public void AddRessourceToSlaveGroup(Guid resource, Guid slaveGroupId) {
SlaveGroup cg = Context.Resources.OfType().SingleOrDefault(c => c.ResourceId.Equals(slaveGroupId));
Resource res = Context.Resources.SingleOrDefault(r => r.ResourceId.Equals(resource));
cg.SlaveGroup_Resources_Children.Add(new SlaveGroup_Resource { SlaveGroup = cg, Resource = res });
CommitChanges();
}
public void RemoveRessourceFromSlaveGroup(Guid resource, Guid slaveGroupId) {
SlaveGroup_Resource cgr =
Context.SlaveGroup_Resources.SingleOrDefault(
cg => cg.ResourceId.Equals(resource) && cg.SlaveGroupId.Equals(slaveGroupId));
Context.SlaveGroup_Resources.DeleteOnSubmit(cgr);
CommitChanges();
}
public IEnumerable MemberOf(SlaveDto slave) {
return (from cgr in Context.SlaveGroup_Resources
where cgr.ResourceId.Equals(slave.Id)
select EntityToDto(cgr.SlaveGroup, null)).ToList();
}
public IEnumerable FindAllWithSubGroupsAndSlaves() {
List groupList = new List();
var q = (from cg in Context.Resources.OfType()
where !Context.SlaveGroup_Resources.Any(cgr => cgr.ResourceId.Equals(cg.ResourceId))
select cg);
foreach (SlaveGroup cg in q) {
SlaveGroupDto cgd = EntityToDto(cg, null);
groupList.Add(cgd);
FillSubGroupsAndSlavesRecursivly(cgd, cg.SlaveGroup_Resources_Children);
}
return groupList;
}
private void FillSubGroupsAndSlavesRecursivly(SlaveGroupDto parentSlaveGroup, System.Data.Linq.EntitySet parentResourceSet) {
SlaveDao cd = new SlaveDao();
//Get all the Groups
var qGroups = (from cg in Context.Resources.OfType()
where cg.SlaveGroup_Resources_Parents.Any(cgr => cgr.SlaveGroupId.Equals(parentSlaveGroup.Id))
select cg);
foreach (SlaveGroup cg in qGroups) {
SlaveGroupDto cgd = EntityToDto(cg, null);
parentSlaveGroup.Resources.Add(cgd);
FillSubGroupsAndSlavesRecursivly(cgd, cg.SlaveGroup_Resources_Children);
}
//get the slaves
var qSlaves = (from cl in Context.Resources.OfType()
where cl.SlaveGroup_Resources_Parents.Any(cgr => cgr.SlaveGroupId.Equals(parentSlaveGroup.Id))
select cl);
foreach (Slave slave in qSlaves) {
parentSlaveGroup.Resources.Add(cd.EntityToDto(slave, null));
}
}
public IEnumerable FindAllGroupAndParentGroupIdsForSlave(Guid slaveId) {
List guids = new List();
Slave c = Context.Resources.OfType().SingleOrDefault(slave => slave.ResourceId.Equals(slaveId));
FindAllGroupAndParentGroupIdsForSlaveRecursive(c, guids);
return guids;
}
//Massivly error Prone
private void FindAllGroupAndParentGroupIdsForSlaveRecursive(Resource resource, List guids) {
foreach (SlaveGroup_Resource cgr in resource.SlaveGroup_Resources_Parents) {
guids.Add(cgr.SlaveGroupId);
FindAllGroupAndParentGroupIdsForSlaveRecursive(cgr.SlaveGroup, guids);
}
}
#endregion
public override SlaveGroup DtoToEntity(SlaveGroupDto source, SlaveGroup target) {
if (source == null)
return null;
if (target == null)
target = new SlaveGroup();
target.Name = source.Name;
target.ResourceId = source.Id;
return target;
}
public override SlaveGroupDto EntityToDto(SlaveGroup source, SlaveGroupDto target) {
if (source == null)
return null;
if (target == null)
target = new SlaveGroupDto();
target.Id = source.ResourceId;
target.Name = source.Name;
return target;
}
public IEnumerable FindByName(string res) {
return (from cq in Context.Resources.OfType()
where cq.Name == res
select EntityToDto(cq, null)).ToList();
}
}
}