com.pureinfo.ark.auth2.model.CaseDef.java Source code

Java tutorial

Introduction

Here is the source code for com.pureinfo.ark.auth2.model.CaseDef.java

Source

/**
 * PureInfo Ark
 * @(#)CaseDef.java   1.0 2006-9-30
 * 
 * Copyright(c) 2004-2005, PureInfo Information Technology Corp. Ltd. 
 * All rights reserved, see the license file.
 * 
 * www.pureinfo.com.cn
 */

package com.pureinfo.ark.auth2.model;

import org.dom4j.Element;

import com.pureinfo.ark.auth.model.IUser;
import com.pureinfo.ark.content.model.ArkContent;
import com.pureinfo.dolphin.mapping.EntityMetadata;
import com.pureinfo.dolphin.query.SQLOperator;
import com.pureinfo.dolphin.query.logic.ISQLLogic;
import com.pureinfo.dolphin.query.logic.SQLCondition;
import com.pureinfo.dolphin.query.logic.SQLLogicBuilder;
import com.pureinfo.dolphin.query.logic.SQLLogicString;
import com.pureinfo.force.exception.PureException;
import com.pureinfo.force.object.Values;
import com.pureinfo.force.xml.XMLUtil;

/**
 * <P>
 * Created on 2006-9-30 10:41:04 <BR>
 * Last modified on 2006-9-30
 * </P>
 * CaseDef: definition for case condition.
 * 
 * @author Why
 * @version 1.0, 2006-9-30
 * @since Ark 1.2
 */
public class CaseDef extends RightDefBase {
    private int m_nValueType;

    private Values m_values = new Values();

    /**
     * Constructor
     * 
     * @param _resourceMetadata
     * @param _nValueType
     */
    public CaseDef(EntityMetadata _resourceMetadata, int _nValueType) {
        super(_resourceMetadata);
        this.setValueType(_nValueType);
    }

    /**
     * Returns the valueType.
     * 
     * @return the valueType.
     */
    public int getValueType() {
        return m_nValueType;
    }

    /**
     * Sets the valueType.
     * 
     * @param _nValueType
     *            the valueType to set.
     */
    public void setValueType(int _nValueType) {
        m_nValueType = _nValueType;
    }

    /**
     * Returns the values.
     * 
     * @return the values.
     */
    public Values getValues() {
        return m_values;
    }

    /**
     * Sets the values.
     * 
     * @param _sValue
     *            the values to set.
     * @param _nDataType
     *            data type of each value
     */
    public void setValues(String _sValue) {
        if (m_values == null) {
            m_values = new Values();
        }
        m_values.fromString(_sValue, m_nValueType);
    }

    //=========================================================================
    //logic interface for authorization

    /**
     * Returns <code>true</code> if the specified value matchs with current
     * case condition.
     * 
     * @param _value
     *            the specified value
     */
    public boolean match(Object _value) {
        return m_values.indexOf(_value) >= 0;
    }

    /**
     * Returns the restriction rule as SQL logic when the user do the specified
     * action on the resource. <br>
     * NOTICE: if the parameter _sProperty4sql is <code>null</code>, the SQL
     * logic rule to return will contains the case condition value filter;
     * otherwise, only the inside logic in this case will be returned.
     * 
     * @param _loginUser
     *            the current user
     * @param _resource
     *            the resource to be operated
     * @param _nActionIndex
     *            index of the action
     * @param _sProperty4sql
     *            the property name for SQL filter, like "{this.name}",
     *            "{org.name}", etc.
     * @return <code>null</code>, if no definition of the specified action;;
     *         othwise, the restriction rule as SQL logic when the user do the
     *         specified action on the resource.
     * @throws PureException
     *             if failed to render the logic.
     */
    public ISQLLogic getSQLLogicRule(IUser _loginUser, ArkContent _resource, int _nActionIndex,
            String _sProperty4sql) throws PureException {
        ISQLLogic logic = super.getSQLLogicRule(_loginUser, _resource, _nActionIndex);
        if (logic == null || logic == SQLLogicString.FALSE || _sProperty4sql == null)
            return logic;

        //else, _sProperty4sql is set
        ISQLLogic valueLogic = getValueSQLLogic(_sProperty4sql);
        if (logic == SQLLogicString.TRUE) {
            return valueLogic;
        }

        //else
        SQLCondition condition = new SQLCondition(true, 2);
        condition.append(valueLogic).append(logic);
        return condition;
    }

    /**
     * Returns the SQL logic about the case value condition.
     * 
     * @param _sProperty4sql
     *            the property for SQL filter, like "{this.name}","{org.name}",
     *            etc.
     * @return the SQL logic about the case value condition.
     */
    public ISQLLogic getValueSQLLogic(String _sProperty4sql) {
        return SQLLogicBuilder.build(_sProperty4sql, SQLOperator.IN, m_nValueType, m_values.getValues());
    }

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

    /**
     * @see com.pureinfo.force.xml.IXMLSupporter#toXMLElement(org.dom4j.Element)
     */
    public void toXMLElement(Element _element) throws PureException {
        _element.addAttribute("value", m_values.toString());
        super.toXMLElement(_element);
    }

    /**
     * @see com.pureinfo.force.xml.IXMLSupporter#fromXML(org.dom4j.Element)
     */
    public void fromXML(Element _element) throws PureException {
        this.setValues(XMLUtil.getAttributeValueTrim(_element, "value"));
        super.fromXML(_element);
    }
}