예제 #1
0
파일: Server.cs 프로젝트: jesumarquez/lt
        private IData Decode(int version, string data)
        {
            var transform = DataTransformFactory.GetDataTransform(version);

            if (transform == null)
            {
                throw new DataTransformException();
            }

            try
            {
                var idata = transform.Decode(data);
                if (idata == null)
                {
                    throw new FormatException();
                }

                return(idata);
            }
            catch
            {
                throw new DataTransformException();
            }
        }
예제 #2
0
        public void Process()
        {
            if (!Login())
            {
                return;
            }

            var transform = DataTransformFactory.GetDataTransform(-1);

            if (transform == null)
            {
                throw new DataTransformException();
            }

            var connectionError = false;

            foreach (var import in Configuration.Import)
            {
                if (connectionError)
                {
                    break;
                }
                var imported = 0;

                var strategy = import.DataSource.DataStrategy;
                var newData  = strategy.GetNewData();
                if (newData == null || newData.Rows.Count == 0)
                {
                    continue;
                }

                var parsers = import.Entity.Select(ent => ParserFactory.GetEntityParser(ent)).Where(parser => parser != null);

                foreach (var row in newData.Rows)
                {
                    if (connectionError)
                    {
                        break;
                    }
                    try
                    {
                        foreach (var parser in parsers)
                        {
                            var data = parser.Parse(row);

                            if (data.Operation == -1)
                            {
                                continue;
                            }

                            var encodedData = transform.Encode(data);

                            Logger.Info("Importing data with session: " + SessionToken);
                            var result = Server.Import.ImportData(SessionToken, ConfigImportClient.Company, ConfigImportClient.Branch, encodedData, data.Version);

                            if (!result.RespuestaOk && !IsSessionActive())
                            {
                                Logger.Info("Inactive session: " + SessionToken);
                                Login();
                                Logger.Info("Importing data with session: " + SessionToken);
                                result = Server.Import.ImportData(SessionToken, ConfigImportClient.Company, ConfigImportClient.Branch, encodedData, data.Version);
                            }

                            if (!result.RespuestaOk)
                            {
                                throw new ApplicationException(result.Mensaje);
                            }
                            imported++;
                        }
                    }
                    catch (Exception ex)
                    {
                        if (ex.GetType() == typeof(TimeoutException))
                        {
                            Logger.Error("Error de conexión. No se puede acceder al server.");
                            connectionError = true;
                        }
                        else
                        {
                            Logger.Error(ex);
                        }

                        // TODO: Guardar Registro como mal importado en archivo de errores para reprocesar (csv)
                    }
                }

                if (imported == 0 || connectionError)
                {
                    import.DataSource.DataStrategy.Revert();
                }
            }
        }