示例#1
0
        private async Task <Dictionary <string, string> > RestoreDoorNotesAndGetMapAsync(ProjectArchive projectArchive, string newProjectId, RestoreIdMaps idMaps, string userId)
        {
            var items = projectArchive.DoorNotes;

            if (items != null)
            {
                foreach (var item in items)
                {
                    if (idMaps.Templates.ContainsKey(item.TemplateId))
                    {
                        item.TemplateId = idMaps.Templates[item.TemplateId];
                    }
                    if (idMaps.Workorders.ContainsKey(item.WorkorderId))
                    {
                        item.WorkorderId = idMaps.Workorders[item.WorkorderId];
                    }
                    if (idMaps.WorkorderDoors.ContainsKey(item.DoorId))
                    {
                        item.DoorId = idMaps.WorkorderDoors[item.DoorId];
                    }
                    item.ProjectId = newProjectId;
                }
                return(await ObjectLocator.DoorNotesService.CreateBulkAsync(items));
            }

            return(null);
        }
示例#2
0
        private async Task <Dictionary <string, string> > RestoreHardwareCollectionsAndGetMapAsync(ProjectArchive projectArchive, string newProjectId, RestoreIdMaps idMaps, string userId)
        {
            var items = projectArchive.HardwareCollections;

            if (items != null)
            {
                foreach (var item in items)
                {
                    if (idMaps.Templates.ContainsKey(item.TemplateId))
                    {
                        item.TemplateId = idMaps.Templates[item.TemplateId];
                    }
                    if (item.ChecklistId != null && idMaps.Checklists.ContainsKey(item.ChecklistId))
                    {
                        item.ChecklistId = idMaps.Checklists[item.ChecklistId];
                    }
                }
                return(await ObjectLocator.WorkorderTemplateHardwareCollectionService.CreateBulkAsync(newProjectId, items));
            }

            return(null);
        }
示例#3
0
        private async Task <Dictionary <string, string> > RestoreTemplatesAndGetMapAsync(ProjectArchive projectArchive, string newProjectId, RestoreIdMaps idMaps, string userId)
        {
            var items = projectArchive.Templates;

            if (items != null)
            {
                try
                {
                    foreach (var item in items)
                    {
                        if (idMaps.Companies.ContainsKey(item.CompanyId))
                        {
                            item.CompanyId = idMaps.Companies[item.CompanyId];
                        }
                    }

                    return(await ObjectLocator.WorkorderTemplateService.CreateBulkAsync(newProjectId, items));
                }
                catch (Exception e)
                {
                    return(null);
                }
            }

            return(null);
        }
示例#4
0
        private async Task <Dictionary <string, string> > RestoreTemplateDoorsAndGetMapAsync(ProjectArchive projectArchive, string newProjectId, RestoreIdMaps idMaps, string userId)
        {
            var items = projectArchive.TemplateDoors;

            if (items != null)
            {
                var _service = ObjectLocator.WorkorderTemplateDoorService;

                _service.SetHardwareOwnership(items, projectArchive.HardwareCollections);

                foreach (var item in items)
                {
                    if (idMaps.Templates.ContainsKey(item.TemplateId))
                    {
                        item.TemplateId = idMaps.Templates[item.TemplateId];
                    }
                    if (idMaps.Doors.ContainsKey(item.Door.Id))
                    {
                        item.Door.Id = idMaps.Doors[item.Door.Id];
                    }
                }

                return(await _service.CreateBulkAsync(newProjectId, items));
            }

            return(null);
        }
示例#5
0
        private async Task RestoreEmailLog(ProjectArchive projectArchive, string newProjectId, RestoreIdMaps idMaps, string userId)
        {
            var items = projectArchive.EmailLogs;

            if (items != null)
            {
                await ObjectLocator.EmailLogService.CreateBulkAsync(newProjectId, items);
            }
        }
示例#6
0
        private async Task <Dictionary <string, string> > RestoreChecklistsAndGetMap(ProjectArchive projectArchive, string newProjectId, RestoreIdMaps idMaps, string userId)
        {
            var items = projectArchive.Checklists;

            if (items != null)
            {
                foreach (var item in items)
                {
                    item.ProjectId = newProjectId;
                }
                return(await ObjectLocator.ChecklistService.CreateBulk(newProjectId, items));
            }

            return(null);
        }
