com.pureinfo.srm.org.action.SubQueryAction.java Source code

Java tutorial

Introduction

Here is the source code for com.pureinfo.srm.org.action.SubQueryAction.java

Source

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

package com.pureinfo.srm.org.action;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import org.apache.struts.action.ActionForward;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;

import com.pureinfo.ark.content.ArkContentHelper;
import com.pureinfo.ark.interaction.ActionBase;
import com.pureinfo.common.namedvalue.NamedValueHelper;
import com.pureinfo.common.namedvalue.model.INamedValue;
import com.pureinfo.force.exception.PureException;
import com.pureinfo.srm.SRMConstants;
import com.pureinfo.srm.SRMNamedValueTypes;
import com.pureinfo.srm.org.domain.IOrganizationMgr;
import com.pureinfo.srm.org.model.Organization;

/**
 * <P>
 * Created on 2006-5-15 9:46:19 <BR>
 * Last modified on 2006-5-15
 * </P>
 * TODO describe SubQueryAction here ...
 * 
 * @author elmar.chen
 * @version 1.0, 2006-5-15
 * @since Command 1.0
 */

public class SubQueryAction extends ActionBase {

    public static final int TYPE_OTHER = 2;

    public static final int TYPE_COLLEGE = 1;

    public static final int TYPE_ADMIN = 0;

    boolean m_bActiveOnly = false;
    boolean m_bIncludeLab = false;

    protected boolean isLoginNeeded() {
        return false;
    }

    /**
     * @throws PureException
     * @see com.pureinfo.ark.interaction.ActionBase#executeAction()
     */
    public ActionForward executeAction() throws PureException {
        Document xDoc = null;
        try {
            m_bActiveOnly = request.getBoolean("activeOnly");
            m_bIncludeLab = request.getBoolean("includeLab");
            boolean bShowOther = request.getBoolean("showOther");

            // memo: v1=1v2=, v3=, v3_2=
            String sV1, sV2, sV3, sV3_2;
            int nOrgId = -1;
            String sOrgCode = request.getParameter("orgCode");
            if (request.getParameter("orgId") != null) {
                nOrgId = Integer.parseInt(request.getParameter("orgId"));
            } else if (sOrgCode != null) {
                IOrganizationMgr mgr = (IOrganizationMgr) ArkContentHelper.getContentMgrOf(Organization.class);
                Organization org = mgr.lookupByCode(sOrgCode, false);
                if (org != null)
                    nOrgId = org.getId();
            }
            if (nOrgId > 0) {
                String[] params = extraParams(nOrgId);
                sV1 = params[0];
                sV2 = params[1];
                sV3 = params[2];
                logger.debug(sV1 + "-" + sV2 + "-" + sV3);
            } else {
                sV1 = request.getParameter("v1");
                sV2 = request.getParameter("v2");
                sV3 = request.getParameter("v3");
            }
            sV3_2 = request.getParameter("v3_2");

            OPGroup[] groups = getGroups(sV1, sV2, sV3, sV3_2, m_bActiveOnly, bShowOther);
            xDoc = groups2Doc(groups);
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/xml");
            response.setHeader("Cache-Control", "no-cache"); // HTTP 1.1
            response.setHeader("Pragma", "no-cache"); // HTTP 1.0
            response.setDateHeader("Expires", -1);
            response.setDateHeader("max-age", 0);
            XMLWriter writer = new XMLWriter(response.getWriter());

            writer.write(xDoc);
            writer.close();
        } catch (IOException ex1) {
            ex1.printStackTrace(System.err);
        }

        return null;
    }

    private String[] extraParams(int _nOrgId) throws PureException {
        String[] params = new String[3];
        IOrganizationMgr mgr = (IOrganizationMgr) ArkContentHelper.getContentMgrOf(Organization.class);
        Organization org = (Organization) mgr.lookupById(_nOrgId);
        if (SRMConstants.ORG_TYPE.OTHER.equals(org.getInternalType())) {
            params[0] = String.valueOf(TYPE_OTHER);
            params[1] = org.getOrgType();
            params[2] = String.valueOf(org.getId());
        } else {
            params[0] = String.valueOf(
                    SRMConstants.ORG_TYPE.ADMIN_PART.equals(org.getInternalType()) ? TYPE_ADMIN : TYPE_COLLEGE);
            params[org.getLevel() == 0 ? 1 : 2] = String.valueOf(org.getId());
        }
        return params;
    }

