jp.terasoluna.fw.batch.util.JobUtil.java Source code

Java tutorial

Introduction

Here is the source code for jp.terasoluna.fw.batch.util.JobUtil.java

Source

/*
 * Copyright (c) 2011 NTT DATA Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package jp.terasoluna.fw.batch.util;

import java.sql.Date;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import jp.terasoluna.fw.batch.constants.JobStatusConstants;
import jp.terasoluna.fw.batch.constants.LogId;
import jp.terasoluna.fw.batch.exception.BatchException;
import jp.terasoluna.fw.batch.executor.dao.SystemDao;
import jp.terasoluna.fw.batch.executor.vo.BatchJobData;
import jp.terasoluna.fw.batch.executor.vo.BatchJobListParam;
import jp.terasoluna.fw.batch.executor.vo.BatchJobListResult;
import jp.terasoluna.fw.batch.executor.vo.BatchJobManagementParam;
import jp.terasoluna.fw.batch.executor.vo.BatchJobManagementUpdateParam;
import jp.terasoluna.fw.logger.TLogger;

import org.apache.ibatis.session.RowBounds;
import org.springframework.dao.DataAccessException;

/**
 * ?<br>
 * <br>
 * ??AbstractJobBatchExecutor??<br>
 * @see jp.terasoluna.fw.batch.executor.AbstractJobBatchExecutor
 */
public class JobUtil {

    /**
     * .
     */
    private static final TLogger LOGGER = TLogger.getLogger(JobUtil.class);

    /**
     * 
     */
    protected JobUtil() {
    }

    /**
     * <h6>?.</h6>
     * @param systemDao DAO
     * @return 
     */
    public static List<BatchJobListResult> selectJobList(SystemDao systemDao) {
        return selectJobList(null, systemDao);
    }

    /**
     * <h6>?.</h6>
     * @param systemDao SystemDao
     * @param beginIndex ??
     * @param maxCount ??
     * @return 
     */
    public static List<BatchJobListResult> selectJobList(SystemDao systemDao, int beginIndex, int maxCount) {
        return selectJobList(null, systemDao, beginIndex, maxCount);
    }

    /**
     * <h6>?.</h6>
     * @param jobAppCd 
     * @param systemDao DAO
     * @return 
     */
    public static List<BatchJobListResult> selectJobList(String jobAppCd, SystemDao systemDao) {
        return selectJobList(jobAppCd, systemDao, -1, -1);
    }

    /**
     * <h6>?.</h6> ????
     * @param jobAppCd 
     * @param systemDao DAO
     * @param beginIndex ??
     * @param maxCount ??
     * @return 
     */
    public static List<BatchJobListResult> selectJobList(String jobAppCd, SystemDao systemDao, int beginIndex,
            int maxCount) {
        // 
        List<String> curAppStatusList = new ArrayList<String>();

        // 
        curAppStatusList.add(JobStatusConstants.JOB_STATUS_UNEXECUTION);

        return selectJobList(jobAppCd, curAppStatusList, systemDao, beginIndex, maxCount);
    }

    /**
     * <h6>?.</h6>
     * @param jobAppCd 
     * @param curAppStatusList ???
     * @param systemDao DAO
     * @param beginIndex ??
     * @param maxCount ??
     * @return 
     */
    public static List<BatchJobListResult> selectJobList(String jobAppCd, List<String> curAppStatusList,
            SystemDao systemDao, int beginIndex, int maxCount) {

        BatchJobListParam param = new BatchJobListParam();

        // 
        param.setJobAppCd(jobAppCd);

        // ??
        if (curAppStatusList != null) {
            param.setCurAppStatusList(curAppStatusList);
        }

        List<BatchJobListResult> result = null;
        try {
            if (beginIndex == -1 || maxCount == -1) {
                result = systemDao.selectJobList(param);
            } else {
                RowBounds rowBounds = new RowBounds(beginIndex, maxCount);
                result = systemDao.selectJobList(rowBounds, param);
            }
        } catch (Exception e) {
            throw new BatchException(LOGGER.getLogMessage(LogId.EAL025039), e);
        }

        return result;
    }

    /**
     * <h6>1?.</h6>
     * @param jobSequenceId ID
     * @param forUpdate ???true
     * @param systemDao DAO
     * @return
     */
    public static BatchJobData selectJob(String jobSequenceId, boolean forUpdate, SystemDao systemDao) {
        BatchJobManagementParam param = new BatchJobManagementParam();

        // 
        param.setJobSequenceId(jobSequenceId);

        // FOR UPDATE
        param.setForUpdate(forUpdate);

        BatchJobData result = null;
        try {
            result = systemDao.selectJob(param);
        } catch (Exception e) {
            if (LOGGER.isErrorEnabled()) {
                LOGGER.error(LogId.EAL025040, e);
            }
            if (e instanceof DataAccessException) {
                throw (DataAccessException) e;
            }
        }

        return result;
    }

    /**
     * <h6>.</h6>
     * @param jobSequenceId ID
     * @param curAppStatus ???
     * @param blogicAppStatus ????
     * @return ???????true
     */
    public static boolean updateJobStatus(String jobSequenceId, String curAppStatus, String blogicAppStatus,
            SystemDao systemDao) {
        BatchJobManagementUpdateParam param = new BatchJobManagementUpdateParam();

        // 
        param.setJobSequenceId(jobSequenceId);

        // 
        param.setBLogicAppStatus(blogicAppStatus);

        // 
        param.setCurAppStatus(curAppStatus);

        // 
        Timestamp updDateTime = getCurrentTime(systemDao);
        param.setUpdDateTime(updDateTime);

        int count = -1;
        try {
            count = systemDao.updateJobTable(param);
        } catch (Exception e) {
            LOGGER.error(LogId.EAL025041, e);
            if (e instanceof DataAccessException) {
                throw (DataAccessException) e;
            }
            return false;
        }

        if (count != 1) {
            LOGGER.error(LogId.EAL025042);

            return false;
        }

        return true;
    }

    /**
     * <h6>??.</h6>
     * @param systemDao ?DAO
     * @return Timestamp 
     */
    @Deprecated
    public static Timestamp getCurrentTime(SystemDao systemDao) {
        Timestamp result = null;
        try {
            result = systemDao.readCurrentTime();
        } catch (Exception e) {
            LOGGER.error(LogId.EAL025043, e);
            if (e instanceof DataAccessException) {
                throw (DataAccessException) e;
            }
        }
        return result;
    }

    /**
     * <h6>??.</h6>
     * @param systemDao DAO
     * @return Date 
     */
    @Deprecated
    public static Date getCurrentDate(SystemDao systemDao) {
        Date result = null;
        try {
            result = systemDao.readCurrentDate();
        } catch (Exception e) {
            LOGGER.error(LogId.EAL025043, e);

        }
        return result;
    }

    /**
     * <h6>?????.</h6>
     * <p>
     * ?????? "" ?
     * </p>
     * @param name
     * @return
     */
    public static String getenv(String name) {
        String ret = System.getenv(name);
        if (ret == null) {
            return "";
        }
        return ret;
    }
}