Exemple #1
0
        public async Task Install(string uploadFileId, InstallWay installWay = InstallWay.Merge)
        {
            var zipFile = await _fileSeviceProvider.DownloadFileAsync(uploadFileId);

            var fileNameWithoutExt = FileUtil.GetFileNameWithoutExt(zipFile.FileName);
            var restoreFolderPath  = Path.Combine(Environment.CurrentDirectory, "Temp", "InstallationPackages");

            Directory.CreateDirectory(restoreFolderPath);
            var restoreFilePath = Path.Combine(restoreFolderPath, zipFile.FileName);

            using (var fileStream = File.Create(restoreFilePath))
            {
                fileStream.Write(zipFile.FileBytes, 0, zipFile.FileBytes.Length);
            }
            // Release file in memory
            zipFile.FileBytes = null;
            var folderExtractingPath = Path.Combine(restoreFolderPath, fileNameWithoutExt);

            if (Directory.Exists(folderExtractingPath))
            {
                Directory.Delete(folderExtractingPath, true);
            }
            ZipFile.ExtractToDirectory(restoreFilePath, folderExtractingPath);

            var jsonAppPackageFilePath = Path.Combine(folderExtractingPath, fileNameWithoutExt + ".json");

            var jsonAppPackageString = File.ReadAllText(jsonAppPackageFilePath);
            var appFlatternModel     = ConvertUtil.DeserializeObject <AppPackageFlatternModel>(jsonAppPackageString);

            var isExist = await _appRepository.IsExistAsync(a => a.Id == appFlatternModel.App.Id);

            switch (installWay)
            {
            case InstallWay.Merge:
                break;

            case InstallWay.Wipe:
                if (isExist)
                {
                    // Wipe all data
                    await _standardServiceProvider.DeleteAllByAppIdAsync(appFlatternModel.App.Id);

                    await _dynamicListServiceProvider.DeleteByAppIdAsync(appFlatternModel.App.Id);

                    await _chartServiceProvider.DeleteByAppIdAsync(appFlatternModel.App.Id);

                    await _pageServiceProvider.DeleteByAppIdAsync(appFlatternModel.App.Id);

                    await _localizationProvider.DeleteByAppIdAsync(appFlatternModel.App.Id);
                }
                break;
            }

            foreach (var chainingFile in appFlatternModel.ChainingFiles)
            {
                switch (chainingFile)
                {
                case STANDARD_FILE:
                    var standardFilePath = Path.Combine(folderExtractingPath, STANDARD_FILE);
                    var standardsString  = File.ReadAllText(standardFilePath);
                    var standardsList    = ConvertUtil.DeserializeObject <IEnumerable <StandardComponent> >(standardsString);
                    await _standardServiceProvider.ForceUpdateStandards(standardsList);

                    break;

                case DYNAMICLIST_FILE:
                    var dynamicListFilePath = Path.Combine(folderExtractingPath, DYNAMICLIST_FILE);
                    var dynamicListString   = File.ReadAllText(dynamicListFilePath);
                    var dynamicListsList    = ConvertUtil.DeserializeObject <IEnumerable <DynamicList> >(dynamicListString);
                    await _dynamicListServiceProvider.ForceUpdateDynamicLists(dynamicListsList);

                    break;

                case CHART_FILE:
                    var chartFilePath = Path.Combine(folderExtractingPath, CHART_FILE);
                    var chartsString  = File.ReadAllText(chartFilePath);
                    var chartsList    = ConvertUtil.DeserializeObject <IEnumerable <Chart> >(chartsString);
                    await _chartServiceProvider.ForceUpdateCharts(chartsList);

                    break;

                case PAGE_FILE:
                    var pageFilePath = Path.Combine(folderExtractingPath, PAGE_FILE);
                    var pagesString  = File.ReadAllText(pageFilePath);
                    var pagesList    = ConvertUtil.DeserializeObject <IEnumerable <Page> >(pagesString);
                    await _pageServiceProvider.ForceUpdatePages(pagesList);

                    break;

                case LOCALE_FILE:
                    var localeFilePath = Path.Combine(folderExtractingPath, LOCALE_FILE);
                    var localesString  = File.ReadAllText(localeFilePath);
                    var localesList    = ConvertUtil.DeserializeObject <IEnumerable <Localization> >(localesString);
                    await _localizationProvider.ForceUpdateLocalizations(localesList);

                    break;
                }
            }

            await _appRepository.ForceUpdateAsync(appFlatternModel.App.Id, appFlatternModel.App);

            Directory.Delete(folderExtractingPath, true);
            File.Delete(restoreFilePath);
        }