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

Java tutorial

Introduction

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

Source

/**
 * PureInfo Ark
 * @(#)ResourceDef.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.content.ArkContentHelper;
import com.pureinfo.ark.content.model.ContentType;
import com.pureinfo.dolphin.mapping.EntityMetadata;
import com.pureinfo.force.exception.PureException;
import com.pureinfo.force.xml.IXMLSupporter;
import com.pureinfo.force.xml.XMLUtil;

/**
 * <P>
 * Created on 2006-9-30 11:12:42 <BR>
 * Last modified on 2006-9-30
 * </P>
 * ResourceDef:
 * 
 * @author Why
 * @version 1.0, 2006-9-30
 * @since Ark 1.2
 */
public class ResourceDef implements IXMLSupporter {
    private ContentType m_type;

    private BaseDef m_base;

    private RightDef[] m_rights;

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

    /**
     * Returns the type.
     * 
     * @return the type.
     */
    public ContentType getType() {
        return m_type;
    }

    /**
     * Returns the type id.
     * 
     * @return the type id.
     */
    public int getTypeId() {
        return m_type.getId();
    }

    /**
     * Sets the type.
     * 
     * @param _nTypeId
     *            the type to set.
     * @throws PureException
     *             if the content metadata not found.
     */
    public void setType(int _nTypeId) throws PureException {
        m_type = ArkContentHelper.lookupTypeById(_nTypeId, true);
    }

    /**
     * Returns the base.
     * 
     * @return the base.
     */
    public BaseDef getBase() {
        return m_base;
    }

    /**
     * Sets the base.
     * 
     * @param _base
     *            the base to set.
     */
    public void setBase(BaseDef _base) {
        m_base = _base;
    }

    /**
     * Returns the rights.
     * 
     * @return the rights.
     */
    public RightDef[] getRights() {
        return m_rights;
    }

    /**
     * Sets the rights.
     * 
     * @param _rights
     *            the rights to set.
     */
    public void setRights(RightDef[] _rights) {
        m_rights = _rights;
    }

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

    /**
     * @see com.pureinfo.force.xml.IXMLSupporter#toXMLElement(org.dom4j.Element)
     */
    public void toXMLElement(Element _element) throws PureException {
        _element.addAttribute("type", String.valueOf(m_type.getId()));
        _element.addAttribute("desc", m_type.getDesc());

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

    /**
     * @see com.pureinfo.force.xml.IXMLSupporter#fromXML(org.dom4j.Element)
     */
    public void fromXML(Element _element) throws PureException {
        this.setType(XMLUtil.getAttributeValueAsInt(_element, "type"));

        EntityMetadata metadata = m_type.getContentMetadata(true);

        // to read the base
        Element ele = _element.element("base");
        if (ele == null) {
            m_base = null;
        } else {
            m_base = new BaseDef(metadata);
            m_base.fromXML(ele);
        }

        //to read rights
        List list = _element.elements("right");
        if (list == null || list.isEmpty()) {
            m_rights = null;
        } else {
            try {
                m_rights = new RightDef[list.size()];
                RightDef rightDef;
                for (int i = 0; i < list.size(); i++) {
                    rightDef = new RightDef(metadata);
                    rightDef.fromXML((Element) list.get(i));
                    m_rights[i] = rightDef;
                }
            } finally {
                list.clear();
            }
        }
    }

}