Exemplo n.º 1
0
        static void Main(string[] args)
        {
            var options = new Options();

              var extractor = Container.GetInstance<IExtractor<KitLabel>>();
              var settings = Container.GetInstance<ISettingsService>();

              var jobValidator = new JobValidator(settings);
              var columnMapValidator = new ColumnMapValidator();
              var optionsValidator = new OptionsValidator();
              var kitLabels = new List<KitLabel>();
              Job job;

              CommandLine.Parser.Default.ParseArguments(args, options);

              try {
            optionsValidator.ValidateAndThrow(options);

            extractor.Initialize(options.InputPath);
            var columnMap = extractor.GetColumnMap(settings, 1, 1);

            columnMapValidator.ValidateAndThrow(columnMap);

            kitLabels.AddRange(extractor.Extract(columnMap, 1, 2));
            job = new Job(settings.OutputHeaderString, kitLabels);

            jobValidator.ValidateAndThrow(job);
              }

              catch (ValidationException exc) {
            var messages = string.Join("; ", exc.Errors.Select(e => e.ErrorMessage));
            var messageText = string.Format("** Warning! Input data file validation failed:; {0}", messages);

            kitLabels = new List<KitLabel>
            {
              new KitLabel(69) {Attn = "Data errors detected", Sbu = "Error", KitName = messageText}
            };
              }

              catch (ArgumentOutOfRangeException exception) {
            var message = "** Warning! Excel conversion errors occurred:; ";
            var lookup = new Dictionary<string, string>
            {
              {"sheetIndex", "Unable to find a worksheet from which to extract data"},
              {"startRowIndex", "No kit label rows are present in the data file"}
            };

            string errorText;

            if (lookup.TryGetValue(exception.ParamName, out errorText)) {
              message += errorText;
            }

            kitLabels = new List<KitLabel>
            {
              new KitLabel(69) {Attn = "Range error detected", Sbu = "Error",  KitName = message}
            };
              }

              catch (Exception exc) {
            var message = string.Format("** Warning! Excel conversion errors occurred:; {0}", exc.Message);
            kitLabels = new List<KitLabel>
            {
              new KitLabel(70) {Attn = "Unknown Error detected", Sbu = "Error",  KitName = message}
            };
              }

              finally {
            job = new Job(settings.OutputHeaderString, kitLabels);
            File.WriteAllLines(options.OutputPath, job.GetOutputList(), Encoding.Unicode);
              }
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            var options = new Options();
              var extractor = Container.GetInstance<IExtractor<RenameRow>>();
              var settings = Container.GetInstance<ISettingsService>();

              var optionsValidator = new OptionsValidator();
              var columnMapValidator = new ColumnMapValidator(settings);
              var handlerValidator = new ImageHandlerValidator();
              var existingFiles = new List<FileInfo>();
              var messages = new List<string>();

              Parser.Default.ParseArguments(args, options);

              try {
            optionsValidator.ValidateAndThrow(options);
            var sourceDir = new DirectoryInfo(options.InputPath);

            existingFiles.AddRange(sourceDir.EnumerateFiles("*.*", SearchOption.AllDirectories));

            // Throws InvalidOperationException if not found
            var excelFile = existingFiles.Single(f => Regex.IsMatch(f.Extension, @"\.xlsx?"));
            existingFiles.Remove(excelFile);

            extractor.Initialize(excelFile.FullName);
            var columnMap = extractor.GetColumnMap(settings, settings.SheetIndex, settings.HeaderRowIndex);
            columnMapValidator.ValidateAndThrow(columnMap);
            var rows = extractor.Extract(columnMap, settings.SheetIndex, settings.StartRowIndex);

            var handler = new ImageHandler(options.InputPath, options.OutputPath);
            handler.AddRenameRowRange(rows);
            handler.AddArtFileRange(existingFiles.Select(f => new ArtFile(f)));

            var result = handlerValidator.Validate(handler);
            messages.AddRange(result.Errors.Select(e => e.ErrorMessage).OrderBy(e => e));

            handler.CopyValidFiles();
            handler.Cleanup();
              }

              catch (ValidationException exc) {
            messages.AddRange(exc.Errors.Select(f => f.ErrorMessage));
              }

              catch (FlexCelExtractionException exc) {
            messages.Add(exc.Message);
              }

              catch (InvalidOperationException) {
            messages.Add("Unable to find an Excel file for this group.");
              }

              catch (KeyNotFoundException exc) {
            messages.Add(exc.Message);
              }

              catch (Exception exc) {
            messages.Add(exc.Message);
              }

              finally {
            var outputFilename = Path.Combine(options.OutputPath, settings.OutputReportFileName);
            File.WriteAllLines(outputFilename, messages.OrderBy(p => p));
              }
        }