Example usage for org.springframework.transaction.support TransactionTemplate execute

List of usage examples for org.springframework.transaction.support TransactionTemplate execute

Introduction

In this page you can find the example usage for org.springframework.transaction.support TransactionTemplate execute.

Prototype

@Override
    @Nullable
    public <T> T execute(TransactionCallback<T> action) throws TransactionException 

Source Link

Usage

From source file:database.DataLoader.java

private void moveBranches() throws SQLException, ClassNotFoundException, Exception {
    try {//from   ww  w .  java 2  s .  c o m
        final String tableName = BRANCH;
        final ResultSet branchSet = getFromOldBase(getSelectAll(tableName));
        while (branchSet.next()) {
            TransactionTemplate temp = new TransactionTemplate(transactionManager);
            temp.execute(new TransactionCallbackWithoutResult() {

                @Override
                protected void doInTransactionWithoutResult(TransactionStatus ts) {
                    Long oldId = 0L;
                    try {
                        oldId = branchSet.getLong("id");
                        String name = branchSet.getString("name");
                        String letter = branchSet.getString("letter");
                        boolean canUseFreeAuthors = branchSet.getBoolean("can_use_free_authors");
                        Branch branch = new Branch();
                        branch.setAbbrevation(letter);
                        branch.setFreeAuthors(canUseFreeAuthors);
                        branch.setName(name);
                        saveObjectAndLink(branch, oldId, tableName);
                    } catch (Exception e) {
                        ts.setRollbackOnly();
                        String message = "branch: " + oldId + " " + StringAdapter.getStackExeption(e);
                        addErrorMessage(message);
                    }
                }
            });

        }
    } catch (Throwable th) {
        log.warn("moveBranches " + StringAdapter.getStackExeption(th));
    }
}

From source file:database.DataLoader.java

private void moveAuthorMessages() throws Exception {
    try {//w ww. ja v a  2  s.  c  om
        String modelName = Z_BID_DISCUSS;
        ResultSet set = getFromOldBase(
                "select dis.*, bid.order_id, bid.author_id from z_zbid_discuss dis, z_zbid bid where dis.bid_id = bid.id order by dis.id limit 10000, 200000");
        while (set.next()) {
            final TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
            transactionTemplate.execute(new AuthorMessageTransactionCallback(set, modelName));
        }
    } catch (Throwable th) {
        log.warn("moveAuthorMessages " + StringAdapter.getStackExeption(th));
    }
}

From source file:database.DataLoader.java

private void moveAuthorSalary() throws Exception {
    try {//  w  w w  .j a  va  2 s.c om
        final String tableName = Z_BID;
        final ResultSet set = getFromOldBase(getSelectAll(tableName));
        while (set.next()) {

            TransactionTemplate temp = new TransactionTemplate(transactionManager);
            temp.execute(new TransactionCallbackWithoutResult() {

                @Override
                protected void doInTransactionWithoutResult(TransactionStatus ts) {
                    Long oldId = 0L;
                    try {
                        oldId = set.getLong("id");

                        Long oldOrderId = set.getLong("order_id");
                        Long oldAuthorId = set.getLong("author_id");
                        Double cost = set.getDouble("reward");
                        Long newOrderId = getNewId(oldOrderId, ORDER);
                        Long newAuthorId = getNewId(oldAuthorId, USERS);
                        Order order = orderDao.find(newOrderId);
                        Author author = authorDao.find(newAuthorId);
                        AuthorSalary authorSalary = new AuthorSalary();
                        authorSalary.setOrder(order);
                        authorSalary.setAuthor(author);
                        authorSalary.setCost(cost);
                        saveObjectAndLink(authorSalary, oldId, tableName);
                    } catch (Exception e) {
                        ts.setRollbackOnly();
                        addErrorMessage("authorSalary: " + oldId + " " + StringAdapter.getStackExeption(e));
                        log.warn("authorSalary: " + oldId + " " + StringAdapter.getStackExeption(e));
                    }
                }
            });

        }
    } catch (Throwable th) {
        log.warn("moveAuthorToDirections " + StringAdapter.getStackExeption(th));
    }
}

From source file:database.DataLoader.java

