#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 UptimeCalendarDao: BaseDao, IUptimeCalendarDao {
public override UptimeCalendar DtoToEntity(AppointmentDto source, UptimeCalendar target) {
if(source == null)
return null;
if (target == null)
target = new UptimeCalendar();
target.AllDayEvent = source.AllDayEvent;
target.EndDate = source.EndDate;
target.StartDate = source.StartDate;
target.Recurring = source.Recurring;
target.RecurringId = source.RecurringId;
target.ResourceId = source.ResourceId;
return target;
}
public override AppointmentDto EntityToDto(UptimeCalendar source, AppointmentDto target) {
if (source == null)
return null;
if (target == null)
target = new AppointmentDto();
target.AllDayEvent = source.AllDayEvent;
target.EndDate = source.EndDate;
target.StartDate = source.StartDate;
target.Recurring = source.Recurring;
target.RecurringId = source.RecurringId;
target.ResourceId = source.ResourceId;
return target;
}
#region IGenericDao Members
public AppointmentDto FindById(Guid id) {
return (from app in Context.UptimeCalendars
where app.UptimeCalendarId.Equals(id)
select EntityToDto(app, null)).SingleOrDefault();
}
public IEnumerable FindAll() {
return (from app in Context.UptimeCalendars
select EntityToDto(app, null)).ToList();
}
public AppointmentDto Insert(AppointmentDto bObj) {
UptimeCalendar uc = DtoToEntity(bObj, null);
Context.UptimeCalendars.InsertOnSubmit(uc);
CommitChanges();
bObj.Id = uc.UptimeCalendarId;
return bObj;
}
public void Delete(Guid id) {
Context.UptimeCalendars.DeleteOnSubmit(Context.UptimeCalendars.SingleOrDefault(uc => uc.UptimeCalendarId.Equals(id)));
CommitChanges();
}
public void Update(AppointmentDto bObj) {
UptimeCalendar cc = Context.UptimeCalendars.SingleOrDefault(c => c.UptimeCalendarId.Equals(bObj.Id));
DtoToEntity(bObj, cc);
CommitChanges();
}
public IEnumerable GetUptimeCalendarForResource(Guid resourceId) {
return (from uc in Context.UptimeCalendars
where uc.ResourceId.Equals(resourceId)
select EntityToDto(uc, null)).ToList();
}
public void SetUptimeCalendarForResource(Guid resourceId, IEnumerable appointments) {
var q = (from uc in Context.UptimeCalendars
where uc.ResourceId.Equals(resourceId)
select uc);
Context.UptimeCalendars.DeleteAllOnSubmit(q);
foreach (AppointmentDto appdto in appointments) {
UptimeCalendar uc = DtoToEntity(appdto, null);
uc.ResourceId = resourceId;
Context.UptimeCalendars.InsertOnSubmit(uc);
}
CommitChanges();
}
public IEnumerable GetCalendarForSlave(SlaveDto slave) {
Slave dbc = Context.Resources.OfType().SingleOrDefault(c => c.ResourceId.Equals(slave.Id));
IList appointments = new List();
if (dbc != null) {
SlaveGroup cg =
Context.Resources.OfType().SingleOrDefault(cgroup => cgroup.ResourceId.Equals(dbc.UseCalendarFromResourceId));
//in case no plan has been set
if (cg == null)
if (dbc.SlaveGroup_Resources_Parents.FirstOrDefault() != null)
cg = dbc.SlaveGroup_Resources_Parents.FirstOrDefault().SlaveGroup;
if (cg == null)
return appointments;
while (cg.UptimeCalendars.Count == 0) {
if (cg.SlaveGroup_Resources_Parents.FirstOrDefault() != null)
cg = cg.SlaveGroup_Resources_Parents.FirstOrDefault().SlaveGroup;
else {
break;
}
}
foreach (UptimeCalendar appointment in cg.UptimeCalendars) {
appointments.Add(EntityToDto(appointment,null));
}
}
return appointments;
}
public void NotifySlavesOfNewCalendar(Guid groupId, bool forcePush) {
//Get the current SlaveGroup
SlaveGroup cg = Context.Resources.OfType().SingleOrDefault(cgroup => cgroup.ResourceId.Equals(groupId));
if(cg == null)
return;
//Get all the affected slaves
List slaves = Context.Resources.OfType().Where(c => c.UseCalendarFromResourceId.Equals(cg.ResourceId)).ToList();
//Set new state
foreach (Slave slave in slaves) {
slave.CalendarSyncStatus = (forcePush ? Enum.GetName(typeof(CalendarState), CalendarState.ForceFetch) : Enum.GetName(typeof(CalendarState), CalendarState.Fetch));
}
CommitChanges();
//Get all Subgroups
List groups = (from cg1 in Context.Resources.OfType()
where cg1.SlaveGroup_Resources_Parents.Any(
cgr => cgr.SlaveGroupId.Equals(groupId))
select cg1).ToList();
//If they have their own calendar - stop propagation
//otherweise - propagate
foreach (SlaveGroup cgroup in groups) {
if(cgroup.UptimeCalendars.Count == 0)
NotifySlavesOfNewCalendar(cgroup.ResourceId, forcePush);
}
}
#endregion
}
}