Ejemplo n.º 1
0
        //public ImportResult ImportProcess<TWorksheetItem, TItem, TRoot>(
        //    IFileImportModel model,
        //    IEnumerable<ImportPropertyDescriptor<TWorksheetItem>> descriptors,
        //    Func<IEnumerable<TWorksheetItem>, IEnumerable<TItem>> itemsTransform,
        //    IImportDataSaver<TRoot, TItem> dataSaver) where TWorksheetItem : new()
        //{
        //    if (!File.Exists(model.SavedFileName))
        //    {
        //        return new ImportResult
        //        {
        //            OperationResult = ImportOperationResult.ErrorOnUploadStage,
        //            Message = "File was deleted. Please, reupload it.",
        //        };
        //    }

        //    //SetUserMappedColumns(descriptors, Request);

        //    // TRICK: force set mapping AUTO
        //    //model.FileColumns = ImportUtility.GetFileHeaders(model.SavedFileName);
        //    //FillModelMappedColumns(model, descriptors);

        //    //if (!ValidateMappedRequiredColumns(descriptors))
        //    if (model.NotAutoMappedColumns.Any(c => c.Requred))
        //    {
        //        IEnumerable<string> notMappedRequiredColumns = model.NotAutoMappedColumns
        //            .Where(c => c.Requred)
        //            .Select(c => "'" + c.DisplayName + "'");

        //        return new ImportResult
        //        {
        //            OperationResult = ImportOperationResult.ErrorOnImportStage,
        //            Message = string.Format("Required columns ({0}) not found. Please verify that the Microsoft Excel file to be imported meets the guidelines.",
        //                string.Join(", ", notMappedRequiredColumns)),
        //        };
        //    }

        //    int importedCount;
        //    int notImportedCount;

        //    // NOTE: parse only mapped columns
        //    IEnumerable<TItem> batchItems = ImportUtility.GetWorksheetData(
        //        model.SavedFileName,
        //        descriptors.Where(d => d.ExcelFileColumnIndex != NotMarkedExcelFileColumnIndex),
        //        itemsTransform,
        //        out importedCount, out notImportedCount);

        //    if (batchItems.Any())
        //    {
        //        //using (var tran = new TransactionScope())
        //        //{
        //        //notInsertedItems = (IEnumerable<int>)dataSaver.InsertItems(batch, batchItems);

        //        int reallyInserted = dataSaver.InsertItems(dataSaver.GetRoot(), batchItems);

        //        //tran.Complete();
        //        //}

        //        return new ImportResult
        //        {
        //            OperationResult = ImportOperationResult.CompletedSuccessfully,
        //            Message = string.Format("Successfuly imported: '{0}' records, not imported: '{1}' records.",
        //            reallyInserted,
        //            importedCount + notImportedCount - reallyInserted),
        //        };
        //    }
        //    else
        //    {
        //        return new ImportResult
        //        {
        //            OperationResult = ImportOperationResult.CompletedSuccessfully,
        //            Message = "There is no data to upload. Please verify that the Microsoft Excel file to be imported meets the guidelines.",
        //        };
        //        // TODO:UserMessages.AddWarningMessage(TempData,
        //        //"There is no data to upload. Please verify that the Microsoft Excel file to be imported meets the guidelines.");
        //    }
        //}

        public static void InitImportModel(
            IFileImportModel emptyImport,
            string filePath,
            int worksheetIndex,
            IEnumerable <IImportPropertyDescriptor> descriptors)
        {
            //Check.Argument.IsNotEmpty(savedFilePath, "savedFilePath");
            //Check.Argument.IsNotNull(descriptors, "descriptors");

            var importModel = emptyImport;

            importModel.FileColumns = ImportUtility.GetImportedFileHeaders(filePath, worksheetIndex);

            AutoMapColumns(descriptors, importModel.FileColumns);
            FillModelMappedColumns(importModel, descriptors);

            // TODO:
            //importModel.ValidationRules = GetValidationRules(descriptors);
        }
Ejemplo n.º 2
0
        public static void InitImportModel(
            IFileImportModel emptyImport,
            string savedFilePath,
            IEnumerable <IImportPropertyDescriptor> descriptors)
        {
            //Check.Argument.IsNotEmpty(savedFilePath, "savedFilePath");
            //Check.Argument.IsNotNull(descriptors, "descriptors");

            var importModel = emptyImport;

            importModel.SavedFileName = savedFilePath;
            //importModel.OriginFileName = Path.GetFileName(postedFile.FileName);
            importModel.FileColumns = ImportUtility.GetImportedFileHeaders(savedFilePath);

            AutoMapColumns(descriptors, importModel.FileColumns);
            FillModelMappedColumns(importModel, descriptors);

            // TODO:
            //importModel.ValidationRules = GetValidationRules(descriptors);
        }