/// <summary> /// Shallow copy. /// </summary> public Remark ConvertFromArchived(ArchivedProtocolRemark aRemark) { var remark = new Remark(); remark.Text = aRemark.Remark; return(remark); }
public ActionResponse ArchiveDiary(Guid diaryId) { var res = new ActionResponse(); try { var diaryW = Db.Diaries.Where(x => x.Id == diaryId).Select(DiaryMappings.ToDiaryW).FirstOrDefault(); var archivedDiary = new ArchivedDiary(); archivedDiary.Id = Guid.NewGuid(); archivedDiary.Number = diaryW.Number; archivedDiary.AcceptanceDateAndTime = diaryW.AcceptanceDateAndTime; archivedDiary.LetterNumber = diaryW.LetterNumber?.ToString(); archivedDiary.LetterDate = diaryW.LetterDate; archivedDiary.Contractor = diaryW.Contractor; archivedDiary.Client = diaryW.Client.Name; archivedDiary.ClientMobile = diaryW.Client.Mobile; archivedDiary.Comment = diaryW.Comment; var meta = Db.AcreditationMetas.First(); archivedDiary.AcreditationRegisteredDate = meta.Registered; archivedDiary.AcreditationValidDate = meta.ValidTo; var request = diaryW.Request; archivedDiary.RequestDate = request.Date; archivedDiary.RequestAcceptedBy = request.User.FirstName.Substring(0, 1) + ". " + request.User.LastName; archivedDiary.RequestTestingPeriod = request.TestingPeriod; archivedDiary.Remark = diaryW.Remark; var protocol = request.Protocols.First(); archivedDiary.ProtocolIssuedDate = protocol.IssuedDate; archivedDiary.ProtocolTester = protocol.Tester; archivedDiary.ProtocolLabLeader = protocol.LabLeader; foreach (var remark in protocol.ProtocolsRemarks) { var aremark = new ArchivedProtocolRemark() { Id = Guid.NewGuid(), ArchivedDiaryId = archivedDiary.Id, Remark = remark.Remark.Text, AcredetationLevel = remark.AcredetationLevel.Level, Number = remark.Number }; Db.ArchivedProtocolRemarks.Add(aremark); } Db.ProtocolsRemarks.RemoveRange(protocol.ProtocolsRemarks); var products = diaryW.Products; foreach (var product in products) { var aproduct = new ArchivedProduct() { Id = Guid.NewGuid(), Name = product.Name, Number = product.Number, Quantity = product.Quantity }; var productTests = product.ProductTests; foreach (var ptest in productTests) { var aptest = new ArchivedProductTest() { Id = Guid.NewGuid(), TestName = ptest.Test.Name, TestUnitName = ptest.Test.UnitName, TestMethods = ptest.TestMethod.Method, TestAcredetationLevel = ptest.Test.AcredetationLevel.Level, TestTemperature = ptest.Test.Temperature, TestCategory = ptest.Test.TestCategory.Name, TestType = ptest.Test.TestType.Type, TestTypeShortName = ptest.Test.TestType.ShortName, MethodValue = ptest.MethodValue, Remark = ptest.Remark }; var protocolResults = ptest.ProtocolResults; foreach (var presult in protocolResults) { var apresult = new ArchivedProtocolResult() { Id = Guid.NewGuid(), Results = presult.Results, ResultNumber = presult.ResultNumber, ArchivedDiaryId = archivedDiary.Id }; aptest.ArchivedProtocolResults.Add(apresult); } Db.ProtocolResults.RemoveRange(ptest.ProtocolResults); aproduct.ArchivedProductTests.Add(aptest); } Db.ProductTests.RemoveRange(product.ProductTests); archivedDiary.ArchivedProducts.Add(aproduct); } Db.Products.RemoveRange(diaryW.Products); Db.ArchivedDiaries.Add(archivedDiary); Db.Protocols.RemoveRange(request.Protocols); Db.Requests.Remove(request); var diary = Db.Diaries.FirstOrDefault(x => x.Id == diaryW.Id); Db.Diaries.Remove(diary); Db.SaveChanges(); res.IsSuccess = true; res.ResponseObject = archivedDiary.Id; res.SuccessMsg = "Архивирането на дневник: " + diaryW.Number + " премина успешно."; } catch (DbEntityValidationException sqlEx) { foreach (var validationErrors in sqlEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage); } } } catch (Exception exc) { Elmah.ErrorSignal.FromCurrentContext().Raise(exc); res.Error = ErrorFactory.UnableToArchiveDiary; } return(res); }