Changeset 2771 for branches/DeploymentServer Prototype/HeuristicLab.Services/HeuristicLab.Services.Deployment.DataAccess
- Timestamp:
- 02/09/10 17:24:13 (14 years ago)
- Location:
- branches/DeploymentServer Prototype/HeuristicLab.Services/HeuristicLab.Services.Deployment.DataAccess
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DeploymentServer Prototype/HeuristicLab.Services/HeuristicLab.Services.Deployment.DataAccess/PluginDescription.cs
r2742 r2771 34 34 35 35 [DataMember(Name = "Dependencies")] 36 private IEnumerable<PluginDescription> dependencies;37 public IEnumerable<PluginDescription> Dependencies {36 private List<PluginDescription> dependencies; 37 public List<PluginDescription> Dependencies { 38 38 get { return dependencies; } 39 39 } … … 51 51 this.name = name; 52 52 this.version = version; 53 this.dependencies = new List<PluginDescription>(dependencies) .AsReadOnly();53 this.dependencies = new List<PluginDescription>(dependencies); //.AsReadOnly(); 54 54 //this.isDatabaseInstance = true; 55 55 } -
branches/DeploymentServer Prototype/HeuristicLab.Services/HeuristicLab.Services.Deployment.DataAccess/PluginStore.cs
r2766 r2771 10 10 public class PluginStore { 11 11 12 private PluginStoreClassesDataContext ctx;13 12 public PluginStore() { 14 ctx = new PluginStoreClassesDataContext();15 } 16 13 } 14 15 #region context creating members 17 16 public IEnumerable<ProductDescription> Products { 18 17 get { 19 return from p in ctx.Products 20 let plugins = from pair in ctx.ProductPlugins 21 from plugin in ctx.Plugins 22 where pair.ProductId == p.Id 23 where plugin.Id == pair.PluginId 24 select plugin 25 select MakeProductDescription(p, plugins); 26 } 27 } 28 29 private ProductDescription MakeProductDescription(Product p, IQueryable<Plugin> plugins) { 30 var desc = new ProductDescription(p.Id, p.Name, new Version(p.Version), from plugin in plugins 31 select MakePluginDescription(plugin)); 32 return desc; 33 } 34 35 private PluginDescription MakePluginDescription(Plugin plugin) { 36 var desc = new PluginDescription(plugin.Id, plugin.Name, new Version(plugin.Version), from dep in GetDependencies(plugin) 37 select MakePluginDescription(dep)); 38 return desc; 39 } 40 41 private IEnumerable<Plugin> GetDependencies(Plugin plugin) { 42 return from pair in ctx.Dependencies 43 from dependency in ctx.Plugins 44 where pair.PluginId == plugin.Id 45 where pair.DependencyId == dependency.Id 46 select dependency; 18 using (var ctx = new PluginStoreClassesDataContext()) { 19 return (from p in ctx.Products 20 let plugins = from pair in ctx.ProductPlugins 21 from plugin in ctx.Plugins 22 where pair.ProductId == p.Id 23 where plugin.Id == pair.PluginId 24 select plugin 25 select MakeProductDescription(ctx, p, plugins)).ToList(); 26 } 27 } 47 28 } 48 29 49 30 public IEnumerable<PluginDescription> Plugins { 50 31 get { 51 return from plugin in ctx.Plugins 52 select MakePluginDescription(plugin); 32 using (var ctx = new PluginStoreClassesDataContext()) { 33 return (from plugin in ctx.Plugins 34 select MakePluginDescription(ctx, plugin)).ToList(); 35 } 53 36 } 54 37 } 55 38 56 39 public byte[] PluginFile(PluginDescription pluginDescription) { 57 return GetExistingPlugin(pluginDescription.Name, pluginDescription.Version).PluginPackage.Data.ToArray(); 40 using (var ctx = new PluginStoreClassesDataContext()) { 41 return GetExistingPlugin(ctx, pluginDescription.Name, pluginDescription.Version).PluginPackage.Data.ToArray(); 42 } 58 43 } 59 44 60 45 public void Persist(PluginDescription pluginDescription, byte[] pluginPackage) { 61 try { 62 using (var transaction = new TransactionScope()) { 63 Plugin pluginEntity = InsertOrUpdatePlugin(pluginDescription); 64 if (pluginEntity.PluginPackage == null) { 65 // insert 66 pluginEntity.PluginPackage = MakePluginPackage(pluginDescription, pluginPackage); 67 } else { 68 // update 69 pluginEntity.PluginPackage.Data = pluginPackage; 46 using (var ctx = new PluginStoreClassesDataContext()) { 47 try { 48 using (var transaction = new TransactionScope()) { 49 Plugin pluginEntity = InsertOrUpdatePlugin(ctx, pluginDescription); 50 if (pluginEntity.PluginPackage == null) { 51 // insert 52 pluginEntity.PluginPackage = MakePluginPackage(pluginDescription, pluginPackage); 53 } else { 54 // update 55 pluginEntity.PluginPackage.Data = pluginPackage; 56 } 57 ctx.SubmitChanges(); 58 transaction.Complete(); 70 59 } 71 ctx.SubmitChanges(); 72 transaction.Complete(); 73 } 74 } 75 catch (SqlException ex) { 76 throw new ArgumentException("Something went wrong while trying to persist plugin", ex); 77 } 78 catch (InvalidOperationException ex) { 79 throw new ArgumentException("Something went wrong while trying to persist plugin", ex); 60 } 61 catch (SqlException ex) { 62 throw new ArgumentException("Something went wrong while trying to persist plugin", ex); 63 } 64 catch (InvalidOperationException ex) { 65 throw new ArgumentException("Something went wrong while trying to persist plugin", ex); 66 } 80 67 } 81 68 } 82 69 83 70 public void Persist(ProductDescription product) { 84 try { 85 using (var transaction = new TransactionScope()) { 86 InsertOrUpdateProduct(product); 87 foreach (var plugin in product.Plugins) { 88 InsertOrUpdatePlugin(plugin); 71 using (var ctx = new PluginStoreClassesDataContext()) { 72 try { 73 using (var transaction = new TransactionScope()) { 74 foreach (var plugin in product.Plugins) { 75 var pluginEntity = GetExistingPlugin(ctx, plugin.Name, plugin.Version); 76 UpdatePlugin(ctx, pluginEntity, plugin); 77 } 78 InsertOrUpdateProduct(ctx, product); 79 ctx.SubmitChanges(); 80 transaction.Complete(); 89 81 } 90 ctx.SubmitChanges(); 91 transaction.Complete(); 92 } 93 } 94 catch (SqlException ex) { 95 throw new ArgumentException("Something went wrong while trying to persist product", ex); 96 } 97 catch (InvalidOperationException ex) { 98 throw new ArgumentException("Something went wrong while trying to persist product", ex); 99 } 100 } 101 102 private void InsertOrUpdateProduct(ProductDescription product) { 82 } 83 catch (SqlException ex) { 84 throw new ArgumentException("Something went wrong while trying to persist product", ex); 85 } 86 catch (InvalidOperationException ex) { 87 throw new ArgumentException("Something went wrong while trying to persist product", ex); 88 } 89 } 90 } 91 92 #endregion 93 94 #region insert/update product 95 private void InsertOrUpdateProduct(PluginStoreClassesDataContext ctx, ProductDescription product) { 103 96 var productEntity = (from p in ctx.Products 104 97 where p.Name == product.Name … … 113 106 product.Id = productEntity.Id; 114 107 115 DeleteOldPlugins( productEntity);108 DeleteOldPlugins(ctx, productEntity); 116 109 117 110 foreach (var plugin in product.Plugins) { 118 var existingPlugin = GetExistingPlugin( plugin.Name, plugin.Version);111 var existingPlugin = GetExistingPlugin(ctx, plugin.Name, plugin.Version); 119 112 ProductPlugin prodPlugin = new ProductPlugin(); 120 113 prodPlugin.PluginId = existingPlugin.Id; … … 124 117 } 125 118 126 private void DeleteOldPlugins(P roduct productEntity) {119 private void DeleteOldPlugins(PluginStoreClassesDataContext ctx, Product productEntity) { 127 120 var oldPlugins = (from p in ctx.ProductPlugins 128 121 where p.ProductId == productEntity.Id … … 131 124 ctx.SubmitChanges(); 132 125 } 133 134 private Plugin InsertOrUpdatePlugin(PluginDescription pluginDescription) { 126 #endregion 127 128 #region insert/update plugins 129 private Plugin InsertOrUpdatePlugin(PluginStoreClassesDataContext ctx, PluginDescription pluginDescription) { 135 130 var pluginEntity = (from p in ctx.Plugins 136 131 where p.Name == pluginDescription.Name … … 142 137 ctx.SubmitChanges(); 143 138 } 139 140 UpdatePlugin(ctx, pluginEntity, pluginDescription); 141 return pluginEntity; 142 } 143 144 private void UpdatePlugin(PluginStoreClassesDataContext ctx, Plugin pluginEntity, PluginDescription pluginDescription) { 145 // delete cached entry 146 if (pluginDescriptions.ContainsKey(pluginEntity)) pluginDescriptions.Remove(pluginEntity); 147 144 148 pluginDescription.Id = pluginEntity.Id; 145 149 146 DeleteOldDependencies( pluginEntity);150 DeleteOldDependencies(ctx, pluginEntity); 147 151 148 152 foreach (var dependency in pluginDescription.Dependencies) { 149 var dependencyEntity = GetExistingPlugin( dependency.Name, dependency.Version);153 var dependencyEntity = GetExistingPlugin(ctx, dependency.Name, dependency.Version); 150 154 Dependency d = new Dependency(); 151 155 d.PluginId = pluginDescription.Id; … … 153 157 ctx.Dependencies.InsertOnSubmit(d); 154 158 } 155 return pluginEntity;156 157 } 158 159 private void DeleteOldDependencies(Plugin pluginEntity) {159 } 160 161 162 163 private void DeleteOldDependencies(PluginStoreClassesDataContext ctx, Plugin pluginEntity) { 160 164 var oldDependencies = (from dep in ctx.Dependencies 161 165 where dep.PluginId == pluginEntity.Id … … 165 169 ctx.SubmitChanges(); 166 170 } 167 168 private Plugin GetExistingPlugin(string name, Version version) { 169 return (from p in ctx.Plugins 170 where p.Name == name 171 where p.Version == version.ToString() 172 select p).Single(); 171 #endregion 172 173 #region product <-> productDescription transformation 174 private ProductDescription MakeProductDescription(PluginStoreClassesDataContext ctx, Product p, IQueryable<Plugin> plugins) { 175 var desc = new ProductDescription(p.Id, p.Name, new Version(p.Version), from plugin in plugins 176 select MakePluginDescription(ctx, plugin)); 177 return desc; 178 } 179 private Product MakeProductFromDescription(ProductDescription desc) { 180 var product = new Product(); 181 product.Id = desc.Id; 182 product.Name = desc.Name; 183 product.Version = desc.Version.ToString(); 184 return product; 185 } 186 #endregion 187 188 #region plugin <-> pluginDescription transformation 189 // cache for plugin descriptions 190 private Dictionary<Plugin, PluginDescription> pluginDescriptions = new Dictionary<Plugin, PluginDescription>(); 191 private PluginDescription MakePluginDescription(PluginStoreClassesDataContext ctx, Plugin plugin) { 192 if (!pluginDescriptions.ContainsKey(plugin)) { 193 // no cached description -> create new 194 var desc = new PluginDescription(plugin.Id, plugin.Name, new Version(plugin.Version), from dep in GetDependencies(ctx, plugin) 195 select MakePluginDescription(ctx, dep)); 196 pluginDescriptions[plugin] = desc; 197 } 198 return pluginDescriptions[plugin]; 173 199 } 174 200 … … 181 207 } 182 208 183 private Product MakeProductFromDescription(ProductDescription desc) {184 var product = new Product();185 product.Id = desc.Id;186 product.Name = desc.Name;187 product.Version = desc.Version.ToString();188 return product;189 }190 191 209 private PluginPackage MakePluginPackage(PluginDescription pluginDescription, byte[] pluginPackage) { 192 210 var package = new PluginPackage(); … … 196 214 return package; 197 215 } 216 217 #endregion 218 219 #region helper queries 220 private Plugin GetExistingPlugin(PluginStoreClassesDataContext ctx, string name, Version version) { 221 return (from p in ctx.Plugins 222 where p.Name == name 223 where p.Version == version.ToString() 224 select p).Single(); 225 } 226 227 private IEnumerable<Plugin> GetDependencies(PluginStoreClassesDataContext ctx, Plugin plugin) { 228 return from pair in ctx.Dependencies 229 from dependency in ctx.Plugins 230 where pair.PluginId == plugin.Id 231 where pair.DependencyId == dependency.Id 232 select dependency; 233 } 234 #endregion 198 235 } 199 236 }
Note: See TracChangeset
for help on using the changeset viewer.