com.pureinfo.dolphin.mapping.PropertyRef.java Source code

Java tutorial

Introduction

Here is the source code for com.pureinfo.dolphin.mapping.PropertyRef.java

Source

/**
 * PureInfo Dolphin
 * @(#)PropertyRef.java   1.0 Oct 18, 2005
 * 
 * Copyright(c) 2004-2005, PureInfo Information Technology Corp. Ltd. 
 * All rights reserved, see the license file.
 * 
 * www.pureinfo.com.cn
 */

package com.pureinfo.dolphin.mapping;

import java.util.List;

import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.pureinfo.dolphin.DolphinConstants;
import com.pureinfo.dolphin.persister.IRefLocator;
import com.pureinfo.force.PureFactory;
import com.pureinfo.force.exception.PureException;
import com.pureinfo.force.exception.PureRuntimeException;
import com.pureinfo.force.xml.IXMLSupporter;

/**
 * <P>
 * Created on Oct 18, 2005 3:10:46 PM <BR>
 * Last modified on Oct 18, 2005
 * </P>
 * PropertyRef: property reference metadata.
 * 
 * <BR>
 * &lt;property name="authorId" column="AUTHOR_ID" type="string"&gt; <BR>
 * &lt;ref locator="NamedValueRefLocator" <BR>
 * type="com.pureinfo.common.namedvalue.ListNamedValue" property="id"&gt; <BR>
 * &lt;index name="type" property="type" /&gt; <BR>
 * &lt;/ref&gt; <BR>
 * &lt;/property&gt; <BR>
 * 
 * @author Why
 * @version 1.0, Oct 18, 2005
 * @since Dolphin 1.0
 */
public class PropertyRef implements IXMLSupporter {
    private String m_sType;

    private String m_sKey;

    private String m_sLocator;

    private RefIndex[] m_indexes = new RefIndex[0];

    //cached data
    private Class m_typeClass;

    /**
     * Constructor: default
     */
    public PropertyRef() {
        super();
    }

    /**
     * 
     * <P>
     * Created on Oct 18, 2005 3:48:02 PM <BR>
     * Last modified on Oct 18, 2005
     * </P>
     * RefIndex: Property reference index.
     * 
     * @author Why
     * @version 1.0, Oct 18, 2005
     * @since Dolphin 1.0
     */
    public static class RefIndex implements IXMLSupporter {
        private String m_sName;

        private String m_sValue;

        private String m_sProperty;

        /**
         * Constructor: default
         */
        public RefIndex() {
            super();
        }

        public String getName() {
            return m_sName;
        }

        public void setName(String _sName) {
            m_sName = _sName;
        }

        public String getValue() {
            return m_sValue;
        }

        public void setValue(String _sValue) {
            m_sValue = _sValue;
        }

        public String getProperty() {
            return m_sProperty;
        }

        public void setProperty(String _sProperty) {
            m_sProperty = _sProperty;
        }

        /**
         * @see java.lang.Object#toString()
         */
        public String toString() {
            StringBuffer sbuff = new StringBuffer();
            try {
                sbuff.append("RefIndex: name=").append(m_sName);
                if (m_sValue != null) {
                    sbuff.append(" value=").append(m_sValue);
                }
                if (m_sProperty != null) {
                    sbuff.append(" property=").append(m_sProperty);
                }
                return sbuff.toString();
            } finally {
                sbuff.setLength(0);
            }
        }

        /**
         * @see com.pureinfo.force.xml.IXMLSupporter#toXMLElement(org.dom4j.Element)
         */
        public void toXMLElement(Element _element) throws PureException {
            _element.addAttribute("name", m_sName);
            if (m_sValue != null) {
                _element.addAttribute("value", m_sValue);
            }
            if (m_sProperty != null) {
                _element.addAttribute("property", m_sProperty);
            }
        }

        /**
         * @see com.pureinfo.force.xml.IXMLSupporter#fromXML(org.dom4j.Element)
         */
        public void fromXML(Element _element) throws PureException {
            m_sName = _element.attributeValue("name");
            m_sValue = _element.attributeValue("value");
            m_sProperty = _element.attributeValue("property");
        }
    }