private void moveAdminMessages() throws SQLException, ClassNotFoundException, Exception {
    try {/*from   w w  w.j  a  v a 2  s  .  c  o m*/
        final String tableName = ADMIN_DISCUSS;
        final ResultSet resSet = getFromOldBase(getSelectAll(tableName));
        while (resSet.next()) {

            TransactionTemplate temp = new TransactionTemplate(transactionManager);
            temp.execute(new TransactionCallbackWithoutResult() {

                @Override
                protected void doInTransactionWithoutResult(TransactionStatus ts) {
                    Long oldId = 0L;
                    try {
                        oldId = resSet.getLong("id");
                        Long oldOrderId = resSet.getLong("order_id");
                        Long oldAdminId = resSet.getLong("admin_id");
                        Long newOrderId = getNewId(oldOrderId, ORDER);
                        Long newAdminId = getNewId(oldAdminId, USERS);
                        Timestamp date = resSet.getTimestamp("timestamp");
                        Order order = orderDao.find(newOrderId);
                        User admin = userDao.find(newAdminId);
                        String message = resSet.getString("message");
                        AdminMessage ent = new AdminMessage();
                        ent.setAdmin(admin);
                        ent.setMessageDate(date);
                        ent.setOrder(order);
                        ent.setText(message);

                        saveObjectAndLink(ent, oldId, tableName);
                    } catch (Throwable e) {
                        ts.setRollbackOnly();
                        addErrorMessage("admin: " + oldId + " "
                                + StringAdapter.transliterate(StringAdapter.getStackExeption(e)));
                    }
                }
            });

        }
    } catch (Throwable e) {
        log.warn("admin:  " + StringAdapter.transliterate(StringAdapter.getStackExeption(e)));
    }
}

From source file:database.DataLoader.java

private void moveAuthorViews() throws Exception {
    try {/* w  w  w . ja v  a  2s .co m*/
        final String tableName = AUTHOR_VIEWS;
        final ResultSet set = getFromOldBase(getSelectAll(tableName));
        final List<OrderView> allList = orderViewDao.getAll();
        while (set.next()) {

            TransactionTemplate temp = new TransactionTemplate(transactionManager);
            temp.execute(new TransactionCallbackWithoutResult() {

                @Override
                protected void doInTransactionWithoutResult(TransactionStatus ts) {
                    Long oldOrderId = 0L;
                    Long oldAuthorId = 0L;
                    try {
                        oldOrderId = set.getLong("order_id");
                        oldAuthorId = set.getLong("author_id");

                        Long newOrderId = getNewId(oldOrderId, ORDER);
                        Long newAuthorId = getNewId(oldAuthorId, USERS);

                        Order order = getOrder(newOrderId);
                        User user = getUser(newAuthorId);

                        if (!contains(allList, user, order)) {
                            OrderView orderView = new OrderView();
                            orderView.setOrder(order);
                            orderView.setUser(user);
                            orderViewDao.save(orderView);
                        }
                    } catch (Throwable e) {
                        ts.setRollbackOnly();
                        addErrorMessage("moveAuthorViews: " + oldOrderId + " " + oldAuthorId + " "
                                + StringAdapter.getStackExeption(e));
                        log.warn("moveAuthorViews: " + oldOrderId + " " + oldAuthorId + " "
                                + StringAdapter.getStackExeption(e));
                    }
                }
            });

        }
    } catch (Throwable th) {
        log.warn("moveAuthorViews " + StringAdapter.getStackExeption(th));
    }
}

From source file:database.DataLoader.java

private void moveAuthorToDirections() throws SQLException, ClassNotFoundException, Exception {
    try {/*from  w ww  . j a  v  a  2 s .  co  m*/
        final String tableName = AUTHOR_SPHERE;
        final ResultSet set = getFromOldBase(getSelectAll(tableName));
        while (set.next()) {

            TransactionTemplate temp = new TransactionTemplate(transactionManager);

            temp.execute(new TransactionCallbackWithoutResult() {

                @Override
                protected void doInTransactionWithoutResult(TransactionStatus ts) {
                    Long oldAuthorId = 0L;
                    Long oldSphereId = 0L;

                    try {
                        oldAuthorId = set.getLong("author_id");
                        oldSphereId = set.getLong("sphere_id");
                        Long newAuthorId = getNewId(oldAuthorId, USERS);
                        Long newSphereId = getNewId(oldSphereId, SPHERE);
                        Direction dir = directionDao.find(newSphereId);
                        Author author = authorDao.find(newAuthorId);

                        List<Direction> directionList = author.getDirections();
                        if (directionList == null) {
                            directionList = new ArrayList();
                        }
                        directionList.add(dir);
                        author.setDirections(directionList);
                        // ??    , ? ??    ,   dao
                        authorDao.update(author);
                    } catch (Exception e) {
                        ts.setRollbackOnly();
                        addErrorMessage("authorTodirection: " + oldAuthorId + " " + oldSphereId + " "
                                + StringAdapter.getStackExeption(e));
                        log.warn("authorTodirection: " + oldAuthorId + " " + oldSphereId + " "
                                + StringAdapter.getStackExeption(e));
                    }
                }
            });

        }
    } catch (Throwable th) {
        log.warn("moveAuthorToDirections " + StringAdapter.getStackExeption(th));
    }
}

