com.pureinfo.srm.config.notice.domain.NoticeMgrImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.pureinfo.srm.config.notice.domain.NoticeMgrImpl.java

Source

/**
 * PureInfo Quake
 * @(#)NoticeMgrImpl.java   1.0 Nov 10, 2005
 * 
 * Copyright(c) 2004-2005, PureInfo Information Technology Corp. Ltd. 
 * All rights reserved, see the license file.
 * 
 * www.pureinfo.com.cn
 */

package com.pureinfo.srm.config.notice.domain;

import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import com.pureinfo.force.exception.PureException;
import com.pureinfo.force.io.ClassResourceUtil;
import com.pureinfo.force.xml.XMLUtil;
import com.pureinfo.srm.config.notice.model.Notice;

/**
 * <P>
 * Created on Nov 10, 2005 2:36:06 PM <BR>
 * Last modified on Nov 10, 2005
 * </P>
 * 
 * @author Freeman.Hu
 * @version 1.0, Nov 10, 2005
 * @since Quake 1.0
 */
public class NoticeMgrImpl implements INoticeMgr {
    public static final String DATA_FILENAME = "notice.xml";

    public static final String ELEMENT_NOTICES = "notices";

    public static final String ELEMENT_NOTICE = "notice";

    private Map m_notices;

    /**
     * @see com.pureinfo.srm.config.notice.domain.INoticeMgr#getNotices()
     */
    public Map getNotices() {
        return m_notices;
    }

    private boolean m_bLoaded;

    public void setLoaded(boolean _bLoaded) {
        m_bLoaded = _bLoaded;
    }

    public boolean isLoaded() {
        return m_bLoaded;
    }

    private void insureLoaded() throws PureException {
        if (isLoaded())
            return;
        synchronized (this) {
            if (isLoaded())
                return;

            String sFileName = ClassResourceUtil.mapFullPath(DATA_FILENAME, true);
            Element element = XMLUtil.fileToElement(sFileName);
            m_notices = new HashMap();
            for (Iterator iter = element.elementIterator(ELEMENT_NOTICE); iter.hasNext();) {
                Notice notice = new Notice();
                notice.fromXML((Element) iter.next());
                m_notices.put(notice.getName(), notice);
            }

            setLoaded(true);
        }
    }

    public void store() throws PureException {
        if (!isLoaded())
            return;
        synchronized (this) {
            Element root = DocumentHelper.createElement(ELEMENT_NOTICES);
            for (Iterator iter = m_notices.values().iterator(); iter.hasNext();) {
                Notice notice = (Notice) iter.next();
                notice.toXMLElement(root.addElement(ELEMENT_NOTICE));
            }
            String sFileName = ClassResourceUtil.mapFullPath(DATA_FILENAME, true);
            try {
                new XMLWriter(new FileOutputStream(sFileName), OutputFormat.createPrettyPrint())
                        .write(DocumentHelper.createDocument(root));
            } catch (Exception ex) {
                throw new PureException(PureException.UNKNOWN, "", ex);
            }
        }
    }

    /**
     * @throws PureException
     * @see com.pureinfo.srm.config.notice.domain.INoticeMgr#getNotice(java.lang.String)
     */
    public Notice getNotice(String _sName) throws PureException {
        insureLoaded();
        return (Notice) m_notices.get(_sName);
    }

    /**
     * @throws PureException
     * @see com.pureinfo.srm.config.notice.domain.INoticeMgr#updateNotce(com.pureinfo.srm.config.notice.model.Notice)
     */
    public void updateNotce(Notice _notice) throws PureException {
        insureLoaded();
        if (_notice.getItems().size() == 0) {
            m_notices.remove(_notice.getName());
        } else {
            m_notices.put(_notice.getName(), _notice);
        }
    }
}