    /**
     * @param _groups
     * @return
     */
    private Document groups2Doc(OPGroup[] _groups) {
        Document doc = DocumentHelper.createDocument();

        Element xRoot = doc.addElement("datas");
        Element xType = xRoot.addElement("types");
        xType.addAttribute("default", _groups[0].sDefault);
        Element xOP = xType.addElement("type");
        xOP.addText("----");
        xOP.addAttribute("value", "");
        for (int i = 0; i < _groups[0].options.length; i++) {
            xOP = xType.addElement("type");
            xOP.addText(_groups[0].options[i][1]);
            xOP.addAttribute("value", _groups[0].options[i][0]);
        }

        if (_groups[1] == null) {
            Element xV1 = xRoot.addElement("level0");
            xOP = xV1.addElement("org");
            xOP.addText("----");
            xOP.addAttribute("value", "");
        } else {
            Element xV1 = xRoot.addElement("level0");
            xV1.addAttribute("default", _groups[1].sDefault);
            xOP = xV1.addElement("org");
            xOP.addText("----");
            xOP.addAttribute("value", "");
            for (int i = 0; i < _groups[1].options.length; i++) {
                xOP = xV1.addElement("org");
                xOP.addText(_groups[1].options[i][1]);
                xOP.addAttribute("value", _groups[1].options[i][0]);
                if (_groups[1].options[i].length > 2) {
                    xOP.addAttribute("code", _groups[1].options[i][2]);
                }
            }
        }

        if (_groups[2] == null) {
            Element xV1 = xRoot.addElement("level1");
            xOP = xV1.addElement("org");
            xOP.addText("----");
            xOP.addAttribute("value", "");
        } else {
            OPGroup group = _groups[2];
            if (group.options.length == 0) {
                Element xV1 = xRoot.addElement("level1");
                xV1.addAttribute("default", group.sDefault);
                xOP = xV1.addElement("org");
                xOP.addText("");
                xOP.addAttribute("value", "");
            } else {
                Element xV1 = xRoot.addElement("level1");
                xV1.addAttribute("default", group.sDefault);
                xOP = xV1.addElement("org");
                xOP.addText("----");
                xOP.addAttribute("value", "");
                for (int i = 0; i < group.options.length; i++) {
                    xOP = xV1.addElement("org");
                    xOP.addText(group.options[i][1]);
                    xOP.addAttribute("value", group.options[i][0]);
                    if (group.options[i].length > 2) {
                        xOP.addAttribute("code", group.options[i][2]);
                    }
                }
            }
        }

        if (_groups.length > 3 && _groups[3] != null) {
            OPGroup group = _groups[3];
            if (group.options.length == 0) {
                Element xV1 = xRoot.addElement("level1_2");
                xV1.addAttribute("default", group.sDefault);
                xOP = xV1.addElement("org");
                xOP.addText("");
                xOP.addAttribute("value", "");
            } else {
                Element xV1 = xRoot.addElement("level1_2");
                xV1.addAttribute("default", group.sDefault);
                xOP = xV1.addElement("org");
                xOP.addText("----");
                xOP.addAttribute("value", "");
                for (int i = 0; i < group.options.length; i++) {
                    xOP = xV1.addElement("org");
                    xOP.addText(group.options[i][1]);
                    xOP.addAttribute("value", _groups[3].options[i][0]);
                    if (group.options[i].length > 2) {
                        xOP.addAttribute("code", group.options[i][2]);
                    }
                }
            }
        }

        return doc;
    }

    /**
     * @param _bShowOther
     * @param _bActiveOnly
     * @param _sV3
     * @param _sV2
     * @param _sV1
     * @return
     * @throws PureException
     */
    private OPGroup[] getGroups(String _sV1, String _sV2, String _sV3, String _sV3_2, boolean _bActiveOnly,
            boolean _bShowOther) throws PureException {
        if (_sV3 != null && _sV3.length() > 0) {
            return getGroupsByV3(_sV3, _sV3_2, _bActiveOnly, _bShowOther);
        }
        if (_sV2 != null && _sV2.length() > 0) {
            return getGroupsByV2(_sV1, _sV2, _bActiveOnly, _bShowOther);
        }
        if (_sV1 != null && _sV1.length() > 0) {
            return getGroupsByV1(_sV1, _bActiveOnly, _bShowOther);
        }
        return getDefalutGroups(_bShowOther);
    }

    /**
     * @param _bShowOther
     * @return
     */
    private OPGroup[] getDefalutGroups(boolean _bShowOther) {
        OPGroup[] groups = new OPGroup[3];
        groups[0] = getTypeGroup(_bShowOther);
        return groups;
    }