示例#7
0
        private async Task <Dictionary <string, string> > RestoreDoorsAndGetMap(ProjectArchive projectArchive, string newProjectId, RestoreIdMaps idMaps, string userId)
        {
            var doors = projectArchive.Doors;

            if (doors != null)
            {
                foreach (var door in doors)
                {
                    door.ProjectId = newProjectId;
                }
                return(await ObjectLocator.DoorService.CreateDoorsBulk(newProjectId, doors));
            }

            return(null);
        }
示例#8
0
        public async Task <bool> RestoreArchive(string contentType, Stream fileContent, string userId)
        {
            var projectArchive = new ProjectArchive();

            MemoryStream memoryStream = new MemoryStream();

            byte[] data = new byte[fileContent.Length];
            int    bytesRead;

            while ((bytesRead = fileContent.Read(data, 0, data.Length)) > 0)
            {
                memoryStream.Write(data, 0, bytesRead);
            }

            using (ZipArchive zip = new ZipArchive(memoryStream, ZipArchiveMode.Read, true))
            {
                var entries  = zip.Entries;
                var dataFile = entries[0];

                using (var stream = dataFile.Open())
                {
                    var streamReader = new StreamReader(stream);

                    var json = streamReader.ReadToEnd();

                    projectArchive = JsonConvert.DeserializeObject <ProjectArchive>(json);

                    if (projectArchive != null)
                    {
                        string newProjectId = await RestoreProject(projectArchive, userId);

                        if (!string.IsNullOrEmpty(newProjectId))
                        {
                            var idMaps = new RestoreIdMaps();

                            try
                            {
                                idMaps.Companies = await RestoreCompaniesAndGetMap(projectArchive, newProjectId, userId);

                                idMaps.Doors = await RestoreDoorsAndGetMap(projectArchive, newProjectId, idMaps, userId);

                                idMaps.Checklists = await RestoreChecklistsAndGetMap(projectArchive, newProjectId, idMaps, userId);

                                await RestoreSyncLog(projectArchive, newProjectId, idMaps, userId);
                                await RestoreEmailLog(projectArchive, newProjectId, idMaps, userId);

                                idMaps.Templates = await RestoreTemplatesAndGetMapAsync(projectArchive, newProjectId, idMaps, userId);

                                idMaps.HardwareCollections = await RestoreHardwareCollectionsAndGetMapAsync(projectArchive, newProjectId, idMaps, userId);

                                idMaps.TemplateDoors = await RestoreTemplateDoorsAndGetMapAsync(projectArchive, newProjectId, idMaps, userId);

                                idMaps.Workorders = await RestoreWorkordersAndGetMapAsync(projectArchive, newProjectId, idMaps, userId);

                                idMaps.WorkorderDoors = await RestoreWorkorderDoorsAndGetMapAsync(projectArchive, newProjectId, idMaps, userId);

                                idMaps.DoorNotes = await RestoreDoorNotesAndGetMapAsync(projectArchive, newProjectId, idMaps, userId);

                                streamReader.Dispose();
                                return(true);
                            }
                            catch (Exception ex)
                            {
                                if (idMaps.Companies != null)
                                {
                                    await ObjectLocator.CompanyService.DeleteBulk(idMaps.Companies.Values.ToArray(), newProjectId);
                                }
                                if (idMaps.Doors != null)
                                {
                                    await ObjectLocator.DoorService.DeleteBulk(newProjectId);
                                }
                                if (idMaps.Checklists != null)
                                {
                                    await ObjectLocator.ChecklistService.DeleteBulk(idMaps.Checklists.Values.ToArray());
                                }

                                if (idMaps.Templates != null)
                                {
                                    await ObjectLocator.WorkorderTemplateService.DeleteBulk(idMaps.Templates.Values.ToArray());
                                }
                                if (idMaps.HardwareCollections != null)
                                {
                                    await ObjectLocator.WorkorderTemplateHardwareCollectionService.DeleteBulk(idMaps.HardwareCollections.Values.ToArray());
                                }
                                if (idMaps.TemplateDoors != null)
                                {
                                    await ObjectLocator.WorkorderTemplateDoorService.DeleteBulk(idMaps.TemplateDoors.Values.ToArray());
                                }

                                if (idMaps.Workorders != null)
                                {
                                    await ObjectLocator.WorkorderService.DeleteBulk(idMaps.Workorders.Values.ToArray());
                                }
                                if (idMaps.DoorNotes != null)
                                {
                                    await ObjectLocator.DoorNotesService.DeleteBulk(idMaps.WorkorderDoors.Values.ToArray());
                                }

                                streamReader.Dispose();
                                return(false);
                            }
                        }
                    }

                    return(false);
                }
            }
        }