public ElectricalEquipmentComponentTypeProperty AddElectricalComponentTypeProperty(ElectricalEquipmentComponentTypeProperty mecp) { using (CmsEntities cee = new CmsEntities()) { //Check if this component property already exist ElectricalEquipmentComponentTypeProperty temp = (from x in cee.ElectricalEquipmentComponentTypeProperties where x.ElectricalEquipmentPropertyId == mecp.ElectricalEquipmentPropertyId && x.ElectricalEquipmentComponentTypeId == mecp.ElectricalEquipmentComponentTypeId select x).FirstOrDefault(); if (temp == null) { //Add new Component Type temp = new ElectricalEquipmentComponentTypeProperty(); temp.ElectricalEquipmentPropertyId = mecp.ElectricalEquipmentPropertyId; temp.ElectricalEquipmentComponentTypeId = mecp.ElectricalEquipmentComponentTypeId; temp.Ordinal = mecp.Ordinal; cee.ElectricalEquipmentComponentTypeProperties.Add(temp); cee.SaveChanges(); } else { temp.Ordinal = mecp.Ordinal; cee.SaveChanges(); } return temp; } }
public DbOperationResult SaveIssueFiles(List<IssueFile> issueFiles) { try { using (var cee = new CmsEntities()) { foreach (IssueFile attachment in issueFiles) { IssueFile existingAttachment = (from x in cee.IssueFiles where x.Filename == attachment.Filename && x.Path == attachment.Path select x).FirstOrDefault(); if (existingAttachment != null) { existingAttachment.Description = attachment.Description; } else { attachment.User = null; attachment.Issue = null; attachment.AttachmentType = null; cee.IssueFiles.Add(attachment); } } cee.SaveChanges(); } } catch (Exception ex) { return new DbOperationResult { ServerErrorMessages = new List<string> { ex.Message } }; } return new DbOperationResult(); }
public DbOperationResult<IssueTypeMilestoneType> AddIssueTypeMilestoneType(IssueType issueType, IssueMilestoneType milestoneType) { var result = new DbOperationResult<IssueTypeMilestoneType>(); //guard against duplicate. using (var cee = new CmsEntities()) { int k = (from x in cee.IssueTypeMilestoneTypes where x.IssueTypeId == issueType.Id && x.MilestoneTypeId == milestoneType.Id select x.Id).Count(); if (k > 0) { result.ServerErrorMessages.Add(string.Format("Insert Failed. An IssueTypeMilestoneType with the combination IssueType Name: '{0}' and Issue SubType Name: '{1}' already exists.", issueType.Name, milestoneType.Name)); return result; } var issueTypeMilestoneType = new IssueTypeMilestoneType { MilestoneTypeId = milestoneType.Id, IssueTypeId = issueType.Id, Ordinal = 0 }; cee.IssueTypeMilestoneTypes.Add(issueTypeMilestoneType); cee.SaveChanges(); result.EntityResult = issueTypeMilestoneType; } return result; }
public void Fix() { CmsEntities cee = new CmsEntities(); List<IssueRelatedIssue> itemsToAdd = new List<IssueRelatedIssue>(); var allRelatedIssues = cee.IssueRelatedIssues.ToList(); foreach (var relatedIssue in allRelatedIssues) { var reverseExist = (allRelatedIssues.FirstOrDefault(x => x.RelatedIssueId == relatedIssue.IssueId)); if (reverseExist == null) { if (itemsToAdd.FirstOrDefault(x => x.IssueId == relatedIssue.IssueId && x.RelatedIssueId == relatedIssue.RelatedIssueId) == null) { itemsToAdd.Add(new IssueRelatedIssue {IssueId = relatedIssue.RelatedIssueId, RelatedIssueId = relatedIssue.IssueId}); } } } if (itemsToAdd.Any()) { foreach (var issueRelatedIssue in itemsToAdd) { cee.IssueRelatedIssues.Add(new IssueRelatedIssue { IssueId = issueRelatedIssue.IssueId, RelatedIssueId = issueRelatedIssue.RelatedIssueId }); } } cee.SaveChanges(); }
/// <summary> /// 添加文章 /// </summary> /// <param name="varticle"></param> public int AddArticle(cms_varticle varticle) { using (CmsEntities entity = new CmsEntities()) { cms_varticle obj = varticle; entity.cms_varticle.AddObject(obj); entity.SaveChanges(); return obj.id; } }
public ControlSystemComponentType AddControlSystemComponentType(ControlSystemComponentType controlSystemComponentType) { var newControlSystemComponentType = new ControlSystemComponentType(); using (var cee = new CmsEntities()) { //Check if this component type already exist var componentType = (from x in cee.ControlSystemComponentTypes where x.Id == controlSystemComponentType.Id select x).FirstOrDefault(); if (componentType != null) { //Edit the Component Type componentType.Name = controlSystemComponentType.Name; componentType.Description = controlSystemComponentType.Description; componentType.Ordinal = controlSystemComponentType.Ordinal; cee.SaveChanges(); } else { //Add new Component Type componentType = new ControlSystemComponentType(); componentType.Name = controlSystemComponentType.Name; componentType.Description = controlSystemComponentType.Description; componentType.Code = controlSystemComponentType.Name.Replace(" ", ""); componentType.IsActive = true; cee.ControlSystemComponentTypes.Add(componentType); cee.SaveChanges(); } newControlSystemComponentType.Id = componentType.Id; newControlSystemComponentType.Name = componentType.Name; newControlSystemComponentType.Description = componentType.Description; newControlSystemComponentType.Ordinal = componentType.Ordinal; newControlSystemComponentType.Code = componentType.Code; return newControlSystemComponentType; } }
/// <summary> /// 删除文章 /// </summary> /// <param name="varticle"></param> public void DelArticle(cms_varticle varticle) { using (CmsEntities entity = new CmsEntities()) { var query = entity.cms_varticle.FirstOrDefault(m => m.id == varticle.id); if (query != null) { entity.cms_varticle.DeleteObject(query); entity.SaveChanges(); } } }
public void AddSupervisorsToApprovals() { using (TransactionScope transaction = new TransactionScope()) { using (CmsEntities cee = new CmsEntities(120, mConnectCMS)) { var supervisorCategory = (from x in cee.IssueCategories where x.Code == "SUPERVISOR" select x).FirstOrDefault(); var pcsAdminUser = (from x in cee.Users where x.UserName == "pcsadmin" select x).FirstOrDefault(); var issues = (from x in cee.Issues orderby x.Id select x).ToList(); foreach (var issue in issues) { Console.Out.WriteLine("Processing issue {0}", issue.Id); Issue issue1 = issue; var hasAnyApprovalsChecked = (from x in cee.IssueApprovals where x.IssueId == issue1.Id && x.IssueCategoryId != supervisorCategory.Id && x.Approved select x).Any(); var hasSupervisor = (from x in cee.IssueApprovals where x.IssueId == issue1.Id && x.IssueCategoryId == supervisorCategory.Id select x).FirstOrDefault(); if (hasSupervisor == null && hasAnyApprovalsChecked) { var newIssueApproval = new IssueApproval { IssueId = issue.Id, IssueCategoryId = supervisorCategory.Id, Approved = true, Approver = pcsAdminUser, Date = DateTime.Now }; cee.IssueApprovals.Add(newIssueApproval); Console.Out.WriteLine("Added Supervisor for IssueApproval on issue {0}", issue.Id); } } cee.SaveChanges(); transaction.Complete(); Console.Out.WriteLine("Transaction Complete"); Console.ReadLine(); } } }
public ElectricalEquipmentComponentType AddElectricalComponentType(ElectricalEquipmentComponentType electricalEquipmentComponentType) { ElectricalEquipmentComponentType newMect = new ElectricalEquipmentComponentType(); using (CmsEntities cee = new CmsEntities()) { //Check if this component type already exist ElectricalEquipmentComponentType originalMect = (from x in cee.ElectricalEquipmentComponentTypes where x.Id == electricalEquipmentComponentType.Id select x).FirstOrDefault(); if (originalMect != null) { //Edit the Component Type originalMect.Name = electricalEquipmentComponentType.Name; originalMect.Description = electricalEquipmentComponentType.Description; //pct.Code = mect.Code; cee.SaveChanges(); } else { //Add new Component Type originalMect = new ElectricalEquipmentComponentType(); originalMect.Name = electricalEquipmentComponentType.Name; originalMect.Description = electricalEquipmentComponentType.Description; originalMect.Code = electricalEquipmentComponentType.Name.Replace(" ", ""); originalMect.IsActive = true; cee.ElectricalEquipmentComponentTypes.Add(originalMect); //cee.AddToPipeComponentTypes(pct); cee.SaveChanges(); } newMect.Id = originalMect.Id; newMect.Name = originalMect.Name; newMect.Description = originalMect.Description; return newMect; } }
public IssueTypeClassification AddIssueTypeClassification(int issueClassificationId, int issueTypeId) { using (var cee = new CmsEntities()) { var issueTypeClassification = new IssueTypeClassification(); issueTypeClassification.IssueClassificationId = issueClassificationId; issueTypeClassification.IssueTypeId = issueTypeId; cee.IssueTypeClassifications.Add(issueTypeClassification); cee.SaveChanges(); return issueTypeClassification; } }
public void RunFix() { using (TransactionScope transaction = new TransactionScope()) { using (CmsEntities cee = new CmsEntities(120,mConnectCMS)) { var issues = (from x in cee.Issues.Include("IssueAssignedCategories") orderby x.Id select x).ToList(); foreach (var issue in issues) { Console.Out.WriteLine("Processing issue {0}", issue.Id); foreach (var issueAssignedCategory in issue.IssueAssignedCategories) { var closeoutExist = (from x in cee.IssueCloseouts where x.IssueId == issue.Id && x.IssueCategoryId == issueAssignedCategory.IssueCategoryId select x).FirstOrDefault(); if (closeoutExist == null) { //CloseOut Doesnt exist add one var newCloseout = new IssueCloseout { IssueId = issue.Id, IssueCategoryId = issueAssignedCategory.IssueCategoryId }; cee.IssueCloseouts.Add(newCloseout); Console.Out.WriteLine("Added new category '{0}' closeout to issue {1}", issueAssignedCategory.IssueCategoryId,issue.Id); } } } cee.SaveChanges(); transaction.Complete(); Console.Out.WriteLine("Transaction Complete"); Console.ReadLine(); } } }
public PipeSpecialFeature SavePipeSpecialFeature(PipeSpecialFeature pipeSpecialFeature) { using (CmsEntities cee = new CmsEntities()) { //Check if the pipe area exist var originalPipeSpecialFeature = (from x in cee.PipeSpecialFeatures where x.Id == pipeSpecialFeature.Id select x).FirstOrDefault(); if (originalPipeSpecialFeature == null) { //Add new Pipe Special Feature pipeSpecialFeature.Pipes = null; cee.PipeSpecialFeatures.Add(pipeSpecialFeature); } else { //Update existing Pipe Special Feature originalPipeSpecialFeature.Name = pipeSpecialFeature.Name; originalPipeSpecialFeature.Description = pipeSpecialFeature.Description; } cee.SaveChanges(); } return pipeSpecialFeature; }
private void DeletePipeAddHistory(Pipe pipe, int userId, Pipe originalPipe, CmsEntities cee) { IEnumerable<PipeComponent> componentsToBeDeleted = (from x in originalPipe.PipeComponents where !pipe.PipeComponents.Any(x1 => x1.Id == x.Id) select x); PipeRevisionHistory rh = new PipeRevisionHistory(); string[] componentMessages = (from x in componentsToBeDeleted select "Name: " + x.Name + " Type: " + x.PipeComponentType.Name).ToArray(); if (componentMessages.Length > 0) { string removedItems = string.Join(",", componentMessages); rh.Description = string.Format("Pipe Components were removed : {0}", removedItems); rh.PipeId = pipe.Id; rh.Date = DateTime.Now; rh.UserId = userId; rh.IsSystemMessage = true; var latestPrh = (from x in cee.PipeRevisionHistories where x.PipeId == pipe.Id select x.Revision).ToList(); if (latestPrh.Count > 0) { rh.Revision = latestPrh.AsQueryable().Max() + VERSIONINCREMENT; } else { rh.Revision = VERSIONINCREMENT; } cee.PipeRevisionHistories.Add(rh); } cee.SaveChanges(); foreach (var pipeComponent in componentsToBeDeleted) { DeletePipeComponent(pipeComponent); } }
public PipeProperty SavePipeProperty(PipeProperty pipeProperty) { using (CmsEntities cee = new CmsEntities()) { PipeProperty original = (from x in cee.PipeProperties where x.Id == pipeProperty.Id select x).FirstOrDefault(); if (original == null) { cee.PipeProperties.Add(pipeProperty); } else { cee.Entry(original).CurrentValues.SetValues(pipeProperty); } cee.SaveChanges(); } return pipeProperty; }
public bool SavePipePropertyValues(List<PipePropertyValue> pipeComponentPropertyValues) { using (CmsEntities cee = new CmsEntities()) { foreach (var pipeComponentPropertyValue in pipeComponentPropertyValues) { var q = (from x in cee.PipePropertyValues where x.Id == pipeComponentPropertyValue.Id select x).FirstOrDefault(); if (q == null) { q = new PipePropertyValue { ComponentId = pipeComponentPropertyValue.ComponentId, PipePropertyId = pipeComponentPropertyValue.PipePropertyId, Value = pipeComponentPropertyValue.Value }; cee.PipePropertyValues.Add(q); //cee.AddToPipePropertyValues(q); } else { q.PipePropertyId = pipeComponentPropertyValue.PipePropertyId; q.ComponentId = pipeComponentPropertyValue.ComponentId; q.Value = pipeComponentPropertyValue.Value; } cee.SaveChanges(); } } return true; }
public void AssignIssueToPipe(int pipeId, int issueId) { using (CmsEntities cee = new CmsEntities()) { IssueRelatedPipe pi = new IssueRelatedPipe(); pi.PipeId = pipeId; pi.IssueId = issueId; cee.IssueRelatedPipes.Add(pi); cee.SaveChanges(); } }
public PipeComponentTypeProperty AddPipeComponentTypeProperty(PipeComponentTypeProperty pcpt) { PipeComponentTypeProperty tempPcpt; using (CmsEntities cee = new CmsEntities()) { //Check if this component property already exist tempPcpt = (from x in cee.PipeComponentTypeProperties where x.PipeProperty.Id == pcpt.PipePropertyId && x.PipeComponentType.Id == pcpt.ComponentTypeId select x).FirstOrDefault(); if (tempPcpt == null) { //Add new Component Type tempPcpt = new PipeComponentTypeProperty(); tempPcpt.PipePropertyId = pcpt.PipePropertyId; tempPcpt.ComponentTypeId = pcpt.ComponentTypeId; tempPcpt.Ordinal = pcpt.Ordinal; cee.PipeComponentTypeProperties.Add(tempPcpt); cee.SaveChanges(); } else { tempPcpt.Ordinal = pcpt.Ordinal; cee.SaveChanges(); } return tempPcpt; } }
public DbOperationResult DeletePipeProperty(int pipePipePropertyId) { DbOperationResult result = new DbOperationResult(); try { using (CmsEntities cee = new CmsEntities()) { cee.Configuration.LazyLoadingEnabled = true; var componentTypeProperties = (from x in cee.PipeComponentTypeProperties where x.PipePropertyId == pipePipePropertyId select x).ToList(); if (componentTypeProperties.Any()) { result.ServerErrorMessages.Add(String.Format("Can't delete Property '{0}' as it is assigned to {1} Pipe Component Types.", componentTypeProperties[0].PipeProperty.Name, componentTypeProperties.Count)); } else { var pipeProperty = (from x in cee.PipeProperties where x.Id == pipePipePropertyId select x).FirstOrDefault(); cee.PipeProperties.Remove(pipeProperty); } cee.SaveChanges(); } } catch (Exception ex) { log.Error("", ex, ex.ToString()); result.ServerErrorMessages.Add(string.Format("Could not delete Mechanical Property.{0}{1}", Environment.NewLine, ex.Message)); } return result; }
public DbOperationResult<PipeComponentType> AddEditPipeComponentType(PipeComponentType pipeComponentType) { DbOperationResult<PipeComponentType> result = new DbOperationResult<PipeComponentType>(); try { if (!pipeComponentType.InheritArea && pipeComponentType.Area == null) { throw new Exception("Area has not been set and InheritArea is false."); } PipeComponentType newPct = new PipeComponentType(); using (CmsEntities cee = new CmsEntities()) { //Check if this component type already exist PipeComponentType pct = (from x in cee.PipeComponentTypes where x.Id == pipeComponentType.Id select x).FirstOrDefault(); if (pct != null) { //Edit the Component Type pct.Name = pipeComponentType.Name; pct.Description = pipeComponentType.Description; pct.Code = pipeComponentType.Code; pct.Ordinal = pipeComponentType.Ordinal; pct.InheritArea = pipeComponentType.InheritArea; pct.DefaultAreadId = pipeComponentType.DefaultAreadId; cee.SaveChanges(); } else { //Add new Component Type pct = new PipeComponentType { Name = pipeComponentType.Name, Description = pipeComponentType.Description, Code = pipeComponentType.Code, Ordinal = pipeComponentType.Ordinal, InheritArea = pipeComponentType.InheritArea }; if (pipeComponentType.Area != null) { pct.DefaultAreadId = pipeComponentType.Area.Id; } cee.PipeComponentTypes.Add(pct); cee.SaveChanges(); } newPct.Id = pct.Id; newPct.Name = pct.Name; newPct.Description = pct.Description; newPct.Ordinal = pct.Ordinal; newPct.DefaultAreadId = pct.DefaultAreadId; newPct.InheritArea = pct.InheritArea; result.EntityResult = newPct; return result; } } catch (Exception ex) { return BuildOperationalErrorResults<PipeComponentType>(ex); } }
private void SavePipeComponents(Pipe pipe, CmsEntities cee, int pipeId, int userId) { foreach (var pipeComponent in pipe.PipeComponents) { var q = (from x in cee.PipeComponents where x.Id == pipeComponent.Id select x).FirstOrDefault(); if (q != null) { if (q.LastInspectedDate != pipeComponent.LastInspectedDate) { PipeRevisionHistory rv = new PipeRevisionHistory { PipeId = pipeId, Date = DateTime.Now, UserId = userId, Description = string.Format("Component '{0}': Last Inspected Date changed from '{1}' to '{2}'.", pipeComponent.Name, q.LastInspectedDate, pipeComponent.LastInspectedDate), IsSystemMessage = true }; AddPipeRevisionHistoryInternal(rv, cee); } //Update Pipe Componet cee.Entry(q).CurrentValues.SetValues(pipeComponent); } else { q = new PipeComponent { PipeId = pipeComponent.PipeId, PipeComponentTypeId = pipeComponent.PipeComponentTypeId, SpecialFeature = pipeComponent.SpecialFeature, Ordinal = pipeComponent.Ordinal, Description = pipeComponent.Description, SubArea = pipeComponent.SubArea, Number = pipeComponent.Number, DrawingId = pipeComponent.DrawingId, NextInspectionDate = pipeComponent.NextInspectionDate, LastInspectedById = pipeComponent.LastInspectedById, LastInspectedDate = pipeComponent.LastInspectedDate, LastModifiedDate = pipeComponent.LastModifiedDate, LastModifiedById = pipeComponent.LastModifiedById, ManufacturerId = pipeComponent.ManufacturerId, ModelId = pipeComponent.ModelId, AreaId = pipeComponent.AreaId }; //Add new Pipe Component cee.PipeComponents.Add(q); } foreach (var pipeComponentPropertyValue in pipeComponent.PipePropertyValues) { var qq = (from x in cee.PipePropertyValues where x.Id == pipeComponentPropertyValue.Id select x).FirstOrDefault(); if (qq != null) { cee.Entry(qq).CurrentValues.SetValues(pipeComponentPropertyValue); } else { cee.PipePropertyValues.Add(pipeComponentPropertyValue); } } } cee.SaveChanges(); }
private void DeletePipeComponent(PipeComponent pc) { using (CmsEntities cee = new CmsEntities()) { var pipeComponentPropertyValues = (from x in cee.PipePropertyValues where x.ComponentId == pc.Id select x).ToList(); foreach (var pipeComponentPropertyValue in pipeComponentPropertyValues) { cee.PipePropertyValues.Remove(pipeComponentPropertyValue); } var q = (from x in cee.PipeComponents where x.Id == pc.Id select x).FirstOrDefault(); if (q != null) { cee.PipeComponents.Remove(q); cee.SaveChanges(); } } }
public PipeCategory SavePipeCategory(PipeCategory pipeCategory) { using (CmsEntities cee = new CmsEntities()) { //Check if the pipe area exist var originalPipeIssueCategory = (from x in cee.PipeCategories where x.Id == pipeCategory.Id select x).FirstOrDefault(); if (originalPipeIssueCategory == null) { //Add new Pipe Class pipeCategory.Pipes = null; cee.PipeCategories.Add(pipeCategory); } else { //Update existing Area originalPipeIssueCategory.Name = pipeCategory.Name; originalPipeIssueCategory.Description = pipeCategory.Description; } cee.SaveChanges(); } return pipeCategory; }
public List<PipeAttachment> DeletePipeAttachments(List<PipeAttachment> pipeAttachments) { List<PipeAttachment> failToDeleteFiles = new List<PipeAttachment>(); using (CmsEntities cee = new CmsEntities()) { foreach (var pipeAttachment in pipeAttachments) { var toBeDeleted = (from x in cee.PipeAttachments where x.Filename == pipeAttachment.Filename && x.Path == pipeAttachment.Path select x).FirstOrDefault(); if (toBeDeleted != null) { //First try to delete the file from HDD try { string settingValue = CommonUtils.GetAppSettingValue(CommonUtils.AppSettingKey.UploadedAttachmentsPathRoot); string folderPath = String.Format(@"{0}\{1}", settingValue, toBeDeleted.Path); if (Directory.Exists(folderPath)) { foreach (var file in Directory.GetFiles(folderPath)) { File.Delete(file); } Directory.Delete(folderPath); } } catch (Exception ex) { log.Error("", ex, ex.ToString()); failToDeleteFiles.Add(pipeAttachment); } //Delete from database try { cee.PipeAttachments.Remove(toBeDeleted); cee.SaveChanges(); } catch (Exception ex) { log.Error("", ex, ex.ToString()); failToDeleteFiles.Add(pipeAttachment); } } } } return failToDeleteFiles; }
public PipeClass SavePipeClass(PipeClass pipeClass) { using (CmsEntities cee = new CmsEntities()) { //Check if the pipe area exist var originalPipeClass = (from x in cee.PipeClasses where x.Id == pipeClass.Id select x).FirstOrDefault(); if (originalPipeClass == null) { //Add new Pipe Class pipeClass.Pipes = null; cee.PipeClasses.Add(pipeClass); } else { //Update existing PipeSzie originalPipeClass.Name = pipeClass.Name; originalPipeClass.Description = pipeClass.Description; } cee.SaveChanges(); } return pipeClass; }
public DbOperationResult DeletePipeComponentType(int pipeComponentTypeId) { DbOperationResult ds = new DbOperationResult(); try { using (CmsEntities cee = new CmsEntities()) { cee.Configuration.LazyLoadingEnabled = true; var pipeComponentType = (from x in cee.PipeComponentTypes where x.Id == pipeComponentTypeId select x).FirstOrDefault(); if (pipeComponentType != null) { //Check if the Pip Component Type has Property if (pipeComponentType.PipeComponentTypeProperties.Any()) { //this property is assigned - can't delete it ds.ServerErrorMessages.Add(String.Format("Can't delete '{0}' component type as it has assigned '{1}' property.", pipeComponentType.Name, pipeComponentType.PipeComponentTypeProperties[0].PipeProperty.Name)); } if (!ds.HasErrors) { //check if pipe component is assigned to a pipe var pipe = (from x in cee.PipeComponents where x.PipeComponentTypeId == pipeComponentTypeId select x.Pipe).FirstOrDefault(); if (pipe != null) { ds.ServerErrorMessages.Add(String.Format("Can't delete '{0}' component type as it is assigned to '{1}' pipe.", pipeComponentType.Name, pipe.Name)); } else { cee.PipeComponentTypes.Remove(pipeComponentType); } } } cee.SaveChanges(); } } catch (Exception ex) { log.Error("", ex, ex.ToString()); ds.ServerErrorMessages.Add(string.Format("Could not remove Pipe Component Property Type.{0}{1}", Environment.NewLine, ex.Message)); } return ds; }
public PipeComponent SavePipeComponent(PipeComponent pipeComponent) { using (CmsEntities cee = new CmsEntities()) { var q = (from x in cee.PipeComponents where x.Id == pipeComponent.Id select x).FirstOrDefault(); if (q != null) { //Update Pipe Componet q.PipeComponentTypeId = pipeComponent.PipeComponentTypeId; q.SpecialFeature = pipeComponent.SpecialFeature; q.SubArea = pipeComponent.SubArea; q.Number = pipeComponent.Number; q.NextInspectionDate = pipeComponent.NextInspectionDate; q.LastInspectedById = pipeComponent.LastInspectedById; q.LastInspectedDate = pipeComponent.LastInspectedDate; q.LastModifiedById = pipeComponent.LastModifiedById; q.LastModifiedDate = pipeComponent.LastModifiedDate; q.DrawingId = pipeComponent.DrawingId; q.Description = pipeComponent.Description; pipeComponent = q; } else { PipeComponent pc = new PipeComponent(); pc.PipeId = pipeComponent.Pipe.Id; pc.PipeComponentTypeId = pipeComponent.PipeComponentTypeId; pc.SpecialFeature = pipeComponent.SpecialFeature; pc.SubArea = pipeComponent.SubArea; pc.Number = pipeComponent.Number; pc.NextInspectionDate = pipeComponent.NextInspectionDate; pc.LastInspectedById = pipeComponent.LastInspectedById; pc.LastInspectedDate = pipeComponent.LastInspectedDate; pc.LastModifiedById = pipeComponent.LastModifiedById; pc.LastModifiedDate = pipeComponent.LastModifiedDate; pc.DrawingId = pipeComponent.DrawingId; pc.Description = pipeComponent.Description; //Add new Pipe Component cee.PipeComponents.Add(pc); cee.SaveChanges(); pipeComponent = pc; } pipeComponent = (from x in cee.PipeComponents.Include("Pipe").Include("PipeComponentType") where x.Id == pipeComponent.Id select x).FirstOrDefault(); pipeComponent.Pipe.Area = (from x in cee.Areas where x.Id == pipeComponent.Pipe.AreaId select x).FirstOrDefault(); } return pipeComponent; }
public DbOperationResult DeletePipeSpecialFeature(int pipeSpecialFeatureId) { DbOperationResult ds = new DbOperationResult(); using (CmsEntities cee = new CmsEntities()) { cee.Configuration.LazyLoadingEnabled = true; var pipeSpecialFeature = (from x in cee.PipeSpecialFeatures where x.Id == pipeSpecialFeatureId select x).FirstOrDefault(); if (pipeSpecialFeature != null) { if (pipeSpecialFeature.Pipes.Count() > 0) { //this area is assigned - can't delete it ds.ServerErrorMessages.Add(String.Format("Can't delete '{0}' Special Feature as it is assigned to '{1}' pipe.", pipeSpecialFeature.Name, pipeSpecialFeature.Pipes[0].Name)); return ds; } else { cee.PipeSpecialFeatures.Remove(pipeSpecialFeature); } } cee.SaveChanges(); return ds; } }
public PipeRevisionHistory AddPipeRevisionHistoryInternal(PipeRevisionHistory prh, CmsEntities cee) { const decimal incrediment = 0.001m; var latestPrh = (from x in cee.PipeRevisionHistories where x.PipeId == prh.PipeId select x.Revision).ToList(); if (latestPrh.Count > 0) { prh.Revision = latestPrh.AsQueryable().Max() + incrediment; } else { prh.Revision = incrediment; } prh.User = null; //int issueId = prh.Issue.Id; prh.Issue = null; //prh.IssueId = issueId; cee.PipeRevisionHistories.Add(prh); cee.SaveChanges(); return prh; }
/// <summary> /// Import the tuning parameters /// </summary> //public void Import(XDocument xmlDoc, Dictionary<string, TuningParams> list, Dictionary<string, bool> tuningParamTrendLookup, ISS.Logger.Interfaces.ILogger log, // List<ControlSystemComponent> components, List<ControlSystemTuningPropertyValue> controlSystemTuningPropertyValues, List<ControlSystemTuningProperty> controlSystemTuningProperties) //public void Import(XDocument xmlDoc, Dictionary<string, TuningParams> list, Dictionary<string, bool> tuningParamTrendLookup, ISS.Logger.Interfaces.ILogger log) public void Import(XDocument xmlDoc, Dictionary<string, TuningParams> list, Dictionary<string, bool> tuningParamTrendLookup, ISS.Logger.Interfaces.ILogger log, Dictionary<int, String> components, List<TempTuningPropertyValue> controlSystemTuningPropertyValues, Dictionary<int, String> controlSystemTuningProperties) { IEnumerable<ImportMask> masks = from m in xmlDoc.Descendants("Mask") let elementNameElement = m.Element("ElementName") where elementNameElement != null let paranNameElement = m.Element("ParamName") where paranNameElement != null select new ImportMask { ElementNameMask = elementNameElement.Value, ParamNameMask = paranNameElement.Value }; ignoreImportMasks = new List<ImportMask>(masks.ToList()); ConvertMasksToRegularExpressions(); List<string> ignoredItems = new List<string>(); List<string> warnings = new List<string>(); List<string> processedItems = new List<string>(); // // Import // using (var cee = new CmsEntities()) { cee.Configuration.AutoDetectChangesEnabled = false; cee.Configuration.ValidateOnSaveEnabled = false; int importCount = 0; foreach (var kvp in list) { string componentName = kvp.Key; ControlSystemComponent component = cee.ControlSystemComponents.Include("ControlSystemComponentType").FirstOrDefault(x => x.Name == componentName); //Check if the Component is ignored - this shoudl return true only if a Component is ignored and ParamName is set to * if (MatchesIgnoreMask(componentName, string.Empty)) { //log.Info(" [" + componentName + "] IGNORED DUE TO MASK MATCH"); ignoredItems.Add(" [" + componentName + "] IGNORED DUE TO MASK MATCH"); continue; } if (!components.ContainsValue(componentName)) { // log warning //log.Warning("","Component not found in database [" + componentName + "]"); warnings.Add("Component not found in database [" + componentName + "]"); } else { //log.Info("Component name: " + componentName); foreach (var tpkvp in kvp.Value) { string propertyName = tpkvp.Key; double propertyValue = tpkvp.Value; if (propertyName.Length > 0) { if (MatchesIgnoreMask(componentName, propertyName)) { //log.Info(" [" + componentName + ", " + propertyName + "] IGNORED DUE TO MASK MATCH"); ignoredItems.Add(" [" + componentName + ", " + propertyName + "] IGNORED DUE TO MASK MATCH"); } else { // Next lookup will never fail bool trend; if (tuningParamTrendLookup.TryGetValue(propertyName, out trend)) { if (trend) { // trend used to be column on a tuning parameter in the old CMS // in new CMS it is on ControlSystemTuningPropertyValue } else { int componentId = components.FirstOrDefault(x => x.Value.ToLower() == componentName.ToLower()).Key; int propertyId = controlSystemTuningProperties.FirstOrDefault(x => x.Value.ToLower() == propertyName.ToLower()).Key; if (componentId == 0 || propertyId == 0) { if (componentId == 0) { //log.Warning("", c warnings.Add(String.Format("could not recognise Component '{0}'", componentName)); } else { //log.Warning("", "could not recognise c '{0}'", propertyName); warnings.Add(String.Format("could not recognise Property '{0}'", propertyName)); } continue; } //Check whether the Tuning Property actualy exist for this component var controlSystemComponentTypeTuningProperty = cee.ControlSystemComponentTypeTuningProperties.FirstOrDefault( x => x.ControlSystemComponentTypeId == component.ControlSystemComponentTypeId && x.ControlSystemTuningPropertyId == propertyId); if (controlSystemComponentTypeTuningProperty == null) { //log.Warning("", "Tuning Property '{0}' does not exist for Component '{1}' ComponentType '{2}'" // , propertyName, componentName, component.ControlSystemComponentType.Name); warnings.Add(String.Format("Tuning Property '{0}' does not exist for Component '{1}' ComponentType '{2}'" , propertyName, componentName, component.ControlSystemComponentType.Name)); continue; } var tuningPropertyValue = controlSystemTuningPropertyValues.FirstOrDefault(x => x.ComponentId == componentId && x.Propertyid == propertyId); if (tuningPropertyValue == null) { //This doesnt have entry so create it string sqlInsert = string.Format("INSERT INTO control.ControlSystemTuningPropertyValue " + "(ControlSystemComponentId, ControlSystemTuningPropertyId,PcsValue, Value, Imported, Trended, Reportable) VALUES " + "({0},{1},'{2}','{2}', 1, 0, 0)", componentId, propertyId, propertyValue); cee.Database.ExecuteSqlCommand(sqlInsert); //log.Verbose(sqlInsert); //log.Info("Component '{0}' TuningProperty '{1}' = {2} Imported into PCS VALUE and VALUE (reason: Record didn't Exist)", // componentName, propertyName, propertyValue); processedItems.Add(String.Format("Component '{0}' TuningProperty '{1}' = {2} Imported into PCS VALUE and VALUE (reason: Record didn't Exist)", componentName, propertyName, propertyValue)); controlSystemTuningPropertyValues.Add(new TempTuningPropertyValue { ComponentId = componentId, Propertyid = propertyId, Value = propertyValue.ToString() }); importCount++; } else { if (tuningPropertyValue.Trended) { // log warning //log.Warning("", "Component '{0}' TuningProperty '{1}' is a TREND and is ignored", // componentName, propertyName, propertyValue); warnings.Add(String.Format("Component '{0}' TuningProperty '{1}' is a TREND and is ignored", componentName, propertyName, propertyValue)); } else { if (tuningPropertyValue.Value == null) { //If current Value is null update the Value and PcsValue string sqlUpdate = string.Format("UPDATE control.ControlSystemTuningPropertyValue SET pcsvalue = '{0}', value = '{0}'" + " WHERE ControlSystemComponentId = {1} " + " AND ControlSystemTuningPropertyId = {2}", propertyValue, componentId, propertyId); cee.Database.ExecuteSqlCommand(sqlUpdate); //log.Verbose(sqlUpdate); //log.Info("Component '{0}' TuningProperty '{1}' = {2} Imported into PCS VALUE and VALUE (reason: VALUE was null)", // componentName, propertyName, propertyValue); processedItems.Add(String.Format("Component '{0}' TuningProperty '{1}' = {2} Imported into PCS VALUE and VALUE (reason: VALUE was null)", componentName, propertyName, propertyValue)); } else { //Update just PcsValue as Value already exist string sqlUpdate = string.Format("UPDATE control.ControlSystemTuningPropertyValue SET pcsvalue = '{0}'" + " WHERE ControlSystemComponentId = {1} " + " AND ControlSystemTuningPropertyId = {2}", propertyValue, componentId, propertyId); cee.Database.ExecuteSqlCommand(sqlUpdate); //log.Verbose(sqlUpdate); //log.Info("Component '{0}' TuningProperty '{1}' = {2} Imported into PCS VALUE", // componentName, propertyName, propertyValue); processedItems.Add(String.Format("Component '{0}' TuningProperty '{1}' = {2} Imported into PCS VALUE", componentName, propertyName, propertyValue)); } importCount++; } } } } } } } } } log.Warning("", String.Format("Ignored items ({0}) \t\t\t- Double click for more details. {1}{1}{2}", ignoredItems.Count, Environment.NewLine, string.Join(Environment.NewLine, ignoredItems.ToArray()))); log.Warning("", String.Format("Warnings ({0}) \t\t\t- Double click for more details. {1}{1}{2}", warnings.Count, Environment.NewLine, string.Join(Environment.NewLine, warnings.ToArray()))); log.Info(String.Format("Imported ({0}) \t\t\t- Double click for more details. {1}{1}{2}", processedItems.Count, Environment.NewLine, string.Join(Environment.NewLine, processedItems.ToArray()))); log.Info("*** Saving changes ... ***"); cee.SaveChanges(); } }
public PipeFluidCode SavePipeFluidCode(PipeFluidCode pipeFluidCode) { using (CmsEntities cee = new CmsEntities()) { //Check if the pipe area exist var originalPipeFluidCode = (from x in cee.PipeFluidCodes where x.Id == pipeFluidCode.Id select x).FirstOrDefault(); if (originalPipeFluidCode == null) { //Add new Pipe Class pipeFluidCode.Pipes = null; cee.PipeFluidCodes.Add(pipeFluidCode); } else { //Update existing Area originalPipeFluidCode.Name = pipeFluidCode.Name; originalPipeFluidCode.Description = pipeFluidCode.Description; originalPipeFluidCode.PhaseFluidType = pipeFluidCode.PhaseFluidType; originalPipeFluidCode.FlangeGuards = pipeFluidCode.FlangeGuards; originalPipeFluidCode.FlangeCover = pipeFluidCode.FlangeCover; originalPipeFluidCode.CatchTrays = pipeFluidCode.CatchTrays; originalPipeFluidCode.DoubleContainment = pipeFluidCode.DoubleContainment; originalPipeFluidCode.PipeMaterial = pipeFluidCode.PipeMaterial; originalPipeFluidCode.ContainmentNotes = pipeFluidCode.ContainmentNotes; } cee.SaveChanges(); } return pipeFluidCode; }