com.pureinfo.srm.config.affirm.domain.AffirmMgrImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.pureinfo.srm.config.affirm.domain.AffirmMgrImpl.java

Source

/**
 * PureInfo Quake
 * @(#)AffirmMgrImpl.java   1.0 Nov 11, 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.affirm.domain;

import java.io.FileOutputStream;

import org.apache.commons.lang.math.NumberUtils;
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.affirm.model.Affirm;

/**
 * <P>
 * Created on Nov 11, 2005 8:36:18 PM <BR>
 * Last modified on Nov 11, 2005
 * </P>
 * 
 * @author Freeman.Hu
 * @version 1.0, Nov 11, 2005
 * @since Quake 1.0
 */
public class AffirmMgrImpl implements IAffirmMgr {
    public static final String DATA_FILENAME = "affirm.xml";

    public static final String ELEMENT_AFFIRMS = "affirms";

    private Affirm m_affirm;

    /**
     * @throws PureException
     * @see com.pureinfo.srm.config.affirm.domain.IAffirmMgr#getMaxDayString(String,
     *      String)
     */
    public String getMaxDayString(String _sPublicForm, String _sPubLevel) throws PureException {
        insureLoaded();
        return m_affirm.getDayString(_sPublicForm + "$" + _sPubLevel);
    }

    public void setMaxDayString(String _sPublicForm, String _sPubLevel, String _sDay) throws PureException {
        insureLoaded();
        if (NumberUtils.isDigits(_sDay)) {
            m_affirm.setDayString(_sPublicForm + "$" + _sPubLevel, _sDay);
        } else {
            m_affirm.remove(_sPublicForm + "$" + _sPubLevel);
        }
    }

    public void store() throws PureException {
        if (!isLoaded())
            return;
        synchronized (this) {
            Element root = DocumentHelper.createElement(ELEMENT_AFFIRMS);
            m_affirm.toXMLElement(root);
            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);
            }
        }
    }

    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;

            m_affirm = new Affirm();
            String sFileName = ClassResourceUtil.mapFullPath(DATA_FILENAME, true);
            Element element = XMLUtil.fileToElement(sFileName);
            m_affirm.fromXML(element);

            setLoaded(true);
        }
    }

}