database.DataLoader.java Source code

Java tutorial

Introduction

Here is the source code for database.DataLoader.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package database;

import entity.Admin;
import entity.AdminMessage;
import entity.Author;
import entity.AuthorFile;
import entity.AuthorMessage;
import entity.AuthorReject;
import entity.AuthorSalary;
import entity.Branch;
import entity.DataLink;
import entity.Direction;
import entity.Message;
import entity.MessageFile;
import entity.Order;
import entity.OrderFile;
import entity.OrderType;
import entity.OrderView;
import entity.Payment;
import entity.ReadyOrderFile;
import entity.User;
import entity.orderStatus.OrderStatus;
import entity.parent.PrimEntity;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
import persistence.AdminDao;
import persistence.AdminMessageDao;
import persistence.AuthorDao;
import persistence.AuthorMessageDao;
import persistence.AuthorRejectDao;
import persistence.AuthorSalaryDao;
import persistence.BranchDao;
import persistence.DirectionDao;
import persistence.MessageDao;
import persistence.OrderDao;
import persistence.OrderFileDao;
import persistence.OrderTypeDao;
import persistence.OrderViewDao;
import persistence.PaymentDao;
import persistence.RoleDao;
import persistence.TemplateDao;
import persistence.UserBranchRoleDao;
import persistence.UserDao;
import persistence.parent.Dao;
import service.AuthorService;
import service.DataLinkService;
import service.FileService;
import service.PaymentTypeService;
import service.UserService;
import support.ServiceResult;
import support.StringAdapter;
import support.ValidatorUtils;
import support.VuzSecurity;

/**
 *    ?   
 *
 * @author Rice Pavel
 */
@Component
//@Transactional(/*rollbackFor = Exception.class*/)
public class DataLoader {

    protected Logger log = Logger.getLogger(this.getClass());

    private Connection conn;

    private List<String> messageList = new ArrayList();

    @Autowired
    private UserDao userDao;

    @Autowired
    private AdminDao adminDao;

    @Autowired
    private AdminMessageDao adminMessageDao;

    @Autowired
    private AuthorMessageDao authorMessageDao;

    @Autowired
    private MessageDao messageDao;

    @Autowired
    private TemplateDao templateDao;

    @Autowired
    private AuthorDao authorDao;

    @Autowired
    private BranchDao branchDao;

    @Autowired
    private OrderTypeDao orderTypeDao;

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private OrderViewDao orderViewDao;

    @Autowired
    private DirectionDao directionDao;

    @Autowired
    private AuthorSalaryDao authorSalaryDao;

    @Autowired
    private DataLinkService dataLinkService;

    @Autowired
    private AuthorRejectDao authorRejectDao;

    @Autowired
    private UserBranchRoleDao userBranchRoleDao;

    @Autowired
    private FileService fileService;

    @Autowired
    private OrderFileDao orderFileDao;

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private PaymentDao paymentDao;

    @Autowired
    private PaymentTypeService paymentTypeService;

    @Autowired
    private UserService userService;

    @Autowired
    private AuthorService authorService;

    private final String USERS = "z_user_users";

    private final String AUTHOR = "z_zauthor";

    private final String ADMIN = "z_zadmin";

    private final String BRANCH = "z_zbranch";

    private final String TYPE = "z_ztype";

    private final String ADMIN_DISCUSS = "z_zadmin_discuss";

    private final String ORDER = "z_zorder";

    private final String USER_PROFILE = "z_user_profiles";

    private final String AUTHOR_DISCUSS = "z_zauthor_discuss";

    private final String AUTHOR_REJECT = "z_zauthor_reject";

    private final String EMAIL_TEMPLATE = "z_zemail_template";

    private final String CLIENT = "z_zclient";

    private final String SPHERE = "z_zsphere";

    private final String AUTHOR_SPHERE = "z_zauthor_sphere";

    private final String ORDER_SPHERE = "z_zorder_sphere";

    private final String Z_BID = "z_zbid";

    private final String Z_BID_DISCUSS = "z_zbid_discuss";

    private final String SUBADMIN = "z_zsubadmin";

    private final String AUTHOR_VIEWS = "z_zorder_author_views";

    private final String FILE = "z_file";

    @Autowired
    private PlatformTransactionManager transactionManager;

