Java tutorial
/* Copyright (C) 2006 NTT DATA Corporation This program is free software; you can redistribute it and/or Modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. */ package com.clustercontrol.jobmanagement.util; import java.io.Serializable; import java.util.ArrayList; import java.util.Collections; import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.clustercontrol.bean.StatusConstant; import com.clustercontrol.commons.util.JpaTransactionManager; import com.clustercontrol.commons.util.NotifyGroupIdGenerator; import com.clustercontrol.fault.HinemosUnknown; import com.clustercontrol.fault.InvalidRole; import com.clustercontrol.fault.JobInfoNotFound; import com.clustercontrol.fault.JobMasterNotFound; import com.clustercontrol.jobmanagement.bean.JobConstant; import com.clustercontrol.jobmanagement.bean.JobInfo; import com.clustercontrol.jobmanagement.bean.JobTreeItem; import com.clustercontrol.jobmanagement.model.JobInfoEntity; import com.clustercontrol.jobmanagement.model.JobSessionJobEntity; import com.clustercontrol.notify.model.NotifyRelationInfo; import com.clustercontrol.notify.session.NotifyControllerBean; /** * * * ?????<BR> * <li>?? * <li>?????????? * * @version 2.0.0 * @since 2.0.0 */ public class JobUtil { /** ?<BR> */ private static Log m_log = LogFactory.getLog(JobUtil.class); /** * ??? * @param item * @param jobId * @param selectType * @return -1:???0:??1:? */ public static int checkValidJob(JobTreeItem item, String jobId, Integer selectType) { if (item == null || item.getData() == null) { return -1; } //ID? JobInfo info = item.getData(); int type = info.getType(); if (jobId.equals(info.getId())) { if (type == JobConstant.TYPE_JOB || type == JobConstant.TYPE_FILEJOB || type == JobConstant.TYPE_APPROVALJOB || type == JobConstant.TYPE_MONITORJOB) { if (selectType == 1 && !info.isRegisteredModule()) { //????????????? return 1; } else { return 0; } } } //?JobTreeItem? for (JobTreeItem child : item.getChildren()) { int ret = checkValidJob(child, jobId, selectType); if (ret != -1) { return ret; } } return -1; } /** * ???? * @param item * @param jobNetId * @param referJobNet * @return -1:????0:???1:??2:??????? */ public static int checkValidJobNet(JobTreeItem item, String jobNetId, JobInfo referJobNet) { if (item == null || item.getData() == null) { return -1; } //ID? JobInfo info = item.getData(); int type = info.getType(); if (jobNetId.equals(info.getId())) { if (type == JobConstant.TYPE_JOBNET) { if (referJobNet.getReferJobSelectType() == 1 && !info.isRegisteredModule()) { //????????????? return 1; } // ????(?????????) if (isExistReferJobNet(item)) { return 2; } return 0; } } //?JobTreeItem? for (JobTreeItem child : item.getChildren()) { int ret = checkValidJobNet(child, jobNetId, referJobNet); if (ret != -1) { return ret; } } return -1; } /** * ???? * @param item * @param jobId * @return */ public static boolean isExistReferJobNet(JobTreeItem item) { if (item == null || item.getData() == null) { return false; } // ? if (item.getData().getType() == JobConstant.TYPE_REFERJOBNET) { return true; } //?JobTreeItem? for (JobTreeItem child : item.getChildren()) { if (isExistReferJobNet(child)) { return true; } } return false; } /** * ?????? * @param item * @param list * @return */ public static ArrayList<JobInfo> findReferJob(JobTreeItem item) { ArrayList<JobInfo> ret = new ArrayList<JobInfo>(); if (item == null || item.getData() == null) { return ret; } //ID? m_log.trace("checkReferJob Id=" + item.getData().getId()); JobInfo jobInfo = item.getData(); if (jobInfo.getType() == JobConstant.TYPE_REFERJOB) { if (jobInfo.getReferJobUnitId() != null && jobInfo.getReferJobId() != null) { ret.add(item.getData()); m_log.trace("JobId =" + jobInfo.getId() + ", UnitId =" + jobInfo.getReferJobUnitId() + ", referJobId =" + jobInfo.getReferJobId()); } } //?JobTreeItem? for (JobTreeItem child : item.getChildren()) { ret.addAll(findReferJob(child)); } return ret; } /** * ??????? * @param item * @param list * @return */ public static ArrayList<JobInfo> findReferJobNet(JobTreeItem item) { ArrayList<JobInfo> ret = new ArrayList<JobInfo>(); if (item == null || item.getData() == null) { return ret; } //ID? m_log.trace("checkReferJobNet Id=" + item.getData().getId()); JobInfo jobInfo = item.getData(); if (jobInfo.getType() == JobConstant.TYPE_REFERJOBNET) { if (jobInfo.getReferJobUnitId() != null && jobInfo.getReferJobId() != null) { ret.add(item.getData()); m_log.trace("JobId =" + jobInfo.getId() + ", UnitId =" + jobInfo.getReferJobUnitId() + ", referJobId =" + jobInfo.getReferJobId()); } } //?JobTreeItem? for (JobTreeItem child : item.getChildren()) { ret.addAll(findReferJobNet(child)); } return ret; } /** * ? * @param item */ public static void sort(JobTreeItem item) { ArrayList<JobTreeItem> children = item.getChildren(); if (children == null || children.size() == 0) { return; } Collections.sort(item.getChildren(), new DataComparator()); for (JobTreeItem child : children) { sort(child); } } private static class DataComparator implements java.util.Comparator<JobTreeItem>, Serializable { /** * */ private static final long serialVersionUID = 1L; @Override public int compare(JobTreeItem o1, JobTreeItem o2) { String s1 = o1.getData().getId(); String s2 = o2.getData().getId(); m_log.trace("s1=" + s1 + ", s2=" + s2); return s1.compareTo(s2); } } /** * ?????? * * @param sessionId * @param jobunitId * @param jobId * @return ??true??????false? * @throws JobInfoNotFound * @throws InvalidRole */ public static boolean isRunJob(String sessionId, String jobunitId, String jobId) throws JobInfoNotFound, InvalidRole { // ID?ID??? JpaTransactionManager jtm = new JpaTransactionManager(); try { JobSessionJobEntity sessionJobEntity = QueryUtil.getJobSessionJobPK(sessionId, jobunitId, jobId); if (sessionJobEntity.getStatus() == StatusConstant.TYPE_END) { return false; } else return true; } finally { jtm.close(); } } /** * JobNotice? * * @param jobInfoEntity * @param parentJobId * @return * @throws InvalidRole * @throws JobMasterNotFound * @throws HinemosUnknown */ public static void copyJobNoticeProperties(JobInfoEntity jobInfoEntity, String parentJobId) throws HinemosUnknown { String sessionId = jobInfoEntity.getId().getSessionId(); String jobunitId = jobInfoEntity.getId().getJobunitId(); try { JobInfoEntity parentJobInfoEntity = QueryUtil.getJobSessionJobPK(sessionId, jobunitId, parentJobId) .getJobInfoEntity(); String infoNotifyGroupId = NotifyGroupIdGenerator.generate(jobInfoEntity); jobInfoEntity.setNotifyGroupId(infoNotifyGroupId); jobInfoEntity.setBeginPriority(parentJobInfoEntity.getBeginPriority()); jobInfoEntity.setNormalPriority(parentJobInfoEntity.getNormalPriority()); jobInfoEntity.setWarnPriority(parentJobInfoEntity.getWarnPriority()); jobInfoEntity.setAbnormalPriority(parentJobInfoEntity.getAbnormalPriority()); // ????ID???? List<NotifyRelationInfo> ct = new NotifyControllerBean() .getNotifyRelation(NotifyGroupIdGenerator.generate(jobInfoEntity)); // JobNoticeInfo?ID????? for (NotifyRelationInfo relation : ct) { relation.setNotifyGroupId(infoNotifyGroupId); } // Job?NotifyRelationInfo?????? new NotifyControllerBean().addNotifyRelation(ct); } catch (InvalidRole e) { throw new HinemosUnknown(e.getMessage()); } catch (JobInfoNotFound e) { throw new HinemosUnknown(e.getMessage()); } } }