Exemple #1
0
 public TestsReferenceW(ArchivedProductTest test)
 {
     this.TestName        = test.TestName;
     this.ProductName     = test.ArchivedProduct.Name;
     this.ProductQuantity = test.ArchivedProduct.Quantity;
     this.ClientName      = test.ArchivedProduct.ArchivedDiary.Client;
 }
        public ArchivedProductTest ToBase()
        {
            var atest = new ArchivedProductTest();

            atest.Id = this.Id;
            atest.ArchivedProductId     = this.ArchivedProductId;
            atest.TestName              = this.TestName;
            atest.TestUnitName          = this.TestUnitName;
            atest.TestMethods           = this.TestMethods;
            atest.TestAcredetationLevel = this.TestAcredetationLevel;
            atest.TestTemperature       = this.TestTemperature;
            atest.TestCategory          = this.TestCategory;
            atest.TestType              = this.TestType;
            atest.TestTypeShortName     = this.TestTypeShortName;
            atest.MethodValue           = this.MethodValue;
            atest.Remark = this.Remark;

            if (this.Results != null)
            {
                atest.ArchivedProtocolResults = this.Results.Select(ar => ar.ToBase()).ToList();
            }
            else
            {
                atest.ArchivedProtocolResults = new List <ArchivedProtocolResult>();
            }

            return(atest);
        }
        /// <summary>
        /// Shallow copy.
        /// </summary>
        public ProductTest ConvertFromArchived(ArchivedProductTest aproductTest)
        {
            var productTest = new ProductTest();

            productTest.Test          = new Test();
            productTest.Test.Name     = aproductTest.TestName;
            productTest.Test.UnitName = aproductTest.TestUnitName;
            productTest.TestMethod    = new TestMethod()
            {
                Id = Guid.NewGuid(), Method = aproductTest.TestMethods
            };
            productTest.Test.AcredetationLevel       = new AcredetationLevel();
            productTest.Test.AcredetationLevel.Level = aproductTest.TestAcredetationLevel;
            productTest.Test.Temperature             = aproductTest.TestTemperature;
            productTest.Test.TestCategory            = new TestCategory();
            productTest.Test.TestCategory.Name       = aproductTest.TestCategory;
            productTest.Test.TestType = new TestType()
            {
                Id = Guid.NewGuid(), Type = aproductTest.TestType, ShortName = aproductTest.TestTypeShortName
            };

            productTest.Product          = new Product(); //not using converter because RECURSION
            productTest.Product.Number   = aproductTest.ArchivedProduct.Number;
            productTest.Product.Name     = aproductTest.ArchivedProduct.Name;
            productTest.Product.Quantity = aproductTest.ArchivedProduct.Quantity;

            return(productTest);
        }
        /// <summary>
        /// Shallow copy.
        /// </summary>
        public ArchivedProductTest ConvertToArchived(ProductTest productTest)
        {
            var archivedProductTest = new ArchivedProductTest();

            archivedProductTest.TestName              = productTest.Test.Name;
            archivedProductTest.TestUnitName          = productTest.Test.UnitName;
            archivedProductTest.TestMethods           = productTest.TestMethod.Method;
            archivedProductTest.TestAcredetationLevel = productTest.Test.AcredetationLevel.Level;
            archivedProductTest.TestTemperature       = productTest.Test.Temperature;
            archivedProductTest.TestCategory          = productTest.Test.TestCategory.Name;

            return(archivedProductTest);
        }
        public ArchivedProductTestW(ArchivedProductTest atest)
        {
            this.Id = atest.Id;
            this.ArchivedProductId     = atest.ArchivedProductId;
            this.TestName              = atest.TestName;
            this.TestUnitName          = atest.TestUnitName;
            this.TestMethods           = atest.TestMethods;
            this.TestAcredetationLevel = atest.TestAcredetationLevel;
            this.TestTemperature       = atest.TestTemperature;
            this.TestCategory          = atest.TestCategory;
            this.TestType              = atest.TestType;
            this.TestTypeShortName     = atest.TestTypeShortName;
            this.MethodValue           = atest.MethodValue;
            this.Remark = atest.Remark;

            this.Results = atest.ArchivedProtocolResults.Select(ar => new ArchivedProtocolResultW(ar));
        }
        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);
        }