com.sds.acube.ndisc.xmigration.XNDiscMigration.java Source code

Java tutorial

Introduction

Here is the source code for com.sds.acube.ndisc.xmigration.XNDiscMigration.java

Source

/*
 * <pre>
 * Copyright (c) 2014 Samsung SDS.
 * All right reserved.
 *
 * This software is the confidential and proprietary information of Samsung
 * SDS. You shall not disclose such Confidential Information and
 * shall use it only in accordance with the terms of the license agreement
 * you entered into with Samsung SDS.
 *
 * Author             : Takkies
 * Date                : 2014. 04. 01.
 * Description      : 
 * </pre>
 */
package com.sds.acube.ndisc.xmigration;

import java.util.ArrayList;
import java.util.Arrays;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.sds.acube.jstor.JSTORApi;
import com.sds.acube.jstor.JSTORApiFactory;
import com.sds.acube.ndisc.model.NFile;
import com.sds.acube.ndisc.xmigration.db.XNDiscMigConnDB;
import com.sds.acube.ndisc.xmigration.db.XNDiscMigDBUtil;
import com.sds.acube.ndisc.xmigration.db.XNDiscMigData;
import com.sds.acube.ndisc.xmigration.db.XNDiscMigItem;
import com.sds.acube.ndisc.xmigration.util.XNDiscMigConfig;
import com.sds.acube.ndisc.xmigration.util.XNDiscMigUtil;
import com.sds.acube.ndisc.xnapi.XNApi;

/**
 * 
 * @author Takkies
 *
 */
public class XNDiscMigration {

    /* XMigration  */
    private static String XMIGRATION_VERSION;

    /* logger ? */
    private static Logger logger = Logger.getLogger(XNDiscMigration.class);

    /* Migration  ? JSTOR Api ? */
    private JSTORApi jSTOR = null;

    /* Migration   api ? */
    private XNApi xNApi = null;

    /* Migration DB ? */
    private XNDiscMigConnDB migdb = null;

    /* Migration  ? JSTOR  ? */
    private int jstorConnID = -1;

    /* Migration    ? */
    private int ndiscConnID = -1;

    /* Migration ?  ?  */
    private String MIG_TABLE = XNDiscMigConfig.getString("mig-table", "S2N_MIG");

    /* Migration   */
    private StringBuilder msg = new StringBuilder();

    /* OS  ??  */
    protected static String LINE_SEPERATOR = System.getProperty("line.separator");

    static {
        XMIGRATION_VERSION = "XMigration " + XNDiscMigUtil.getXMigrationVersion() + "("
                + XNDiscMigUtil.getXMigrationPublshingDate() + ")";
        StringBuilder smsg = new StringBuilder(LINE_SEPERATOR);
        smsg.append("").append(StringUtils.rightPad("", 60, "-")).append("?").append(LINE_SEPERATOR);
        smsg.append("").append(StringUtils.center("", 60, " ")).append("").append(LINE_SEPERATOR);
        smsg.append("").append(StringUtils.rightPad("   Company        : SAMSUNG SDS", 60, " ")).append("")
                .append(LINE_SEPERATOR);
        smsg.append("").append(StringUtils.rightPad("   Product Name   : ACUBE XMIGRATION", 60, " "))
                .append("").append(LINE_SEPERATOR);
        smsg.append("").append(StringUtils.rightPad("   Version        : " + XMIGRATION_VERSION, 60, " "))
                .append("").append(LINE_SEPERATOR);
        smsg.append("").append(StringUtils.center("", 60, " ")).append("").append(LINE_SEPERATOR);
        smsg.append("").append(StringUtils.rightPad("", 60, "-")).append("").append(LINE_SEPERATOR);
        System.out.println(smsg.toString());
    }

