예제 #1
0
 public virtual bool IsValidJsonData(JsonImporterConfiguration json, out string message)
 {
     if (json == null)
     {
         message = "Json configuration cannot be empty";
         return(false);
     }
     if (string.IsNullOrEmpty(json?.ExcelReader?.Type))
     {
         message = "Excel reader cannot be empty";
         return(false);
     }
     if (string.IsNullOrEmpty(json?.ContentRoot?.Value))
     {
         message = "Content Root cannot be empty";
         return(false);
     }
     if (string.IsNullOrEmpty(json?.RowToContentIndicator?.Type))
     {
         message = "Row To Content Indicator cannot be empty";
         return(false);
     }
     if (json?.ColumnMappings == null || !json.ColumnMappings.Any())
     {
         message = "Column Mappings cannot be empty";
         return(false);
     }
     if (string.IsNullOrEmpty(json?.TargetPageType))
     {
         message = "Target Page Type cannot be empty";
         return(false);
     }
     message = string.Empty;
     return(true);
 }
예제 #2
0
        public virtual ExcelImportResult Import(string jsonContent, ExcelWorkbook workbook)
        {
            JsonImporterConfiguration jsonConfiguration = new JsonImporterConfiguration();

            _configTranslator.FillDefaultValue(jsonContent, out jsonConfiguration);
            return(Import(jsonConfiguration, workbook));
        }
예제 #3
0
        /// <summary>
        /// Public interface for the core DoImport function
        /// </summary>
        /// <param name="jsonConfiguration"></param>
        /// <param name="workbook"></param>
        /// <returns></returns>
        public virtual ExcelImportResult Import(JsonImporterConfiguration jsonConfiguration, ExcelWorkbook workbook)
        {
            ExcelImportResult          importResult = new ExcelImportResult();
            ExcelImporterConfiguration importerConfiguration;
            var configTranslationResult = _configTranslator.Translate(jsonConfiguration);

            if (!configTranslationResult.IsSuccess)
            {
                _logger.Error(configTranslationResult.Message);
                importResult.AddMessage("Import Configuration Invalid: " + configTranslationResult.Message);
                return(importResult);
            }
            importerConfiguration = configTranslationResult.Configuration;
            return(DoImport(workbook, importerConfiguration));
        }
예제 #4
0
        public void FillDefaultValue(string input, out JsonImporterConfiguration json)
        {
            JsonImporterConfiguration rawData = JsonConvert.DeserializeObject <JsonImporterConfiguration>(input);

            if (rawData.ExcelReader == null)
            {
                rawData.ExcelReader = new JsonLibraryClass(typeof(DefaultExcelReader));
            }
            if (rawData.ContentRoot != null)
            {
                if (string.IsNullOrEmpty(rawData.ContentRoot.ContentRootType))
                {
                    rawData.ContentRoot.ContentRootType = ContentRootTypes.IDS;
                }
                if (string.IsNullOrEmpty(rawData.ContentRoot.GetChildren))
                {
                    rawData.ContentRoot.GetChildren = "DefaultContentChildrenLoader";
                }
            }
            if (rawData.ColumnMappings == null)
            {
                rawData.ColumnMappings = new JsonColumnMapping[0];
            }
            if (rawData.DefaultValueMappings == null)
            {
                rawData.DefaultValueMappings = new JsonColumnMapping[0];
            }
            if (rawData.ExcelValidators == null)
            {
                rawData.ExcelValidators = new JsonLibraryClass[0];
            }
            if (rawData.RowStart == null)
            {
                rawData.RowStart = 2;
            }
            json = rawData;
        }
예제 #5
0
        public virtual ExcelImporterTranslationResult Translate(JsonImporterConfiguration json)
        {
            var result = new ExcelImporterTranslationResult();

            result.IsSuccess = true;
            string validationMsg = string.Empty;

            if (!IsValidJsonData(json, out validationMsg))
            {
                result.IsSuccess = false;
                result.Message   = validationMsg;
                return(result);
            }
            var excelReader = LoadInstance <IExcelReader>(json.ExcelReader.Type);

            if (excelReader == null)
            {
                result.IsSuccess = false;
                result.Message   = "Invalid Excel Reader";
                return(result);
            }
            var excelValidators = new List <IExcelValidator>();

            foreach (var validatorType in json.ExcelValidators)
            {
                var validator = LoadInstance <IExcelValidator>(validatorType.Type);
                if (validator == null)
                {
                    result.IsSuccess = false;
                    result.Message   = "Invalid Excel Validator";
                    return(result);
                }
                excelValidators.Add(validator);
            }
            var rowToContentIndicator = LoadInstance <IRowToContentIndicator>(json.RowToContentIndicator.Type);

            if (rowToContentIndicator == null)
            {
                result.IsSuccess = false;
                result.Message   = "Invalid row to content indicator";
                return(result);
            }
            var contentRoots = TranslateContentRoots(json.ContentRoot);

            if (contentRoots == null || !contentRoots.ContentRoots.Any())
            {
                result.IsSuccess = false;
                result.Message   = "Content roots cannot be null or empty";
                return(result);
            }
            var targetPageType = LoadType(json.TargetPageType);
            var customParsers  = GetColumnDataParsers();
            var columnMappings = new List <ExcelColumnMapping>();

            foreach (var columnMapping in json.ColumnMappings)
            {
                IColumnDataParser parser = null;
                if (columnMapping.ParserType != null)
                {
                    customParsers.TryGetValue(columnMapping.ParserType, out parser);
                }
                columnMappings.Add(new ExcelColumnMapping()
                {
                    ExcelColumnNumber = columnMapping.ExcelColumnNumber,
                    Parser            = parser,
                    PropertyIndicator = columnMapping.PropertyIndicator,
                    DefaultValue      = columnMapping.DefaultValue,
                    UseDefaultValue   = columnMapping.UseDefaultValue,
                    IgnoreIfNull      = columnMapping.IgnoreIfNull,
                    Configuration     = result.Configuration
                });
            }
            var defaultValueMappings = new List <ExcelDefaultValueMapping>();

            foreach (var defaultValueMapping in json.DefaultValueMappings)
            {
                IColumnDataParser parser = null;
                if (defaultValueMapping.ParserType != null)
                {
                    customParsers.TryGetValue(defaultValueMapping.ParserType, out parser);
                }
                defaultValueMappings.Add(new ExcelDefaultValueMapping()
                {
                    Parser            = parser,
                    PropertyIndicator = defaultValueMapping.PropertyIndicator,
                    DefaultValue      = defaultValueMapping.DefaultValue,
                    UseDefaultValue   = defaultValueMapping.UseDefaultValue,
                    IgnoreIfNull      = defaultValueMapping.IgnoreIfNull,
                    Configuration     = result.Configuration
                });
            }
            result.Configuration.ColumnMappings       = columnMappings;
            result.Configuration.ContentRoots         = contentRoots;
            result.Configuration.ExcelReader          = excelReader;
            result.Configuration.ExcelValidators      = excelValidators;
            result.Configuration.ExtraConfigurations  = json.ExtraConfigurations;
            result.Configuration.RowIndicators        = rowToContentIndicator;
            result.Configuration.DefaultValueMappings = defaultValueMappings;
            result.Configuration.TargetPageType       = targetPageType;
            result.Configuration.RowStart             = json.RowStart.Value;
            result.Configuration.SaveWithoutPublish   = json.SaveWithoutPublish;
            return(result);
        }