Esempio n. 1
0
        private bool ProcessInstruction(OutputInstruction instruction)
        {
            var result    = false;
            var itemCount = instruction.Format == OutputFormat.Delete ? instruction.Deletions.Count : instruction.Updates.Count;
            var startTime = DateTime.Now;

            _logger.DebugFormat("Starting to process an instruction with type of {0} containing {1} entries.", instruction.Format, itemCount);
            if (instruction.Format == OutputFormat.Delete)
            {
                result = ProcessDeletionInstruction(instruction);
            }
            else
            {
                result = ProcessUpdateInstruction(instruction);
            }

            if (instruction.FileCount == 1)
            {
                _logger.InfoFormat("Completed processing of the {1} instruction with file count of {2}. Elapsed time is {0}.", (DateTime.Now - startTime).ToString(@"dd\.hh\:mm\:ss"), instruction.Format, instruction.FileCount);
            }
            else
            {
                _logger.DebugFormat("Completed processing of the {1} instruction with file count of {2}. Elapsed time is {0}.", (DateTime.Now - startTime).ToString(@"dd\.hh\:mm\:ss"), instruction.Format, instruction.FileCount);
            }

            return(result);
        }
Esempio n. 2
0
        private void ProcessAvailableInstruction(FileInfo fileInfo)
        {
            _logger.DebugFormat("Picked {0} as the file to process", fileInfo.FullName);
            try
            {
                OutputInstruction instruction = null;
                using (var reader = File.OpenText(fileInfo.FullName))
                {
                    using (var jsonStreamer = new JsonTextReader(reader))
                    {
                        JsonSerializer serializer = new JsonSerializer();
                        instruction = serializer.Deserialize <OutputInstruction>(jsonStreamer);
                    }
                }

                if (ProcessInstruction(instruction))
                {
                    RemoveFile(fileInfo);
                }
            }
            catch (Exception exception)
            {
                _hasError = true;
                _logger.ErrorFormat("Error occurred while processing {0}.", fileInfo.FullName + " - " + exception.Message + " - " + exception.StackTrace);
            }
        }
        private void OutputBatch(OutputFormat batchOutput, IEnumerable <BaseExportData> batchData, int batchCounter, string identifier, string catalogName = null)
        {
            var data    = batchData.ToList();
            var message = string.Format("Starting batch {0} {1} for identifier {2} containing {3} records.", batchOutput.ToString(), batchCounter, identifier, data.Count);

            if (batchCounter == 1)
            {
                _logger.InfoFormat(message);
            }
            else
            {
                _logger.DebugFormat(message);
            }

            var instruction = new OutputInstruction
            {
                Format         = batchOutput,
                CatalogName    = catalogName,
                Count          = batchCounter,
                OutputLocation = identifier,
                OutputName     = string.Format("{0}_{1}", batchOutput.ToString(), batchCounter),
                Data           = data
            };

            RecordOutput(instruction);
            _logger.DebugFormat("Completed batch {0}.", batchOutput.ToString());
        }
Esempio n. 4
0
        public override FileInfo FileWrite(OutputInstruction instruction)
        {
            if (instruction == null || instruction.Data == null || !instruction.Data.Any())
            {
                return(null);
            }

            var        filePath   = GeneratorHelper.GetFilePath(instruction.OutputName, instruction.OutputLocation, _fileNameFormat);
            XmlWriter  xmlWriter  = null;
            GZipStream gZipStream = null;

            if (_isGzip)
            {
                gZipStream = new GZipStream(FileCreate(filePath + ".gz"), CompressionMode.Compress);
                xmlWriter  = FileWriterCreate(gZipStream);
            }
            else
            {
                xmlWriter = FileWriterCreate(filePath);
            }

            var serializer = GetSerializer(instruction.Data.First().GetType(), string.Empty);

            using (xmlWriter)
            {
                serializer.Serialize(xmlWriter, instruction.Data);
            }

            if (gZipStream != null)
            {
                gZipStream.Close();
            }

            return(new FileInfo(filePath));
        }
        public void RecordOutput(OutputInstruction instruction)
        {
            var file = WriteOutputInstructionFile(instruction);

            if (file != null)
            {
                _files.Add(file);
            }
        }
Esempio n. 6
0
 private bool ProcessUpdateInstruction(OutputInstruction instruction)
 {
     if (!_googleRequestProcessor.SendUpdateBatch(instruction.Updates))
     {
         _logger.Info("Error sending update batch to Google. Moving on to next.");
         _hasError = true;
         return(false);
     }
     return(true);
 }
