private void ImportMechanicalEquipment_DoWork(object sender, DoWorkEventArgs e) { CmsEquipmentEntities cmsEquipmentEntities = new CmsEquipmentEntities(); var mechanicalEquipments = (from x in cmsEquipmentEntities.MechanicalEquipments select x).ToList(); foreach (var mechanicalEquipment in mechanicalEquipments) { //B-313-DRY-001 AddMessage(MessageType.Added, String.Format("Processing Mechanical Equipment Id '{0}' Name '{1}' .", mechanicalEquipment.Id, mechanicalEquipment.Name)); var array = mechanicalEquipment.Name.Split('-'); if (array.Length > 1) { int areaNumber = -1; if (int.TryParse(array[1], out areaNumber)) { var area = (from x in cmsEquipmentEntities.Areas where x.AreaNumber == areaNumber select x).FirstOrDefault(); if (area == null) { AddMessage(MessageType.Warning, String.Format("Could not find Assigned Area name '{0}' from Mechanical Equipment Id '{1}' Name '{2}'.", areaNumber, mechanicalEquipment.Id, mechanicalEquipment.Name)); AddMessage(MessageType.Added, String.Format("Addign new Area name '{0}'.", areaNumber)); cmsEquipmentEntities.Areas.AddObject(new Area { Name = areaNumber.ToString(), AreaNumber = areaNumber }); cmsEquipmentEntities.SaveChanges(); area = (from x in cmsEquipmentEntities.Areas where x.AreaNumber == areaNumber select x).FirstOrDefault(); } if (area != null) { mechanicalEquipment.AreaId = area.Id; AddMessage(MessageType.Added, String.Format("Assigned Area Id '{0}' Name '{1}' to Mechanical Equipment Id '{2}' Name '{3}' .", area.Id, area.Name, mechanicalEquipment.Id, mechanicalEquipment.Name)); } else { AddMessage(MessageType.Warning, String.Format("Wrong name of Mechanical Equipment Id '{0}' Name '{1}'. Adding Unknow Area.", mechanicalEquipment.Id, mechanicalEquipment.Name)); mechanicalEquipment.AreaId = ((from x in cmsEquipmentEntities.Areas where x.Name == "Unknown" select x).FirstOrDefault()).Id; } } else { AddMessage(MessageType.Warning, String.Format("Could not recognise Assigned Area name '{0}' from Mechanical Equipment Id '{1}' Name '{2}'. Adding Unknow Area.", areaNumber, mechanicalEquipment.Id, mechanicalEquipment.Name)); mechanicalEquipment.AreaId = ((from x in cmsEquipmentEntities.Areas where x.Name == "Unknown" select x).FirstOrDefault()).Id; } } else { AddMessage(MessageType.Warning, String.Format("Wrong name of Mechanical Equipment Id '{0}' Name '{1}'. Adding Unknow Area.", mechanicalEquipment.Id, mechanicalEquipment.Name)); mechanicalEquipment.AreaId = ((from x in cmsEquipmentEntities.Areas where x.Name == "Unknown" select x).FirstOrDefault()).Id; } } cmsEquipmentEntities.SaveChanges(); }
private void btnCleanUpDupDocVersions_Click(object sender, EventArgs e) { AddMessage(MessageType.Info, "Starting"); try { CmsEquipmentEntities cmsEquipmentEntities = new CmsEquipmentEntities(); List<DocumentVersion> docVersionsToDelete = new List<DocumentVersion>(); List<string> foldersToDelete = new List<string>(); List<DocumentVersion> documentVersions = (from x in cmsEquipmentEntities.DocumentVersions select x).ToList(); foreach (DocumentVersion documentVersion in documentVersions) { List<DocumentVersion> dups = (from x in documentVersions where x.Version == documentVersion.Version && x.DocumentId == documentVersion.DocumentId select x).ToList(); if (dups.Count > 1) { //we have dups //leave one behind List<DocumentVersion> versions = (from x in dups orderby x.Id descending select x).Skip(1).ToList(); List<string> folders = (from x in dups orderby x.Id descending select x.Path).Skip(1).ToList(); if (versions.Count > 0) { //still have dups foreach (DocumentVersion version in versions) { int count = (from x in docVersionsToDelete where x.Id == version.Id select x.Id).Count(); if (count==0) { //not been added AddMessage(MessageType.Info, string.Format("Doc ID :{0}, dups count: {1}", documentVersion.DocumentId, versions.Count)); docVersionsToDelete.Add(version); foldersToDelete.AddRange(folders); } } } } } AddMessage(MessageType.Info, "doc version to delete = " + docVersionsToDelete.Count); foreach (DocumentVersion version in docVersionsToDelete) { cmsEquipmentEntities.DeleteObject(version); } cmsEquipmentEntities.SaveChanges(SaveOptions.None); foreach (string s in foldersToDelete) { string configPath = @"D:\cmsRoot\CMS Document Versions\"; string path = Path.Combine(configPath, s); if (Directory.Exists(path)) { Directory.Delete(path, true); } } AddMessage(MessageType.Info, "Finished"); } catch (Exception ex) { AddMessage(MessageType.Error, ex.ToString()); } }