    /**
     * @param _sV1
     * @param _bActiveOnly
     * @param _bShowOther
     * @return
     * @throws PureException
     */
    private OPGroup[] getGroupsByV1(String _sV1, boolean _bActiveOnly, boolean _bShowOther) throws PureException {
        OPGroup[] groups = new OPGroup[3];
        groups[0] = getTypeGroup(_bShowOther);
        groups[0].sDefault = _sV1;

        groups[1] = new OPGroup();
        List orgs = null;
        IOrganizationMgr mgr = (IOrganizationMgr) ArkContentHelper.getContentMgrOf(Organization.class);
        switch (Integer.parseInt(_sV1)) {
        case TYPE_ADMIN:
            orgs = mgr.findAllByType(SRMConstants.ORG_TYPE.ADMIN_PART, _bActiveOnly);
            groups[1].options = makeOptions(orgs, true);
            break;
        case TYPE_COLLEGE:
            orgs = mgr.findAllByType(SRMConstants.ORG_TYPE.COLLEGE, _bActiveOnly);
            groups[1].options = makeOptions(orgs, true);
            break;
        case TYPE_OTHER:
            groups[1] = getOtherType();
        }

        return groups;
    }

    /**
     * @param _sV1
     * @param _sV2
     * @param _bActiveOnly
     * @param _bShowOther
     * @return
     * @throws PureException
     */
    private OPGroup[] getGroupsByV2(String _sV1, String _sV2, boolean _bActiveOnly, boolean _bShowOther)
            throws PureException {
        if (("" + TYPE_OTHER).equals(_sV1)) {
            return getGroupsByV2ForOther(_sV2, _bActiveOnly, _bShowOther);
        }
        return getGroupsByV2ForAdminNCollege(_sV2, _bActiveOnly, _bShowOther);
    }

    /**
     * @param _sV2
     * @param _bActiveOnly
     * @return
     * @throws PureException
     */
    private OPGroup[] getGroupsByV2ForAdminNCollege(String _sV2, boolean _bActiveOnly, boolean _bShowOther)
            throws PureException {
        int orgId = Integer.parseInt(_sV2);
        IOrganizationMgr mgr = (IOrganizationMgr) ArkContentHelper.getContentMgrOf(Organization.class);
        Organization org = (Organization) mgr.lookupById(orgId);

        OPGroup[] groups = new OPGroup[3];
        groups[0] = getTypeGroup(_bShowOther);
        groups[0].sDefault = ""
                + (SRMConstants.ORG_TYPE.ADMIN_PART.equals(org.getInternalType()) ? TYPE_ADMIN : TYPE_COLLEGE);

        groups[1] = new OPGroup();
        groups[1].sDefault = "" + org.getId();

        List orgs = mgr.findAllByType(org.getInternalType(), _bActiveOnly);
        groups[1].options = makeOptions(orgs, true);

        groups[2] = new OPGroup();
        List children = mgr.getChildren(org.getId(), _bActiveOnly, m_bIncludeLab);
        groups[2].options = makeOptions(children, true);
        return groups;
    }

    /**
     * @param _sV1
     * @param _sV2
     * @param _sActiveOnly
     * @return
     * @throws PureException
     */
    private OPGroup[] getGroupsByV2ForOther(String _sV2, boolean _sActiveOnly, boolean _bShowOther)
            throws PureException {
        logger.debug(_sV2);
        OPGroup[] groups = new OPGroup[3];
        groups[0] = getTypeGroup(_bShowOther);
        groups[0].sDefault = "" + TYPE_OTHER;

        groups[1] = getOtherType();
        groups[1].sDefault = _sV2;

        groups[2] = new OPGroup();
        IOrganizationMgr mgr = (IOrganizationMgr) ArkContentHelper.getContentMgrOf(Organization.class);
        List orgs = mgr.findAllOtherByOrgType(_sV2, _sActiveOnly, false);
        groups[2].options = makeOptions(orgs, true);
        return groups;
    }

    /**
     * @param _sValue
     * @param _bActiveOnly
     * @param _bShowOther
     * @return
     * @throws PureException
     */
    private OPGroup[] getGroupsByV3(String _sValue, String _sValue2, boolean _bActiveOnly, boolean _bShowOther)
            throws PureException {
        int orgId = Integer.parseInt(_sValue);
        IOrganizationMgr mgr = (IOrganizationMgr) ArkContentHelper.getContentMgrOf(Organization.class);
        Organization org = (Organization) mgr.lookupById(orgId);
        if (org == null)
            return new OPGroup[3];

        // else
        OPGroup[] groups;
        if (SRMConstants.ORG_TYPE.OTHER.equals(org.getInternalType())) {
            groups = getGroupsByV3ForOther(org, _bActiveOnly, _bShowOther);
        } else {
            groups = getGroupsByV3ForAdminNCollege(org, _bActiveOnly, _bShowOther);
        }

        // to get crossed brothers
        List brothers = mgr.getCrossedBrothers(org, _bActiveOnly);
        if (brothers != null) {
            if (groups.length < 4) {
                OPGroup[] temp = new OPGroup[4];
                System.arraycopy(groups, 0, temp, 0, groups.length);
                groups = temp;
            }
            groups[3] = new OPGroup();
            groups[3].sDefault = _sValue2;
            groups[3].options = makeOptions(brothers, true);
        }
        return groups;
    }

