- Timestamp:
- 09/13/10 01:21:32 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Services.OKB/3.3/TableService.cs
r4313 r4381 31 31 using HeuristicLab.Services.OKB.AttributeSelection; 32 32 using HeuristicLab.Services.OKB.DataAccess; 33 using log4net;34 33 35 34 namespace HeuristicLab.Services.OKB { … … 38 37 /// Implementation of the <see cref="ITableService"/>. 39 38 /// </summary> 40 [ServiceBehavior( 41 InstanceContextMode = InstanceContextMode.PerSession, 42 IncludeExceptionDetailInFaults = true)] 39 [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, IncludeExceptionDetailInFaults = true)] 43 40 public class TableService : ITableService, IDisposable { 44 45 private static ILog logger = LogManager.GetLogger(typeof(TableService));46 47 private OKBDataContext GetDataContext() {48 return new OKBDataContext();49 }50 51 41 private static List<Type> SupportedTypes = new List<Type>() { 52 42 typeof(int), 43 typeof(long), 53 44 typeof(double), 54 45 typeof(string), … … 57 48 }; 58 49 50 private DataTable tableTemplate; 51 private IEnumerable<PropertyInfo> properties; 52 private IEnumerator rowEnumerator; 53 59 54 #region ITableService Members 60 61 55 /// <summary> 62 56 /// Updates the data table. … … 65 59 /// <param name="tableName">Name of the table.</param> 66 60 public void UpdateDataTable(DataTable updatedRows, string tableName) { 67 logger.Info("updating table: " + tableName);68 61 Type tableType = Assembly.GetAssembly(typeof(Run)).GetType("HeuristicLab.Services.OKB.DataAccess." + tableName, true); 69 62 var properties = from p in tableType.GetProperties() 70 63 where SupportedTypes.Contains(p.PropertyType) 71 64 select p; 72 OKBDataContext okb = GetDataContext(); 73 ITable table = okb.GetTable(tableType); 74 foreach (DataRow row in updatedRows.Rows) { 75 if (row["Id"] == DBNull.Value) 76 table.InsertOnSubmit(CreateEntity(tableType, properties, row)); 77 } 78 var updated = updatedRows.Rows.Cast<DataRow>().Where(row => row["Id"] != DBNull.Value).OrderBy(row => row["Id"]); 79 var entities = DataSetBuilder.GetEntities(okb, tableType, updated.Select(u => (int)u["Id"])); 80 var updateEnum = updated.GetEnumerator(); 81 var entityEnum = entities.GetEnumerator(); 82 while (updateEnum.MoveNext() && entityEnum.MoveNext()) { 83 bool idChecked = false; 84 foreach (var property in properties) { 85 Console.WriteLine("{0}.{1} = '{2}' -> '{3}'", 86 tableName, property.Name, 87 property.GetGetMethod().Invoke(entityEnum.Current, new object[0]), 88 updateEnum.Current[property.Name]); 89 if (property.Name == "Id") { 90 int entityId = (int)property.GetGetMethod().Invoke(entityEnum.Current, new object[0]); 91 int updateId = (int)updateEnum.Current["Id"]; 92 Debug.Assert(entityId == updateId); 93 idChecked = true; 65 using (OKBDataContext okb = new OKBDataContext()) { 66 ITable table = okb.GetTable(tableType); 67 foreach (DataRow row in updatedRows.Rows) { 68 if (row["Id"] == DBNull.Value) 69 table.InsertOnSubmit(CreateEntity(tableType, properties, row)); 70 } 71 var updated = updatedRows.Rows.Cast<DataRow>().Where(row => row["Id"] != DBNull.Value).OrderBy(row => row["Id"]); 72 var entities = DataSetBuilder.GetEntities(okb, tableType, updated.Select(u => (int)u["Id"])); 73 var updateEnum = updated.GetEnumerator(); 74 var entityEnum = entities.GetEnumerator(); 75 while (updateEnum.MoveNext() && entityEnum.MoveNext()) { 76 bool idChecked = false; 77 foreach (var property in properties) { 78 Console.WriteLine("{0}.{1} = '{2}' -> '{3}'", 79 tableName, property.Name, 80 property.GetGetMethod().Invoke(entityEnum.Current, new object[0]), 81 updateEnum.Current[property.Name]); 82 if (property.Name == "Id") { 83 int entityId = (int)property.GetGetMethod().Invoke(entityEnum.Current, new object[0]); 84 int updateId = (int)updateEnum.Current["Id"]; 85 Debug.Assert(entityId == updateId); 86 idChecked = true; 87 } 88 object newValue = updateEnum.Current[property.Name]; 89 if (newValue != DBNull.Value) 90 property.GetSetMethod().Invoke(entityEnum.Current, new object[] { newValue }); 94 91 } 95 object newValue = updateEnum.Current[property.Name]; 96 if (newValue != DBNull.Value) 97 property.GetSetMethod().Invoke(entityEnum.Current, new object[] { newValue }); 98 } 99 Debug.Assert(idChecked); 100 } 101 okb.SubmitChanges(); 92 Debug.Assert(idChecked); 93 } 94 okb.SubmitChanges(); 95 } 102 96 } 103 97 104 98 private object CreateEntity(Type tableType, IEnumerable<PropertyInfo> properties, DataRow row) { 105 99 object instance = Activator.CreateInstance(tableType); 106 logger.Info("Creating new instance of " + tableType.Name);107 100 bool empty = true; 108 101 foreach (var property in properties) { … … 125 118 /// <param name="tableName">Name of the table.</param> 126 119 public void DeleteTableRows(int[] ids, string tableName) { 127 logger.Info("delete rows from table: " + tableName);128 120 Type tableType = Assembly.GetAssembly(typeof(Run)).GetType("HeuristicLab.Services.OKB.DataAccess" + tableName, true); 129 OKBDataContext okb = GetDataContext(); 130 ITable table = okb.GetTable(tableType); 131 table.DeleteAllOnSubmit(DataSetBuilder.GetEntities(okb, tableType, ids)); 132 okb.SubmitChanges(); 133 } 134 135 private DataTable tableTemplate; 136 private IEnumerable<PropertyInfo> properties; 137 private IEnumerator rowEnumerator; 121 using (OKBDataContext okb = new OKBDataContext()) { 122 ITable table = okb.GetTable(tableType); 123 table.DeleteAllOnSubmit(DataSetBuilder.GetEntities(okb, tableType, ids)); 124 okb.SubmitChanges(); 125 } 126 } 138 127 139 128 /// <summary> … … 147 136 /// </returns> 148 137 public DataTable PrepareDataTable(string tableName, out int count) { 149 logger.Info("preparing data table: " + tableName);150 138 Type tableType = Assembly.GetAssembly(typeof(Run)).GetType("HeuristicLab.Services.OKB.DataAccess." + tableName, true); 151 139 properties = from p in tableType.GetProperties() … … 163 151 tableTemplate.Columns.Add(column); 164 152 } 165 OKBDataContext okb = GetDataContext(); 166 ITable table = okb.GetTable(tableType); 167 rowEnumerator = table.GetEnumerator(); 168 count = DataSetBuilder.CountEntities(GetDataContext(), tableType); 169 return tableTemplate; 153 using (OKBDataContext okb = new OKBDataContext()) { 154 ITable table = okb.GetTable(tableType); 155 rowEnumerator = table.GetEnumerator(); 156 count = DataSetBuilder.CountEntities(new OKBDataContext(), tableType); 157 return tableTemplate; 158 } 170 159 } 171 160 … … 198 187 Dispose(); 199 188 } 200 201 189 #endregion 202 190 203 191 #region IDisposable Members 204 205 192 /// <summary> 206 193 /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. … … 210 197 properties = null; 211 198 } 212 213 199 #endregion 214 200 }
Note: See TracChangeset
for help on using the changeset viewer.