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

Java tutorial

Introduction

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

Source

/**
 * PureInfo Ark
 * @(#)SwitchDef.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 java.util.List;

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.mapping.PropertyType;
import com.pureinfo.dolphin.query.logic.ISQLLogic;
import com.pureinfo.dolphin.query.logic.SQLCondition;
import com.pureinfo.dolphin.query.logic.SQLLogicString;
import com.pureinfo.force.exception.PureException;
import com.pureinfo.force.object.DataTypes;
import com.pureinfo.force.xml.IXMLSupporter;
import com.pureinfo.force.xml.XMLUtil;

/**
 * <P>
 * Created on 2006-9-30 10:39:33 <BR>
 * Last modified on 2006-9-30
 * </P>
 * SwitchDef: definition for switch.
 * 
 * @author Why
 * @version 1.0, 2006-9-30
 * @since Ark 1.2
 */
public class SwitchDef implements IXMLSupporter {
    private EntityMetadata m_resourceMetadata;

    private String m_sProperty;

    private String m_sProperty4sql;

    private int m_nValueType;

    private CaseDef[] m_cases;

    //cached data
    private String m_sMyProperty; //for the property may be like "department.internalType"

    private String m_sAlias;

    /**
     * Constructor: default
     */
    public SwitchDef(EntityMetadata _resourceMetadata) {
        super();
        this.setResourceMetadata(_resourceMetadata);
    }

    /**
     * Returns the resourceMetadata.
     * 
     * @return the resourceMetadata.
     */
    public EntityMetadata getResourceMetadata() {
        return m_resourceMetadata;
    }

    /**
     * Sets the resourceMetadata.
     * 
     * @param _resourceMetadata
     *            the resourceMetadata to set.
     */
    public void setResourceMetadata(EntityMetadata _resourceMetadata) {
        m_resourceMetadata = _resourceMetadata;
    }

    /**
     * Returns the property.
     * 
     * @return the property.
     */
    public String getProperty() {
        return m_sProperty;
    }

    /**
     * Sets the property.
     * 
     * @param _sProperty
     *            the property to set.
     */
    public void setProperty(String _sProperty) {
        m_sProperty = _sProperty;

        int nPos = _sProperty.indexOf('.');
        if (nPos < 1) {
            m_sMyProperty = _sProperty;
        } else {
            m_sMyProperty = _sProperty.substring(0, nPos);
        }
    }

    /**
     * Returns the property4sql.
     * 
     * @return the property4sql.
     */
    public String getProperty4sql() {
        String sProperty = m_sProperty4sql != null ? m_sProperty4sql : "this." + m_sProperty;
        return "{" + sProperty + "}";
    }

    /**
     * Sets the property4sql.
     * 
     * @param _sProperty4sql
     *            the property4sql to set.
     */
    public void setProperty4sql(String _sProperty4sql) {
        m_sProperty4sql = _sProperty4sql;
        if (_sProperty4sql == null) {
            m_sAlias = null;
        } else {
            m_sAlias = _sProperty4sql.substring(0, _sProperty4sql.indexOf('.'));
        }
    }

    /**
     * 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 cases.
     * 
     * @return the cases.
     */
    public CaseDef[] getCases() {
        return m_cases;
    }

    /**
     * Sets the cases.
     * 
     * @param _cases
     *            the cases to set.
     */
    public void setCases(CaseDef[] _cases) {
        m_cases = _cases;
    }

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

    /**
     * Parses the allowed action right as bits-value.
     * 
     * @param _actionDictionary
     *            the action dictionary to lookup the action.
     *  
     */
    public void parseRight(IActionDictionary _actionDictionary) throws PureException {
        if (m_cases == null)
            return;

        //else
        for (int i = 0; i < m_cases.length; i++) {
            m_cases[i].parseRight(_actionDictionary);
        }
    }

    /**
     * Returns <code>true</code> if the user has right to do the action on the
     * specified resource.
     * 
     * @param _loginUser
     *            the current login user
     * @param _resource
     *            the resource to be operated
     * @param _nActionIndex
     *            index of the action
     * @return <code>null</code> if no definition of the specified action;
     *         <code>Boolean.TRUE</code> if the user has right to do the
     *         action on the specified resource; <code>Boolean.FALSE</code>,
     *         otherwise.
     * @throws PureException
     *             if failed.
     */
    public Boolean hasRight(IUser _loginUser, ArkContent _resource, int _nActionIndex) throws PureException {
        if (m_cases == null)
            return null; //undefined

        //else
        Object value = _resource.getRefProperty(m_sProperty);
        for (int i = 0; i < m_cases.length; i++) {
            if (m_cases[i].match(value)) {
                return m_cases[i].hasRight(_loginUser, _resource, _nActionIndex);
            }
        }

        return null; //no any case matched
    }