    /**
     *    ?   
     */
    public String moveData() throws SQLException, ClassNotFoundException, Exception {
        String res = "";
        Properties props = new Properties();
        File fl = new File("/usr/local/vuz/logs/prop");
        if (!fl.exists()) {
            fl.createNewFile();
        }
        InputStream inputStream = new FileInputStream(fl);
        props.load(inputStream);
        inputStream.close();
        FileOutputStream fos = new FileOutputStream(fl);
        if (props.get("ready") == null) {
            props.setProperty("ready", "1");
            props.setProperty("state", "moveUsers");
            props.store(fos, "no comment");
            //moveUsers();
            props.setProperty("state", "moveOrderTypes");
            props.store(fos, "no comment");
            //moveOrderTypes();
            props.setProperty("state", "moveDirections");
            props.store(fos, "no comment");
            //moveDirections();
            props.setProperty("state", "moveOrders");
            props.store(fos, "no comment");
            //moveOrders();
            props.setProperty("state", "moveAdminMessages");
            props.store(fos, "no comment");
            //moveAdminMessages();
            props.setProperty("state", "moveAuthorToDirections");
            props.store(fos, "no comment");
            //moveAuthorToDirections();
            props.setProperty("state", "moveAuthorSalary");
            props.store(fos, "no comment");
            //moveAuthorSalary();
            props.setProperty("state", "moveOrdersToDirections");
            props.store(fos, "no comment");
            //moveOrdersToDirections();
            props.setProperty("state", "moveAuthorReject");
            props.store(fos, "no comment");
            //moveAuthorReject();
            props.setProperty("state", "moveAuthorViews");
            props.store(fos, "no comment");
            //moveAuthorViews();
            props.setProperty("state", "moveAuthorMessages");
            props.store(fos, "no comment");
            //moveAuthorMessages();
            props.setProperty("state", "addPaymentsToOrders");
            props.store(fos, "no comment");
            //addPaymentsToOrders();
            props.setProperty("state", "moveOrderFiles");
            props.store(fos, "no comment");
            //moveOrderFiles();
            props.setProperty("state", "moveOrderReadyFiles");
            props.store(fos, "no comment");
            //moveOrderReadyFiles();
            props.setProperty("state", "moveAdminMessageFiles");
            props.store(fos, "no comment");
            //moveAdminMessageFiles();
            props.setProperty("state", "moveAuthorMessageFiles");
            props.store(fos, "no comment");
            //moveAuthorMessageFiles();
            addAuthorRights();
            //res=updatePass();

        } else {
            res = "? , ??: " + props.getProperty("state");
        }
        fos.close();
        return res;
    }

    private String updatePass() {
        ServiceResult sr = authorService.updateAuthorsPassFromWords();
        return sr.getErrors().toString();
    }

