Package ke.go.moh.oec.oecsm.data

Examples of ke.go.moh.oec.oecsm.data.LoggableTransaction


        try {
            connectToShadow();
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery("SELECT `transaction`.`ID`, `transaction`.`TYPE`, `transaction`.`TABLE_ID` FROM `transaction` WHERE `transaction`.`ID` > " + getLastProcessedId() + ";");
            while (rs.next()) {
                LoggableTransaction loggableTransaction = new LoggableTransaction(rs.getInt("ID"), findTable(rs.getInt("TABLE_ID")), TransactionType.valueOf(rs.getString("TYPE")));
                loggableTransaction.setLoggableTransactionDatumList(generateLoggableTransactionDatumList(loggableTransaction));
                loggableTransactionList.add(loggableTransaction);
            }
            if (!loggableTransactionList.isEmpty()) {
                updateLastProcessedId(loggableTransactionList.get(loggableTransactionList.size() - 1).getId());
            }
View Full Code Here


                    }
                } else if (dataTransaction.getType() == TransactionType.DELETE) {
                    sql = "DELETE FROM `cell` WHERE `cell`.`ID` = " + dataTransaction.getCell().getId() + ";";
                }
            } else if (transaction.getClass() == LoggableTransaction.class) {
                LoggableTransaction loggableTransaction = (LoggableTransaction) transaction;
                sql = "INSERT INTO `transaction`(`transaction`.`TYPE`, `transaction`.`TABLE_ID`, `transaction`.`CREATED_DATETIME`) VALUES('" + loggableTransaction.getType() + "', " + loggableTransaction.getTable().getId() + ", NOW());";
            } else if (transaction.getClass() == LoggableTransactionDatum.class) {
                LoggableTransactionDatum loggableTransactionDatum = (LoggableTransactionDatum) transaction;
                sql = "INSERT INTO `transaction_data`(`transaction_data`.`DATA`, `transaction_data`.`COLUMN_ID`, `transaction_data`.`TRANSACTION_ID`) VALUES('" + TransactionConverter.escapeSQL(loggableTransactionDatum.getCell().getData()) + "', " + loggableTransactionDatum.getCell().getColumn().getId() + ", " + loggableTransactionDatum.getLoggableTransaction().getId() + ");";
            }
        }
View Full Code Here

        return pk.replace(" ", "#").replace("-", "_").replace(".", "?");
    }

    private void insert(Table table, String sourcePk, SourceResultSet sourceRs) throws SQLException {
        List<Transaction> transactionList = new ArrayList<Transaction>();
        LoggableTransaction loggableTransaction = new LoggableTransaction(table, TransactionType.INSERT);
        List<LoggableTransactionDatum> loggableTransactionDatumList = new ArrayList<LoggableTransactionDatum>();
        String pk = null;
        for (Column column : table.getColumnList()) {
//            if (pk == null) {
//                pk = sourceRs.getString("PK");
//            }
            Cell cell = new Cell(sourcePk, sourceRs.getString(column));
            cell.setColumn(column);
            transactionList.add(new DataTransaction(cell, TransactionType.INSERT));
            loggableTransactionDatumList.add(new LoggableTransactionDatum(cell, loggableTransaction));
        }
        loggableTransaction.setLoggableTransactionDatumList(loggableTransactionDatumList);
        transactionList.add(loggableTransaction);
        processTransactions(transactionList);
        sourceRsHasRecords = sourceRs.next();
    }
View Full Code Here

        sourceRsHasRecords = sourceRs.next();
    }

    private void delete(Table table, ShadowResultSet shadowRs) throws SQLException {
        List<Transaction> transactionList = new ArrayList<Transaction>();
        LoggableTransaction loggableTransaction = new LoggableTransaction(table, TransactionType.DELETE);
        List<LoggableTransactionDatum> loggableTransactionDatumList = new ArrayList<LoggableTransactionDatum>();
        for (Column column : table.getColumnList()) {
            Cell cell = shadowRs.getCell(column);
            cell.setColumn(column);
            transactionList.add(new DataTransaction(cell, TransactionType.DELETE));
            loggableTransactionDatumList.add(new LoggableTransactionDatum(cell, loggableTransaction));
        }
        loggableTransaction.setLoggableTransactionDatumList(loggableTransactionDatumList);
        transactionList.add(loggableTransaction);
        processTransactions(transactionList);
        shadowRsHasRecords = shadowRs.next();
    }
