Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DeploymentServer Prototype/HeuristicLab.Services/HeuristicLab.Services.Deployment.DataAccess/PluginStore.cs @ 2742

Last change on this file since 2742 was 2742, checked in by gkronber, 14 years ago

Worked on prototype of plugin deployment service. #860 (Deployment server for plugin installation from web locations)

File size: 7.1 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Data.Common;
6using System.Transactions;
7using System.Data.SqlClient;
8
9namespace HeuristicLab.Services.Deployment.DataAccess {
10  public class PluginStore {
11
12    private PluginStoreClassesDataContext ctx;
13    public PluginStore() {
14      ctx = new PluginStoreClassesDataContext();
15    }
16
17    public IEnumerable<ProductDescription> Products {
18      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;
47    }
48
49    public IEnumerable<PluginDescription> Plugins {
50      get {
51        return from plugin in ctx.Plugins
52               select MakePluginDescription(plugin);
53      }
54    }
55
56    public byte[] PluginFile(PluginDescription pluginDescription) {
57      return (from file in ctx.PluginPackages
58              where file.PluginId == pluginDescription.Id
59              select file.Data.ToArray()).Single();
60    }
61
62    public void Persist(PluginDescription pluginDescription, byte[] pluginPackage) {
63      try {
64        using (var transaction = new TransactionScope()) {
65          InsertOrUpdatePlugin(pluginDescription);
66          InsertOrUpdatePluginPackage(pluginDescription, pluginPackage);
67          ctx.SubmitChanges();
68          transaction.Complete();
69        }
70      }
71      catch (SqlException ex) {
72        throw new ArgumentException("Something went wrong while trying to persist plugin", ex);
73      }
74    }
75
76    public void Persist(ProductDescription product) {
77      using (var transaction = new TransactionScope()) {
78        InsertOrUpdateProduct(product);
79        foreach (var plugin in product.Plugins) {
80          InsertOrUpdatePlugin(plugin);
81        }
82        ctx.SubmitChanges();
83        transaction.Complete();
84      }
85    }
86
87    private void InsertOrUpdatePluginPackage(PluginDescription pluginDescription, byte[] pluginPackage) {
88      var existing = from p in ctx.PluginPackages
89                     where p.PluginId == pluginDescription.Id
90                     select p;
91
92      if (existing.Count() == 0) {
93        ctx.PluginPackages.InsertOnSubmit(MakePluginPackage(pluginDescription, pluginPackage));
94      } else {
95        var exisingPackage = existing.Single();
96        exisingPackage.Data = pluginPackage;
97      }
98    }
99
100    private void InsertOrUpdateProduct(ProductDescription product) {
101      var existing = from p in ctx.Products
102                     where p.Id == product.Id
103                     select p;
104      if (existing.Count() == 0) {
105        var newEntity = MakeProductFromDescription(product);
106        ctx.Products.InsertOnSubmit(newEntity);
107        // submit and write back db generated key
108        ctx.SubmitChanges();
109        product.Id = newEntity.Id;
110      } else {
111        var existingProduct = existing.Single();
112        existingProduct.Name = product.Name;
113        existingProduct.Version = product.Version.ToString();
114      }
115      foreach (var plugin in product.Plugins) {
116        InsertOrUpdateProductPlugin(plugin, product);
117        InsertOrUpdatePlugin(plugin);
118      }
119    }
120
121    private void InsertOrUpdateProductPlugin(PluginDescription plugin, ProductDescription product) {
122      var existing = from pair in ctx.ProductPlugins
123                     where pair.PluginId == plugin.Id
124                     where pair.ProductId == product.Id
125                     select pair;
126      if (existing.Count() != 0) {
127        var newEntity = new ProductPlugin();
128        newEntity.PluginId = plugin.Id;
129        newEntity.ProductId = product.Id;
130        ctx.ProductPlugins.InsertOnSubmit(newEntity);
131      }
132    }
133
134    private void InsertOrUpdatePlugin(PluginDescription plugin) {
135      var existing = from p in ctx.Plugins
136                     where p.Name == plugin.Name
137                     where p.Version == plugin.Version.ToString()
138                     select p;
139      if (existing.Count() == 0) {
140        var newEntity = MakePluginFromDescription(plugin);
141        ctx.Plugins.InsertOnSubmit(newEntity);
142        // submit and write back db generated key
143        ctx.SubmitChanges();
144        plugin.Id = newEntity.Id;
145      } else {
146        var existingPlugin = existing.Single();
147        plugin.Id = existingPlugin.Id;
148      }
149      foreach (var dependency in plugin.Dependencies) {
150        InsertOrUpdateDependency(plugin, dependency);
151        InsertOrUpdatePlugin(dependency);
152      }
153    }
154
155    private void InsertOrUpdateDependency(PluginDescription plugin, PluginDescription dependency) {
156      var existing = from pair in ctx.Dependencies
157                     where pair.DependencyId == dependency.Id
158                     where pair.PluginId == plugin.Id
159                     select pair;
160      if (existing.Count() == 0) {
161        var newEntity = new Dependency();
162        newEntity.PluginId = plugin.Id;
163        newEntity.DependencyId = dependency.Id;
164        ctx.Dependencies.InsertOnSubmit(newEntity);
165      }
166    }
167
168    private Plugin MakePluginFromDescription(PluginDescription pluginDescription) {
169      var plugin = new Plugin();
170      plugin.Id = pluginDescription.Id;
171      plugin.Name = pluginDescription.Name;
172      plugin.Version = pluginDescription.Version.ToString();
173      return plugin;
174    }
175
176    private Product MakeProductFromDescription(ProductDescription desc) {
177      var product = new Product();
178      product.Id = desc.Id;
179      product.Name = desc.Name;
180      product.Version = desc.Version.ToString();
181      return product;
182    }
183
184    private PluginPackage MakePluginPackage(PluginDescription pluginDescription, byte[] pluginPackage) {
185      var package = new PluginPackage();
186      package.Data = pluginPackage;
187      package.PluginId = pluginDescription.Id;
188      package.FileName = string.Empty;
189      return package;
190    }
191  }
192}
Note: See TracBrowser for help on using the repository browser.