source: trunk/sources/HeuristicLab.Hive.Server.ADODataAccess/JobAdapter.cs @ 971

Last change on this file since 971 was 971, checked in by svonolfe, 12 years ago

Added Job Adapter (#372)

File size: 6.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Hive.Server.Core.InternalInterfaces.DataAccess;
27using HeuristicLab.Hive.Contracts.BusinessObjects;
28using System.Linq.Expressions;
29using System.Runtime.CompilerServices;
30
31namespace HeuristicLab.Hive.Server.ADODataAccess {
32  class JobAdapter : DataAdapterBase, IJobAdapter {
33    private dsHiveServerTableAdapters.JobTableAdapter adapter =
34        new dsHiveServerTableAdapters.JobTableAdapter();
35
36    private dsHiveServer.JobDataTable data =
37      new dsHiveServer.JobDataTable();
38
39    private IClientAdapter clientAdapter = null;
40
41    private IClientAdapter ClientAdapter {
42      get {
43        if (clientAdapter == null)
44          clientAdapter = ServiceLocator.GetClientAdapter();
45
46        return clientAdapter;
47      }
48    }
49
50    private IUserAdapter userAdapter = null;
51
52    private IUserAdapter UserAdapter {
53      get {
54        if (userAdapter == null) {
55          userAdapter = ServiceLocator.GetUserAdapter();
56        }
57
58        return userAdapter;
59      }
60    }
61
62    public JobAdapter() {
63      adapter.Fill(data);
64    }
65
66    protected override void Update() {
67      this.adapter.Update(this.data);
68    }
69
70    private Job Convert(dsHiveServer.JobRow row,
71      Job job) {
72      if (row != null && job != null) {
73        job.JobId = row.JobId;
74
75        if (!row.IsParentJobIdNull())
76          job.parentJob = GetJobById(row.ParentJobId);
77        else
78          job.parentJob = null;
79
80        if (!row.IsResourceIdNull())
81          job.Client = ClientAdapter.GetClientById(row.ResourceId);
82        else
83          job.Client = null;
84       
85        if (!row.IsStatusNull())
86          job.State = (State)Enum.Parse(job.State.GetType(), row.Status);
87        else
88          job.State = State.idle;
89
90        return job;
91      } else
92        return null;
93    }
94
95    private dsHiveServer.JobRow Convert(Job job,
96      dsHiveServer.JobRow row) {
97      if (job != null && row != null) {
98        if (job.Client != null) {
99          ClientAdapter.UpdateClient(job.Client);
100          row.ResourceId = job.Client.ResourceId;
101        } else
102          row.SetResourceIdNull();
103
104        if (job.parentJob != null) {
105          UpdateJob(job.parentJob);
106          row.ParentJobId = job.parentJob.JobId;
107        } else
108          row.SetParentJobIdNull();
109
110        row.Status = job.State.ToString();
111      }
112
113      return row;
114    }
115
116    #region IJobAdapter Members
117    [MethodImpl(MethodImplOptions.Synchronized)]
118    public void UpdateJob(Job job) {
119      if (job != null) {
120        dsHiveServer.JobRow row =
121          data.FindByJobId(job.JobId);
122
123        if (row == null) {
124          row = data.NewJobRow();
125          data.AddJobRow(row);
126
127          //write row to db to get primary key
128          adapter.Update(row);
129        }
130
131        Convert(job, row);
132        job.JobId = row.JobId;
133      }
134    }
135
136    public Job GetJobById(long id) {
137      dsHiveServer.JobRow row =
138        data.FindByJobId(id);
139
140      if (row != null) {
141        Job job = new Job();
142       
143        Convert(row, job);
144
145        return job;
146      }
147
148      return null;
149    }
150
151    public ICollection<Job> GetAllJobs() {
152      IList<Job> allJobs =
153        new List<Job>();
154
155      foreach (dsHiveServer.JobRow row in data) {
156        Job job = new Job();
157        Convert(row, job);
158        allJobs.Add(job);
159      }
160
161      return allJobs;
162    }
163
164    public ICollection<Job> GetAllSubjobs(Job job) {
165      IList<Job> allJobs =
166        new List<Job>();
167
168      if (job != null) {
169        IEnumerable<dsHiveServer.JobRow> clientJobs =
170         from j in
171           data.AsEnumerable<dsHiveServer.JobRow>()
172         where j.ParentJobId == job.JobId
173         select j;
174
175        foreach (dsHiveServer.JobRow row in
176          clientJobs) {
177          Job j = new Job();
178          Convert(row, j);
179          allJobs.Add(j);
180        }
181      }
182
183      return allJobs;
184    }
185
186    public JobResult GetResult(Job job) {
187      throw new NotImplementedException();
188    }
189
190    public ICollection<Job> GetJobsOf(ClientInfo client) {
191      IList<Job> allJobs =
192        new List<Job>();
193
194      if (client != null) {
195        IEnumerable<dsHiveServer.JobRow> clientJobs =
196         from job in
197           data.AsEnumerable<dsHiveServer.JobRow>()
198         where job.ResourceId == client.ResourceId
199         select job;
200
201        foreach (dsHiveServer.JobRow row in
202          clientJobs) {
203          Job job = new Job();
204          Convert(row, job);
205          allJobs.Add(job);
206        }
207      }
208
209      return allJobs;
210    }
211
212    public ICollection<Job> GetJobsOf(User user) {
213      IList<Job> allJobs =
214        new List<Job>();
215
216      if (user != null) {
217        IEnumerable<dsHiveServer.JobRow> userJobs =
218        from job in
219          data.AsEnumerable<dsHiveServer.JobRow>()
220        where job.PermissionOwnerId == user.PermissionOwnerId
221        select job;
222
223        foreach (dsHiveServer.JobRow row in
224          userJobs) {
225          Job job = new Job();
226          Convert(row, job);
227          allJobs.Add(job);
228        }
229      }
230
231      return allJobs;
232    }
233
234    [MethodImpl(MethodImplOptions.Synchronized)]
235    public bool DeleteJob(Job job) {
236      if (job != null) {
237        dsHiveServer.JobRow row =
238          data.FindByJobId(job.JobId);
239
240        if (row != null) {
241          row.Delete();
242          adapter.Update(row);
243
244          return true;
245        }
246      }
247
248      return false;
249    }
250
251    #endregion
252  }
253}
Note: See TracBrowser for help on using the repository browser.