    /**
     * Returns <code>true</code> if there is path to do the action on the
     * specified resource.
     * 
     * @param _loginUser
     *            the current login user
     * @param _resource
     *            the resource to be operated
     * @param _nActionIndex
     *            index of the action
     * @return <code>null</code> if no definition of the specified action;
     *         <code>Boolean.TRUE</code> if there is path to do the action on
     *         the specified resource; <code>Boolean.FALSE</code>, otherwise.
     * @throws PureException
     *             if failed.
     */
    public Boolean hasPath(IUser _loginUser, ArkContent _resource, int _nActionIndex) throws PureException {
        if (m_cases == null)
            return null; // undefined

        // else
        if (_resource.hasProperty(m_sMyProperty)) {
            Object value = _resource.getRefProperty(m_sProperty);
            for (int i = 0; i < m_cases.length; i++) {
                if (m_cases[i].match(value)) {
                    return m_cases[i].hasPath(_loginUser, _resource, _nActionIndex);
                }
            }
        } else {
            for (int i = 0; i < m_cases.length; i++) {
                Boolean temp = m_cases[i].hasPath(_loginUser, _resource, _nActionIndex);
                if (temp == Boolean.TRUE)
                    return temp;
            }
        }

        return null; // no any case matched
    }

    /**
     * Returns <code>true</code> if there is entry (ignore detailed rules) to
     * do the action on the specified resource.
     * 
     * @param _resource
     *            the resource to be operated
     * @param _nActionIndex
     *            index of the action
     * @return <code>null</code> if no definition of the specified action;
     *         <code>Boolean.TRUE</code> if there is entry to do the action on
     *         the specified resource; <code>Boolean.FALSE</code>, otherwise.
     * @throws PureException
     *             if failed.
     */
    public Boolean hasEntry(ArkContent _resource, int _nActionIndex) throws PureException {
        if (m_cases == null)
            return null; //undefined

        //else
        if (_resource.hasProperty(m_sMyProperty)) {
            Object value = _resource.getRefProperty(m_sProperty);
            for (int i = 0; i < m_cases.length; i++) {
                if (m_cases[i].match(value)) {
                    return m_cases[i].hasEntry(_resource, _nActionIndex);
                }
            }
        } else {
            for (int i = 0; i < m_cases.length; i++) {
                Boolean temp = m_cases[i].hasEntry(_resource, _nActionIndex);
                if (temp != null)
                    return temp;
            }
        }

        return null; //undefined
    }

    /**
     * Returns the restriction rule as SQL logic when the user do the specified
     * action on the resource.
     * 
     * @param _loginUser
     *            the current user
     * @param _resource
     *            the resource to be operated
     * @param _nActionIndex
     *            index of the action
     * @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)
            throws PureException {
        if (m_cases == null)
            return null; //undefined

        //to check if the property value has been specified
        if (_resource.hasProperty(m_sMyProperty)) {
            Object value = _resource.getRefProperty(m_sProperty);
            for (int i = 0; i < m_cases.length; i++) {
                if (m_cases[i].match(value)) {
                    return m_cases[i].getSQLLogicRule(_loginUser, _resource, _nActionIndex, null);
                }
            }

            //else, not found
            return null; //undefined
        }

        //else, the property value is not speicified
        ISQLLogic logic;
        String sProperty4sql = this.getProperty4sql();
        SQLCondition condition = new SQLCondition(false, m_cases.length);
        for (int i = 0; i < m_cases.length; i++) {
            logic = m_cases[i].getSQLLogicRule(_loginUser, _resource, _nActionIndex, sProperty4sql);
            if (logic == SQLLogicString.TRUE) {
                return SQLLogicString.TRUE;
            }
            if (logic != null && logic != SQLLogicString.FALSE) {
                condition.append(logic);
            }
        }

        switch (condition.size()) {
        case 0:
            return null; //undefined
        case 1: {
            logic = (ISQLLogic) condition.getConditions().get(0);
            condition.clear();
            break;
        }
        default:
            logic = condition;
        }

        if (m_sAlias != null) {
            logic.addAlias(m_sAlias);
        }
        return logic;
    }

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

    /**
     * @see com.pureinfo.force.xml.IXMLSupporter#toXMLElement(org.dom4j.Element)
     */
    public void toXMLElement(Element _element) throws PureException {
        _element.addAttribute("property", m_sProperty);
        if (m_sProperty4sql != null) {
            _element.addAttribute("property4sql", m_sProperty4sql);
            _element.addAttribute("type", DataTypes.getName(m_nValueType));
        }

        if (m_cases != null) {
            for (int i = 0; i < m_cases.length; i++) {
                _element.add(XMLUtil.toXMLElement(m_cases[i], "case"));
            }
        }
    }

    /**
     * @see com.pureinfo.force.xml.IXMLSupporter#fromXML(org.dom4j.Element)
     */
    public void fromXML(Element _element) throws PureException {
        this.setProperty(XMLUtil.getAttributeValueTrim(_element, "property"));
        this.setProperty4sql(XMLUtil.getAttributeValueTrim(_element, "property4sql"));

        //read value type
        String sTypeName = XMLUtil.getAttributeValueTrim(_element, "type");
        if (sTypeName != null) {
            this.setValueType(PropertyType.lookupByName(sTypeName).getDataType());
        } else {
            this.setValueType(m_resourceMetadata.getProperty(m_sProperty, true).getDataType());
        }

        //to read cases
        List list = _element.elements("case");
        if (list == null || list.isEmpty()) {
            m_cases = null;
        } else {
            try {
                CaseDef caseDef;
                m_cases = new CaseDef[list.size()];
                for (int i = 0; i < list.size(); i++) {
                    caseDef = new CaseDef(m_resourceMetadata, m_nValueType);
                    caseDef.fromXML((Element) list.get(i));
                    m_cases[i] = caseDef;
                }
            } finally {
                list.clear();
            }
        }
    }
}