From source file:database.DataLoader.java

private void moveOrdersToDirections() throws SQLException, ClassNotFoundException, Exception {
    try {//from   ww  w.j  a v a 2 s .co m
        final String tableName = ORDER_SPHERE;
        final ResultSet set = getFromOldBase(getSelectAll(tableName));
        while (set.next()) {

            TransactionTemplate temp = new TransactionTemplate(transactionManager);
            temp.execute(new TransactionCallbackWithoutResult() {

                @Override
                protected void doInTransactionWithoutResult(TransactionStatus ts) {
                    Long oldOrderId = 0L;
                    Long oldSphereId = 0L;
                    try {
                        oldOrderId = set.getLong("order_id");
                        oldSphereId = set.getLong("sphere_id");
                        Long newOrderId = getNewId(oldOrderId, ORDER);
                        Long newSphereId = getNewId(oldSphereId, SPHERE);
                        Direction dir = directionDao.find(newSphereId);
                        Order order = orderDao.find(newOrderId);

                        List<Direction> directionList = order.getDirections();
                        if (directionList == null) {
                            directionList = new ArrayList();
                        }

                        if (!contains(directionList, dir)) {
                            directionList.add(dir);
                            order.setDirections(directionList);
                            // ??    , ? ??    ,   dao
                            orderDao.update(order);
                        }
                    } catch (Throwable e) {
                        ts.setRollbackOnly();
                        addErrorMessage("moveOrdersToDirections: " + oldOrderId + " " + oldSphereId + " "
                                + StringAdapter.getStackExeption(e));
                        log.warn("moveOrdersToDirections: " + oldOrderId + " " + oldSphereId + " "
                                + StringAdapter.getStackExeption(e));
                    }
                }
            });

        }
    } catch (Throwable th) {
        log.warn("moveOrdersToDirections " + StringAdapter.getStackExeption(th));
    }
}

From source file:database.DataLoader.java

private void addAuthorRights() throws Exception {
    final String modelName = USERS;
    final ResultSet userRes = getFromOldBase("select * from z_zauthor");
    while (userRes.next()) {

        final TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
        transactionTemplate.execute(new TransactionCallbackWithoutResult() {

            @Override/*  ww w.java 2  s  .c om*/
            protected void doInTransactionWithoutResult(TransactionStatus ts) {
                try {
                    Long userId = userRes.getLong("id");
                    Long branchId = userRes.getLong("branch_id");
                    Long newUserId = getNewId(userId, modelName);
                    User user = getUser(newUserId);
                    if (branchId != 0) {
                        Long newBranchId = getNewId(branchId, BRANCH);
                        Branch branch = branchDao.find(newBranchId);
                        List<Long> branchIds = new ArrayList();
                        if (user instanceof Author) {
                            branchIds.add(newBranchId);
                            authorService.changeRoleForAuthor(newUserId, branchIds);
                        }
                    } else {
                        List<Branch> branchList = branchDao.getAll();
                        List<Long> branchIds = new ArrayList();
                        for (Branch branch : branchList) {
                            branchIds.add(branch.getBranchId());
                        }
                        if (user instanceof Author) {
                            authorService.changeRoleForAuthor(newUserId, branchIds);
                        }
                    }
                } catch (Exception e) {
                    ts.setRollbackOnly();
                    addErrorMessage(StringAdapter.getStackExeption(e));
                }
            }
        });
    }
}

From source file:database.DataLoader.java

/**
 *  ?  ?  ?   //  w w w .  j  ava 2s. c  o  m
 */