    public static void main(String args[]) {
        XNDiscMigConnDB db = XNDiscMigDBUtil.create(true);
        try {
            XNDiscMigration mig = new XNDiscMigration(db);
            if (mig.connect()) {
                logger.debug("*** jstor, ndisc connection successfully!!!");
                if (mig.execute()) {
                    logger.debug("*** migration successfully!!!");
                    mig.disconnect();
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        } finally {
            try {
                db.commit();
                db.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * ??
     * 
     * @param migdb Migration  DB ?
     */
    public XNDiscMigration(XNDiscMigConnDB migdb) {
        this.migdb = migdb;
    }

    /**
     * Migration ?, ? 
     * 
     * @return   true,  false
     */
    public boolean connect() {
        boolean rtn = false;
        rtn = getSTORConnection();
        if (rtn) {
            rtn = getXNApiConnection();
        }
        return rtn;
    }

    /**
     * Migration 
     * 
     * @return Migration ? true, ? false
     */
    public boolean execute() {
        boolean rtn = false;
        try {
            if (migdb == null) {
                throw new Exception("Connection is null!!!");
            }
            if (!migdb.hasTable(MIG_TABLE)) {
                logger.debug("*** create migration table[" + MIG_TABLE + "]");
                migdb.appendSQL(XNDiscMigConnDB.getMigrationTable());
                rtn = migdb.queryUpdate() >= 0;
                if (rtn) {
                    migdb.commit();
                }
            }
            if (rtn) {
                migdb.init();
                migdb.appendSQL(XNDiscMigConnDB.getJSTORListCount());
                int total_data_count = migdb.queryInt(0); //  
                int process_once_count = XNDiscMigConfig.getInt("stor-get-count", 100);
                logger.debug("*** total migration count : " + total_data_count);
                logger.debug("*** process once count : " + process_once_count);
                if (total_data_count > 0) {
                    int do_count = (int) Math.ceil((float) total_data_count / (float) process_once_count);
                    do_count = (do_count == 0) ? 1 : do_count; //   ? 1??.
                    logger.debug("*** do count : " + do_count);
                    if (do_count > 0) {
                        int start = 0;
                        int end = 0;
                        for (int i = 0; i < do_count; i++) {
                            XNDiscMigUtil.clearConsoleOutput();
                            msg.setLength(0);
                            start = i * process_once_count; // ?   
                            end = (i + 1) * process_once_count; // ?  ?? 
                            logger.debug("*** migration start : " + start + ", end : " + end);
                            msg.append("*** jstor to xndisc migration paging list from " + start + " to " + end
                                    + XNDiscMigUtil.LINE_SEPERATOR);
                            rtn = migration(start, end);
                            if (rtn) {
                                System.out.println(msg.toString());
                            } else {
                                System.out.println("*** Fail to migration jstor to xndisc!!!");
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return rtn;
    }

    /**
     * Migration  <br>
     * Migration ?  ?? ? .<br>
     * 
     * @param start Migration ? ?? 
     * @param end Migration ? ?? ??
     * @return  ? true, ? false
     */
    private boolean migration(int start, int end) {
        boolean rtn = false;
        try {
            ArrayList<XNDiscMigItem> list = null;
            migdb.init();
            migdb.appendSQL(XNDiscMigConnDB.getJSTORList(start, end));
            ArrayList<XNDiscMigData> jstorlist = migdb.queryList();
            if (jstorlist != null && jstorlist.size() > 0) {
                list = new ArrayList<XNDiscMigItem>();
                XNDiscMigData data = null;
                XNDiscMigItem item = null;
                int mgrs = (jstorlist == null) ? 0 : jstorlist.size();
                for (int i = 0; i < mgrs; i++) {
                    item = new XNDiscMigItem();
                    data = (XNDiscMigData) jstorlist.get(i);
                    item.setFileId(XNDiscMigUtil.getString(data.getString("FLE_ID")));
                    item.setFileMediaId(data.getInt("FLE_MDID"));
                    item.setFileName(XNDiscMigUtil.getString(data.getString("FLE_NAME")));
                    item.setFileRegDate(XNDiscMigUtil.getString(data.getString("FLE_CRTDT")));
                    item.setFileSize(data.getInt("FLE_SIZE"));
                    item.setFileStatus(XNDiscMigUtil.getString(data.getString("FLE_STATUS")));
                    list.add(item);
                }
                if (list != null && list.size() > 0) {
                    rtn = downloadJSTOR(list);
                    if (rtn) {
                        logger.debug("*** jstor down load : " + rtn);
                        rtn = uploadNDISC(list);
                        logger.debug("*** ndisc upload load : " + rtn);
                        if (rtn) {
                            rtn = removeTempFiles(list);
                        }
                    }
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return rtn;
    }

    /**
     * Migration ?,   ?
     */
    public void disconnect() {
        try {
            jSTOR.JSTOR_Disconnect(getJstorConnID());
            xNApi.XNDisc_Disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Migration ? JSTOR 
     * 
     * @return ? true,   false
     */
    private boolean getSTORConnection() {
        boolean rtn = true;
        try {
            System.setProperty("jstor_api_type", XNDiscMigConfig.getString("stor-api-type"));
            System.setProperty("jstor_svr_type", XNDiscMigConfig.getString("stor-svr-type"));

            String stor_host = XNDiscMigConfig.getString("stor-host");
            int stor_port = XNDiscMigConfig.getInt("stor-port");

            logger.debug("*** jstor ip : " + stor_host + ", port : " + stor_port);

            JSTORApiFactory jsFactory = new JSTORApiFactory();
            jSTOR = jsFactory.getInstance();

            setJstorConnID(jSTOR.JSTOR_Connect(stor_host, stor_port));
            if (getJstorConnID() < 0) {
                rtn = false;
                throw new Exception(
                        "stor connection fail - " + jSTOR.JSTOR_getErrCode() + ", " + jSTOR.JSTOR_getErrMsg());
            }
        } catch (Exception e) {
            rtn = false;
            logger.error(e.getMessage());
        }
        return rtn;
    }

    /**
     * Migration ? 
     * 
     * @return   true,  false
     */
    private boolean getXNApiConnection() {
        boolean rtn = true;
        try {
            System.setProperty("xnapi_ssl", XNDiscMigConfig.getString("xnapi-ssl"));
            System.setProperty("xnapi_keyfile", XNDiscMigConfig.getString("xnapi-keyfile"));
            System.setProperty("xnapi_keypwd", XNDiscMigConfig.getString("xnapi-keypwd"));
            String ndisc_host = XNDiscMigConfig.getString("ndisc-host");
            int ndisc_port = XNDiscMigConfig.getInt("ndisc-port");

            logger.debug("*** ndisc ip : " + ndisc_host + ", port : " + ndisc_port);

            xNApi = new XNApi(false);
            setNdiscConnID(xNApi.XNDisc_Connect(ndisc_host, ndisc_port));
            if (getNdiscConnID() < 0) {
                rtn = false;
                throw new Exception("can not connect to ndisc server");
            }
        } catch (Exception e) {
            rtn = false;
            logger.error(e.getMessage());
        }
        return rtn;
    }

    /**
     * Migration ? ?  
     * 
     * @param list Migration ? 
     * @return  ? true, ? false
     */
    private boolean downloadJSTOR(ArrayList<XNDiscMigItem> list) {
        boolean rtn = false;
        try {
            String[][] fileGetInfo = new String[list.size()][3];
            XNDiscMigItem item = null;
            for (int i = 0; i < list.size(); i++) {
                item = (XNDiscMigItem) list.get(i);
                fileGetInfo[i][0] = item.getFileId();
                fileGetInfo[i][1] = XNDiscMigUtil.getFilePath(item.getFileId(),
                        XNDiscMigUtil.getRealFileName(item.getFileName()));
                fileGetInfo[i][2] = "-1"; // auto reverse filter id
            }
            rtn = jSTOR.JSTOR_FileGet(getJstorConnID(), list.size(), fileGetInfo, 0) >= 0;
            if (rtn) {
                jSTOR.JSTOR_Commit(getJstorConnID());
            }
            logger.debug("*** jstor download : " + rtn);
            msg.append("*** jstor file download result is " + rtn + XNDiscMigUtil.LINE_SEPERATOR);
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return rtn;
    }

    /**
     * Migration ? ? 
     * 
     * @param list Migration ? 
     * @return  ? true, ? false
     */
    private boolean uploadNDISC(ArrayList<XNDiscMigItem> list) {
        boolean rtn = false;
        try {
            String ndiscStatus = XNDiscMigConfig.getString("ndisc-status", "0");
            NFile[] nFile = new NFile[list.size()];
            XNDiscMigItem item = null;
            for (int i = 0; i < list.size(); i++) {
                item = (XNDiscMigItem) list.get(i);
                nFile[i] = new NFile();
                nFile[i].setId(item.getFileId());
                nFile[i].setName(XNDiscMigUtil.getFilePath(item.getFileId(),
                        XNDiscMigUtil.getRealFileName(item.getFileName())));
                nFile[i].setCreatedDate(item.getFileRegDate());
                if ("R".equalsIgnoreCase(ndiscStatus)) {
                    nFile[i].setStatType(item.getFileStatus());
                } else {
                    nFile[i].setStatType(ndiscStatus);
                }
            }
            String files[] = xNApi.XNDISC_FileReg(nFile);

            if (files != null && files.length > 0) {
                logger.debug("*** ndisc upload count : " + files.length);
                msg.append("*** xndisc file upload count (" + files.length + ")" + XNDiscMigUtil.LINE_SEPERATOR);
                msg.append("*** xndisc file ids " + Arrays.toString(files) + XNDiscMigUtil.LINE_SEPERATOR);
                migdb.init();
                migdb.appendSQL(XNDiscMigConnDB.getMigrationHistory());
                for (int i = 0; i < files.length; i++) {
                    migdb.setParam(1, files[i]);
                    migdb.queryUpdate();
                }
                migdb.commit();
            }
            rtn = true;
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return rtn;
    }

    /**
     * Migration   ? ?  
     * 
     * @param list Migration ? 
     * @return  ? true,  false
     */
    private boolean removeTempFiles(ArrayList<XNDiscMigItem> list) {
        boolean rtn = false;
        try {
            XNDiscMigItem item = null;
            for (int i = 0; i < list.size(); i++) {
                item = (XNDiscMigItem) list.get(i);
                XNDiscMigUtil.deleteTmpFiles(item.getFileId());
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return rtn;
    }

    /**
     * JSTOR  ? 
     * 
     * @return JSTOR  ?
     */
    int getJstorConnID() {
        return jstorConnID;
    }

    /**
     * JSTOR  ? 
     * 
     * @param jstorConnID JSTOR  ?
     */
    void setJstorConnID(int jstorConnID) {
        this.jstorConnID = jstorConnID;
    }

    /**
     * XNAPI  ? 
     * 
     * @return XNAPI  ?
     */
    int getNdiscConnID() {
        return ndiscConnID;
    }

    /**
     * XNAPI  ? 
     * 
     * @param ndiscConnID XNAPI  ?
     */
    void setNdiscConnID(int ndiscConnID) {
        this.ndiscConnID = ndiscConnID;
    }
}