com.clustercontrol.notify.factory.ModifyNotify.java Source code

Java tutorial

Introduction

Here is the source code for com.clustercontrol.notify.factory.ModifyNotify.java

Source

/*
    
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.notify.factory;

import java.util.Collection;
import java.util.List;

import javax.persistence.EntityExistsException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.clustercontrol.accesscontrol.bean.PrivilegeConstant.ObjectPrivilegeMode;
import com.clustercontrol.commons.util.HinemosEntityManager;
import com.clustercontrol.commons.util.JpaTransactionManager;
import com.clustercontrol.fault.HinemosUnknown;
import com.clustercontrol.fault.InvalidRole;
import com.clustercontrol.fault.MailTemplateNotFound;
import com.clustercontrol.fault.NotifyDuplicate;
import com.clustercontrol.fault.NotifyNotFound;
import com.clustercontrol.notify.bean.NotifyTypeConstant;
import com.clustercontrol.notify.mail.model.MailTemplateInfo;
import com.clustercontrol.notify.model.NotifyCommandInfo;
import com.clustercontrol.notify.model.NotifyEventInfo;
import com.clustercontrol.notify.model.NotifyHistoryEntity;
import com.clustercontrol.notify.model.NotifyInfo;
import com.clustercontrol.notify.model.NotifyInfraInfo;
import com.clustercontrol.notify.model.NotifyJobInfo;
import com.clustercontrol.notify.model.NotifyLogEscalateInfo;
import com.clustercontrol.notify.model.NotifyMailInfo;
import com.clustercontrol.notify.model.NotifyRelationInfo;
import com.clustercontrol.notify.model.NotifyStatusInfo;
import com.clustercontrol.notify.util.NotifyUtil;
import com.clustercontrol.notify.util.QueryUtil;
import com.clustercontrol.util.HinemosTime;

/**
 * ???
 *
 * @version 3.0.0
 * @since 1.0.0
 */
public class ModifyNotify {

    /** ? */
    private static Log m_log = LogFactory.getLog(ModifyNotify.class);

    /**
     * ????
     * <p>
     * <ol>
     *  <li>????</li>
     *  <li>???????</li>
     *  <li>?????????
     *      ??????????????</li>
     * </ol>
     * 
     * @param info ??
     * @return ???????<code> true </code>
     * @throws HinemosUnknown
     * @throws NotifyDuplicate
     * 
     * @see com.clustercontrol.notify.ejb.entity.NotifyInfoBean
     * @see com.clustercontrol.notify.ejb.entity.NotifyEventInfoBean
     */
    public boolean add(NotifyInfo info, String user) throws HinemosUnknown, NotifyDuplicate {
        m_log.debug("add " + "NotifyID = " + info.getNotifyId());

        JpaTransactionManager jtm = new JpaTransactionManager();

        try {
            long now = HinemosTime.currentTimeMillis();

            // ??
            jtm.checkEntityExists(NotifyInfo.class, info.getNotifyId());

            info.setRegDate(now);
            info.setRegUser(user);
            info.setUpdateDate(now);
            info.setUpdateUser(user);

            info.persistSelf(jtm.getEntityManager());
        } catch (EntityExistsException e) {
            m_log.info("add() : " + e.getClass().getSimpleName() + ", " + e.getMessage());
            throw new NotifyDuplicate(e.getMessage(), e);
        } catch (Exception e) {
            m_log.warn("add() : " + e.getClass().getSimpleName() + ", " + e.getMessage(), e);
            throw new HinemosUnknown(e.getMessage(), e);
        }

        return true;
    }