private void moveUsers() throws SQLException, ClassNotFoundException, Exception {
    try {
        //    
        final String modelName = USERS;
        final ResultSet userRes = getFromOldBase(getSelectAll(modelName));
        // ?   
        while (userRes.next()) {

            final TransactionTemplate temp = new TransactionTemplate(transactionManager);
            temp.execute(new TransactionCallbackWithoutResult() {

                @Override
                protected void doInTransactionWithoutResult(TransactionStatus ts) {
                    Long userId = 0L;
                    try {
                        userId = userRes.getLong("id");

                        String email = trim(userRes.getString("email"));
                        String password = trim(userRes.getString("password"));
                        String hash = VuzSecurity.getHash(password);
                        String firstname = "";
                        String lastname = "";
                        String fathername = "";
                        String phone = "";

                        ResultSet userProfileRes = getUserProfile(userId);
                        if (getRowCount(userProfileRes) > 0) {
                            firstname = trim(userProfileRes.getString("firstname"));
                            lastname = trim(userProfileRes.getString("lastname"));
                            fathername = trim(userProfileRes.getString("fathername"));
                            phone = userProfileRes.getString("phone");
                        }

                        ResultSet adminRes = getAdmin(userId);
                        ResultSet subadminSet = getFromOldBase(
                                " select * from " + SUBADMIN + " where id = " + userId);

                        if (ValidatorUtils.isEmail(email)) {
                            if (getRowCount(adminRes) > 0 || getRowCount(subadminSet) > 0) {
                                saveAdmin(email, password, firstname, lastname, fathername, phone, userId,
                                        modelName);
                            } else {
                                Author author = new Author();
                                author.setLogin(email);
                                author.setPassword(password);
                                author.setName(firstname);
                                author.setSurname(lastname);
                                author.setMiddlename(fathername);
                                author.setPhone(phone);
                                author.setWord(password);
                                saveObjectAndLink(author, userId, modelName);
                            }
                        }
                    } catch (Exception e) {
                        ts.setRollbackOnly();
                        String message = "user: " + userId + " " + StringAdapter.getStackExeption(e);
                        log.warn(message);
                        messageList.add(message);
                    }

                }
            });

        }
    } catch (Throwable th) {
        log.warn("moveUsers " + StringAdapter.getStackExeption(th));
    }
}

From source file:database.DataLoader.java

private void addPaymentsToOrders() throws Exception {
    String tableName = ORDER;//from ww w  .  j a  v  a 2s .c  o m
    final ResultSet set = getFromOldBase(getSelectAll(tableName));
    final List<Payment> paymentList = getAllPayments();
    while (set.next()) {

        Long oldOrderId = 0L;
        try {
            oldOrderId = set.getLong("id");

            final TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
            transactionTemplate.execute(new TransactionCallbackWithoutResult() {

                @Override
                protected void doInTransactionWithoutResult(TransactionStatus ts) {
                    Long oldOrderId = 0L;
                    try {
                        oldOrderId = set.getLong("id");
                        Double prepay = set.getDouble("prepay");
                        Long newOrderId = getNewId(oldOrderId, ORDER);
                        Order order = getOrder(newOrderId);
                        if (!containsPayment(paymentList, order)) {
                            Payment payment = new Payment();
                            payment.setAmount(prepay);
                            payment.setOrder(order);
                            Date paymentDate;
                            if (order.getRealDate() != null) {
                                paymentDate = order.getRealDate();
                            } else if (order.getDeadlineDate() != null) {
                                paymentDate = order.getDeadlineDate();
                            } else if (order.getOrderDate() != null) {
                                paymentDate = order.getOrderDate();
                            } else {
                                paymentDate = new Date();
                            }
                            payment.setPaymentDate(paymentDate);
                            payment.setPaymentType(paymentTypeService.getStandartTypeFromOldDb());
                            payment.setUser(userService.getStandartAdmin());
                            if (payment.getAmount() != null && order.getCost() != null) {
                                if (payment.getAmount() >= order.getCost()) {
                                    payment.setFinalPayment(true);
                                }
                            }
                            paymentDao.save(payment);
                        }
                    } catch (Throwable e) {
                        ts.setRollbackOnly();
                        messageList
                                .add(" - " + oldOrderId + " " + StringAdapter.getStackExeption(e));
                        log.warn(" - " + oldOrderId + " " + StringAdapter.getStackExeption(e));
                    }
                }
            });

        } catch (Throwable e) {
            messageList.add(" - " + oldOrderId + " " + StringAdapter.getStackExeption(e));
            log.warn(" - " + oldOrderId + " " + StringAdapter.getStackExeption(e));
        }

    }
}