public IEnumerable<ResultMessageViewModel> ImportLocalFiles()
        {
            var results = new List<ResultMessageViewModel>();

            var request = HttpContext.Current.Request;

            XMLDataToImport headerFileXMLFileToImport = null;

            if (request.Files.AllKeys.Contains(HeaderFileInputElementName))
            {
                var headerFile = request.Files[HeaderFileInputElementName];
                var headerFileData = new DataFromFileSystem(headerFile.FileName, headerFile.InputStream);
                headerFileXMLFileToImport = new XMLDataToImport(headerFileData);
            }
            else
            {
                results.Add(new ResultMessageViewModel(ResultMessageViewModel.RESULT_LEVEL_WARN, "Header file is not uploaded, use default value."));
            }
            
            var sampleFile = request.Files[SampleFileInputElementName];
            var chemistryFile = request.Files[ChemistryFileInputElementName];

            var sampleFileData = new DataFromFileSystem(sampleFile.FileName, sampleFile.InputStream);
            var sampleFileCSVFileToImport = new CSVDataToImport(sampleFileData);

            var chemistryFileData = new DataFromFileSystem(chemistryFile.FileName, chemistryFile.InputStream);
            var chemistryCSVFileToImport = new CSVDataToImport(chemistryFileData);

            var esdatDataToImport = new ESDATDataToImport(headerFileXMLFileToImport, sampleFileCSVFileToImport, chemistryCSVFileToImport);
            var importer = ESDATDataImportHelper.BuildESDATDataImporter(_wqDefaultValueProvider);

            results.AddRange(PersistESDATData(esdatDataToImport, importer));

            return results;
        }
        public void ExtractESDATDataFromNoHeaderFileTest()
        {
            var mockDefaultValueProvider = new Mock<IWQDefaultValueProvider>();
            mockDefaultValueProvider.Setup(x => x.OrganizationNameSampleCollection).Returns("Default organization name from provider");

            var chemistryFileToImport = CreateCSVDataToImport(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DataFiles", "ChemistryFileExample.csv"));
            var sampleFileToImport = CreateCSVDataToImport(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DataFiles", "SampleFileExample.csv"));

            var testESDATDataToImport = new ESDATDataToImport(null, sampleFileToImport, chemistryFileToImport);
            var simpleValueAssginer = new SimpleValueAssigner();

            var sampleDataImporter = ESDATTestHelper.BuildSampleDataFileImporter();
            var sampleFileChildObjectExtractConfiguration = new SampleFileChildObjectExtractConfiguration(sampleDataImporter, "SampleFileData", simpleValueAssginer);

            var chemistryDataImporter = ESDATTestHelper.BuildChemistryFileImporter();
            var chemistryFileChildObjectExtractConfiguration = new ChemistryFileChildObjectExtractConfiguration(chemistryDataImporter, "ChemistryData", simpleValueAssginer);

            var testESDATDataImporter = new ESDATDataImporter(ResultLevel.ERROR, mockDefaultValueProvider.Object);

            AddXMLExtractConfigurationsToImporter(testESDATDataImporter);
            testESDATDataImporter.AddExtractConfiguration(sampleFileChildObjectExtractConfiguration);
            testESDATDataImporter.AddExtractConfiguration(chemistryFileChildObjectExtractConfiguration);

            var extractResult = testESDATDataImporter.Extract<ESDATModel>(testESDATDataToImport);

            Assert.NotNull(extractResult);

            var entity = extractResult.ExtractedEntities.Cast<ESDATModel>().SingleOrDefault();

            Assert.NotNull(entity);
            Assert.AreEqual("Default organization name from provider", entity.LabName);
            Assert.AreEqual(3, entity.SampleFileData.Count());
            Assert.AreEqual(9, entity.ChemistryData.Count());
        }
        public IEnumerable<ResultMessageViewModel> ImportHttpFiles(HttpFileImportDataViewModel data)
        {
            XMLDataToImport headerFileXMLFileToImport = null;

            if (!string.IsNullOrEmpty(data.HeaderFileURL))
            {
                var headerFileHttpFileSystem = new HttpFileSystem(data.HeaderFileURL);
                headerFileXMLFileToImport = new XMLDataToImport(headerFileHttpFileSystem.FetchData());
            }
            

            var sampleFileHttpFileSystem = new HttpFileSystem(data.SampleFileURL);
            var sampleFileCSVFileToImport = new CSVDataToImport(sampleFileHttpFileSystem.FetchData());

            var chemistryFileHttpFileSystem = new HttpFileSystem(data.ChemistryFileURL);
            var chemistryCSVFileToImport = new CSVDataToImport(chemistryFileHttpFileSystem.FetchData());

            var esdatDataToImport = new ESDATDataToImport(headerFileXMLFileToImport, sampleFileCSVFileToImport, chemistryCSVFileToImport);
            var importer = ESDATDataImportHelper.BuildESDATDataImporter(_wqDefaultValueProvider);

            var results = PersistESDATData(esdatDataToImport, importer);

            return results;
        }
        private IEnumerable<ResultMessageViewModel> PersistESDATData(ESDATDataToImport esdatDataToImport, IDataImporter importer)
        {
            var extractedResults = importer.Extract<ESDATModel>(esdatDataToImport);

            if (!extractedResults.IsExtractedSuccess)
            {
                var failResults = from parsingResult in ImportResultHelper.FilterWarningAndErrorResult(extractedResults.AllParsingResults)
                                  select new ResultMessageViewModel
                                  (
                                      parsingResult.Level.ToString(),
                                      parsingResult.Message
                                  );

                return failResults;
            }
            else
            {
                var esdatModel = extractedResults.ExtractedEntities.First();

                var allResults = (from parsingResult in extractedResults.AllParsingResults
                                 select new ResultMessageViewModel(parsingResult.Level.ToString(), parsingResult.Message)).ToList();

                var duplicateChecker = new ODM2DuplicateChecker(_dbContext);

                var convertResult = new List<IResult>();
                var sampleCollectionFactory = new ESDATSampleCollectionMapperFactory(duplicateChecker, _wqDefaultValueProvider, wayToHandleNewData, convertResult);


                var chemistryFactory = new ESDATChemistryMapperFactory(duplicateChecker, _wqDefaultValueProvider, wayToHandleNewData, convertResult);

                var mapper = new SampleCollectionActionMapper(duplicateChecker, sampleCollectionFactory, _wqDefaultValueProvider, chemistryFactory, wayToHandleNewData, convertResult);

                var converter = new ESDATConverter(mapper);

                var convertResults = converter.Convert(esdatModel);

                var convertResultViewModels = from converResult in convertResults
                                              select new ResultMessageViewModel(converResult.Level.ToString(), converResult.Message);

                allResults.AddRange(convertResultViewModels.ToList());

                var convertParsingResult = convertResults.Where(x => x is IParsingResult).FirstOrDefault();

                if (convertParsingResult == null)
                {
                    var failResult = new ResultMessageViewModel(ResultMessageViewModel.RESULT_LEVEL_ERROR, "System is not able to find the converted value from the converted results.");
                    allResults.Add(failResult);
                }
                else
                {
                    var action = (Action)((IParsingResult)convertParsingResult).Value;
                    _dbContext.Add<Hatfield.EnviroData.Core.Action>(action);
                    _dbContext.SaveChanges();
                    allResults.Add(new ResultMessageViewModel
                    (
                        ResultMessageViewModel.RESULT_LEVEL_INFO,
                        "Import success"
                    ));
                }
                
                return allResults;
            }
        }