#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.DataAccess;
using HeuristicLab.Hive.Contracts.BusinessObjects;
using System.Linq.Expressions;
using HeuristicLab.DataAccess.ADOHelper;
namespace HeuristicLab.Hive.Server.ADODataAccess {
class JobAdapter :
CachedDataAdapter,
IJobAdapter {
public JobAdapter() :
base(ServiceLocator.GetDBSynchronizer()) {
}
#region Fields
private IClientAdapter clientAdapter = null;
private IClientAdapter ClientAdapter {
get {
if (clientAdapter == null)
clientAdapter = ServiceLocator.GetClientAdapter();
return clientAdapter;
}
}
private IJobResultsAdapter resultsAdapter = null;
private IJobResultsAdapter ResultsAdapter {
get {
if (resultsAdapter == null) {
resultsAdapter = ServiceLocator.GetJobResultsAdapter();
}
return resultsAdapter;
}
}
#endregion
#region Overrides
protected override Job ConvertRow(dsHiveServer.JobRow row,
Job job) {
if (row != null && job != null) {
job.Id = row.JobId;
if (!row.IsParentJobIdNull())
job.ParentJob = GetById(row.ParentJobId);
else
job.ParentJob = null;
if (!row.IsResourceIdNull())
job.Client = ClientAdapter.GetById(row.ResourceId);
else
job.Client = null;
if (!row.IsPermissionOwnerIdNull())
job.UserId = Guid.Empty;
else
job.UserId = Guid.Empty;
if (!row.IsJobStateNull())
job.State = (State)Enum.Parse(job.State.GetType(), row.JobState);
else
job.State = State.nullState;
if (!row.IsPercentageNull())
job.Percentage = row.Percentage;
else
job.Percentage = 0.0;
if (!row.IsSerializedJobNull())
job.SerializedJob = row.SerializedJob;
else
job.SerializedJob = null;
if (!row.IsDateCreatedNull())
job.DateCreated = row.DateCreated;
else
job.DateCreated = DateTime.MinValue;
if (!row.IsDateCalculatedNull())
job.DateCalculated = row.DateCalculated;
else
job.DateCalculated = DateTime.MinValue;
if (!row.IsPriorityNull())
job.Priority = row.Priority;
else
job.Priority = default(int);
return job;
} else
return null;
}
protected override dsHiveServer.JobRow ConvertObj(Job job,
dsHiveServer.JobRow row) {
if (job != null && row != null) {
if (job.Client != null) {
if (row.IsResourceIdNull() ||
row.ResourceId != job.Client.Id) {
ClientAdapter.Update(job.Client);
row.ResourceId = job.Client.Id;
}
} else
row.SetResourceIdNull();
if (job.ParentJob != null) {
if (row.IsParentJobIdNull() ||
row.ParentJobId != job.ParentJob.Id) {
Update(job.ParentJob);
row.ParentJobId = job.ParentJob.Id;
}
} else
row.SetParentJobIdNull();
if (job.UserId != Guid.Empty) {
if (row.IsPermissionOwnerIdNull() ||
row.PermissionOwnerId != 0) {
row.PermissionOwnerId = 0;
}
} else
row.SetPermissionOwnerIdNull();
if (job.State != State.nullState)
row.JobState = job.State.ToString();
else
row.SetJobStateNull();
row.Percentage = job.Percentage;
row.SerializedJob = job.SerializedJob;
if (job.DateCreated != DateTime.MinValue)
row.DateCreated = job.DateCreated;
else
row.SetDateCreatedNull();
if (job.DateCalculated != DateTime.MinValue)
row.DateCalculated = job.DateCalculated;
else
row.SetDateCalculatedNull();
row.Priority = job.Priority;
}
return row;
}
protected override void UpdateRow(dsHiveServer.JobRow row) {
Adapter.Update(row);
}
protected override dsHiveServer.JobRow
InsertNewRow(Job job) {
dsHiveServer.JobDataTable data =
new dsHiveServer.JobDataTable();
dsHiveServer.JobRow row = data.NewJobRow();
data.AddJobRow(row);
return row;
}
protected override dsHiveServer.JobRow
InsertNewRowInCache(Job job) {
dsHiveServer.JobRow row = cache.NewJobRow();
cache.AddJobRow(row);
return row;
}
protected override void FillCache() {
Adapter.FillByActive(cache);
}
protected override void SynchronizeWithDb() {
this.Adapter.Update(cache);
}
protected override bool PutInCache(Job job) {
return job != null
&& (job.State == State.calculating
|| job.State == State.idle);
}
protected override IEnumerable
FindById(long id) {
return Adapter.GetDataById(id);
}
protected override dsHiveServer.JobRow
FindCachedById(long id) {
return cache.FindByJobId(id);
}
protected override IEnumerable
FindAll() {
return FindMultipleRows(
new Selector(Adapter.GetData),
new Selector(cache.AsEnumerable));
}
#endregion
#region IJobAdapter Members
public ICollection GetAllSubjobs(Job job) {
if (job != null) {
return
base.FindMultiple(
delegate() {
return Adapter.GetDataBySubjobs(job.Id);
},
delegate() {
return from j in
cache.AsEnumerable()
where !j.IsParentJobIdNull() &&
j.ParentJobId == job.Id
select j;
});
}
return null;
}
public ICollection GetJobsByState(State state) {
return
base.FindMultiple(
delegate() {
return Adapter.GetDataByState(state.ToString());
},
delegate() {
return from job in
cache.AsEnumerable()
where !job.IsJobStateNull() &&
job.JobState == state.ToString()
select job;
});
}
public ICollection GetJobsOf(ClientInfo client) {
if (client != null) {
return
base.FindMultiple(
delegate() {
return Adapter.GetDataByClient(client.Id);
},
delegate() {
return from job in
cache.AsEnumerable()
where !job.IsResourceIdNull() &&
job.ResourceId == client.Id
select job;
});
}
return null;
}
public ICollection GetActiveJobsOf(ClientInfo client) {
if (client != null) {
return
base.FindMultiple(
delegate() {
return Adapter.GetDataByCalculatingClient(client.Id);
},
delegate() {
return from job in
cache.AsEnumerable()
where !job.IsResourceIdNull() &&
job.ResourceId == client.Id &&
!job.IsJobStateNull() &&
job.JobState == "calculating"
select job;
});
}
return null;
}
public ICollection GetJobsOf(Guid userId) {
throw new NotImplementedException();
return
base.FindMultiple(
delegate() {
return Adapter.GetDataByUser(0);
},
delegate() {
return from job in
cache.AsEnumerable()
where !job.IsPermissionOwnerIdNull() &&
job.PermissionOwnerId == 0
select job;
});
return null;
}
public override bool Delete(Job job) {
if (job != null) {
dsHiveServer.JobRow row =
GetRowById(job.Id);
if (row != null) {
//Referential integrity with job results
ICollection results =
ResultsAdapter.GetResultsOf(job);
foreach (JobResult result in results) {
ResultsAdapter.Delete(result);
}
return base.Delete(job);
}
}
return false;
}
#endregion
}
}