[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 |
|
---|
[5155] | 84 | public IEnumerable<DT.Job> GetWaitingParentJobs(Guid slaveId) {
|
---|
[4649] | 85 | using (var db = CreateContext()) {
|
---|
| 86 | // todo: slaveId is unused!
|
---|
| 87 | var query = from ar in db.AssignedResources
|
---|
| 88 | where 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
|
---|
| 95 | orderby ar.Job.Priority descending
|
---|
| 96 | select Convert.ToDto(ar.Job);
|
---|
[5375] | 97 | return query.ToArray();
|
---|
[4649] | 98 | }
|
---|
[4615] | 99 | }
|
---|
[4629] | 100 |
|
---|
[4905] | 101 | public IEnumerable<DT.Job> GetWaitingJobs(DT.Slave slave) {
|
---|
[4649] | 102 | using (var db = CreateContext()) {
|
---|
| 103 | var query = from j in db.Jobs
|
---|
| 104 | where j.JobState == JobState.Waiting && j.CoresNeeded <= slave.FreeCores && j.MemoryNeeded <= slave.FreeMemory
|
---|
| 105 | orderby j.Priority descending
|
---|
| 106 | select Convert.ToDto(j);
|
---|
[5375] | 107 | var waitingJobs = query.ToArray();
|
---|
| 108 | var waitingParentJobs = GetWaitingParentJobs(slave.Id);
|
---|
| 109 | return waitingJobs.Union(waitingParentJobs).OrderByDescending(x => x.Priority);
|
---|
[4649] | 110 | }
|
---|
[4629] | 111 | }
|
---|
[4598] | 112 | #endregion
|
---|
| 113 |
|
---|
| 114 | #region JobData Methods
|
---|
| 115 |
|
---|
| 116 | public DT.JobData GetJobData(Guid id) {
|
---|
[4649] | 117 | using (var db = CreateContext()) {
|
---|
| 118 | return Convert.ToDto(db.JobDatas.SingleOrDefault(x => x.JobId == id));
|
---|
| 119 | }
|
---|
[4598] | 120 | }
|
---|
| 121 |
|
---|
| 122 | public IEnumerable<DT.JobData> GetJobDatas(Expression<Func<JobData, bool>> predicate) {
|
---|
[4649] | 123 | using (var db = CreateContext()) {
|
---|
| 124 | return db.JobDatas.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
|
---|
| 125 | }
|
---|
[4598] | 126 | }
|
---|
| 127 |
|
---|
| 128 | public Guid AddJobData(DT.JobData dto) {
|
---|
[4649] | 129 | using (var db = CreateContext()) {
|
---|
| 130 | var entity = Convert.ToEntity(dto);
|
---|
| 131 | db.JobDatas.InsertOnSubmit(entity);
|
---|
| 132 | db.SubmitChanges();
|
---|
| 133 | return entity.JobId;
|
---|
| 134 | }
|
---|
[4598] | 135 | }
|
---|
| 136 |
|
---|
| 137 | public void UpdateJobData(DT.JobData dto) {
|
---|
[4649] | 138 | using (var db = CreateContext()) {
|
---|
| 139 | var entity = db.JobDatas.FirstOrDefault(x => x.JobId == dto.JobId);
|
---|
| 140 | if (entity == null) db.JobDatas.InsertOnSubmit(Convert.ToEntity(dto));
|
---|
| 141 | else Convert.ToEntity(dto, entity);
|
---|
| 142 | db.SubmitChanges();
|
---|
| 143 | }
|
---|
[4598] | 144 | }
|
---|
| 145 |
|
---|
| 146 | public void DeleteJobData(Guid id) {
|
---|
[4649] | 147 | using (var db = CreateContext()) {
|
---|
| 148 | 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
|
---|
| 149 | if (entity != null) db.JobDatas.DeleteOnSubmit(entity);
|
---|
| 150 | db.SubmitChanges();
|
---|
| 151 | }
|
---|
[4598] | 152 | }
|
---|
| 153 | #endregion
|
---|
| 154 |
|
---|
| 155 | #region HiveExperiment Methods
|
---|
| 156 | public DT.HiveExperiment GetHiveExperiment(Guid id) {
|
---|
[4649] | 157 | using (var db = CreateContext()) {
|
---|
| 158 | return Convert.ToDto(db.HiveExperiments.SingleOrDefault(x => x.HiveExperimentId == id));
|
---|
| 159 | }
|
---|
[4598] | 160 | }
|
---|
| 161 |
|
---|
| 162 | public IEnumerable<DT.HiveExperiment> GetHiveExperiments(Expression<Func<HiveExperiment, bool>> predicate) {
|
---|
[4649] | 163 | using (var db = CreateContext()) {
|
---|
| 164 | return db.HiveExperiments.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
|
---|
| 165 | }
|
---|
[4598] | 166 | }
|
---|
| 167 |
|
---|
| 168 | public Guid AddHiveExperiment(DT.HiveExperiment dto) {
|
---|
[4649] | 169 | using (var db = CreateContext()) {
|
---|
| 170 | var entity = Convert.ToEntity(dto);
|
---|
| 171 | db.HiveExperiments.InsertOnSubmit(entity);
|
---|
| 172 | db.SubmitChanges();
|
---|
| 173 | return entity.HiveExperimentId;
|
---|
| 174 | }
|
---|
[4598] | 175 | }
|
---|
| 176 |
|
---|
| 177 | public void UpdateHiveExperiment(DT.HiveExperiment dto) {
|
---|
[4649] | 178 | using (var db = CreateContext()) {
|
---|
| 179 | var entity = db.HiveExperiments.FirstOrDefault(x => x.HiveExperimentId == dto.Id);
|
---|
| 180 | if (entity == null) db.HiveExperiments.InsertOnSubmit(Convert.ToEntity(dto));
|
---|
| 181 | else Convert.ToEntity(dto, entity);
|
---|
| 182 | db.SubmitChanges();
|
---|
| 183 | }
|
---|
[4598] | 184 | }
|
---|
| 185 |
|
---|
| 186 | public void DeleteHiveExperiment(Guid id) {
|
---|
[4649] | 187 | using (var db = CreateContext()) {
|
---|
| 188 | var entity = db.HiveExperiments.FirstOrDefault(x => x.HiveExperimentId == id);
|
---|
| 189 | if (entity != null) db.HiveExperiments.DeleteOnSubmit(entity);
|
---|
| 190 | db.SubmitChanges();
|
---|
| 191 | }
|
---|
[4598] | 192 | }
|
---|
| 193 | #endregion
|
---|
| 194 |
|
---|
[4905] | 195 | #region Plugin Methods
|
---|
| 196 | public DT.Plugin GetPlugin(Guid id) {
|
---|
| 197 | using (var db = CreateContext()) {
|
---|
| 198 | return Convert.ToDto(db.Plugins.SingleOrDefault(x => x.PluginId == id));
|
---|
| 199 | }
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | public IEnumerable<DT.Plugin> GetPlugins(Expression<Func<Plugin, bool>> predicate) {
|
---|
| 203 | using (var db = CreateContext()) {
|
---|
| 204 | return db.Plugins.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
|
---|
| 205 | }
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | public Guid AddPlugin(DT.Plugin dto) {
|
---|
| 209 | using (var db = CreateContext()) {
|
---|
| 210 | var entity = Convert.ToEntity(dto);
|
---|
| 211 | db.Plugins.InsertOnSubmit(entity);
|
---|
| 212 | db.SubmitChanges();
|
---|
| 213 | return entity.PluginId;
|
---|
| 214 | }
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | public void UpdatePlugin(DT.Plugin dto) {
|
---|
| 218 | using (var db = CreateContext()) {
|
---|
| 219 | var entity = db.Plugins.FirstOrDefault(x => x.PluginId == dto.Id);
|
---|
| 220 | if (entity == null) db.Plugins.InsertOnSubmit(Convert.ToEntity(dto));
|
---|
| 221 | else Convert.ToEntity(dto, entity);
|
---|
| 222 | db.SubmitChanges();
|
---|
| 223 | }
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | public void DeletePlugin(Guid id) {
|
---|
| 227 | using (var db = CreateContext()) {
|
---|
| 228 | var entity = db.Plugins.FirstOrDefault(x => x.PluginId == id);
|
---|
| 229 | if (entity != null) db.Plugins.DeleteOnSubmit(entity);
|
---|
| 230 | db.SubmitChanges();
|
---|
| 231 | }
|
---|
| 232 | }
|
---|
| 233 | #endregion
|
---|
| 234 |
|
---|
| 235 | #region PluginData Methods
|
---|
| 236 |
|
---|
| 237 | public DT.PluginData GetPluginData(Guid id) {
|
---|
| 238 | using (var db = CreateContext()) {
|
---|
[5402] | 239 | return Convert.ToDto(db.PluginDatas.SingleOrDefault(x => x.PluginDataId == id));
|
---|
[4905] | 240 | }
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | public IEnumerable<DT.PluginData> GetPluginDatas(Expression<Func<PluginData, bool>> predicate) {
|
---|
| 244 | using (var db = CreateContext()) {
|
---|
| 245 | return db.PluginDatas.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
|
---|
| 246 | }
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | public Guid AddPluginData(DT.PluginData dto) {
|
---|
| 250 | using (var db = CreateContext()) {
|
---|
| 251 | var entity = Convert.ToEntity(dto);
|
---|
| 252 | db.PluginDatas.InsertOnSubmit(entity);
|
---|
| 253 | db.SubmitChanges();
|
---|
[5402] | 254 | return entity.PluginDataId;
|
---|
[4905] | 255 | }
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 | public void UpdatePluginData(DT.PluginData dto) {
|
---|
| 259 | using (var db = CreateContext()) {
|
---|
| 260 | var entity = db.PluginDatas.FirstOrDefault(x => x.PluginId == dto.PluginId);
|
---|
| 261 | if (entity == null) db.PluginDatas.InsertOnSubmit(Convert.ToEntity(dto));
|
---|
| 262 | else Convert.ToEntity(dto, entity);
|
---|
| 263 | db.SubmitChanges();
|
---|
| 264 | }
|
---|
| 265 | }
|
---|
| 266 |
|
---|
| 267 | public void DeletePluginData(Guid id) {
|
---|
| 268 | using (var db = CreateContext()) {
|
---|
[5402] | 269 | 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] | 270 | if (entity != null) db.PluginDatas.DeleteOnSubmit(entity);
|
---|
| 271 | db.SubmitChanges();
|
---|
| 272 | }
|
---|
| 273 | }
|
---|
| 274 | #endregion
|
---|
| 275 |
|
---|
[4598] | 276 | #region Slave Methods
|
---|
| 277 | public DT.Slave GetSlave(Guid id) {
|
---|
[4649] | 278 | using (var db = CreateContext()) {
|
---|
| 279 | return Convert.ToDto(db.Resources.OfType<Slave>().SingleOrDefault(x => x.ResourceId == id));
|
---|
| 280 | }
|
---|
[4598] | 281 | }
|
---|
| 282 |
|
---|
| 283 | public IEnumerable<DT.Slave> GetSlaves(Expression<Func<Slave, bool>> predicate) {
|
---|
[4649] | 284 | using (var db = CreateContext()) {
|
---|
| 285 | return db.Resources.OfType<Slave>().Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
|
---|
| 286 | }
|
---|
[4598] | 287 | }
|
---|
| 288 |
|
---|
| 289 | public Guid AddSlave(DT.Slave dto) {
|
---|
[4649] | 290 | using (var db = CreateContext()) {
|
---|
| 291 | var entity = Convert.ToEntity(dto);
|
---|
| 292 | db.Resources.InsertOnSubmit(entity);
|
---|
| 293 | db.SubmitChanges();
|
---|
| 294 | return entity.ResourceId;
|
---|
| 295 | }
|
---|
[4598] | 296 | }
|
---|
| 297 |
|
---|
| 298 | public void UpdateSlave(DT.Slave dto) {
|
---|
[4649] | 299 | using (var db = CreateContext()) {
|
---|
| 300 | var entity = db.Resources.OfType<Slave>().FirstOrDefault(x => x.ResourceId == dto.Id);
|
---|
| 301 | if (entity == null) db.Resources.InsertOnSubmit(Convert.ToEntity(dto));
|
---|
| 302 | else Convert.ToEntity(dto, entity);
|
---|
| 303 | db.SubmitChanges();
|
---|
| 304 | }
|
---|
[4598] | 305 | }
|
---|
| 306 |
|
---|
| 307 | public void DeleteSlave(Guid id) {
|
---|
[4649] | 308 | using (var db = CreateContext()) {
|
---|
| 309 | var entity = db.Resources.OfType<Slave>().FirstOrDefault(x => x.ResourceId == id);
|
---|
| 310 | if (entity != null) db.Resources.DeleteOnSubmit(entity);
|
---|
| 311 | db.SubmitChanges();
|
---|
| 312 | }
|
---|
[4598] | 313 | }
|
---|
| 314 | #endregion
|
---|
| 315 |
|
---|
| 316 | #region SlaveGroup Methods
|
---|
| 317 | public DT.SlaveGroup GetSlaveGroup(Guid id) {
|
---|
[4649] | 318 | using (var db = CreateContext()) {
|
---|
| 319 | return Convert.ToDto(db.Resources.OfType<SlaveGroup>().SingleOrDefault(x => x.ResourceId == id));
|
---|
| 320 | }
|
---|
[4598] | 321 | }
|
---|
| 322 |
|
---|
| 323 | public IEnumerable<DT.SlaveGroup> GetSlaveGroups(Expression<Func<SlaveGroup, bool>> predicate) {
|
---|
[4649] | 324 | using (var db = CreateContext()) {
|
---|
| 325 | return db.Resources.OfType<SlaveGroup>().Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
|
---|
| 326 | }
|
---|
[4598] | 327 | }
|
---|
| 328 |
|
---|
| 329 | public Guid AddSlaveGroup(DT.SlaveGroup dto) {
|
---|
[4649] | 330 | using (var db = CreateContext()) {
|
---|
[5375] | 331 | if (dto.Id == Guid.Empty)
|
---|
| 332 | dto.Id = Guid.NewGuid();
|
---|
[4649] | 333 | var entity = Convert.ToEntity(dto);
|
---|
| 334 | db.Resources.InsertOnSubmit(entity);
|
---|
| 335 | db.SubmitChanges();
|
---|
| 336 | return entity.ResourceId;
|
---|
| 337 | }
|
---|
[4598] | 338 | }
|
---|
| 339 |
|
---|
| 340 | public void UpdateSlaveGroup(DT.SlaveGroup dto) {
|
---|
[4649] | 341 | using (var db = CreateContext()) {
|
---|
| 342 | var entity = db.Resources.OfType<SlaveGroup>().FirstOrDefault(x => x.ResourceId == dto.Id);
|
---|
| 343 | if (entity == null) db.Resources.InsertOnSubmit(Convert.ToEntity(dto));
|
---|
| 344 | else Convert.ToEntity(dto, entity);
|
---|
| 345 | db.SubmitChanges();
|
---|
| 346 | }
|
---|
[4598] | 347 | }
|
---|
| 348 |
|
---|
| 349 | public void DeleteSlaveGroup(Guid id) {
|
---|
[4649] | 350 | using (var db = CreateContext()) {
|
---|
| 351 | var entity = db.Resources.OfType<SlaveGroup>().FirstOrDefault(x => x.ResourceId == id);
|
---|
| 352 | if (entity != null) db.Resources.DeleteOnSubmit(entity);
|
---|
| 353 | db.SubmitChanges();
|
---|
| 354 | }
|
---|
[4598] | 355 | }
|
---|
| 356 | #endregion
|
---|
| 357 |
|
---|
| 358 | #region Resource Methods
|
---|
| 359 | public DT.Resource GetResource(Guid id) {
|
---|
[4649] | 360 | using (var db = CreateContext()) {
|
---|
| 361 | return Convert.ToDto(db.Resources.SingleOrDefault(x => x.ResourceId == id));
|
---|
| 362 | }
|
---|
[4598] | 363 | }
|
---|
| 364 |
|
---|
| 365 | public IEnumerable<DT.Resource> GetResources(Expression<Func<Resource, bool>> predicate) {
|
---|
[4649] | 366 | using (var db = CreateContext()) {
|
---|
| 367 | return db.Resources.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
|
---|
| 368 | }
|
---|
[4598] | 369 | }
|
---|
| 370 |
|
---|
| 371 | public Guid AddResource(DT.Resource dto) {
|
---|
[4649] | 372 | using (var db = CreateContext()) {
|
---|
| 373 | var entity = Convert.ToEntity(dto);
|
---|
| 374 | db.Resources.InsertOnSubmit(entity);
|
---|
| 375 | db.SubmitChanges();
|
---|
| 376 | return entity.ResourceId;
|
---|
| 377 | }
|
---|
[4598] | 378 | }
|
---|
| 379 |
|
---|
| 380 | public void UpdateResource(DT.Resource dto) {
|
---|
[4649] | 381 | using (var db = CreateContext()) {
|
---|
| 382 | var entity = db.Resources.FirstOrDefault(x => x.ResourceId == dto.Id);
|
---|
| 383 | if (entity == null) db.Resources.InsertOnSubmit(Convert.ToEntity(dto));
|
---|
| 384 | else Convert.ToEntity(dto, entity);
|
---|
| 385 | db.SubmitChanges();
|
---|
| 386 | }
|
---|
[4598] | 387 | }
|
---|
| 388 |
|
---|
| 389 | public void DeleteResource(Guid id) {
|
---|
[4649] | 390 | using (var db = CreateContext()) {
|
---|
| 391 | var entity = db.Resources.FirstOrDefault(x => x.ResourceId == id);
|
---|
| 392 | if (entity != null) db.Resources.DeleteOnSubmit(entity);
|
---|
| 393 | db.SubmitChanges();
|
---|
| 394 | }
|
---|
[4598] | 395 | }
|
---|
[5155] | 396 |
|
---|
| 397 | public void AssignJobToResource(Guid jobId, Guid resourceId) {
|
---|
| 398 | using (var db = CreateContext()) {
|
---|
| 399 | var job = db.Jobs.Where(x => x.JobId == jobId).Single();
|
---|
| 400 | job.AssignedResources.Add(new AssignedResource() { JobId = jobId, ResourceId = resourceId });
|
---|
| 401 | db.SubmitChanges();
|
---|
| 402 | }
|
---|
| 403 | }
|
---|
| 404 |
|
---|
| 405 | public IEnumerable<DT.Resource> GetAssignedResources(Guid jobId) {
|
---|
| 406 | using (var db = CreateContext()) {
|
---|
| 407 | var job = db.Jobs.Where(x => x.JobId == jobId).Single();
|
---|
| 408 | return job.AssignedResources.Select(x => Convert.ToDto(x.Resource)).ToArray();
|
---|
| 409 | }
|
---|
| 410 | }
|
---|
[4598] | 411 | #endregion
|
---|
| 412 |
|
---|
| 413 | #region Authorization Methods
|
---|
| 414 | public bool IsUserAuthorizedForJobs(Guid userId, params Guid[] jobIds) {
|
---|
[4649] | 415 | using (var db = CreateContext()) {
|
---|
| 416 | var userIds = from job in db.Jobs // this needs to be fast!
|
---|
| 417 | where jobIds.Contains(job.JobId)
|
---|
| 418 | select job.UserId;
|
---|
| 419 | return userIds.All(x => x == userId);
|
---|
| 420 | }
|
---|
[4598] | 421 | }
|
---|
| 422 | #endregion
|
---|
| 423 | }
|
---|
| 424 | }
|
---|