[5156] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
[4598] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Linq.Expressions;
|
---|
| 26 |
|
---|
| 27 | namespace HeuristicLab.Services.Hive.DataAccess {
|
---|
[4615] | 28 | using HeuristicLab.Services.Hive.Common.DataTransfer;
|
---|
[4649] | 29 | using HeuristicLab.Services.Hive.DataAccess.Properties;
|
---|
[5156] | 30 | using DT = HeuristicLab.Services.Hive.Common.DataTransfer;
|
---|
[4598] | 31 |
|
---|
| 32 | public class HiveDao : IHiveDao {
|
---|
[4905] | 33 | public static HiveDataContext CreateContext() {
|
---|
[4649] | 34 | return new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString);
|
---|
| 35 | }
|
---|
[4598] | 36 |
|
---|
[4649] | 37 | public HiveDao() {
|
---|
[4598] | 38 | }
|
---|
| 39 |
|
---|
| 40 | #region Job Methods
|
---|
| 41 | public DT.Job GetJob(Guid id) {
|
---|
[4649] | 42 | using (var db = CreateContext()) {
|
---|
| 43 | return Convert.ToDto(db.Jobs.SingleOrDefault(x => x.JobId == id));
|
---|
| 44 | }
|
---|
[4598] | 45 | }
|
---|
| 46 |
|
---|
| 47 | public IEnumerable<DT.Job> GetJobs(Expression<Func<Job, bool>> predicate) {
|
---|
[4649] | 48 | using (var db = CreateContext()) {
|
---|
| 49 | return db.Jobs.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
|
---|
| 50 | }
|
---|
[4598] | 51 | }
|
---|
| 52 |
|
---|
| 53 | public Guid AddJob(DT.Job dto) {
|
---|
[4649] | 54 | using (var db = CreateContext()) {
|
---|
| 55 | var entity = Convert.ToEntity(dto);
|
---|
| 56 | db.Jobs.InsertOnSubmit(entity);
|
---|
| 57 | db.SubmitChanges();
|
---|
[5402] | 58 | foreach (Guid pluginId in dto.PluginsNeededIds) {
|
---|
| 59 | db.RequiredPlugins.InsertOnSubmit(new RequiredPlugin() { JobId = entity.JobId, PluginId = pluginId });
|
---|
| 60 | }
|
---|
| 61 | db.SubmitChanges();
|
---|
[4649] | 62 | return entity.JobId;
|
---|
| 63 | }
|
---|
[4598] | 64 | }
|
---|
| 65 |
|
---|
| 66 | public void UpdateJob(DT.Job dto) {
|
---|
[4649] | 67 | using (var db = CreateContext()) {
|
---|
| 68 | var entity = db.Jobs.FirstOrDefault(x => x.JobId == dto.Id);
|
---|
| 69 | if (entity == null) db.Jobs.InsertOnSubmit(Convert.ToEntity(dto));
|
---|
| 70 | else Convert.ToEntity(dto, entity);
|
---|
[5402] | 71 | // todo: update required plugins
|
---|
[4649] | 72 | db.SubmitChanges();
|
---|
| 73 | }
|
---|
[4598] | 74 | }
|
---|
| 75 |
|
---|
| 76 | public void DeleteJob(Guid id) {
|
---|
[4649] | 77 | using (var db = CreateContext()) {
|
---|
| 78 | var entity = db.Jobs.FirstOrDefault(x => x.JobId == id);
|
---|
| 79 | if (entity != null) db.Jobs.DeleteOnSubmit(entity);
|
---|
[5106] | 80 | db.SubmitChanges(); // JobData and child jobs are deleted by db-trigger
|
---|
[4649] | 81 | }
|
---|
[4598] | 82 | }
|
---|
[4615] | 83 |
|
---|
[5404] | 84 | public IEnumerable<DT.Job> GetWaitingParentJobs(IEnumerable<Guid> resourceIds, int count) {
|
---|
[4649] | 85 | using (var db = CreateContext()) {
|
---|
| 86 | var query = from ar in db.AssignedResources
|
---|
[5404] | 87 | where resourceIds.Contains(ar.ResourceId)
|
---|
| 88 | && ar.Job.JobState == JobState.WaitingForChildJobs
|
---|
| 89 | && (from child in db.Jobs
|
---|
| 90 | where child.ParentJobId == ar.Job.JobId
|
---|
| 91 | select child.JobState == JobState.Finished).All(x => x)
|
---|
| 92 | && (from child in db.Jobs // avoid returning WaitForChildJobs jobs where no child-jobs exist (yet)
|
---|
| 93 | where child.ParentJobId == ar.Job.JobId
|
---|
| 94 | select child).Count() > 0
|
---|
[4649] | 95 | orderby ar.Job.Priority descending
|
---|
| 96 | select Convert.ToDto(ar.Job);
|
---|
[5404] | 97 | return count == 0 ? query.ToArray() : query.Take(count).ToArray();
|
---|
[4649] | 98 | }
|
---|
[4615] | 99 | }
|
---|
[4629] | 100 |
|
---|
[5404] | 101 | public IEnumerable<DT.Job> GetWaitingJobs(DT.Slave slave, int count) {
|
---|
[4649] | 102 | using (var db = CreateContext()) {
|
---|
[5404] | 103 | var resourceIds = GetParentResources(slave.Id).Select(r => r.Id);
|
---|
| 104 | var waitingParentJobs = GetWaitingParentJobs(resourceIds, count);
|
---|
| 105 | if (count > 0 && waitingParentJobs.Count() >= count) return waitingParentJobs.Take(count).ToArray();
|
---|
| 106 |
|
---|
| 107 | var query = from ar in db.AssignedResources
|
---|
| 108 | where resourceIds.Contains(ar.ResourceId)
|
---|
| 109 | && ar.Job.JobState == JobState.Waiting
|
---|
| 110 | && ar.Job.CoresNeeded <= slave.FreeCores
|
---|
| 111 | && ar.Job.MemoryNeeded <= slave.FreeMemory
|
---|
| 112 | orderby ar.Job.Priority descending
|
---|
| 113 | select Convert.ToDto(ar.Job);
|
---|
| 114 | var waitingJobs = (count == 0 ? query : query.Take(count)).ToArray();
|
---|
[5375] | 115 | return waitingJobs.Union(waitingParentJobs).OrderByDescending(x => x.Priority);
|
---|
[4649] | 116 | }
|
---|
[4629] | 117 | }
|
---|
[4598] | 118 | #endregion
|
---|
| 119 |
|
---|
| 120 | #region JobData Methods
|
---|
| 121 |
|
---|
| 122 | public DT.JobData GetJobData(Guid id) {
|
---|
[4649] | 123 | using (var db = CreateContext()) {
|
---|
| 124 | return Convert.ToDto(db.JobDatas.SingleOrDefault(x => x.JobId == id));
|
---|
| 125 | }
|
---|
[4598] | 126 | }
|
---|
| 127 |
|
---|
| 128 | public IEnumerable<DT.JobData> GetJobDatas(Expression<Func<JobData, bool>> predicate) {
|
---|
[4649] | 129 | using (var db = CreateContext()) {
|
---|
| 130 | return db.JobDatas.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
|
---|
| 131 | }
|
---|
[4598] | 132 | }
|
---|
| 133 |
|
---|
| 134 | public Guid AddJobData(DT.JobData dto) {
|
---|
[4649] | 135 | using (var db = CreateContext()) {
|
---|
| 136 | var entity = Convert.ToEntity(dto);
|
---|
| 137 | db.JobDatas.InsertOnSubmit(entity);
|
---|
| 138 | db.SubmitChanges();
|
---|
| 139 | return entity.JobId;
|
---|
| 140 | }
|
---|
[4598] | 141 | }
|
---|
| 142 |
|
---|
| 143 | public void UpdateJobData(DT.JobData dto) {
|
---|
[4649] | 144 | using (var db = CreateContext()) {
|
---|
| 145 | var entity = db.JobDatas.FirstOrDefault(x => x.JobId == dto.JobId);
|
---|
| 146 | if (entity == null) db.JobDatas.InsertOnSubmit(Convert.ToEntity(dto));
|
---|
| 147 | else Convert.ToEntity(dto, entity);
|
---|
| 148 | db.SubmitChanges();
|
---|
| 149 | }
|
---|
[4598] | 150 | }
|
---|
| 151 |
|
---|
| 152 | public void DeleteJobData(Guid id) {
|
---|
[4649] | 153 | using (var db = CreateContext()) {
|
---|
| 154 | var entity = db.JobDatas.FirstOrDefault(x => x.JobId == id); // check if all the byte[] is loaded into memory here. otherwise work around to delete without loading it
|
---|
| 155 | if (entity != null) db.JobDatas.DeleteOnSubmit(entity);
|
---|
| 156 | db.SubmitChanges();
|
---|
| 157 | }
|
---|
[4598] | 158 | }
|
---|
| 159 | #endregion
|
---|
| 160 |
|
---|
| 161 | #region HiveExperiment Methods
|
---|
| 162 | public DT.HiveExperiment GetHiveExperiment(Guid id) {
|
---|
[4649] | 163 | using (var db = CreateContext()) {
|
---|
| 164 | return Convert.ToDto(db.HiveExperiments.SingleOrDefault(x => x.HiveExperimentId == id));
|
---|
| 165 | }
|
---|
[4598] | 166 | }
|
---|
| 167 |
|
---|
| 168 | public IEnumerable<DT.HiveExperiment> GetHiveExperiments(Expression<Func<HiveExperiment, bool>> predicate) {
|
---|
[4649] | 169 | using (var db = CreateContext()) {
|
---|
| 170 | return db.HiveExperiments.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
|
---|
| 171 | }
|
---|
[4598] | 172 | }
|
---|
| 173 |
|
---|
| 174 | public Guid AddHiveExperiment(DT.HiveExperiment dto) {
|
---|
[4649] | 175 | using (var db = CreateContext()) {
|
---|
| 176 | var entity = Convert.ToEntity(dto);
|
---|
| 177 | db.HiveExperiments.InsertOnSubmit(entity);
|
---|
| 178 | db.SubmitChanges();
|
---|
| 179 | return entity.HiveExperimentId;
|
---|
| 180 | }
|
---|
[4598] | 181 | }
|
---|
| 182 |
|
---|
| 183 | public void UpdateHiveExperiment(DT.HiveExperiment dto) {
|
---|
[4649] | 184 | using (var db = CreateContext()) {
|
---|
| 185 | var entity = db.HiveExperiments.FirstOrDefault(x => x.HiveExperimentId == dto.Id);
|
---|
| 186 | if (entity == null) db.HiveExperiments.InsertOnSubmit(Convert.ToEntity(dto));
|
---|
| 187 | else Convert.ToEntity(dto, entity);
|
---|
| 188 | db.SubmitChanges();
|
---|
| 189 | }
|
---|
[4598] | 190 | }
|
---|
| 191 |
|
---|
| 192 | public void DeleteHiveExperiment(Guid id) {
|
---|
[4649] | 193 | using (var db = CreateContext()) {
|
---|
| 194 | var entity = db.HiveExperiments.FirstOrDefault(x => x.HiveExperimentId == id);
|
---|
| 195 | if (entity != null) db.HiveExperiments.DeleteOnSubmit(entity);
|
---|
| 196 | db.SubmitChanges();
|
---|
| 197 | }
|
---|
[4598] | 198 | }
|
---|
| 199 | #endregion
|
---|
| 200 |
|
---|
[4905] | 201 | #region Plugin Methods
|
---|
| 202 | public DT.Plugin GetPlugin(Guid id) {
|
---|
| 203 | using (var db = CreateContext()) {
|
---|
| 204 | return Convert.ToDto(db.Plugins.SingleOrDefault(x => x.PluginId == id));
|
---|
| 205 | }
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | public IEnumerable<DT.Plugin> GetPlugins(Expression<Func<Plugin, bool>> predicate) {
|
---|
| 209 | using (var db = CreateContext()) {
|
---|
| 210 | return db.Plugins.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
|
---|
| 211 | }
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | public Guid AddPlugin(DT.Plugin dto) {
|
---|
| 215 | using (var db = CreateContext()) {
|
---|
| 216 | var entity = Convert.ToEntity(dto);
|
---|
| 217 | db.Plugins.InsertOnSubmit(entity);
|
---|
| 218 | db.SubmitChanges();
|
---|
| 219 | return entity.PluginId;
|
---|
| 220 | }
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | public void UpdatePlugin(DT.Plugin dto) {
|
---|
| 224 | using (var db = CreateContext()) {
|
---|
| 225 | var entity = db.Plugins.FirstOrDefault(x => x.PluginId == dto.Id);
|
---|
| 226 | if (entity == null) db.Plugins.InsertOnSubmit(Convert.ToEntity(dto));
|
---|
| 227 | else Convert.ToEntity(dto, entity);
|
---|
| 228 | db.SubmitChanges();
|
---|
| 229 | }
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | public void DeletePlugin(Guid id) {
|
---|
| 233 | using (var db = CreateContext()) {
|
---|
| 234 | var entity = db.Plugins.FirstOrDefault(x => x.PluginId == id);
|
---|
| 235 | if (entity != null) db.Plugins.DeleteOnSubmit(entity);
|
---|
| 236 | db.SubmitChanges();
|
---|
| 237 | }
|
---|
| 238 | }
|
---|
| 239 | #endregion
|
---|
| 240 |
|
---|
| 241 | #region PluginData Methods
|
---|
| 242 |
|
---|
| 243 | public DT.PluginData GetPluginData(Guid id) {
|
---|
| 244 | using (var db = CreateContext()) {
|
---|
[5402] | 245 | return Convert.ToDto(db.PluginDatas.SingleOrDefault(x => x.PluginDataId == id));
|
---|
[4905] | 246 | }
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | public IEnumerable<DT.PluginData> GetPluginDatas(Expression<Func<PluginData, bool>> predicate) {
|
---|
| 250 | using (var db = CreateContext()) {
|
---|
| 251 | return db.PluginDatas.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
|
---|
| 252 | }
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 | public Guid AddPluginData(DT.PluginData dto) {
|
---|
| 256 | using (var db = CreateContext()) {
|
---|
| 257 | var entity = Convert.ToEntity(dto);
|
---|
| 258 | db.PluginDatas.InsertOnSubmit(entity);
|
---|
| 259 | db.SubmitChanges();
|
---|
[5402] | 260 | return entity.PluginDataId;
|
---|
[4905] | 261 | }
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 | public void UpdatePluginData(DT.PluginData dto) {
|
---|
| 265 | using (var db = CreateContext()) {
|
---|
| 266 | var entity = db.PluginDatas.FirstOrDefault(x => x.PluginId == dto.PluginId);
|
---|
| 267 | if (entity == null) db.PluginDatas.InsertOnSubmit(Convert.ToEntity(dto));
|
---|
| 268 | else Convert.ToEntity(dto, entity);
|
---|
| 269 | db.SubmitChanges();
|
---|
| 270 | }
|
---|
| 271 | }
|
---|
| 272 |
|
---|
| 273 | public void DeletePluginData(Guid id) {
|
---|
| 274 | using (var db = CreateContext()) {
|
---|
[5402] | 275 | var entity = db.PluginDatas.FirstOrDefault(x => x.PluginDataId == id); // todo: check if all the byte[] is loaded into memory here. otherwise work around to delete without loading it
|
---|
[4905] | 276 | if (entity != null) db.PluginDatas.DeleteOnSubmit(entity);
|
---|
| 277 | db.SubmitChanges();
|
---|
| 278 | }
|
---|
| 279 | }
|
---|
| 280 | #endregion
|
---|
| 281 |
|
---|
[4598] | 282 | #region Slave Methods
|
---|
| 283 | public DT.Slave GetSlave(Guid id) {
|
---|
[4649] | 284 | using (var db = CreateContext()) {
|
---|
| 285 | return Convert.ToDto(db.Resources.OfType<Slave>().SingleOrDefault(x => x.ResourceId == id));
|
---|
| 286 | }
|
---|
[4598] | 287 | }
|
---|
| 288 |
|
---|
| 289 | public IEnumerable<DT.Slave> GetSlaves(Expression<Func<Slave, bool>> predicate) {
|
---|
[4649] | 290 | using (var db = CreateContext()) {
|
---|
| 291 | return db.Resources.OfType<Slave>().Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
|
---|
| 292 | }
|
---|
[4598] | 293 | }
|
---|
| 294 |
|
---|
| 295 | public Guid AddSlave(DT.Slave dto) {
|
---|
[4649] | 296 | using (var db = CreateContext()) {
|
---|
| 297 | var entity = Convert.ToEntity(dto);
|
---|
| 298 | db.Resources.InsertOnSubmit(entity);
|
---|
| 299 | db.SubmitChanges();
|
---|
| 300 | return entity.ResourceId;
|
---|
| 301 | }
|
---|
[4598] | 302 | }
|
---|
| 303 |
|
---|
| 304 | public void UpdateSlave(DT.Slave dto) {
|
---|
[4649] | 305 | using (var db = CreateContext()) {
|
---|
| 306 | var entity = db.Resources.OfType<Slave>().FirstOrDefault(x => x.ResourceId == dto.Id);
|
---|
| 307 | if (entity == null) db.Resources.InsertOnSubmit(Convert.ToEntity(dto));
|
---|
| 308 | else Convert.ToEntity(dto, entity);
|
---|
| 309 | db.SubmitChanges();
|
---|
| 310 | }
|
---|
[4598] | 311 | }
|
---|
| 312 |
|
---|
| 313 | public void DeleteSlave(Guid id) {
|
---|
[4649] | 314 | using (var db = CreateContext()) {
|
---|
| 315 | var entity = db.Resources.OfType<Slave>().FirstOrDefault(x => x.ResourceId == id);
|
---|
| 316 | if (entity != null) db.Resources.DeleteOnSubmit(entity);
|
---|
| 317 | db.SubmitChanges();
|
---|
| 318 | }
|
---|
[4598] | 319 | }
|
---|
| 320 | #endregion
|
---|
| 321 |
|
---|
| 322 | #region SlaveGroup Methods
|
---|
| 323 | public DT.SlaveGroup GetSlaveGroup(Guid id) {
|
---|
[4649] | 324 | using (var db = CreateContext()) {
|
---|
| 325 | return Convert.ToDto(db.Resources.OfType<SlaveGroup>().SingleOrDefault(x => x.ResourceId == id));
|
---|
| 326 | }
|
---|
[4598] | 327 | }
|
---|
| 328 |
|
---|
| 329 | public IEnumerable<DT.SlaveGroup> GetSlaveGroups(Expression<Func<SlaveGroup, bool>> predicate) {
|
---|
[4649] | 330 | using (var db = CreateContext()) {
|
---|
| 331 | return db.Resources.OfType<SlaveGroup>().Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
|
---|
| 332 | }
|
---|
[4598] | 333 | }
|
---|
| 334 |
|
---|
| 335 | public Guid AddSlaveGroup(DT.SlaveGroup dto) {
|
---|
[4649] | 336 | using (var db = CreateContext()) {
|
---|
[5375] | 337 | if (dto.Id == Guid.Empty)
|
---|
| 338 | dto.Id = Guid.NewGuid();
|
---|
[4649] | 339 | var entity = Convert.ToEntity(dto);
|
---|
| 340 | db.Resources.InsertOnSubmit(entity);
|
---|
| 341 | db.SubmitChanges();
|
---|
| 342 | return entity.ResourceId;
|
---|
| 343 | }
|
---|
[4598] | 344 | }
|
---|
| 345 |
|
---|
| 346 | public void UpdateSlaveGroup(DT.SlaveGroup dto) {
|
---|
[4649] | 347 | using (var db = CreateContext()) {
|
---|
| 348 | var entity = db.Resources.OfType<SlaveGroup>().FirstOrDefault(x => x.ResourceId == dto.Id);
|
---|
| 349 | if (entity == null) db.Resources.InsertOnSubmit(Convert.ToEntity(dto));
|
---|
| 350 | else Convert.ToEntity(dto, entity);
|
---|
| 351 | db.SubmitChanges();
|
---|
| 352 | }
|
---|
[4598] | 353 | }
|
---|
| 354 |
|
---|
| 355 | public void DeleteSlaveGroup(Guid id) {
|
---|
[4649] | 356 | using (var db = CreateContext()) {
|
---|
| 357 | var entity = db.Resources.OfType<SlaveGroup>().FirstOrDefault(x => x.ResourceId == id);
|
---|
[5404] | 358 | if (entity != null) {
|
---|
| 359 | if (db.Resources.Where(r => r.ParentResourceId == id).Count() > 0) {
|
---|
| 360 | throw new DaoException("Cannot delete SlaveGroup as long as there are Slaves in the group");
|
---|
| 361 | }
|
---|
| 362 | db.Resources.DeleteOnSubmit(entity);
|
---|
| 363 | }
|
---|
[4649] | 364 | db.SubmitChanges();
|
---|
| 365 | }
|
---|
[4598] | 366 | }
|
---|
| 367 | #endregion
|
---|
| 368 |
|
---|
| 369 | #region Resource Methods
|
---|
| 370 | public DT.Resource GetResource(Guid id) {
|
---|
[4649] | 371 | using (var db = CreateContext()) {
|
---|
| 372 | return Convert.ToDto(db.Resources.SingleOrDefault(x => x.ResourceId == id));
|
---|
| 373 | }
|
---|
[4598] | 374 | }
|
---|
| 375 |
|
---|
| 376 | public IEnumerable<DT.Resource> GetResources(Expression<Func<Resource, bool>> predicate) {
|
---|
[4649] | 377 | using (var db = CreateContext()) {
|
---|
| 378 | return db.Resources.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
|
---|
| 379 | }
|
---|
[4598] | 380 | }
|
---|
| 381 |
|
---|
| 382 | public Guid AddResource(DT.Resource dto) {
|
---|
[4649] | 383 | using (var db = CreateContext()) {
|
---|
| 384 | var entity = Convert.ToEntity(dto);
|
---|
| 385 | db.Resources.InsertOnSubmit(entity);
|
---|
| 386 | db.SubmitChanges();
|
---|
| 387 | return entity.ResourceId;
|
---|
| 388 | }
|
---|
[4598] | 389 | }
|
---|
| 390 |
|
---|
| 391 | public void UpdateResource(DT.Resource dto) {
|
---|
[4649] | 392 | using (var db = CreateContext()) {
|
---|
| 393 | var entity = db.Resources.FirstOrDefault(x => x.ResourceId == dto.Id);
|
---|
| 394 | if (entity == null) db.Resources.InsertOnSubmit(Convert.ToEntity(dto));
|
---|
| 395 | else Convert.ToEntity(dto, entity);
|
---|
| 396 | db.SubmitChanges();
|
---|
| 397 | }
|
---|
[4598] | 398 | }
|
---|
| 399 |
|
---|
| 400 | public void DeleteResource(Guid id) {
|
---|
[4649] | 401 | using (var db = CreateContext()) {
|
---|
| 402 | var entity = db.Resources.FirstOrDefault(x => x.ResourceId == id);
|
---|
| 403 | if (entity != null) db.Resources.DeleteOnSubmit(entity);
|
---|
| 404 | db.SubmitChanges();
|
---|
| 405 | }
|
---|
[4598] | 406 | }
|
---|
[5155] | 407 |
|
---|
| 408 | public void AssignJobToResource(Guid jobId, Guid resourceId) {
|
---|
| 409 | using (var db = CreateContext()) {
|
---|
| 410 | var job = db.Jobs.Where(x => x.JobId == jobId).Single();
|
---|
| 411 | job.AssignedResources.Add(new AssignedResource() { JobId = jobId, ResourceId = resourceId });
|
---|
| 412 | db.SubmitChanges();
|
---|
| 413 | }
|
---|
| 414 | }
|
---|
| 415 |
|
---|
| 416 | public IEnumerable<DT.Resource> GetAssignedResources(Guid jobId) {
|
---|
| 417 | using (var db = CreateContext()) {
|
---|
| 418 | var job = db.Jobs.Where(x => x.JobId == jobId).Single();
|
---|
| 419 | return job.AssignedResources.Select(x => Convert.ToDto(x.Resource)).ToArray();
|
---|
| 420 | }
|
---|
| 421 | }
|
---|
[5404] | 422 |
|
---|
| 423 | /// <summary>
|
---|
| 424 | /// Returns all parent resources of a resource (the given resource is also added)
|
---|
| 425 | /// </summary>
|
---|
| 426 | private IEnumerable<DT.Resource> GetParentResources(Guid resourceId) {
|
---|
| 427 | using (var db = CreateContext()) {
|
---|
| 428 | var resources = new List<Resource>();
|
---|
| 429 | CollectParentResources(resources, db.Resources.Where(r => r.ResourceId == resourceId).Single());
|
---|
| 430 | return resources.Select(r => Convert.ToDto(r)).ToArray();
|
---|
| 431 | }
|
---|
| 432 | }
|
---|
| 433 |
|
---|
| 434 | private void CollectParentResources(List<Resource> resources, Resource resource) {
|
---|
| 435 | if (resource == null) return;
|
---|
| 436 | resources.Add(resource);
|
---|
| 437 | CollectParentResources(resources, resource.ParentResource);
|
---|
| 438 | }
|
---|
[4598] | 439 | #endregion
|
---|
| 440 |
|
---|
| 441 | #region Authorization Methods
|
---|
| 442 | public bool IsUserAuthorizedForJobs(Guid userId, params Guid[] jobIds) {
|
---|
[4649] | 443 | using (var db = CreateContext()) {
|
---|
| 444 | var userIds = from job in db.Jobs // this needs to be fast!
|
---|
| 445 | where jobIds.Contains(job.JobId)
|
---|
| 446 | select job.UserId;
|
---|
| 447 | return userIds.All(x => x == userId);
|
---|
| 448 | }
|
---|
[4598] | 449 | }
|
---|
| 450 | #endregion
|
---|
| 451 | }
|
---|
| 452 | }
|
---|