Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Server.ADODataAccess/JobResultsAdapter.cs @ 1497

Last change on this file since 1497 was 1468, checked in by svonolfe, 16 years ago

Added transaction management (#527)

File size: 5.2 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Hive.Server.DataAccess;
6using HeuristicLab.Hive.Contracts.BusinessObjects;
7using HeuristicLab.DataAccess.ADOHelper;
8using HeuristicLab.Hive.Server.ADODataAccess.dsHiveServerTableAdapters;
9using System.Data.Common;
10using System.Data.SqlClient;
11
12namespace HeuristicLab.Hive.Server.ADODataAccess {
13  class JobResultsAdapterWrapper :
14    DataAdapterWrapperBase<dsHiveServerTableAdapters.JobResultTableAdapter,
15                    JobResult,
16                    dsHiveServer.JobResultRow> {   
17    public override void UpdateRow(dsHiveServer.JobResultRow row) {
18      TransactionalAdapter.Update(row);
19    }
20
21    public override dsHiveServer.JobResultRow InsertNewRow(JobResult obj) {
22      dsHiveServer.JobResultDataTable data =
23        new dsHiveServer.JobResultDataTable();
24
25      dsHiveServer.JobResultRow row = data.NewJobResultRow();
26      row.JobResultId = obj.Id;
27      data.AddJobResultRow(row);
28
29      return row;
30    }
31
32    public override IEnumerable<dsHiveServer.JobResultRow> FindById(Guid id) {
33      return TransactionalAdapter.GetDataById(id);
34    }
35
36    public override IEnumerable<dsHiveServer.JobResultRow> FindAll() {
37      return TransactionalAdapter.GetData();
38    }
39
40    protected override void SetConnection(DbConnection connection) {
41      adapter.Connection = connection as SqlConnection;
42    }
43
44    protected override void SetTransaction(DbTransaction transaction) {
45      adapter.Transaction = transaction as SqlTransaction;
46    }
47  }
48
49  class JobResultsAdapter:
50    DataAdapterBase<dsHiveServerTableAdapters.JobResultTableAdapter,
51                    JobResult,
52                    dsHiveServer.JobResultRow>,
53    IJobResultsAdapter {
54    #region Fields
55    private IClientAdapter clientAdapter = null;
56
57    private IClientAdapter ClientAdapter {
58      get {
59        if (clientAdapter == null)
60          clientAdapter =
61            this.Session.GetDataAdapter<ClientInfo, IClientAdapter>();
62
63        return clientAdapter;
64      }
65    }
66
67    private IJobAdapter jobAdapter = null;
68
69    private IJobAdapter JobAdapter {
70      get {
71        if (jobAdapter == null)
72          jobAdapter =
73            this.Session.GetDataAdapter<Job, IJobAdapter>();
74
75        return jobAdapter;
76      }
77    }
78    #endregion
79
80    public JobResultsAdapter(): base(new JobResultsAdapterWrapper()) {
81    }
82
83    #region Overrides
84    protected override dsHiveServer.JobResultRow ConvertObj(JobResult result,
85      dsHiveServer.JobResultRow row) {
86      if (row != null && result != null) {
87        if (result.Job != null)
88          row.JobId = result.Job.Id;
89        else
90          row.SetJobIdNull();
91
92        if (result.Result != null)
93          row.JobResult = result.Result;
94        else
95          row.SetJobResultNull();
96
97        if (result.Client != null)  {
98          ClientInfo client =
99                 ClientAdapter.GetById(result.Client.Id);
100
101          if (client != null)
102            row.ResourceId = client.Id;
103          else
104            row.SetResourceIdNull();
105        }           
106        else
107          row.SetResourceIdNull();
108
109        if (result.Exception != null)
110          row.Message = result.Exception.ToString();
111        else
112          row.SetMessageNull();
113
114        row.Percentage = result.Percentage;
115
116        if (result.DateFinished != DateTime.MinValue)
117          row.DateFinished = result.DateFinished;
118        else
119          row.SetDateFinishedNull();
120
121        return row;
122      } else
123        return null;
124    }
125
126    protected override JobResult ConvertRow(dsHiveServer.JobResultRow row,
127      JobResult result) {
128      if (row != null && result != null) {
129        row.JobResultId = result.Id;
130        result.Id = row.JobResultId;
131
132        if (!row.IsJobIdNull())
133          result.Job = JobAdapter.GetById(row.JobId);
134        else
135          result.Job = null;
136
137        if (!row.IsJobResultNull())
138          result.Result = row.JobResult;
139        else
140          result.Result = null;
141
142        if (!row.IsResourceIdNull())
143          result.Client = ClientAdapter.GetById(row.ResourceId);
144        else
145          result.Client = null;
146
147        if (!row.IsMessageNull())
148          result.Exception = new Exception(row.Message);
149        else
150          result.Exception = null;
151
152        result.Percentage = row.Percentage;
153
154        if (!row.IsDateFinishedNull())
155          result.DateFinished = row.DateFinished;
156        else
157          result.DateFinished = DateTime.MinValue;
158
159        return result;
160      } else
161        return null;
162    }
163    #endregion
164
165    #region IJobResultsAdapter Members
166    protected override void doUpdate(JobResult result) {
167      if (result != null) {
168        ClientAdapter.Update(result.Client);
169        JobAdapter.Update(result.Job);
170
171        base.doUpdate(result);
172      }
173    }
174
175    public ICollection<JobResult> GetResultsOf(Job job) {
176      if (job != null) {
177        return
178          base.FindMultiple(
179            delegate() {
180              return Adapter.GetDataByJob(job.Id);
181            });
182      }
183
184      return null;
185    }
186    #endregion
187  }
188}
Note: See TracBrowser for help on using the repository browser.