View Full Code Here

        shadowRsHasRecords = shadowRs.next();
    }

    private void update(Table table, String sourcePk, SourceResultSet sourceRs, ShadowResultSet shadowRs) throws SQLException {
        List<Transaction> transactionList = new ArrayList<Transaction>();
        LoggableTransaction loggableTransaction = new LoggableTransaction(table, TransactionType.UPDATE);
        List<LoggableTransactionDatum> loggableTransactionDatumList = new ArrayList<LoggableTransactionDatum>();
//        String pk = null;
        for (Column column : table.getColumnList()) {
            /*
             * Ensure cells associated with new columns are created
             */
//            if (pk == null) {
//                pk = sourceRs.getString("PK");              
//            }
            Cell shadowCell = shadowRs.getCell(column);
            String sourceColumnValue = sourceRs.getString(column);
            if (shadowCell == null) {
                Cell cell = new Cell(shadowRs.getCell("PK").getData(), sourceColumnValue);
                cell.setColumn(column);
                transactionList.add(new DataTransaction(cell, TransactionType.INSERT));
                loggableTransactionDatumList.add(new LoggableTransactionDatum(cell, loggableTransaction));
                continue;
            }
            /*
             * Judge which columns we need to compare
             */
            String shadowColumnValue = shadowRs.getCell(column).getData();
            boolean pkColumn = table.getPk().contains(column.getName() + ",");
            boolean compare = (shadowColumnValue != null && sourceColumnValue != null)//compare only if both values are not null and this column is not (part of) the primary key
                    && !pkColumn;
            boolean ignore = (shadowColumnValue == null && sourceColumnValue == null);//if both values are null, no update has taken place
            boolean update = false;
            if (compare) {
                update = !shadowColumnValue.equals(sourceColumnValue);//if both values are not equal then an update has taken place
            } else {
                if (!pkColumn) {
                    update = !ignore;//if only one value is null then an update has taken place
                }
            }
            if (update) {
                Cell cell = shadowRs.getCell(column);
                cell.setData(sourceColumnValue);
                cell.setColumn(column);
                transactionList.add(new DataTransaction(cell, TransactionType.UPDATE));
                loggableTransactionDatumList.add(new LoggableTransactionDatum(cell, loggableTransaction));
            }
        }
        if (!loggableTransactionDatumList.isEmpty()) {
            // String pk = null;
            Cell cell = shadowRs.getCell("PK");
//            cell.setData(sourceRs.getString("PK"));
            cell.setData(sourcePk);

            //TODO: Accommodate cases where the pk is not the first column of the table
            cell.setColumn(table.getColumnList().get(0));
            loggableTransactionDatumList.add(new LoggableTransactionDatum(cell, loggableTransaction));

            loggableTransaction.setLoggableTransactionDatumList(loggableTransactionDatumList);
            transactionList.add(loggableTransaction);
        }
        processTransactions(transactionList);
        sourceRsHasRecords = sourceRs.next();
        shadowRsHasRecords = shadowRs.next();
View Full Code Here

            for (Transaction dataTransaction : dataTransactionList) {
                Mediator.getLogger(DataSynchronizer.class.getName()).log(Level.FINEST, TransactionConverter.convertToSQL(dataTransaction));
                if (statement.executeUpdate(TransactionConverter.convertToSQL(dataTransaction), Statement.RETURN_GENERATED_KEYS) == 1) {
                    if (dataTransaction.getClass() == LoggableTransaction.class) {
                        ResultSet rs = statement.getGeneratedKeys();
                        LoggableTransaction loggableTransaction = (LoggableTransaction) dataTransaction;
                        if (rs.next()) {
                            loggableTransaction.setId(rs.getInt(1));
                        }
                        for (LoggableTransactionDatum loggableTransactionDatum : loggableTransaction.getLoggableTransactionDatumList()) {
                            Mediator.getLogger(DataSynchronizer.class.getName()).log(Level.FINEST, TransactionConverter.convertToSQL(loggableTransactionDatum));
                            statement.executeUpdate(TransactionConverter.convertToSQL(loggableTransactionDatum));
                        }
                    }
                }
View Full Code Here

TOP

Related Classes of ke.go.moh.oec.oecsm.data.LoggableTransaction

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.