    /**
     * ???
     * <p>
     * <ol>
     *  <li>ID?????</li>
     *  <li>???</li>
     *  <li>???????</li>
     *  <li>???????</li>
     *  <li>?????????
     *      ??????????????</li>
     * </ol>
     *
     * @param info ?
     * @return ??????<code> true </code>
     * @throws NotifyDuplicate
     * @throws InvalidRole
     * @throws HinemosUnknown
     *
     * @see com.clustercontrol.notify.ejb.entity.NotifyInfoBean
     * @see com.clustercontrol.notify.ejb.entity.NotifyEventInfoBean
     * @see com.clustercontrol.notify.factory.DeleteNotify#deleteEvents(Collection)
     */
    public boolean modify(NotifyInfo info, String user) throws NotifyDuplicate, InvalidRole, HinemosUnknown {
        HinemosEntityManager em = new JpaTransactionManager().getEntityManager();

        try {
            long now = HinemosTime.currentTimeMillis();

            // ?
            NotifyInfo notify = QueryUtil.getNotifyInfoPK(info.getNotifyId(), ObjectPrivilegeMode.MODIFY);

            // 
            notify.setDescription(info.getDescription());
            notify.setValidFlg(info.getValidFlg());
            notify.setInitialCount(info.getInitialCount());
            notify.setRenotifyType(info.getRenotifyType());
            notify.setNotFirstNotify(info.getNotFirstNotify());
            notify.setRenotifyPeriod(info.getRenotifyPeriod());
            notify.setUpdateDate(now);
            notify.setUpdateUser(user);
            notify.setValidFlg(info.getValidFlg());
            notify.setOwnerRoleId(info.getOwnerRoleId());
            notify.setCalendarId(info.getCalendarId());

            // ???????
            if (!notify.getValidFlg().booleanValue()) {
                m_log.debug("remove NotifyHistory");
                List<NotifyHistoryEntity> list = QueryUtil.getNotifyHistoryByNotifyId(notify.getNotifyId());

                for (NotifyHistoryEntity history : list) {
                    m_log.debug("remove NotifyHistory : " + history);

                    try {
                        em.remove(history);
                    } catch (Exception e) {
                        m_log.warn("modify() : " + e.getClass().getSimpleName() + ", " + e.getMessage(), e);
                    }
                }
            }

            // 
            switch (info.getNotifyType()) {
            case NotifyTypeConstant.TYPE_COMMAND:
                modifyNotifyCommand(info, notify);
                break;
            case NotifyTypeConstant.TYPE_EVENT:
                modifyNotifyEvent(info, notify);
                break;
            case NotifyTypeConstant.TYPE_JOB:
                modifyNotifyJob(info, notify);
                break;
            case NotifyTypeConstant.TYPE_LOG_ESCALATE:
                modifyNotifyLogEscalate(info, notify);
                break;
            case NotifyTypeConstant.TYPE_MAIL:
                modifyNotifyMail(info, notify);
                break;
            case NotifyTypeConstant.TYPE_STATUS:
                modifyNotifyStatus(info, notify);
                break;
            case NotifyTypeConstant.TYPE_INFRA:
                modifyNotifyInfra(info, notify);
                break;
            }

        } catch (NotifyNotFound e) {
            NotifyDuplicate e2 = new NotifyDuplicate(e.getMessage(), e);
            e2.setNotifyId(info.getNotifyId());
            throw e2;
        } catch (InvalidRole e) {
            throw e;
        } catch (Exception e) {
            m_log.warn("modify() : " + e.getClass().getSimpleName() + ", " + e.getMessage(), e);
            throw new HinemosUnknown(e.getMessage(), e);
        }

        return true;
    }

    private boolean modifyNotifyCommand(NotifyInfo info, NotifyInfo notify) throws NotifyNotFound {
        NotifyCommandInfo command = info.getNotifyCommandInfo();
        if (command != null) {
            NotifyCommandInfo entity = QueryUtil.getNotifyCommandInfoPK(info.getNotifyId());
            NotifyUtil.copyProperties(command, entity);
        }
        return true;
    }

    private boolean modifyNotifyEvent(NotifyInfo info, NotifyInfo notify) throws NotifyNotFound {
        NotifyEventInfo event = info.getNotifyEventInfo();
        if (event != null) {
            NotifyEventInfo entity = QueryUtil.getNotifyEventInfoPK(info.getNotifyId());
            NotifyUtil.copyProperties(event, entity);
        }
        return true;
    }

    private boolean modifyNotifyJob(NotifyInfo info, NotifyInfo notify) throws NotifyNotFound {
        NotifyJobInfo job = info.getNotifyJobInfo();
        if (job != null) {
            NotifyJobInfo entity = QueryUtil.getNotifyJobInfoPK(info.getNotifyId());
            NotifyUtil.copyProperties(job, entity);
        }
        return true;
    }

