Example #1
0
        private static void Download(Inspection inspection)
        {
            var newInspection  = inspection.GetCleanModel();
            var inspectionTask = inspection.Task;

            // Checklist
            var checklist = new Checklist
            {
                ID              = inspectionTask.Checklist.ID,
                Name            = inspectionTask.Checklist.Name,
                DateTimeCreated = inspectionTask.Checklist.DateTimeCreated,
                Remarks         = inspectionTask.Checklist.Remarks,
                Hash            = inspectionTask.Checklist.Hash
            };

            checklist.Inspections.Add(newInspection);

            OfflineDatabase.Checklists.Add(checklist);

            // ParkingLot
            var parkingLot = inspectionTask.ParkingLot.GetCleanModel();

            parkingLot.Address = OfflineDatabase.Addresses.FirstOrDefault(x => x.Hash == inspectionTask.ParkingLot.Address.Hash);

            OfflineDatabase.ParkingLots.Add(parkingLot);

            // ChecklistQuestions
            inspection.Checklist.ChecklistQuestions.ToList().ForEach(x =>
            {
                var question          = x.Question.GetCleanModel();
                question.QuestionType = OfflineDatabase.QuestionTypes.FirstOrDefault(a => a.Name == x.Question.QuestionType.Name);

                // AnswerSetValues
                x.Question.AnswerSetValues.ToList().ForEach(b =>
                {
                    var asv = OfflineDatabase.AnswerSetValues.FirstOrDefault(k => k.Value == b.Value) ?? new AnswerSetValue {
                        Value = b.Value
                    };

                    question.AnswerSetValues.Add(asv);
                });

                checklist.ChecklistQuestions.Add(new ChecklistQuestion {
                    Question = question
                });
            });

            // InspectionInspector
            inspection.InspectionInspectors.ToList().ForEach(x =>
            {
                newInspection.InspectionInspectors.Add(new InspectionInspector
                {
                    Employee    = OfflineDatabase.Employees.FirstOrDefault(b => b.Hash == x.Employee.Hash),
                    Remarks     = x.Remarks,
                    LastUpdated = x.LastUpdated
                });
            });

            // Task
            var task = inspectionTask.GetCleanModel();

            task.ParkingLot = parkingLot;
            task.Checklist  = checklist;
            task.Inspections.Add(newInspection);
            task.Customer = OfflineDatabase.Customers.FirstOrDefault(x => x.Hash == inspectionTask.Customer.Hash);

            OfflineDatabase.Tasks.Add(task);
            OfflineDatabase.SaveChanges();
        }