#region License Information
/* HeuristicLab
* Copyright (C) 2002-2011 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.ServiceModel;
using HeuristicLab.Services.OKB.DataAccess;
namespace HeuristicLab.Services.OKB.Administration {
///
/// Implementation of the OKB administration service (interface ).
///
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class AdministrationService : IAdministrationService {
#region Platform Methods
public DataTransfer.Platform GetPlatform(long id) {
using (OKBDataContext okb = new OKBDataContext()) {
return Convert.ToDto(okb.Platforms.FirstOrDefault(x => x.Id == id));
}
}
public IEnumerable GetPlatforms() {
using (OKBDataContext okb = new OKBDataContext()) {
return okb.Platforms.Select(x => Convert.ToDto(x)).ToArray();
}
}
public long AddPlatform(DataTransfer.Platform dto) {
using (OKBDataContext okb = new OKBDataContext()) {
DataAccess.Platform entity = Convert.ToEntity(dto); entity.Id = 0;
okb.Platforms.InsertOnSubmit(entity);
okb.SubmitChanges();
return entity.Id;
}
}
public void UpdatePlatform(DataTransfer.Platform dto) {
using (OKBDataContext okb = new OKBDataContext()) {
DataAccess.Platform entity = okb.Platforms.FirstOrDefault(x => x.Id == dto.Id);
Convert.ToEntity(dto, entity);
okb.SubmitChanges();
}
}
public void DeletePlatform(long id) {
using (OKBDataContext okb = new OKBDataContext()) {
DataAccess.Platform entity = okb.Platforms.FirstOrDefault(x => x.Id == id);
if (entity != null) okb.Platforms.DeleteOnSubmit(entity);
okb.SubmitChanges();
}
}
#endregion
#region AlgorithmClass Methods
public DataTransfer.AlgorithmClass GetAlgorithmClass(long id) {
using (OKBDataContext okb = new OKBDataContext()) {
return Convert.ToDto(okb.AlgorithmClasses.FirstOrDefault(x => x.Id == id));
}
}
public IEnumerable GetAlgorithmClasses() {
using (OKBDataContext okb = new OKBDataContext()) {
return okb.AlgorithmClasses.Select(x => Convert.ToDto(x)).ToArray();
}
}
public long AddAlgorithmClass(DataTransfer.AlgorithmClass dto) {
using (OKBDataContext okb = new OKBDataContext()) {
DataAccess.AlgorithmClass entity = Convert.ToEntity(dto); entity.Id = 0;
okb.AlgorithmClasses.InsertOnSubmit(entity);
okb.SubmitChanges();
return entity.Id;
}
}
public void UpdateAlgorithmClass(DataTransfer.AlgorithmClass dto) {
using (OKBDataContext okb = new OKBDataContext()) {
DataAccess.AlgorithmClass entity = okb.AlgorithmClasses.FirstOrDefault(x => x.Id == dto.Id);
Convert.ToEntity(dto, entity);
okb.SubmitChanges();
}
}
public void DeleteAlgorithmClass(long id) {
using (OKBDataContext okb = new OKBDataContext()) {
DataAccess.AlgorithmClass entity = okb.AlgorithmClasses.FirstOrDefault(x => x.Id == id);
if (entity != null) okb.AlgorithmClasses.DeleteOnSubmit(entity);
okb.SubmitChanges();
}
}
#endregion
#region Algorithm Methods
public DataTransfer.Algorithm GetAlgorithm(long id) {
using (OKBDataContext okb = new OKBDataContext()) {
return Convert.ToDto(okb.Algorithms.FirstOrDefault(x => x.Id == id));
}
}
public IEnumerable GetAlgorithms() {
using (OKBDataContext okb = new OKBDataContext()) {
return okb.Algorithms.Select(x => Convert.ToDto(x)).ToArray();
}
}
public long AddAlgorithm(DataTransfer.Algorithm dto) {
using (OKBDataContext okb = new OKBDataContext()) {
DataAccess.Algorithm entity = Convert.ToEntity(dto, okb); entity.Id = 0;
okb.Algorithms.InsertOnSubmit(entity);
okb.SubmitChanges();
return entity.Id;
}
}
public void UpdateAlgorithm(DataTransfer.Algorithm dto) {
using (OKBDataContext okb = new OKBDataContext()) {
DataAccess.Algorithm entity = okb.Algorithms.FirstOrDefault(x => x.Id == dto.Id);
Convert.ToEntity(dto, entity, okb);
okb.SubmitChanges();
}
}
public void DeleteAlgorithm(long id) {
using (OKBDataContext okb = new OKBDataContext()) {
DataAccess.Algorithm entity = okb.Algorithms.FirstOrDefault(x => x.Id == id);
if (entity != null) okb.Algorithms.DeleteOnSubmit(entity);
okb.SubmitChanges();
}
}
public IEnumerable GetAlgorithmUsers(long algorithmId) {
using (OKBDataContext okb = new OKBDataContext()) {
return okb.AlgorithmUsers.Where(x => x.AlgorithmId == algorithmId).Select(x => x.UserId).ToArray();
}
}
public void UpdateAlgorithmUsers(long algorithmId, IEnumerable users) {
using (OKBDataContext okb = new OKBDataContext()) {
okb.AlgorithmUsers.DeleteAllOnSubmit(okb.AlgorithmUsers.Where(x => x.AlgorithmId == algorithmId));
okb.AlgorithmUsers.InsertAllOnSubmit(users.Select(x => new DataAccess.AlgorithmUser { AlgorithmId = algorithmId, UserId = x }));
okb.SubmitChanges();
}
}
public byte[] GetAlgorithmData(long algorithmId) {
using (OKBDataContext okb = new OKBDataContext()) {
var data = okb.Algorithms.Where(x => x.Id == algorithmId).Select(x => x.BinaryData).FirstOrDefault();
if (data != null) return data.Data.ToArray();
else return null;
}
}
public void UpdateAlgorithmData(long algorithmId, byte[] data) {
using (OKBDataContext okb = new OKBDataContext()) {
var entity = okb.Algorithms.Where(x => x.Id == algorithmId).FirstOrDefault();
if (entity != null)
entity.BinaryData = Convert.ToEntity(data, okb);
okb.SubmitChanges();
}
}
#endregion
#region ProblemClass Methods
public DataTransfer.ProblemClass GetProblemClass(long id) {
using (OKBDataContext okb = new OKBDataContext()) {
return Convert.ToDto(okb.ProblemClasses.FirstOrDefault(x => x.Id == id));
}
}
public IEnumerable GetProblemClasses() {
using (OKBDataContext okb = new OKBDataContext()) {
return okb.ProblemClasses.Select(x => Convert.ToDto(x)).ToArray();
}
}
public long AddProblemClass(DataTransfer.ProblemClass dto) {
using (OKBDataContext okb = new OKBDataContext()) {
DataAccess.ProblemClass entity = Convert.ToEntity(dto); entity.Id = 0;
okb.ProblemClasses.InsertOnSubmit(entity);
okb.SubmitChanges();
return entity.Id;
}
}
public void UpdateProblemClass(DataTransfer.ProblemClass dto) {
using (OKBDataContext okb = new OKBDataContext()) {
DataAccess.ProblemClass entity = okb.ProblemClasses.FirstOrDefault(x => x.Id == dto.Id);
Convert.ToEntity(dto, entity);
okb.SubmitChanges();
}
}
public void DeleteProblemClass(long id) {
using (OKBDataContext okb = new OKBDataContext()) {
DataAccess.ProblemClass entity = okb.ProblemClasses.FirstOrDefault(x => x.Id == id);
if (entity != null) okb.ProblemClasses.DeleteOnSubmit(entity);
okb.SubmitChanges();
}
}
#endregion
#region Problem Methods
public DataTransfer.Problem GetProblem(long id) {
using (OKBDataContext okb = new OKBDataContext()) {
return Convert.ToDto(okb.Problems.FirstOrDefault(x => x.Id == id));
}
}
public IEnumerable GetProblems() {
using (OKBDataContext okb = new OKBDataContext()) {
return okb.Problems.Select(x => Convert.ToDto(x)).ToArray();
}
}
public long AddProblem(DataTransfer.Problem dto) {
using (OKBDataContext okb = new OKBDataContext()) {
DataAccess.Problem entity = Convert.ToEntity(dto, okb); entity.Id = 0;
okb.Problems.InsertOnSubmit(entity);
okb.SubmitChanges();
return entity.Id;
}
}
public void UpdateProblem(DataTransfer.Problem dto) {
using (OKBDataContext okb = new OKBDataContext()) {
DataAccess.Problem entity = okb.Problems.FirstOrDefault(x => x.Id == dto.Id);
Convert.ToEntity(dto, entity, okb);
okb.SubmitChanges();
}
}
public void DeleteProblem(long id) {
using (OKBDataContext okb = new OKBDataContext()) {
DataAccess.Problem entity = okb.Problems.FirstOrDefault(x => x.Id == id);
if (entity != null) okb.Problems.DeleteOnSubmit(entity);
okb.SubmitChanges();
}
}
public IEnumerable GetProblemUsers(long problemId) {
using (OKBDataContext okb = new OKBDataContext()) {
return okb.ProblemUsers.Where(x => x.ProblemId == problemId).Select(x => x.UserId).ToArray();
}
}
public void UpdateProblemUsers(long problemId, IEnumerable users) {
using (OKBDataContext okb = new OKBDataContext()) {
okb.ProblemUsers.DeleteAllOnSubmit(okb.ProblemUsers.Where(x => x.ProblemId == problemId));
okb.ProblemUsers.InsertAllOnSubmit(users.Select(x => new DataAccess.ProblemUser { ProblemId = problemId, UserId = x }));
okb.SubmitChanges();
}
}
public byte[] GetProblemData(long problemId) {
using (OKBDataContext okb = new OKBDataContext()) {
var data = okb.Problems.Where(x => x.Id == problemId).Select(x => x.BinaryData).FirstOrDefault();
if (data != null) return data.Data.ToArray();
else return null;
}
}
public void UpdateProblemData(long problemId, byte[] data) {
using (OKBDataContext okb = new OKBDataContext()) {
var entity = okb.Problems.Where(x => x.Id == problemId).FirstOrDefault();
if (entity != null)
entity.BinaryData = Convert.ToEntity(data, okb);
okb.SubmitChanges();
}
}
#endregion
}
}