Esempio n. 7
0
        private bool ProcessDeletionInstruction(OutputInstruction instruction)
        {
            if (!_googleRequestProcessor.SendDeletionBatch(instruction.Deletions))
            {
                _logger.Info("Error sending deletion batch to Google. Moving on to next.");
                _hasError = true;
                return(false);
            }

            return(true);
        }
        private bool SendToDestination(OutputInstruction instruction)
        {
            if (!_destinationProcessor.Process(instruction))
            {
                _logger.Info("Error sending data to destination. Moving on to next.");
                _hasError = true;
                return(false);
            }

            return(true);
        }
Esempio n. 9
0
        /// <summary>
        /// Reads a json file and converts it to output instruction.
        /// </summary>
        /// <param name="fileInfo">File info.</param>
        /// <returns>Output instruction.</returns>
        public override OutputInstruction FileRead(FileInfo fileInfo)
        {
            OutputInstruction instruction = null;

            using (var reader = File.OpenText(fileInfo.FullName))
            {
                using (var jsonStreamer = new JsonTextReader(reader))
                {
                    JsonSerializer serializer = new JsonSerializer();
                    instruction = serializer.Deserialize <OutputInstruction>(jsonStreamer);
                }
            }

            return(instruction);
        }
 protected override void ProcessAvailableInstruction(FileInfo fileInfo)
 {
     _logger.DebugFormat("Picked {0} as the file to process", fileInfo.FullName);
     try
     {
         OutputInstruction instruction = _fileProcessor.FileRead(fileInfo);
         if (ProcessInstruction(instruction))
         {
             _fileProcessor.FileRemove(fileInfo);
         }
     }
     catch (Exception exception)
     {
         _hasError = true;
         _logger.ErrorFormat("Error occurred while processing {0}.", fileInfo.FullName + " - " + exception.Message + " - " + exception.StackTrace);
     }
 }
Esempio n. 11
0
        /// <summary>
        /// Writes a json file with given <paramref name="instruction"/>.
        /// </summary>
        /// <param name="instruction">Instruction with content.</param>
        /// <returns>File info.</returns>
        public override FileInfo FileWrite(OutputInstruction instruction)
        {
            var filePath = Path.Combine(instruction.OutputLocation, instruction.OutputName + ".json");

            using (var stream = new FileStream(filePath, FileMode.Create))
            {
                using (StreamWriter writer = new StreamWriter(stream))
                {
                    using (JsonWriter jsonWriter = new JsonTextWriter(writer))
                    {
                        var serializer = new JsonSerializer();
                        serializer.Serialize(jsonWriter, instruction);
                    }
                }
            }

            return(new FileInfo(filePath));
        }
Esempio n. 12
0
        private void OutputBatch(OutputFormat batchOutput, IEnumerable <BaseExportData> batchData, int batchCounter, string identifier, string catalogName = null)
        {
            var data = batchData.ToList();

            LogStartBatch(batchOutput, batchCounter, identifier, data.Count);

            var instruction = new OutputInstruction
            {
                Format         = batchOutput,
                CatalogName    = catalogName,
                Count          = batchCounter,
                OutputLocation = OutputFolderPath,
                OutputName     = batchOutput.ToString() + identifier + batchCounter,
                Data           = data
            };

            _outputInstructionProcessor.RecordOutput(instruction);
            Log.DebugFormat("Completed batch {0}{1}{2}.", batchOutput.ToString(), identifier, batchCounter);
        }
Esempio n. 13
0
        public Instruction GiveConcreteInstruction(Opcode instruction)
        {
            Instruction concreteInstruction = null;
            int         opCode = instruction.GetOpCode();

            switch (opCode)
            {
            case (int)Opcodes.Addition:
                concreteInstruction = new AdditionInstruction(instruction.GetFirstParameterMode(), instruction.GetSecondParameterMode());
                break;

            case (int)Opcodes.Multiplication:
                concreteInstruction = new MultiplyInstruction(instruction.GetFirstParameterMode(), instruction.GetSecondParameterMode());
                break;

            case (int)Opcodes.Input:
                concreteInstruction = new InputInstruction(program.Inputs[program.CurrentInputPointer++], instruction.GetFirstParameterMode());
                break;

            case (int)Opcodes.Output:
                concreteInstruction = new OutputInstruction(instruction.GetFirstParameterMode());
                break;

            case (int)Opcodes.JumpIfTrue:
                concreteInstruction = new JumpIfTrueInstruction(instruction.GetFirstParameterMode(), instruction.GetSecondParameterMode());
                break;

            case (int)Opcodes.JumpIfFalse:
                concreteInstruction = new JumpIfFalseInstruction(instruction.GetFirstParameterMode(), instruction.GetSecondParameterMode());
                break;

            case (int)Opcodes.LessThan:
                concreteInstruction = new LessThanInstruction(instruction.GetFirstParameterMode(), instruction.GetSecondParameterMode(), instruction.GetThirdParameterMode());
                break;

            case (int)Opcodes.Equals:
                concreteInstruction = new EqualsInstruction(instruction.GetFirstParameterMode(), instruction.GetSecondParameterMode(), instruction.GetThirdParameterMode());
                break;
            }
            return(concreteInstruction);
        }
