[9602] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Data.Entity;
|
---|
[9645] | 4 | using System.Data.Entity.Infrastructure;
|
---|
[9577] | 5 | using System.Data.Entity.ModelConfiguration;
|
---|
| 6 | using System.Data.Entity.ModelConfiguration.Conventions;
|
---|
[9645] | 7 | using HeuristicLab.Services.Optimization.Billing.Interfaces;
|
---|
[9577] | 8 | using HeuristicLab.Services.Optimization.Billing.Model;
|
---|
| 9 |
|
---|
| 10 | namespace HeuristicLab.Services.Optimization.Billing.DataAccess {
|
---|
| 11 | public class BillingContext : DbContext {
|
---|
| 12 | public DbSet<Product> Products { get; set; }
|
---|
| 13 | public DbSet<User> Users { get; set; }
|
---|
| 14 | public DbSet<Order> Orders { get; set; }
|
---|
| 15 | public DbSet<OrderLine> OrderLines { get; set; }
|
---|
| 16 | public DbSet<Invoice> Invoices { get; set; }
|
---|
| 17 | public DbSet<InvoiceLine> InvoiceLines { get; set; }
|
---|
[9579] | 18 | public DbSet<UsageRecord> UsageRecords { get; set; }
|
---|
[9602] | 19 | public DbSet<ContactInformation> ContactInformations { get; set; }
|
---|
| 20 | public DbSet<PaymentInformation> PaymentInformations { get; set; }
|
---|
[9577] | 21 |
|
---|
| 22 | // enum PaymentMethod
|
---|
| 23 | // enum OrderState
|
---|
| 24 |
|
---|
| 25 | public BillingContext()
|
---|
[9602] | 26 | : this("name=BillingContext") {
|
---|
[9577] | 27 |
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 | public BillingContext(string connectionString)
|
---|
| 31 | : base(connectionString) {
|
---|
[9645] | 32 |
|
---|
| 33 | Database.SetInitializer(new HeuristicLab.Services.Optimization.Billing.DataAccess.BillingContext.BillingContextInitiliazer());
|
---|
| 34 |
|
---|
| 35 | // Hook up event to mark existing entities as Unchanged
|
---|
| 36 | ((IObjectContextAdapter)this).ObjectContext.ObjectMaterialized += (sender, args) => {
|
---|
| 37 | var entity = args.Entity as IObjectWithState;
|
---|
| 38 | if (entity != null) {
|
---|
| 39 | entity.EntityState = State.Unchanged;
|
---|
| 40 | }
|
---|
| 41 | };
|
---|
| 42 |
|
---|
[9619] | 43 | this.Configuration.LazyLoadingEnabled = false;
|
---|
| 44 | this.Configuration.ProxyCreationEnabled = false;
|
---|
[9577] | 45 | }
|
---|
| 46 |
|
---|
| 47 | protected override void OnModelCreating(DbModelBuilder modelBuilder) {
|
---|
[9645] | 48 | modelBuilder.Configurations.Add(new ContactInformationConfiguration());
|
---|
| 49 | modelBuilder.Configurations.Add(new PaymentInformationConfiguration());
|
---|
| 50 | modelBuilder.Configurations.Add(new ProductConfiguration());
|
---|
[9577] | 51 | modelBuilder.Configurations.Add(new OrderConfiguration());
|
---|
| 52 | modelBuilder.Configurations.Add(new OrderLineConfiguration());
|
---|
| 53 | modelBuilder.Configurations.Add(new InvoiceConfiguration());
|
---|
| 54 | modelBuilder.Configurations.Add(new InvoiceLineConfiguration());
|
---|
| 55 | modelBuilder.Configurations.Add(new UsageRecordConfiguration());
|
---|
[9602] | 56 | modelBuilder.Configurations.Add(new UserConfiguration());
|
---|
[9577] | 57 |
|
---|
| 58 | modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
|
---|
| 59 |
|
---|
| 60 | base.OnModelCreating(modelBuilder);
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | #region EntityTypeConfigurations
|
---|
| 64 |
|
---|
[9645] | 65 | private class ContactInformationConfiguration : EntityTypeConfiguration<ContactInformation> {
|
---|
| 66 | internal ContactInformationConfiguration() {
|
---|
| 67 | this.Ignore(ci => ci.EntityState);
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | private class PaymentInformationConfiguration : EntityTypeConfiguration<PaymentInformation> {
|
---|
| 72 | internal PaymentInformationConfiguration() {
|
---|
| 73 | this.Ignore(pi => pi.EntityState);
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | private class ProductConfiguration : EntityTypeConfiguration<Product> {
|
---|
| 78 | internal ProductConfiguration() {
|
---|
| 79 | this.Ignore(p => p.EntityState);
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 |
|
---|
[9577] | 83 | private class OrderConfiguration : EntityTypeConfiguration<Order> {
|
---|
| 84 | internal OrderConfiguration() {
|
---|
[9645] | 85 | this.Ignore(o => o.EntityState);
|
---|
[9577] | 86 | this.HasRequired(o => o.User).WithMany().HasForeignKey(o => o.UserId);
|
---|
| 87 | // TODO: OrderState?
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | private class OrderLineConfiguration : EntityTypeConfiguration<OrderLine> {
|
---|
| 92 | internal OrderLineConfiguration() {
|
---|
[9645] | 93 | this.Ignore(ol => ol.EntityState);
|
---|
[9579] | 94 | this.HasRequired(ol => ol.Order).WithMany(o => o.OrderLines).HasForeignKey(ol => ol.OrderId).WillCascadeOnDelete(true);
|
---|
[9577] | 95 | this.HasRequired(ol => ol.Product).WithMany().HasForeignKey(ol => ol.ProductId);
|
---|
| 96 | }
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | private class InvoiceConfiguration : EntityTypeConfiguration<Invoice> {
|
---|
| 100 | internal InvoiceConfiguration() {
|
---|
[9645] | 101 | this.Ignore(i => i.EntityState);
|
---|
[9577] | 102 | this.HasRequired(i => i.User).WithMany().HasForeignKey(i => i.UserId);
|
---|
[9579] | 103 | this.HasRequired(i => i.Order).WithMany(o => o.Invoices).HasForeignKey(i => i.OrderId);
|
---|
[9577] | 104 | }
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | private class InvoiceLineConfiguration : EntityTypeConfiguration<InvoiceLine> {
|
---|
| 108 | internal InvoiceLineConfiguration() {
|
---|
[9645] | 109 | this.Ignore(i => i.EntityState);
|
---|
[9579] | 110 | this.HasRequired(il => il.Invoice).WithMany(i => i.InvoiceLines).HasForeignKey(il => il.InvoiceId).WillCascadeOnDelete(true);
|
---|
[9577] | 111 | this.HasRequired(il => il.Product).WithMany().HasForeignKey(il => il.ProductId);
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | private class UsageRecordConfiguration : EntityTypeConfiguration<UsageRecord> {
|
---|
| 116 | internal UsageRecordConfiguration() {
|
---|
[9645] | 117 | this.Ignore(ur => ur.EntityState);
|
---|
[9577] | 118 | this.HasRequired(ur => ur.User).WithMany().HasForeignKey(ur => ur.UserId);
|
---|
[9579] | 119 | this.HasRequired(ur => ur.Product).WithMany().HasForeignKey(ur => ur.ProductId);
|
---|
[9577] | 120 | }
|
---|
| 121 | }
|
---|
| 122 |
|
---|
[9602] | 123 | private class UserConfiguration : EntityTypeConfiguration<User> {
|
---|
| 124 | internal UserConfiguration() {
|
---|
[9645] | 125 | this.Ignore(u => u.EntityState);
|
---|
[9602] | 126 | this.HasMany(u => u.PaymentInformation).WithRequired(pi => pi.User).HasForeignKey(pi => pi.UserId);
|
---|
[9641] | 127 | this.HasRequired(u => u.ContactInformation).WithMany().HasForeignKey(u => u.ContactInformationId);
|
---|
[9602] | 128 | }
|
---|
| 129 | }
|
---|
| 130 |
|
---|
[9577] | 131 | #endregion
|
---|
| 132 |
|
---|
| 133 | #region DB Seed Methods
|
---|
| 134 |
|
---|
[9602] | 135 | // - DropCreateDatabaseAlways<BillingContext>
|
---|
| 136 | // - DropCreateDatabaseIfModelChanges<BillingContext>
|
---|
| 137 | public class BillingContextInitiliazer : DropCreateDatabaseAlways<BillingContext> {
|
---|
[9577] | 138 | protected override void Seed(BillingContext context) {
|
---|
[9602] | 139 | Product p1 = new Product() {
|
---|
[9641] | 140 | Name = "OaaS Basic Service Charge",
|
---|
[9577] | 141 | Description = "Create and run your experiments within HeuristicLab Hive",
|
---|
[9619] | 142 | Price = 20.0,
|
---|
[9577] | 143 | ProductType = "Optimization Service"
|
---|
[9602] | 144 | };
|
---|
[9619] | 145 |
|
---|
| 146 | Product p2 = new Product() {
|
---|
| 147 | Name = "Oaas Usage Charges",
|
---|
| 148 | Description = "Charges for OaaS based on usage data",
|
---|
| 149 | Price = 0.0,
|
---|
| 150 | ProductType = "Optimization Service"
|
---|
| 151 | };
|
---|
| 152 |
|
---|
| 153 | Product p3 = new Product() {
|
---|
| 154 | Name = "Oaas Support Package",
|
---|
| 155 | Description = "Charges for OaaS based on usage data",
|
---|
| 156 | Price = 50.0,
|
---|
| 157 | ProductType = "Optimization Service"
|
---|
| 158 | };
|
---|
| 159 |
|
---|
[9602] | 160 | p1 = context.Products.Add(p1);
|
---|
[9619] | 161 | p2 = context.Products.Add(p2);
|
---|
| 162 | p3 = context.Products.Add(p3);
|
---|
[9602] | 163 |
|
---|
| 164 | User u1 = new Model.User() { Name = "spimming" };
|
---|
| 165 | User u2 = new Model.User() {
|
---|
| 166 | Name = "fschoeppl",
|
---|
| 167 | PaymentInformation = new List<PaymentInformation>() {
|
---|
| 168 | new PaymentInformation() {
|
---|
| 169 | PaymentMethod = PaymentMethod.Visa,
|
---|
| 170 | CardNumber = "123456789" } }
|
---|
| 171 | };
|
---|
| 172 |
|
---|
| 173 | u1 = context.Users.Add(u1);
|
---|
| 174 | u2 = context.Users.Add(u2);
|
---|
| 175 |
|
---|
| 176 | ContactInformation ci1 = new ContactInformation() {
|
---|
| 177 | FirstName = "Max",
|
---|
| 178 | LastName = "Mustermann",
|
---|
| 179 | Email = "max.mustermann@fh-hagenberg.at",
|
---|
[9641] | 180 | OrganizationName = "University of Applied Sciences Upper Austria " + Environment.NewLine + "School of Informatics/Communications/Media",
|
---|
[9602] | 181 | Street = "Softwarepark 11",
|
---|
| 182 | PostalCode = "4232",
|
---|
[9641] | 183 | City = "Hagenberg",
|
---|
[9602] | 184 | };
|
---|
| 185 |
|
---|
| 186 | ci1 = context.ContactInformations.Add(ci1);
|
---|
[9641] | 187 | u1.ContactInformation = ci1;
|
---|
[9602] | 188 |
|
---|
[9641] | 189 | ContactInformation ci2 = new ContactInformation() {
|
---|
| 190 | FirstName = "Max",
|
---|
| 191 | LastName = "Mustermann",
|
---|
| 192 | Email = "max.mustermann@fh-hagenberg.at",
|
---|
| 193 | OrganizationName = "University of Applied Sciences Upper Austria " + Environment.NewLine + "School of Informatics/Communications/Media",
|
---|
| 194 | Street = "Softwarepark 11",
|
---|
| 195 | PostalCode = "4232",
|
---|
| 196 | City = "Hagenberg",
|
---|
| 197 | };
|
---|
| 198 |
|
---|
| 199 | ci2 = context.ContactInformations.Add(ci2);
|
---|
| 200 | u2.ContactInformation = ci2;
|
---|
| 201 |
|
---|
[9602] | 202 | Order o1 = new Order() {
|
---|
| 203 | User = u2,
|
---|
| 204 | State = OrderState.Created,
|
---|
| 205 | BillingType = BillingType.Post,
|
---|
| 206 | BillingPeriod = BillingPeriod.Monthly,
|
---|
| 207 | };
|
---|
[9645] | 208 |
|
---|
[9602] | 209 | OrderLine ol1 = new OrderLine() {
|
---|
| 210 | Order = o1,
|
---|
| 211 | Product = p1,
|
---|
| 212 | ProductPrice = p1.Price,
|
---|
| 213 | Quantity = 1
|
---|
| 214 | };
|
---|
[9645] | 215 |
|
---|
[9602] | 216 | o1 = context.Orders.Add(o1);
|
---|
| 217 | ol1 = context.OrderLines.Add(ol1);
|
---|
| 218 |
|
---|
[9577] | 219 | context.SaveChanges();
|
---|
[9602] | 220 |
|
---|
| 221 | o1.State = OrderState.Active;
|
---|
| 222 | o1.ActiveSince = DateTime.Now;
|
---|
| 223 | context.Entry(o1).State = System.Data.EntityState.Modified;
|
---|
| 224 | context.SaveChanges();
|
---|
[9577] | 225 | }
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | #endregion
|
---|
| 229 | }
|
---|
| 230 | }
|
---|