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

Java tutorial

Introduction

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

Source

/**
 * PureInfo Dolphin
 * @(#)EntityRelative.java   1.0 Nov 3, 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.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.dom4j.Element;

import com.pureinfo.dolphin.DolphinHelper;
import com.pureinfo.force.container.IClearable;
import com.pureinfo.force.exception.PureException;
import com.pureinfo.force.exception.PureRuntimeException;
import com.pureinfo.force.xml.IXMLSupporter;

/**
 * <P>
 * Created on Nov 3, 2005 1:24:32 PM <BR>
 * Last modified on Nov 3, 2005
 * </P>
 * EntityRelative: entity alias.
 * 
 * @author Why
 * @version 1.0, Nov 3, 2005
 * @since Dolphin 1.0
 */
public class EntityRelative implements IXMLSupporter, IClearable {

    private String m_sAlias;

    private String m_sType;

    private String m_sJoinCondition;

    private String[] m_depends;

    private Map m_hPropertyDescs;

    //cached data
    private Class m_typeClass = null;

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

    /**
     * @see com.pureinfo.force.container.IClearable#clear()
     */
    public void clear() {
        if (m_hPropertyDescs != null) {
            m_hPropertyDescs.clear();
        }
    }

    //=========================================================================
    //properties read/write

    /**
     * Returns the name.
     * 
     * @return the name.
     */
    public String getAlias() {
        return m_sAlias;
    }

    /**
     * Sets the name.
     * 
     * @param _sAlias
     *            the name to set.
     */
    public void setAlias(String _sAlias) {
        m_sAlias = _sAlias;
    }

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

    /**
     * Returns the entity class.
     */
    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);
            }
        }

        return m_typeClass;
    }

    /**
     * Sets relative class name.
     * 
     * @param _sClassName
     *            the type class name to set.
     */
    public void setType(String _sClassName) {
        if (!_sClassName.equals(m_sType)) {
            m_sType = _sClassName;
            m_typeClass = null;
        }
    }

    /**
     * Sets the entity class.
     * 
     * @param _class
     *            the entity class.
     */
    public void setType(Class _class) {
        m_sType = _class.getName();
        m_typeClass = _class;
    }

    /**
     * Returns the joinCondition.
     * 
     * @return the joinCondition.
     */
    public String getJoinCondition() {
        return m_sJoinCondition;
    }

    /**
     * Sets the joinCondition.
     * 
     * @param _sJoinCondition
     *            the joinCondition to set.
     */
    public void setJoinCondition(String _sJoinCondition) {
        m_sJoinCondition = _sJoinCondition;
    }

    /**
     * Returns the depends.
     * 
     * @return the depends.
     */
    public String[] getDepends() {
        return m_depends;
    }

    /**
     * Sets the depends.
     * 
     * @param _sDepends
     *            the depends to set.
     */
    public void setDepends(String _sDepends) {
        if (_sDepends == null || (_sDepends = _sDepends.trim()).length() == 0) {
            m_depends = null;
        } else {
            m_depends = StringUtils.split(_sDepends, ',');
        }
    }

    /**
     * @see java.lang.Object#toString()
     */
    public String toString() {
        StringBuffer sbuff = new StringBuffer();
        try {
            sbuff.append("alias=").append(m_sAlias);
            sbuff.append(", type=").append(m_sType);
            sbuff.append(", join-condition=").append(m_sJoinCondition);
            if (m_depends != null) {
                sbuff.append(", depends=").append(StringUtils.join(m_depends, ','));
            }
            return sbuff.toString();
        } finally {
            sbuff.setLength(0);
        }
    }

    //=========================================================================
    //logic operation

    /**
     * Returns the metadata of this relative entity.
     * 
     * @param _bRequired
     *            if the metadata is required to exist
     * @throws PureException
     *             if the metadata is not found but requried.
     */
    public EntityMetadata getMetadata(boolean _bRequired) throws PureException {
        return DolphinHelper.lookupEntityMetadataByName(m_sType, _bRequired);
    }

    /**
     * Returns the description of the specified property.
     * 
     * @param _sName
     *            the property name
     * @return the property description
     * @throws PureException
     *             if failed.
     */
    public String getPropertyDesc(String _sName) throws PureException {
        String sDesc = (m_hPropertyDescs == null) ? null : (String) m_hPropertyDescs.get(_sName);
        if (sDesc == null) {
            PropertyMetadata metadata = this.getMetadata(true).lookupPropertyByName(_sName, true);
            sDesc = metadata.getDesc();
        }
        return sDesc;
    }

    //=========================================================================
    //XML support

    /**
     * @see com.pureinfo.force.xml.IXMLSupporter#toXMLElement(org.dom4j.Element)
     */
    public void toXMLElement(Element _element) throws PureException {
        _element.addAttribute("alias", m_sAlias);
        _element.addAttribute("type", m_sType);
        _element.addAttribute("join-condition", m_sJoinCondition);
        if (m_depends != null) {
            _element.addAttribute("depends", StringUtils.join(m_depends, ','));
        }

        if (m_hPropertyDescs != null) {
            Map.Entry entry;
            Element ele;
            Iterator iter = m_hPropertyDescs.entrySet().iterator();
            while (iter.hasNext()) {
                entry = (Map.Entry) iter.next();
                ele = _element.addElement("property");
                ele.addAttribute("name", (String) entry.getKey());
                ele.addAttribute("desc", (String) entry.getValue());
            }
        }
    }

    /**
     * @see com.pureinfo.force.xml.IXMLSupporter#fromXML(org.dom4j.Element)
     */
    public void fromXML(Element _element) throws PureException {
        this.clear();

        //to load attributes
        this.setAlias(_element.attributeValue("alias"));
        this.setType(_element.attributeValue("type"));
        this.setJoinCondition(_element.attributeValue("join-condition"));
        this.setDepends(_element.attributeValue("depends"));

        //to read properties
        List list = _element.elements("property");
        if (list == null || list.isEmpty()) {
            m_hPropertyDescs = null;
        } else {
            Element ele;
            String sName, sDesc;
            m_hPropertyDescs = new HashMap(list.size());
            for (int i = 0; i < list.size(); i++) {
                ele = (Element) list.get(i);
                sName = ele.attributeValue("name");
                sDesc = ele.attributeValue("desc");
                m_hPropertyDescs.put(sName, sDesc);
            }
            list.clear();
        }
    }
}