    /**
     * @param _org
     * @param _bActiveOnly
     * @param _bShowOther
     * @return
     * @throws PureException
     */
    private OPGroup[] getGroupsByV3ForAdminNCollege(Organization _org, boolean _bActiveOnly, boolean _bShowOther)
            throws PureException {
        OPGroup[] groups = new OPGroup[4];

        // 0: type
        groups[0] = getTypeGroup(_bShowOther);
        groups[0].sDefault = String
                .valueOf(SRMConstants.ORG_TYPE.ADMIN_PART.equals(_org.getParent().getInternalType()) ? TYPE_ADMIN
                        : TYPE_COLLEGE);

        // 1.
        groups[1] = new OPGroup();
        groups[1].sDefault = "" + _org.getParentId();

        IOrganizationMgr mgr = (IOrganizationMgr) ArkContentHelper.getContentMgrOf(Organization.class);
        List orgs = mgr.findAllByType(_org.getParent().getInternalType(), _bActiveOnly);
        groups[1].options = makeOptions(orgs, true);

        // 2.
        groups[2] = new OPGroup();
        groups[2].sDefault = "" + _org.getId();

        List brothers = mgr.getChildren(_org.getParentId(), _bActiveOnly, m_bIncludeLab);
        groups[2].options = makeOptions(brothers, true);

        return groups;
    }

    /**
     * @param _sOrgId
     * @param _sActiveOnly
     * @param _sShowOther
     * @return
     * @throws PureException
     */
    private OPGroup[] getGroupsByV3ForOther(Organization _org, boolean _sActiveOnly, boolean _bShowOther)
            throws PureException {
        OPGroup[] groups = new OPGroup[3];
        groups[0] = getTypeGroup(_bShowOther);
        groups[0].sDefault = "" + TYPE_OTHER;

        groups[1] = getOtherType();
        groups[1].sDefault = _org.getOrgType();

        groups[2] = new OPGroup();
        groups[2].sDefault = "" + _org.getId();

        IOrganizationMgr mgr = (IOrganizationMgr) ArkContentHelper.getContentMgrOf(Organization.class);
        List orgs = mgr.findAllOtherByOrgType(_org.getOrgType(), _sActiveOnly, false);

        String[][] opts = makeOptions(orgs, true);
        groups[2].options = opts;
        return groups;
    }

    /**
     * @param _orgs
     * @return
     */
    private String[][] makeOptions(List _orgs, boolean _bClearFinally) {
        String[][] opts = new String[_orgs.size()][3];

        int i = 0;
        for (Iterator iter = _orgs.iterator(); iter.hasNext(); i++) {
            Organization org = (Organization) iter.next();
            opts[i][0] = org.getId() + "";
            opts[i][1] = org.getName();
            opts[i][2] = org.getCode();
        }

        if (_bClearFinally) {
            _orgs.clear();
        }

        return opts;
    }

    /**
     * @return
     * @throws PureException
     */
    private OPGroup getOtherType() throws PureException {
        OPGroup group = new OPGroup();
        List l = NamedValueHelper.getListNamedValues(SRMNamedValueTypes.ORG_TYPE, true);
        group.options = new String[l.size()][2];
        int i = 0;
        for (Iterator iter = l.iterator(); iter.hasNext(); i++) {
            INamedValue nv = (INamedValue) iter.next();
            group.options[i][0] = nv.getValue();
            group.options[i][1] = nv.getName();
        }
        return group;
    }

    /**
     * @param _sB
     * @return
     */
    private OPGroup getTypeGroup(boolean _sShowOther) {
        OPGroup group = new OPGroup();
        group.options = new String[_sShowOther ? 3 : 2][2];
        group.options[0][0] = "" + TYPE_COLLEGE;
        group.options[0][1] = "";
        group.options[1][0] = "" + TYPE_ADMIN;
        group.options[1][1] = "";
        if (_sShowOther) {
            group.options[2][0] = "" + TYPE_OTHER;
            group.options[2][1] = "";
        }
        return group;
    }

    private static class OPGroup {
        public String sDefault;

        public String[][] options;
    }
}