Exemplo n.º 1
0
        /// <summary>
        /// Fetech the values from source datasource, convert fetched values and insert values in destination dataSource.
        /// </summary>
        /// <param name="fromDataSourceDefinition"></param>
        /// <param name="toDataSourceDefinition"></param>
        private void ConvertAndInsertValues(DataSourceDefinition fromDataSourceDefinition, DataSourceDefinition toDataSourceDefinition)
        {
            GatewayResult result = null;

            MainCursorBuilder cursorBuilder = new MainCursorBuilder(null);

            RuntimeCursor fromRuntimeCursor = cursorBuilder.Build(fromDataSourceDefinition, Access.Read);
            RuntimeCursor toRuntimeCursor   = cursorBuilder.Build(toDataSourceDefinition, Access.Write);

            //Prepare and open source and destnation runtime cursor.
            PrepareAndOpenCursor(fromRuntimeCursor, true);
            PrepareAndOpenCursor(toRuntimeCursor, false);

            //Create fetch command
            GatewayCommandFetch fetchCommand = GatewayCommandsFactory.CreateCursorFetchCommand(fromRuntimeCursor, ClientManager.Instance.LocalManager);

            while (true)
            {
                //Fetch record from source table
                result = fetchCommand.Execute();
                if (!result.Success)
                {
                    if (result.ErrorCode == GatewayErrorCode.NoRecord)
                    {
                        break;
                    }
                    else
                    {
                        throw new DataSourceConversionFailedException(fromDataSourceDefinition.Name, result.ErrorDescription);
                    }
                }

                //Convert values of fields of source table.
                ConvertFields(fromDataSourceDefinition, toDataSourceDefinition, fromRuntimeCursor.RuntimeCursorData.CurrentValues, toRuntimeCursor.RuntimeCursorData.CurrentValues);

                for (int i = 0; i < toDataSourceDefinition.Fields.Count; i++)
                {
                    toRuntimeCursor.CursorDefinition.IsFieldUpdated[i] = true;
                }

                //Insert converted values into temporary table.
                result = GatewayCommandsFactory.CreateCursorInsertCommand(toRuntimeCursor, ClientManager.Instance.LocalManager).Execute();
            }

            // release and close source and destnation runtime cursor.
            ReleaseAndCloseCursor(fromRuntimeCursor, true);
            ReleaseAndCloseCursor(toRuntimeCursor, false);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Update dataview to DataSource.
        /// </summary>
        private ReturnResultBase UpdateDataViewToDataSource()
        {
            bool   transactionOpened = false;
            string error             = string.Empty;

            string dataSourceName = ClientManager.Instance.getEnvParamsTable().translate(updateDataViewToDataSourceCommand.DestDataSourceName);

            if (string.IsNullOrEmpty(dataSourceName))
            {
                dataSourceName = destinationDataSourceDefinition.Name;
            }

            GatewayResult result = GatewayCommandsFactory.CreateFileExistCommand(dataSourceName, destinationDataSourceDefinition, ClientManager.Instance.LocalManager).Execute();

            bool insertMode = !result.Success;

            if (!insertMode)
            {
                uniqueKey = GetUniqueKey();

                if (uniqueKey == null)
                {
                    error = "DataViewToDataSource - When using the DataViewtoDataSource function, a unique index must be defined in the destination data source .";
                    Logger.Instance.WriteExceptionToLog(error);
                    ClientManager.Instance.ErrorToBeWrittenInServerLog = error;
                    return(new ReturnResult(MsgInterface.STR_DATAVIEW_TO_DATASOURCE_OPERATION_FAILED));
                }
                else if (!CheckDestinationColumnListContainUniqueKeyColumns())
                {
                    error = "DataViewToDataSource - When using the DataViewtoDataSource function, all the segments of the unique index must be selected.";
                    Logger.Instance.WriteExceptionToLog(error);
                    ClientManager.Instance.ErrorToBeWrittenInServerLog = error;
                    return(new ReturnResult(MsgInterface.STR_DATAVIEW_TO_DATASOURCE_OPERATION_FAILED));
                }
            }

            result = GatewayCommandsFactory.CreateFileOpenCommand(dataSourceName, destinationDataSourceDefinition, Access.Write, ClientManager.Instance.LocalManager).Execute();
            if (result.Success)
            {
                //Build the runtime cursor.
                MainCursorBuilder cursorBuilder            = new MainCursorBuilder(null);
                RuntimeCursor     destinationRuntimeCursor = cursorBuilder.Build(destinationDataSourceDefinition, Access.Write);

                destinationRuntimeCursor.CursorDefinition.StartPosition   = new DbPos(true);
                destinationRuntimeCursor.CursorDefinition.CurrentPosition = new DbPos(true);

                // Prepare the cursor.
                result = GatewayCommandsFactory.CreateCursorPrepareCommand(destinationRuntimeCursor, ClientManager.Instance.LocalManager).Execute();

                if (result.Success)
                {
                    //If tansaction is not open then open the transaction.
                    if (TaskTransactionManager.LocalOpenedTransactionsCount == 0)
                    {
                        result            = GatewayCommandsFactory.CreateGatewayCommandOpenTransaction(ClientManager.Instance.LocalManager).Execute();
                        transactionOpened = true;
                    }

                    if (result.Success)
                    {
                        SetDataToRuntimeParser();
                        RecordForDataViewToDataSource record = GetRecord();

                        while (record != null)
                        {
                            BuildCurrentValues(destinationRuntimeCursor, record);
                            result = GatewayCommandsFactory.CreateCursorInsertCommand(destinationRuntimeCursor, ClientManager.Instance.LocalManager, true).Execute();

                            if (!result.Success)
                            {
                                if (result.ErrorCode == GatewayErrorCode.DuplicateKey)
                                {
                                    if (!insertMode)
                                    {
                                        //Build the ranges using unique key segments value.
                                        BuildRanges(record, destinationRuntimeCursor);

                                        //Open the cursor and apply the ranges.
                                        result = GatewayCommandsFactory.CreateCursorOpenCommand(destinationRuntimeCursor, ClientManager.Instance.LocalManager, true).Execute();
                                        if (result.Success)
                                        {
                                            //Fetch the record
                                            result = GatewayCommandsFactory.CreateCursorFetchCommand(destinationRuntimeCursor, ClientManager.Instance.LocalManager).Execute();

                                            BuildCurrentValues(destinationRuntimeCursor, record);

                                            //If record found, that means record with same key value exists, so update the current record with destination record.
                                            if (result.Success)
                                            {
                                                result = GatewayCommandsFactory.CreateGatewayCommandCursorUpdateRecord(destinationRuntimeCursor, ClientManager.Instance.LocalManager, true).Execute();
                                            }
                                            else
                                            {
                                                if (!string.IsNullOrEmpty(ClientManager.Instance.ErrorToBeWrittenInServerLog))
                                                {
                                                    ClientManager.Instance.ErrorToBeWrittenInServerLog += "\r\n";
                                                }

                                                ClientManager.Instance.ErrorToBeWrittenInServerLog += result.ErrorDescription;
                                            }

                                            //Close the cursor.
                                            GatewayCommandsFactory.CreateCursorCloseCommand(destinationRuntimeCursor, ClientManager.Instance.LocalManager).Execute();
                                        }
                                        else
                                        {
                                            if (!string.IsNullOrEmpty(ClientManager.Instance.ErrorToBeWrittenInServerLog))
                                            {
                                                ClientManager.Instance.ErrorToBeWrittenInServerLog += "\r\n";
                                            }

                                            ClientManager.Instance.ErrorToBeWrittenInServerLog += result.ErrorDescription;
                                        }
                                    }
                                    else
                                    {
                                        if (!string.IsNullOrEmpty(ClientManager.Instance.ErrorToBeWrittenInServerLog))
                                        {
                                            ClientManager.Instance.ErrorToBeWrittenInServerLog += "\r\n";
                                        }

                                        ClientManager.Instance.ErrorToBeWrittenInServerLog += result.ErrorDescription;

                                        record = GetRecord();

                                        continue;
                                    }
                                }
                                else
                                {
                                    if (!string.IsNullOrEmpty(ClientManager.Instance.ErrorToBeWrittenInServerLog))
                                    {
                                        ClientManager.Instance.ErrorToBeWrittenInServerLog += "\r\n";
                                    }

                                    ClientManager.Instance.ErrorToBeWrittenInServerLog += result.ErrorDescription;

                                    break;
                                }
                            }

                            record = GetRecord();
                        }

                        //If transaction is opened , then close the transaction. If any error occurs then abort the transation else do commit.
                        if (transactionOpened)
                        {
                            GatewayCommandCloseTransaction closeTransactionCommand = GatewayCommandsFactory.CreateGatewayCommandCloseTransaction(ClientManager.Instance.LocalManager);
                            if (result.Success)
                            {
                                closeTransactionCommand.TransactionModes = TransactionModes.Commit;
                            }
                            else
                            {
                                closeTransactionCommand.TransactionModes = TransactionModes.Abort;
                            }

                            closeTransactionCommand.Execute();
                        }

                        //Release the cursor and close the file.
                        GatewayCommandsFactory.CreateCursorReleaseCommand(destinationRuntimeCursor, ClientManager.Instance.LocalManager).Execute();

                        GatewayCommandsFactory.CreateFileCloseCommand(destinationDataSourceDefinition, ClientManager.Instance.LocalManager).Execute();
                    }
                    else
                    {
                        ClientManager.Instance.ErrorToBeWrittenInServerLog = result.ErrorDescription;
                    }
                }
                else
                {
                    ClientManager.Instance.ErrorToBeWrittenInServerLog = result.ErrorDescription;
                }
            }
            else
            {
                ClientManager.Instance.ErrorToBeWrittenInServerLog = result.ErrorDescription;
            }

            return(result);
        }