private DataKey CreateDataKey(int customerId, Meter newMeter) { // Get customer TimeZoneId var customerProfile = RbacEntities.CustomerProfiles.FirstOrDefault(m => m.CustomerId == customerId); newMeter.TimeZoneID = customerProfile.TimeZoneID ?? 0; // Create an audit entry. Audit(newMeter); // Create a datakey var newItem = new DataKey() { //todo - GTC: DataKeys add dataKeyname here when db field is added - might have to pass in the datakey name DataKeyId = newMeter.MeterId,//this is auto increment field, so thats why we have the datakeyidnumber field DataKeyIdNumber = newMeter.MeterId, CustomerID = customerId, }; PemsEntities.DataKeys.Add(newItem); PemsEntities.SaveChanges(); // Add audit record Audit(newItem); // Need a [HousingMaster] HousingMaster housingMaster = PemsEntities.HousingMasters.FirstOrDefault(m => m.HousingName.Equals("Default")); // Create a MeterMap record to join datakey and Meter MeterMap meterMap = new MeterMap() { Customerid = newMeter.CustomerID, Areaid = newMeter.AreaID, MeterId = newMeter.MeterId, DataKeyId = newItem.DataKeyId, HousingId = housingMaster.HousingId }; PemsEntities.MeterMaps.Add(meterMap); PemsEntities.SaveChanges(); return(newItem); }
public HousingController(HousingMaster housingMaster, HousingConfig housingConfig, HousingData housingData) { this.housingMaster = housingMaster; this.housingConfig = housingConfig; this.housingData = housingData; }
private int Clone(int customerId, Int64 assetId) { // Get original datakey and its associated Meter and MeterMap // Since this exists, these entities should exist also. DataKey dataKey = PemsEntities.DataKeys.FirstOrDefault(m => m.DataKeyId == assetId); if (dataKey != null) { MeterMap meterMap = dataKey.MeterMaps.FirstOrDefault(m => m.DataKeyId == dataKey.DataKeyId); if (meterMap != null) { Meter meter = meterMap.Meter; // Create the new meter that is associated with the datakey type of asset. This is not a meter // but rather a an asset in the meter table that represents a datakey. Meter newMeter = CreateBaseAsset(meter.CustomerID, MeterGroups.Datakey, meter.AreaID); // Create a datakey var clonedItem = new DataKey() { //todo - GTC: DataKeys add datakeyname here when db field is added DataKeyDesc = dataKey.DataKeyDesc, DataKeyId = newMeter.MeterId, //this is auto increment, so it wont save the value, its why we have the datakeyidnumber column. DataKeyIdNumber = newMeter.MeterId, DataKeyType = dataKey.DataKeyType, CustomerID = customerId }; PemsEntities.DataKeys.Add(clonedItem); PemsEntities.SaveChanges(); // Add AssetPending record (new PendingFactory(ConnectionStringName, Now)).SetImportPending( AssetTypeModel.AssetType.DataKey, SetToMidnight(Now), AssetStateType.Current, customerId, clonedItem.DataKeyIdNumber.GetValueOrDefault(), (int)AssetStateType.Pending, (int)OperationalStatusType.Inactive, (int)AssetAreaId.DataKey); // Align clonable meter fields. newMeter.Location = meter.Location; newMeter.DemandZone = meter.DemandZone; newMeter.FreeParkingMinute = meter.FreeParkingMinute; newMeter.MeterGroup = meter.MeterGroup; newMeter.MeterName = meter.MeterName; newMeter.MeterType = meter.MeterType; newMeter.TimeZoneID = meter.TimeZoneID; newMeter.TypeCode = meter.TypeCode; newMeter.WarrantyExpiration = meter.WarrantyExpiration; newMeter.NextPreventativeMaintenance = meter.NextPreventativeMaintenance; // Create meter map for cloned item. // Need a [HousingMaster] HousingMaster housingMaster = PemsEntities.HousingMasters.FirstOrDefault(m => m.HousingName.Equals("Default")); // Create a [MeterMap] entry MeterMap newMeterMap = new MeterMap() { Customerid = newMeter.CustomerID, Areaid = newMeter.AreaID, MeterId = newMeter.MeterId, DataKeyId = clonedItem.DataKeyId, HousingId = housingMaster.HousingId }; PemsEntities.MeterMaps.Add(newMeterMap); PemsEntities.SaveChanges(); newMeterMap.AreaId2 = meterMap.AreaId2; newMeterMap.ZoneId = meterMap.ZoneId; newMeterMap.SubAreaID = meterMap.SubAreaID; newMeterMap.CustomGroup1 = meterMap.CustomGroup1; newMeterMap.CustomGroup2 = meterMap.CustomGroup2; newMeterMap.CustomGroup3 = meterMap.CustomGroup3; newMeterMap.MaintRouteId = meterMap.MaintRouteId; PemsEntities.SaveChanges(); // Set audit records. Audit(newMeter); Audit(newMeterMap); Audit(clonedItem); return(clonedItem.DataKeyIdNumber.GetValueOrDefault()); } } return(0); }