com.pureinfo.dolphin.query.QueryEntity.java Source code

Java tutorial

Introduction

Here is the source code for com.pureinfo.dolphin.query.QueryEntity.java

Source

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

package com.pureinfo.dolphin.query;

import org.apache.commons.lang.StringUtils;

import com.pureinfo.dolphin.DolphinConstants;
import com.pureinfo.dolphin.DolphinHelper;
import com.pureinfo.dolphin.mapping.EntityMetadata;
import com.pureinfo.dolphin.mapping.PropertyMetadata;
import com.pureinfo.force.exception.PureException;

/**
 * <P>
 * Created on 2005-10-28 11:25:15 <BR>
 * Last modified on 2005-10-28
 * </P>
 * QueryEntity: register for query.
 * 
 * @author pureinfo
 * @version 1.0, 2005-10-28
 * @since Quake 1.0
 */
public class QueryEntity {
    private EntityMetadata m_metadata;

    private String m_sAlias;

    private String m_sJoinCondition;

    private String[] m_depends;

    private int m_nJoinType = DolphinConstants.JOIN_UNKNOWN;

    private boolean m_bJoined = false;

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

    /**
     * 
     * Constructor
     * 
     * @param _metadata
     * @param _sAlias
     * @param _sJoinCondition
     */
    public QueryEntity(EntityMetadata _metadata, String _sAlias, String _sJoinCondition) {
        m_metadata = _metadata;
        m_sAlias = _sAlias;
        m_sJoinCondition = _sJoinCondition;
    }

    /**
     * 
     * Constructor
     * 
     * @param _metadata
     * @param _sAlias
     * @param _sJoinCondition
     * @param _sDepends
     */
    public QueryEntity(EntityMetadata _metadata, String _sAlias, String _sJoinCondition, String _sDepends) {
        this(_metadata, _sAlias, _sJoinCondition);
        this.setDepends(_sDepends);
    }

    /**
     * Returns the metadata.
     * 
     * @return the metadata.
     */
    public EntityMetadata getMetadata() {
        return m_metadata;
    }

    /**
     * Sets the metadata.
     * 
     * @param _metadata
     *            the metadata to set.
     */
    public void setMetadata(EntityMetadata _metadata) {
        m_metadata = _metadata;
    }

    /**
     * Sets the metadata of this entity.
     * 
     * @param _sName
     *            the metadata name (entity class name)
     */
    public void setMetadata(String _sName) throws PureException {
        m_metadata = DolphinHelper.lookupEntityMetadataByName(_sName, true);
    }

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

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

    /**
     * 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 _arrDepends
     *            the depends to set.
     */
    public void setDepends(String[] _arrDepends) {
        m_depends = _arrDepends;
    }

    /**
     * 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, ',');
        }
    }

    /**
     * Returns the join type.
     * 
     * @return the join type.
     * @since Dolphin V1.2
     */
    public int getJoinType() {
        return m_nJoinType;
    }

    /**
     * Sets the joinType.
     * 
     * @param _nJoinType
     *            the joinType to set, which is defined in DolphinConstants,
     *            like JOIN_LEFT, JOIN_RIGHT, etc.
     * @since Dolphin V1.2
     */
    public void setJoinType(int _nJoinType) {
        m_nJoinType = _nJoinType;
    }

    /**
     * Sets the joined.
     * 
     * @param _bJoined
     *            the joined to set.
     */
    public void setJoined(boolean _bJoined) {
        m_bJoined = _bJoined;
    }

    /**
     * Returns the joined.
     * 
     * @return the joined.
     */
    public boolean isJoined() {
        return m_bJoined;
    }

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

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

    /**
     * Returns the property metadata.
     * 
     * @param _sName
     *            the property name given
     * @param _bRequired
     *            <code>true</code>, the property is required to exist.
     * @throws PureException
     *             if the property is required but not found.
     */
    public PropertyMetadata getPropertyMetadata(String _sName, boolean _bRequired) throws PureException {
        return m_metadata.lookupPropertyByName(_sName, _bRequired);
    }
}