Пример #1
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);
        }