Esempio n. 14
0
        private void SendUpdateBatch(IList <GooglePlaProductData> productDatas, int fileCount, string identifier)
        {
            if (fileCount == 1)
            {
                Log.InfoFormat("Starting to write batch update file {0} for identifier {1} containing {2} products.", fileCount, identifier, productDatas.Count);
            }
            else
            {
                Log.DebugFormat("Starting to write batch update file {0} for identifier {1} containing {2} products.", fileCount, identifier, productDatas.Count);
            }

            var fileContent = new OutputInstruction
            {
                Format    = OutputFormat.Update,
                Updates   = productDatas,
                FileCount = fileCount
            };

            _outputInstructionProcessor.RecordOutputInstruction(fileContent, identifier, "update" + fileCount);
            Log.Debug("Completed writing batch update file.");
        }
Esempio n. 15
0
        private void SendDeletionBatch(IList <string> deletedProductIds, int fileCount, string identifier)
        {
            if (fileCount == 1)
            {
                Log.InfoFormat("Starting to write batch delete file {0} for identifier {1} containing {2} products.", fileCount, identifier, deletedProductIds.Count);
            }
            else
            {
                Log.DebugFormat("Starting to write batch delete file {0} for identifier {1} containing {2} products.", fileCount, identifier, deletedProductIds.Count);
            }

            var fileContent = new OutputInstruction
            {
                Format    = OutputFormat.Delete,
                Deletions = deletedProductIds,
                FileCount = fileCount
            };

            _outputInstructionProcessor.RecordOutputInstruction(fileContent, identifier, "delete" + fileCount);
            Log.Debug("Completed writing batch delete file.");
        }
        private FileInfo WriteOutputInstructionFile(OutputInstruction instruction)
        {
            if (instruction == null)
            {
                throw new ArgumentException("Null instruction was provided.", "instruction");
            }

            if (string.IsNullOrWhiteSpace(instruction.OutputLocation))
            {
                throw new ArgumentException("An invalid output location was provided.", "OutputLocation");
            }

            if (string.IsNullOrWhiteSpace(instruction.OutputName))
            {
                throw new ArgumentException("An invalid output name was provided.", "OutputName");
            }

            instruction.OutputLocation = Path.Combine(OutputFolderPath, instruction.OutputLocation);
            EnsureFolderExists(instruction.OutputLocation);

            return(_fileProcessor.FileWrite(instruction));
        }
        private bool ProcessInstruction(OutputInstruction instruction)
        {
            var result    = false;
            var itemCount = GetItemCount(instruction);
            var startTime = DateTime.Now;

            _logger.DebugFormat("Starting to process an instruction with type of {0} containing {1} entries of catalog {2}.", instruction.Format, itemCount, instruction.CatalogName);

            result = SendToDestination(instruction);

            var message = string.Format("Completed processing of the {1} instruction with file count of {2}. Elapsed time is {0}.", (DateTime.Now - startTime).ToString(@"dd\.hh\:mm\:ss"), instruction.Format, instruction.Count);

            if (instruction.Count == 1)
            {
                _logger.InfoFormat(message);
            }
            else
            {
                _logger.DebugFormat(message);
            }

            return(result);
        }
Esempio n. 18
0
        private static FileInfo WriteOutputInstructionFile(OutputInstruction content, string folderName, string fileName)
        {
            if (content == null)
            {
                throw new ArgumentException("Null content was provided.", "content");
            }

            if (string.IsNullOrWhiteSpace(folderName))
            {
                throw new ArgumentException("An invalid folder name was provided.", "folderName");
            }

            if (string.IsNullOrWhiteSpace(fileName))
            {
                throw new ArgumentException("An invalid file name was provided.", "fileName");
            }

            var folder = Path.Combine(OutputFolderPath, folderName);

            EnsureFolderExists(folder);
            var filePath = Path.Combine(folder, fileName + ".json");

            using (var stream = new FileStream(filePath, FileMode.Create))
            {
                using (StreamWriter writer = new StreamWriter(stream))
                {
                    using (JsonWriter jsonWriter = new JsonTextWriter(writer))
                    {
                        var serializer = new JsonSerializer();
                        serializer.Serialize(jsonWriter, content);
                    }
                }
            }

            return(new FileInfo(filePath));
        }
Esempio n. 19
0
        public void RecordOutputInstruction(OutputInstruction content, string folderName, string fileName)
        {
            var file = WriteOutputInstructionFile(content, folderName, fileName);

            _files.Add(file);
        }
 protected static int GetItemCount(OutputInstruction instruction)
 {
     return(instruction.Data.Count);
 }
Esempio n. 21
0
 public abstract FileInfo FileWrite(OutputInstruction instruction);