    /**
     *  ?  ?  ?   
     */
    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));
        }
    }

    private void moveBranches() throws SQLException, ClassNotFoundException, Exception {
        try {
            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));
        }
    }

    private void moveOrderReadyFiles() throws Exception {
        ResultSet set = getOrderReadyFilesFromOldBase();
        while (set.next()) {
            initFtpClient();
            try {
                final TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
                transactionTemplate.execute(new OrderReadyFileTransactionCallback(set));
            } finally {
                logoutFtpClient();
            }
        }
    }

    private void moveAuthorMessageFiles() throws Exception {
        ResultSet set = getAuthorMessageFiles();
        while (set.next()) {
            initFtpClient();
            try {
                final TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
                transactionTemplate.execute(new AuthorMessageFileTransactionCallback(set));
            } finally {
                logoutFtpClient();
            }
        }
    }

    private class AuthorMessageFileTransactionCallback extends TransactionCallbackWithoutResult {

        private ResultSet set;

        AuthorMessageFileTransactionCallback(ResultSet set) {
            this.set = set;
        }

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            try {
                String path = set.getString("path");
                String name = set.getString("name");
                Long oldBidDiscussId = set.getLong("bid_discuss_id");
                Long fileId = set.getLong("id");
                //if (isRightFilePath(path)) {
                if (!hasOldId(fileId, FILE)) {
                    Long newAuthorMessageId = getNewId(oldBidDiscussId, Z_BID_DISCUSS);
                    Message authorMessage = messageDao.find(newAuthorMessageId);
                    String serverFileName = getServerFileName(path);
                    InputStream is = getFileFromFtp(serverFileName);
                    if (is != null) {
                        MessageFile messageFile = new MessageFile();
                        messageFile.setMessage(authorMessage);
                        messageFile.setRusname(name);
                        messageFile.setUploadDate(new Date());
                        Long newFileId = fileService.saveNewFile(messageFile, is);
                        saveLink(fileId, FILE, newFileId, OrderFile.class);
                    }
                }
                //}
            } catch (Exception e) {
                status.setRollbackOnly();
                addErrorMessage(
                        "AdminMessageFileTransactionCallback exception: " + StringAdapter.getStackExeption(e));
            }
        }
    }

    private void moveAdminMessageFiles() throws Exception {
        ResultSet set = getAdminMessageFiles();
        while (set.next()) {
            initFtpClient();
            try {
                final TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
                transactionTemplate.execute(new AdminMessageFileTransactionCallback(set));
            } finally {
                logoutFtpClient();
            }
        }
    }

    private class AdminMessageFileTransactionCallback extends TransactionCallbackWithoutResult {

        private ResultSet set;

        AdminMessageFileTransactionCallback(ResultSet set) {
            this.set = set;
        }

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            try {
                String path = set.getString("path");
                String name = set.getString("name");
                Long oldAdminMessageId = set.getLong("admin_discuss_id");
                Long fileId = set.getLong("id");
                //if (isRightFilePath(path)) {
                if (!hasOldId(fileId, FILE)) {
                    Long newAdminMessageId = getNewId(oldAdminMessageId, ADMIN_DISCUSS);
                    Message adminMessage = messageDao.find(newAdminMessageId);
                    String serverFileName = getServerFileName(path);
                    InputStream is = getFileFromFtp(serverFileName);
                    if (is != null) {
                        MessageFile messageFile = new MessageFile();
                        messageFile.setMessage(adminMessage);
                        messageFile.setRusname(name);
                        messageFile.setUploadDate(new Date());
                        Long newFileId = fileService.saveNewFile(messageFile, is);
                        saveLink(fileId, FILE, newFileId, OrderFile.class);
                    }
                }
                //}
            } catch (Exception e) {
                status.setRollbackOnly();
                addErrorMessage(
                        "AdminMessageFileTransactionCallback exception: " + StringAdapter.getStackExeption(e));
            }
        }
    }

    private void moveOrderFiles() throws Exception {
        ResultSet set = getOrderFilesFromOldBase();
        while (set.next()) {
            initFtpClient();
            try {
                final TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
                transactionTemplate.execute(new OrderFileTransactionCallback(set));
            } finally {
                logoutFtpClient();
            }
        }
    }

    private void moveAuthorResume() throws Exception {
        final ResultSet set = getFromOldBase(
                "select a.id, f.path, f.name, f.id fileId, f.created from z_zauthor a, z_file f where a.resume = f.id");

        int countSuccess = 0;
        int countError = 0;
        while (set.next()) {
            boolean ok = false;
            initFtpClient();
            try {
                final TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);

                ok = transactionTemplate.execute(new TransactionCallback<Boolean>() {

                    @Override
                    public Boolean doInTransaction(TransactionStatus ts) {
                        boolean result = false;
                        try {
                            Long authorId = set.getLong("id");
                            Long newAuthorId = getNewId(authorId, USERS);
                            Author author = authorDao.find(newAuthorId);
                            if (author != null) {
                                String path = set.getString("path");
                                String name = set.getString("name");
                                Long fileId = set.getLong("fileId");
                                Date created = getTimestampOrToday(set, "created");
                                //if (isRightFilePath(path)) {
                                if (!hasOldId(fileId, FILE)) {
                                    String serverFileName = getServerFileName(path);
                                    InputStream is = getFileFromFtp(serverFileName);
                                    AuthorFile fileEnt = new AuthorFile();
                                    fileEnt.setAuthor(author);
                                    fileEnt.setRusname(name);
                                    fileEnt.setUploadDate(created);
                                    Long newFileId = fileService.saveNewFile(fileEnt, is);
                                    saveLink(fileId, FILE, newFileId, AuthorFile.class);
                                    result = true;
                                }
                                //}
                            } else {
                                messageList.add("   ? " + authorId);
                                ts.isRollbackOnly();
                            }
                        } catch (Exception e) {
                            ts.isRollbackOnly();
                            messageList.add("exception " + StringAdapter.getStackExeption(e));
                        }
                        return result;
                    }
                });
            } finally {
                logoutFtpClient();
            }
            if (ok) {
                countSuccess++;
            } else {
                countError++;
            }
        }
        messageList.add("? ? - " + countSuccess);
        messageList.add("?  - " + countError);
    }

    private class OrderReadyFileTransactionCallback extends TransactionCallbackWithoutResult {

        private ResultSet set;

        OrderReadyFileTransactionCallback(ResultSet set) {
            this.set = set;
        }

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            try {
                String path = set.getString("path");
                String name = set.getString("name");
                Long oldOrderId = set.getLong("order_id");
                Long fileId = set.getLong("id");
                //if (isRightFilePath(path)) {
                if (!hasOldId(fileId, FILE)) {
                    Long newOrderId = getNewId(oldOrderId, ORDER);
                    Order order = getOrder(newOrderId);
                    String serverFileName = getServerFileName(path);
                    InputStream is = getFileFromFtp(serverFileName);
                    if (is != null) {
                        ReadyOrderFile orderFile = new ReadyOrderFile();
                        orderFile.setOrder(order);
                        orderFile.setRusname(name);
                        orderFile.setUploadDate(new Date());
                        Long newFileId = fileService.saveNewFile(orderFile, is);
                        saveLink(fileId, FILE, newFileId, OrderFile.class);
                    }
                }
                //}
            } catch (Exception e) {
                status.setRollbackOnly();
                addErrorMessage("OrderFileTransactionCallback exception: " + StringAdapter.getStackExeption(e));
            }
        }
    }

    private class OrderFileTransactionCallback extends TransactionCallbackWithoutResult {

        private ResultSet set;

        OrderFileTransactionCallback(ResultSet set) {
            this.set = set;
        }

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            try {
                String path = set.getString("path");
                String name = set.getString("name");
                Long oldOrderId = set.getLong("order_id");
                Long fileId = set.getLong("id");
                //if (isRightFilePath(path)) {
                if (!hasOldId(fileId, FILE)) {
                    Long newOrderId = getNewId(oldOrderId, ORDER);
                    Order order = getOrder(newOrderId);
                    String serverFileName = getServerFileName(path);
                    InputStream is = getFileFromFtp(serverFileName);
                    if (is != null) {
                        OrderFile orderFile = new OrderFile();
                        orderFile.setOrder(order);
                        orderFile.setRusname(name);
                        orderFile.setUploadDate(new Date());
                        Long newFileId = fileService.saveNewFile(orderFile, is);
                        saveLink(fileId, FILE, newFileId, OrderFile.class);
                    }
                }
                //}
            } catch (Exception e) {
                status.setRollbackOnly();
                addErrorMessage("OrderFileTransactionCallback exception: " + StringAdapter.getStackExeption(e));
            }
        }
    }

    private String filesPathInDb = "/home/d/dekkar/system.vuz59/public_html/files/";

    private FTPClient ftpClient;

    private void initFtpClient() throws Exception {
        ftpClient = new FTPClient();
        String server = "92.53.114.87";
        String login = "dekkar_ivan";
        String password = "qwerty123";
        ftpClient.connect(server);
        boolean ok = ftpClient.login(login, password);

        if (!ok) {
            ftpClient.logout();
            throw new Exception(" ? ??  FTP");
        }
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        ftpClient.enterLocalPassiveMode();
        int reply = ftpClient.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftpClient.disconnect();
            throw new Exception(" ? ??  FTP.  ReplyCode");
        }
    }

    private void logoutFtpClient() throws Exception {
        if (ftpClient != null) {
            ftpClient.logout();
            ftpClient.disconnect();
        }
    }

    private InputStream getFileFromFtp(String fileName) throws Exception {
        InputStream is = ftpClient.retrieveFileStream(fileName);
        if (is == null) {
            int replyCode = ftpClient.getReplyCode();
            messageList.add("problem with file: " + fileName + ", replyCode: " + replyCode);
            log.warn("problem with file: " + fileName + ", replyCode: " + replyCode);
        }
        return is;
    }

    private String getServerFileName(String path) {
        return path.substring(new String("/home/d/dekkar").length());
    }

    private boolean isRightFilePath(String path) {
        return path.contains(filesPathInDb);
    }

    private ResultSet getOrderReadyFilesFromOldBase() throws Exception {
        return getFromOldBase(
                "select * from z_zorder_ready_file of, z_file f where of.file_id = f.id and (f.path like '/home/d/dekkar/system.vuz59/public_html/files/%' or f.path like '/home/d/dekkar/vuz59/public_html/zachetka/files/%')");
    }

    private ResultSet getOrderFilesFromOldBase() throws Exception {
        return getFromOldBase(
                "select * from z_zorder_file of, z_file f where of.file_id = f.id and (f.path like '/home/d/dekkar/system.vuz59/public_html/files/%' or f.path like '/home/d/dekkar/vuz59/public_html/zachetka/files/%')");
    }

    private ResultSet getAdminMessageFiles() throws Exception {
        return getFromOldBase(
                "select * from z_zadmin_discuss_file of, z_file f where of.file_id = f.id and (f.path like '/home/d/dekkar/system.vuz59/public_html/files/%' or f.path like '/home/d/dekkar/vuz59/public_html/zachetka/files/%')");
    }

    private ResultSet getAuthorMessageFiles() throws Exception {
        return getFromOldBase(
                "select * from z_zbid_discuss_file of, z_file f where of.file_id = f.id and (f.path like '/home/d/dekkar/system.vuz59/public_html/files/%' or f.path like '/home/d/dekkar/vuz59/public_html/zachetka/files/%')");
    }

    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
                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));
                    }
                }
            });
        }
    }

    private void saveAdmin(String email, String password, String firstname, String lastname, String fathername,
            String phone, Long userId, String modelName) throws Exception {
        Admin admin = new Admin();
        admin.setLogin(email);
        admin.setPassword(password);
        admin.setName(firstname);
        admin.setSurname(lastname);
        admin.setMiddlename(fathername);
        admin.setPhone(phone);
        saveObjectAndLink(admin, userId, modelName);
    }

    private void addErrorMessage(String message) {
        log.warn(message);
        messageList.add(message);
    }

    private void moveOrderTypes() throws SQLException, ClassNotFoundException, Exception {
        try {
            final String tableName = TYPE;
            final ResultSet typeSet = getFromOldBase(getSelectAll(tableName));
            while (typeSet.next()) {

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

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

                            String name = typeSet.getString("name");
                            OrderType orderType = new OrderType();
                            orderType.setName(name);
                            saveObjectAndLink(orderType, oldId, tableName);
                        } catch (Exception e) {
                            ts.setRollbackOnly();
                            addErrorMessage("type: " + oldId + " " + StringAdapter.getStackExeption(e));
                        }
                    }
                });

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

    private void moveAdminMessages() throws SQLException, ClassNotFoundException, Exception {
        try {
            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)));
        }
    }

    private void moveAuthorMessages() throws Exception {
        try {
            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));
        }
    }

    /*
     private void moveAuthorMessages() throws Exception {
     try {
     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, 20000");
     while (set.next()) {
     Long oldDiscussId = set.getLong("id");
     Long oldOrderId = set.getLong("order_id");
     Long oldAuthorId = set.getLong("author_id");
     Long oldAdminId = set.getLong("admin_id");
     BigDecimal reward = set.getBigDecimal("reward");
     Double cost = (reward != null ? reward.doubleValue() : 0.0);
     String text = set.getString("message");
     Date created = getDateOrToday(set, "created");
     boolean viewedAuthor = set.getBoolean("viewed_author");
     boolean viewedAdmin = set.getBoolean("viewed_admin");
        
     Long newOrderId = getNewId(oldOrderId, ORDER);
     Long newAuthorId = getNewId(oldAuthorId, USERS);
     User admin = null;
     if (oldAdminId != null && oldAdminId != 0) {
     Long newAdminId = getNewId(oldAdminId, USERS);
     admin = getUser(newAdminId);
     }
        
     Order order = getOrder(newOrderId);
     Author author = getAuthor(newAuthorId);
        
     AuthorMessage authorMessage = new AuthorMessage();
     authorMessage.setText(text);
     authorMessage.setOrder(order);
     authorMessage.setCost(cost);
     authorMessage.setMessageDate(created);
     if (admin != null) {
     authorMessage.setUser(admin);
     authorMessage.setDestinationAuthor(author);
     authorMessage.setReady(viewedAuthor);
     } else {
     authorMessage.setUser(author);
     authorMessage.setReady(viewedAdmin);
     }
     save(authorMessage, oldDiscussId, modelName);
     }
     } catch (Throwable th) {
     log.warn("moveAuthorMessages " + StringAdapter.getStackExeption(th));
     }
     }
     */

    /*
     private void moveAuthorMessages() throws SQLException, ClassNotFoundException, Exception {
     String tableName = AUTHOR_DISCUSS;
     ResultSet set = getFromOldBase(getSelectAll(tableName));
     while (set.next()) {
     Long oldId = set.getLong("id");
     try {
     Long oldOrderId = set.getLong("order_id");
     Long oldUserId = set.getLong("admin_id");
     Date createDate = set.getDate("created");
     String message = set.getString("message");
     Long newOrderId = getNewId(oldOrderId, ORDER);
     Long newUserId = getNewId(oldUserId, USERS);
     Order order = orderDao.find(newOrderId);
     User user = userDao.find(newUserId);
        
     AuthorMessage ent = new AuthorMessage();
     ent.setMessageDate(createDate);
     ent.setOrder(order);
     ent.setUser(user);
     ent.setText(message);
     save(ent, oldId, tableName);
     } catch (Throwable e) {
     log.warn("author: " + oldId + " " + StringAdapter.getStackExeption(e));
     }
     }
     }
     */
    public List<Payment> getAllPayments() {
        return paymentDao.getAll();
    }

    private void addPaymentsToOrders() throws Exception {
        String tableName = ORDER;
        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));
            }

        }
    }

    private boolean containsPayment(List<Payment> paymentList, Order order) {
        for (Payment payment : paymentList) {
            Order orderFromPayment = payment.getOrder();
            if (orderFromPayment != null) {
                if (orderFromPayment.getOrderId().equals(order.getOrderId())) {
                    return true;
                }
            }
        }
        return false;
    }

    private void moveOrders() throws SQLException, ClassNotFoundException, Exception {
        //    
        final String tableName = ORDER;
        final ResultSet orderSet = getFromOldBase(getSelectAll(tableName, "id"));
        while (orderSet.next()) {
            // ?  
            TransactionTemplate temp = new TransactionTemplate(transactionManager);
            temp.execute(new TransactionCallbackWithoutResult() {

                @Override
                protected void doInTransactionWithoutResult(TransactionStatus ts) {
                    Long oldId = 0L;
                    try {
                        oldId = orderSet.getLong("id");
                        String subject = orderSet.getString("theme");
                        if (subject.length() > 250) {
                            subject = subject.substring(0, 250);
                        }
                        Long oldBranchId = orderSet.getLong("branch_id");
                        Long oldOrderTypeId = orderSet.getLong("type_id");
                        String city = orderSet.getString("university");
                        Double price = orderSet.getDouble("price");
                        Double authorSalary = orderSet.getDouble("reward");
                        Date redline = orderSet.getDate("redline");
                        Date deadline = orderSet.getDate("deadline");
                        Long oldAuthorId = orderSet.getLong("author_id");
                        Long clientId = orderSet.getLong("client_id");
                        String rewardComment = orderSet.getString("reward_comment");
                        Boolean flag_0 = orderSet.getBoolean("flag_0");
                        Boolean flag_1 = orderSet.getBoolean("flag_1");
                        Timestamp timestamp = orderSet.getTimestamp("ready_date");
                        String readyComment = orderSet.getString("ready_comment");
                        String comment = orderSet.getString("description");
                        Date readyDate = null;
                        if (timestamp != null) {
                            readyDate = new Date(timestamp.getTime());
                        }

                        Long newBranchId = getNewId(oldBranchId, BRANCH);
                        Branch branch = branchDao.find(newBranchId);

                        Long newAuthorId = getNewId(oldAuthorId, USERS);
                        Author author = authorDao.find(newAuthorId);

                        Long newOrderTypeId = getNewId(oldOrderTypeId, TYPE);
                        OrderType orderType = orderTypeDao.find(newOrderTypeId);

                        Integer statusInt = orderSet.getInt("status");
                        OrderStatus status = getStatus(statusInt);
                        if (status == null) {
                            status = OrderStatus.NEW;
                        }

                        Date orderDate = getDateOrToday(orderSet, "created");

                        Order order = new Order();
                        order.setStatus(status);
                        order.setOrderDate(orderDate);
                        order.setAuthor(author);
                        order.setBranch(branch);
                        order.setOrderType(orderType);
                        order.setSubject(subject);
                        order.setCity(city);
                        order.setCost(price);
                        order.setAuthor_salary(authorSalary);
                        //  ? ? , ? 
                        order.setRealDate(deadline);
                        order.setDeadlineDate(redline);
                        order.setAuthorComment(readyComment);
                        order.setComment(comment);

                        order.setReadyDate(readyDate);
                        order.setFirstFlag(flag_0);
                        order.setSecondFlag(flag_1);
                        order.setCommentToAuthorSalary(rewardComment);
                        order.setOldId(StringAdapter.getString(oldId));

                        ResultSet clientSet = getFromOldBase("select * from " + CLIENT + " where id = " + clientId);
                        String clientEmail = null;
                        String clientFio = null;
                        String clientPhone = null;
                        while (clientSet.next()) {
                            clientEmail = trim(clientSet.getString("email"));
                            clientFio = clientSet.getString("firstname") + " " + clientSet.getString("fathername")
                                    + " " + clientSet.getString("lastname");
                            clientPhone = clientSet.getString("phone");
                        }
                        if (ValidatorUtils.isEmail(clientEmail)) {
                            order.setClientEmail(clientEmail);
                        }
                        order.setClientFio(clientFio);
                        order.setClientPhone(clientPhone);

                        saveObjectAndLink(order, oldId, tableName);
                    } catch (Throwable e) {
                        ts.setRollbackOnly();
                        addErrorMessage("order: " + oldId + " " + StringAdapter.getStackExeption(e));
                        log.warn("order: " + oldId + " " + StringAdapter.getStackExeption(e));
                    }
                }
            });

        }

    }

    private Date getTimestampOrToday(ResultSet set, String paramName) {
        try {
            return new Date(set.getTimestamp(paramName).getTime());
        } catch (Exception e) {
            return new Date();
        }
    }

    @Deprecated
    private Date getDateOrToday(ResultSet set, String paramName) {
        try {
            return set.getDate(paramName);
        } catch (Exception e) {
            return new Date();
        }
    }

    private void moveDirections() throws SQLException, ClassNotFoundException, Exception {
        try {
            final String tableName = 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 oldId = null;
                        try {
                            oldId = set.getLong("id");
                            String name = set.getString("name");
                            Direction dir = new Direction();
                            dir.setName(name);
                            saveObjectAndLink(dir, oldId, tableName);
                        } catch (Exception e) {
                            addErrorMessage("direction: " + oldId + " " + StringAdapter.getStackExeption(e));
                            log.warn("direction: " + oldId + " " + StringAdapter.getStackExeption(e));
                        }
                    }
                });

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

    private void moveAuthorToDirections() throws SQLException, ClassNotFoundException, Exception {
        try {
            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));
        }
    }

    private void moveAuthorViews() throws Exception {
        try {
            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));
        }
    }

    private boolean contains(List<OrderView> allList, User user, Order order) {
        for (OrderView view : allList) {
            if (view != null && view.getOrder() != null && view.getUser() != null) {
                if (view.getOrder().getOrderId().equals(order.getOrderId())
                        && view.getUser().getUserId().equals(user.getUserId())) {
                    return true;
                }
            }
        }
        return false;
    }

    private void moveOrdersToDirections() throws SQLException, ClassNotFoundException, Exception {
        try {
            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));
        }
    }

    private boolean contains(List<Direction> dirList, Direction direction) {
        if (dirList != null && direction != null && direction.getDirectionId() != null) {
            for (Direction dirFromList : dirList) {
                if (dirFromList != null && dirFromList.getDirectionId() != null) {
                    if (dirFromList.getDirectionId().equals(direction.getDirectionId())) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    private void moveAuthorSalary() throws Exception {
        try {
            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));
        }
    }

    private void moveAuthorReject() throws Exception {
        final String tableName = AUTHOR_REJECT;
        final ResultSet set = getFromOldBase(getSelectAll(tableName));
        final List<AuthorReject> allList = authorRejectDao.getAll();
        while (set.next()) {

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

                @Override
                protected void doInTransactionWithoutResult(TransactionStatus ts) {
                    String st = "";
                    try {
                        Long oldAuthorId = set.getLong("author_id");
                        Long oldOrderId = set.getLong("order_id");
                        Long newAuthorId = getNewId(oldAuthorId, USERS);
                        Long newOrderId = getNewId(oldOrderId, ORDER);
                        st = "oldAuth" + oldAuthorId + " oldOrd" + oldOrderId + " newAuth" + newAuthorId + " newOrd"
                                + newOrderId;
                        Author author = getAuthor(newAuthorId);
                        Order order = getOrder(newOrderId);
                        AuthorReject authorReject = new AuthorReject();
                        authorReject.setAuthor(author);
                        authorReject.setOrder(order);
                        if (!exist(allList, author, order)) {
                            authorRejectDao.save(authorReject);
                        }
                    } catch (Exception e) {
                        ts.setRollbackOnly();
                        addErrorMessage("moveAuthorReject: " + st + StringAdapter.getStackExeption(e));
                        log.warn("moveAuthorReject: " + st + StringAdapter.getStackExeption(e));
                    }
                }
            });

        }
    }

    private Map<Long, Author> authorsMap;

    private Map<Long, Order> ordersMap;

    private Map<Long, User> usersMap;

    private Author getAuthor(Long authorId) {
        if (authorsMap == null) {
            authorsMap = new HashMap();
            List<Author> authorsList = authorDao.getAll();
            for (Author author : authorsList) {
                authorsMap.put(author.getUserId(), author);
            }
        }
        return authorsMap.get(authorId);
    }

    private Order getOrder(Long orderId) {
        if (ordersMap == null) {
            ordersMap = new HashMap();
            List<Order> orderList = orderDao.getAll();
            for (Order order : orderList) {
                ordersMap.put(order.getOrderId(), order);
            }
        }
        return ordersMap.get(orderId);
    }

    private User getUser(Long userId) {
        if (usersMap == null) {
            usersMap = new HashMap();
            List<User> userList = userDao.getAll();
            for (User user : userList) {
                usersMap.put(user.getUserId(), user);
            }
        }
        return usersMap.get(userId);
    }

    private boolean exist(List<AuthorReject> allList, Author author, Order order) {
        if (allList != null && author != null && order != null && author.getUserId() != null
                && order.getOrderId() != null) {
            for (AuthorReject reject : allList) {
                if (reject.getAuthor() != null && reject.getOrder() != null
                        && reject.getAuthor().getUserId() != null && reject.getOrder().getOrderId() != null) {
                    if (reject.getAuthor().getUserId().equals(author.getUserId())
                            && reject.getOrder().getOrderId().equals(order.getOrderId())) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    private void saveLink(Long oldId, String oldName, Long newId, Class newCl) {
        dataLinkService.saveLink(oldId, oldName, newId, newCl);
    }

    private ResultSet getUserProfile(Long userId) throws SQLException, ClassNotFoundException {
        return getFromOldBase("select * from " + USER_PROFILE + " where user_id = " + userId);
    }

    private ResultSet getAdmin(Long userId) throws SQLException, ClassNotFoundException {
        String sql = "select * from " + ADMIN + " where id = " + userId;
        return getFromOldBase(sql);
    }

    /**
     *
     * @param oldId - ? 
     * @param oldName -  ? 
     * @return
     */
    /*
     private Long getNewId(Long oldId, String oldName) throws Exception {
     if (oldId != null && oldId != 0) {
     Long newId = dataLinkService.getNewId(oldId, oldName);
     if (newId == null) {
     throw new Exception("oldId " + oldId + " newId " + newId);
     }
     return newId;
     } else {
     return null;
     }
     }
     */
    private final Map<String, List<DataLink>> linkMap = new HashMap();

    private final Map<String, Map<Long, DataLink>> linkDoubleMap = new HashMap();

    private Long getNewId(Long oldId, String oldName) throws Exception {
        initLinkDoubleMap(oldName);
        DataLink link = linkDoubleMap.get(oldName).get(oldId);
        if (link != null) {
            return link.getNewId();
        } else {
            return null;
        }
    }

    private void initLinkDoubleMap(String oldName) {
        if (linkDoubleMap.get(oldName) == null) {
            List<DataLink> linkList = dataLinkService.getByName(oldName);
            Map<Long, DataLink> map = new HashMap();
            for (DataLink link : linkList) {
                map.put(link.getOldId(), link);
            }
            linkDoubleMap.put(oldName, map);
        }
    }

    /*
     private Long getNewId(Long oldId, String oldName) throws Exception {
     if (linkMap.get(oldName) == null) {
     List<DataLink> linkList = dataLinkService.getByName(oldName);
     linkMap.put(oldName, linkList);
     }
     if (oldId != null && oldId != 0) {
     List<DataLink> linkList = linkMap.get(oldName);
     for (DataLink link: linkList) {
     Long oldIdFromList = link.getOldId();
     if (oldIdFromList.equals(oldId)) {
     return link.getNewId();
     }
     }
     }
     return null;
     }
     */
    private int getRowCount(ResultSet res) throws SQLException {
        int count = 0;
        while (res.next()) {
            count++;
        }
        res.first();
        return count;
    }

    private String getSelectAll(String modelName, String limitFrom, String limitCount, String order) {
        return "select * from " + modelName + " order by " + order + " limit " + limitFrom + ", " + limitCount;
    }

    private String getSelectAll(String modelName, String limit, String order) {
        return "select * from " + modelName + " order by " + order + " limit " + limit;
    }

    private String getSelectAll(String modelName, String order) {
        return "select * from " + modelName + " order by " + order;
    }

    private String getSelectAll(String modelName) {
        return "select * from " + modelName;
    }

    private boolean newHasOldId(Long id, String oldModelName) {
        initLinkDoubleMap(oldModelName);
        Map<Long, DataLink> map = linkDoubleMap.get(oldModelName);
        return (map.containsKey(id));
    }

    private boolean hasOldId(Long id, String oldModelName) {
        return dataLinkService.existOldId(id, oldModelName);
    }

    private OrderStatus getStatus(Integer statusNumber) {
        if (statusNumber == null) {
            return null;
        }
        switch (statusNumber) {
        case 0:
            return OrderStatus.NEW;
        case 1:
            return OrderStatus.CONFIRMATION;
        case 2:
            return OrderStatus.WORKING;
        case 3:
            return OrderStatus.REWORK;
        case 4:
            return OrderStatus.CHECK;
        case 5:
            return OrderStatus.READY;
        case 6:
            return OrderStatus.ARCHIVE;
        case 7:
            return OrderStatus.OTHER;
        case 8:
            return OrderStatus.REJECTION;

        }
        return null;
    }

    private String trim(String str) {
        return (str != null ? str.trim() : str);
    }

    private ResultSet getFromOldBase(String query) throws SQLException, ClassNotFoundException {
        Statement st = getConnection().createStatement();
        ResultSet result = st.executeQuery(query);
        return result;
    }

    private void saveObjectAndLink(PrimEntity entity, Long oldId, String tableName) throws Exception {
        if (!hasOldId(oldId, tableName)) {
            Dao dao = null;
            Class cl = null;
            if (entity instanceof User) {
                dao = userDao;
                cl = User.class;
            } else if (entity instanceof Branch) {
                dao = branchDao;
                cl = Branch.class;
            } else if (entity instanceof OrderType) {
                dao = orderTypeDao;
                cl = OrderType.class;
            } else if (entity instanceof Direction) {
                dao = directionDao;
                cl = Direction.class;
            } else if (entity instanceof Order) {
                dao = orderDao;
                cl = Order.class;
            } else if (entity instanceof Message) {
                dao = messageDao;
                cl = Message.class;
            } else if (entity instanceof AuthorSalary) {
                dao = authorSalaryDao;
                cl = AuthorSalary.class;
            }
            if (dao == null) {
                throw new Exception(" ??  ??");
            }
            Long newId = dao.save(entity);
            saveLink(oldId, tableName, newId, cl);
        }
    }

    /*
     private void save(PrimEntity entity, Long oldId, String tableName) throws Exception {
     TransactionCallback tc = new TransactionCallback(entity, oldId, tableName);
     final TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
     transactionTemplate.execute(tc);
     }
     */
    private Connection getConnection() throws ClassNotFoundException, SQLException {
        if (conn == null) {
            String dbDriver = "com.mysql.jdbc.Driver";
            String dbName = "old_base";
            String dbUser = "vuz";
            String dbPass = "nbkbrjvgk.p";
            String dbEncoding = "UTF-8";
            Class.forName(dbDriver);
            String url = "jdbc:mysql://localhost:3306/" + dbName + "?autoReconnect=true";
            Properties properties = new Properties();
            properties.setProperty("user", dbUser);
            properties.setProperty("password", dbPass);
            properties.setProperty("useUnicode", "true");
            properties.setProperty("characterEncoding", dbEncoding);
            conn = DriverManager.getConnection(url, properties);
        }
        return conn;
    }

    public List<String> getMessageList() {
        return messageList;
    }

    private class AuthorMessageTransactionCallback extends TransactionCallbackWithoutResult {

        private ResultSet set;

        private String modelName;

        AuthorMessageTransactionCallback(ResultSet set, String modelName) {
            this.set = set;
            this.modelName = modelName;
        }

        protected void doInTransactionWithoutResult(TransactionStatus status) {
            try {
                Long oldDiscussId = set.getLong("id");
                Long oldOrderId = set.getLong("order_id");
                Long oldAuthorId = set.getLong("author_id");
                Long oldAdminId = set.getLong("admin_id");
                BigDecimal reward = set.getBigDecimal("reward");
                Double cost = (reward != null ? reward.doubleValue() : 0.0);
                String text = set.getString("message");
                Timestamp created = set.getTimestamp("created");
                boolean viewedAuthor = set.getBoolean("viewed_author");
                boolean viewedAdmin = set.getBoolean("viewed_admin");

                Long newOrderId = getNewId(oldOrderId, ORDER);
                Long newAuthorId = getNewId(oldAuthorId, USERS);
                User admin = null;
                if (oldAdminId != null && oldAdminId != 0) {
                    Long newAdminId = getNewId(oldAdminId, USERS);
                    admin = getUser(newAdminId);
                }

                Order order = getOrder(newOrderId);
                Author author = getAuthor(newAuthorId);

                AuthorMessage authorMessage = new AuthorMessage();
                authorMessage.setText(text);
                authorMessage.setOrder(order);
                authorMessage.setCost(cost);
                authorMessage.setMessageDate(created);
                if (admin != null) {
                    authorMessage.setUser(admin);
                    authorMessage.setDestinationAuthor(author);
                    authorMessage.setReady(viewedAuthor);
                } else {
                    authorMessage.setUser(author);
                    authorMessage.setReady(viewedAdmin);
                }
                saveObjectAndLink(authorMessage, oldDiscussId, modelName);
            } catch (Throwable e) {
                status.setRollbackOnly();
                addErrorMessage("error!!!!!" + StringAdapter.transliterate(StringAdapter.getStackExeption(e)));
            }
        }

    }

}
/**
 delete from author_salary;
 delete from order_view;
 delete from author_rejection;
 delete from order_to_direction;
 delete from message;
 delete from payment;
 delete from orders;
 delete from data_link where new_model_name='entity.Payment';
 delete from data_link where new_model_name='entity.Message';
 delete from data_link where new_model_name='entity.Order';
 delete from data_link where new_model_name='entity.AuthorSalary';
 alter table orders AUTO_INCREMENT 0;
 * 
 update user as u,data_link as ln set active=false where u.user_id=ln.new_id
and ln.new_model_name='entity.User'and old_id in(6,10,11,16,21,22,31,33,34,44
,46,59,61,62,78,81,85,86,90,92,95,96,97,100,103,104,107,111,114,119,120,123,125,128
,129,130,136,141,142,145,148,150,156,158,160,163,164,166,168,169,170,175,177
,178,179,183,184,185,187,188,189,193,194,195,198,200,201,202,204,207,208,216
,218,219,220,222,229,235,241,244,248,250,251,259,271,276,280,298,299,305,313
,317,324,326,334,337,342,329,380,395,403);
     
update message m, orders o set m.ready=1 where m.order_id=o.order_id and o.status in ('ARCHIVE','REJECTION');
    
    
insert into user_right (user_id,right_name) select u.user_id,ur.right_name from user u, user_right ur where ur.user_id=4128 
and not exists(select 1 from user_right urr where urr.user_id=u.user_id and urr.right_name=ur.right_name);
    
    
 */