    /**
     * Returns the reference object type (class name).
     * 
     * @return the reference object type (class name).
     */
    public String getType() {
        return m_sType;
    }

    /**
     * Sets reference object type (class name).
     * 
     * @param _sClassName
     *            reference object type (class name).
     */
    public void setType(String _sClassName) {
        m_sType = _sClassName;
    }

    /**
     * Returns the reference property.
     * 
     * @return the reference property.
     */
    public String getKey() {
        return m_sKey;
    }

    /**
     * Sets the reference property.
     * 
     * @param _sProperty
     *            the reference property.
     */
    public void setKey(String _sProperty) {
        m_sKey = _sProperty;
    }

    /**
     * Returns the reference object locator bean id.
     * 
     * @return the reference object locator bean id.
     */
    public String getLocator() {
        return m_sLocator;
    }

    /**
     * Sets the reference object locator bean id.
     * 
     * @param _sLocator
     *            the reference object locator bean id
     */
    public void setLocator(String _sLocator) {
        m_sLocator = _sLocator;
    }

    /**
     * Returns the indexes array.
     * 
     * @return the indexes array.
     */
    public RefIndex[] getIndexes() {
        return m_indexes;
    }

    //=========================================================================
    //logic operations

    /**
     * Returns the locator bean.
     * 
     * @return the locator bean.
     */
    public IRefLocator getLocatorBean() throws PureException {
        String sId = (m_sLocator != null) ? m_sLocator : DolphinConstants.DEFAULT_REF_LOCATOR;
        return (IRefLocator) PureFactory.getBean(sId);
    }

    /**
     * Returns the class of reference object
     * 
     * @return the class of reference object
     * @throws PureRuntimeException
     *             if the class not found
     */
    public Class getTypeClass() {
        if (m_typeClass == null) {
            try {
                m_typeClass = Class.forName(m_sType);
            } catch (Exception ex) {
                throw new PureRuntimeException(PureException.CLASS_NOTFOUND, m_sType, ex);
            }
        }
        return m_typeClass;
    }

    /**
     * Returns the ref index.
     * 
     * @param _sName
     *            the name given
     * @return the ref index found.
     */
    public RefIndex getIndex(String _sName) {
        RefIndex index;
        for (int i = 1; i < m_indexes.length; i++) {
            index = m_indexes[i];
            if (index.getName().equals(_sName))
                return index;
        }

        //else, not found
        return null;
    }

    /**
     * @see java.lang.Object#toString()
     */
    public String toString() {
        StringBuffer sbuff = new StringBuffer();
        try {
            sbuff.append("PropertyRef: type=").append(m_sType);
            sbuff.append(" key=").append(m_sKey);
            sbuff.append(" location=").append(m_sLocator);
            return sbuff.toString();
        } finally {
            sbuff.setLength(0);
        }
    }

    //=========================================================================
    //IXMLSupporter implementation

    /**
     * @see com.pureinfo.force.xml.IXMLSupporter#toXMLElement(org.dom4j.Element)
     */
    public void toXMLElement(Element _element) throws PureException {
        _element.addAttribute("type", m_sType);
        _element.addAttribute("key", m_sKey);
        _element.addAttribute("locator", m_sLocator);

        //to export indexes
        Element ele;
        for (int i = 1; i < m_indexes.length; i++) {
            ele = DocumentHelper.createElement("index");
            m_indexes[i].toXMLElement(ele);
            _element.add(ele);
        }
    }

    /**
     * @see com.pureinfo.force.xml.IXMLSupporter#fromXML(org.dom4j.Element)
     */
    public void fromXML(Element _element) throws PureException {
        m_sType = _element.attributeValue("type");
        m_sKey = _element.attributeValue("key");
        m_sLocator = _element.attributeValue("locator");

        //to read indexes
        List list = _element.elements("index");
        int nSize = list == null ? 0 : list.size();
        m_indexes = new RefIndex[1 + nSize];

        RefIndex index;
        index = new RefIndex();
        index.setName(m_sKey);
        m_indexes[0] = index;

        for (int i = 0; i < nSize; i++) {
            index = new RefIndex();
            index.fromXML((Element) list.get(i));
            m_indexes[i + 1] = index;
        }
    }

}