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

Java tutorial

Introduction

Here is the source code for com.clustercontrol.notify.factory.ModifyNotifyRelation.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.Iterator;
import java.util.List;

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

import com.clustercontrol.commons.util.HinemosEntityManager;
import com.clustercontrol.commons.util.JpaTransactionManager;
import com.clustercontrol.fault.HinemosUnknown;
import com.clustercontrol.fault.NotifyNotFound;
import com.clustercontrol.notify.model.NotifyRelationInfo;
import com.clustercontrol.notify.util.QueryUtil;

/**
 * ???
 *
 * @version 2.1.0
 * @since 2.1.0
 */
public class ModifyNotifyRelation {

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

    /**
     * ????
     * <p>
     * <ol>
     *  <li>????</li>
     *  <li>???????</li>
     * </ol>
     * 
     * @param info ??
     * @return ???????<code> true </code>
     * @throws HinemosUnknown
     * 
     * @see com.clustercontrol.notify.ejb.entity.SystemNotifyInfoBean
     * @see com.clustercontrol.notify.ejb.entity.SystemNotifyEventInfoBean
     */
    public boolean add(Collection<NotifyRelationInfo> info) throws HinemosUnknown {
        NotifyRelationInfo relation = null;

        try {
            if (info != null) {
                // 
                Iterator<NotifyRelationInfo> it = info.iterator();

                HinemosEntityManager em = new JpaTransactionManager().getEntityManager();
                while (it.hasNext()) {
                    relation = it.next();

                    if (relation != null) {
                        em.persist(relation);
                    }
                }
            }

        } 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>
     * </ol>
     * 
     * @param info ?
     * @return ??????<code> true </code>
     * @throws HinemosUnknown
     * @throws NotifyNotFound
     * 
     * @see com.clustercontrol.notify.ejb.entity.SystemNotifyInfoBean
     * @see com.clustercontrol.notify.ejb.entity.SystemNotifyEventInfoBean
     * @see com.clustercontrol.notify.factory.DeleteSystemNotify#deleteEvents(Collection)
     */
    public boolean modify(Collection<NotifyRelationInfo> info, String notifyGroupId)
            throws HinemosUnknown, NotifyNotFound {
        NotifyRelationInfo relation = null;
        m_log.debug("ModifyNotifyRelation.modify() notifyGroupId = " + notifyGroupId);
        try {
            /**
             * ?ID?????????????
             * findByPK????????????
             * ??????????????????
             **/
            // 
            if (notifyGroupId != null && !notifyGroupId.equals("")) {
                delete(notifyGroupId);
            }
            if (info != null) {
                Iterator<NotifyRelationInfo> it = info.iterator();

                while (it.hasNext()) {

                    relation = it.next();

                    if (relation != null) {
                        // 
                        m_log.debug("NotifyRelation ADD before : notifyGroupId = " + relation.getNotifyGroupId()
                                + ", notifyId = " + relation.getNotifyId());
                        NotifyRelationInfo entity = new NotifyRelationInfo(relation.getNotifyGroupId(),
                                relation.getNotifyId());
                        entity.setNotifyType(relation.getNotifyType());
                        m_log.debug("NotifyRelation ADD : notifyGroupId = " + entity.getId().getNotifyGroupId()
                                + ", notifyId = " + entity.getId().getNotifyId());
                    }
                }
            }
        } catch (HinemosUnknown e) {
            throw e;
        } catch (Exception e) {
            m_log.warn("modify() : " + e.getClass().getSimpleName() + ", " + e.getMessage(), e);
            throw new HinemosUnknown(e.getMessage(), e);
        }

        return true;
    }

    /**
     * ID????
     * <p>
     * <ol>
     *  <li>ID????</li>
     * </ol>
     * 
     * @param notifyGroupId ?ID
     * @return ??????<code> true </code>
     * @throws HinemosUnknown
     */
    public boolean delete(String notifyGroupId) throws HinemosUnknown {
        JpaTransactionManager jtm = new JpaTransactionManager();
        HinemosEntityManager em = jtm.getEntityManager();

        try {
            List<NotifyRelationInfo> notifies = QueryUtil.getNotifyRelationInfoByNotifyGroupId(notifyGroupId);

            Iterator<NotifyRelationInfo> it = notifies.iterator();

            while (it.hasNext()) {
                NotifyRelationInfo detail = it.next();
                em.remove(detail);
            }
            // JPA??DML??????????
            jtm.flush();
        } catch (Exception e) {
            m_log.warn("delete() : " + e.getClass().getSimpleName() + ", " + e.getMessage(), e);
            throw new HinemosUnknown(e.getMessage(), e);
        }

        return true;
    }
}