    private boolean modifyNotifyLogEscalate(NotifyInfo info, NotifyInfo notify) throws NotifyNotFound {
        NotifyLogEscalateInfo log = info.getNotifyLogEscalateInfo();
        if (log != null) {
            NotifyLogEscalateInfo entity = QueryUtil.getNotifyLogEscalateInfoPK(info.getNotifyId());
            NotifyUtil.copyProperties(log, entity);
        }
        return true;
    }

    private boolean modifyNotifyMail(NotifyInfo info, NotifyInfo notify) throws NotifyNotFound {
        NotifyMailInfo mail = info.getNotifyMailInfo();
        if (mail != null) {
            MailTemplateInfo mailTemplateInfoEntity = null;
            if (mail.getMailTemplateId() != null && !"".equals(mail.getMailTemplateId())) {
                try {
                    mailTemplateInfoEntity = com.clustercontrol.notify.mail.util.QueryUtil
                            .getMailTemplateInfoPK(mail.getMailTemplateId());
                } catch (MailTemplateNotFound e) {
                    m_log.debug(e.getMessage(), e);
                } catch (InvalidRole e) {
                    m_log.debug(e.getMessage(), e);
                }
            }
            NotifyMailInfo entity = QueryUtil.getNotifyMailInfoPK(info.getNotifyId());
            entity.relateToMailTemplateInfoEntity(mailTemplateInfoEntity);
            NotifyUtil.copyProperties(mail, entity);
        }
        return true;
    }

    private boolean modifyNotifyStatus(NotifyInfo info, NotifyInfo notify) throws NotifyNotFound {
        NotifyStatusInfo status = info.getNotifyStatusInfo();
        if (status != null) {
            NotifyStatusInfo entity = QueryUtil.getNotifyStatusInfoPK(info.getNotifyId());
            NotifyUtil.copyProperties(status, entity);
        }
        return true;
    }

    private boolean modifyNotifyInfra(NotifyInfo info, NotifyInfo notify) throws NotifyNotFound {
        NotifyInfraInfo infra = info.getNotifyInfraInfo();
        if (infra != null) {
            NotifyInfraInfo entity = QueryUtil.getNotifyInfraInfoPK(info.getNotifyId());
            NotifyUtil.copyProperties(infra, entity);
        }
        return true;
    }

    /**
     * ???
     * <p>
     * <ol>
     *  <li>ID?????</li>
     *  <li>???????</li>
     *  <li>???</li>
     *  <li>?????????
     *      ??????????????</li>
     * </ol>
     * 
     * @param notifyId ?ID
     * @return ??????<code> true </code>
     * @throws NotifyNotFound
     * @throws InvalidRole
     * @throws HinemosUnknown
     * 
     * @see com.clustercontrol.notify.ejb.entity.NotifyInfoBean
     * @see com.clustercontrol.notify.ejb.entity.NotifyEventInfoBean
     * @see #deleteEvents(Collection)
     */
    public boolean delete(String notifyId) throws NotifyNotFound, InvalidRole, HinemosUnknown {

        HinemosEntityManager em = new JpaTransactionManager().getEntityManager();

        NotifyInfo notify = null;
        try {
            // ?
            notify = QueryUtil.getNotifyInfoPK(notifyId, ObjectPrivilegeMode.MODIFY);

            // ??????????
            List<NotifyHistoryEntity> list = QueryUtil.getNotifyHistoryByNotifyId(notifyId);
            for (NotifyHistoryEntity history : list) {
                try {
                    em.remove(history);
                } catch (Exception e) {
                    m_log.warn("delete() : " + e.getClass().getSimpleName() + ", " + e.getMessage(), e);
                }
            }

            // 
            List<NotifyRelationInfo> relations = QueryUtil.getNotifyRelationInfoByNotifyId(notifyId);
            for (NotifyRelationInfo relation : relations) {
                try {
                    em.remove(relation);
                } catch (Exception e) {
                    m_log.warn("delete() : " + e.getClass().getSimpleName() + ", " + e.getMessage(), e);
                }
            }

            // 
            em.remove(notify);

        } catch (InvalidRole e) {
            throw e;
        } catch (NotifyNotFound e) {
            throw e;
        } catch (Exception e) {
            m_log.warn("delete() : " + e.getClass().getSimpleName() + ", " + e.getMessage(), e);
            throw new HinemosUnknown(e.getMessage(), e);
        }
        return true;
    }
}