com.pureinfo.srm.project.action.CardIdGenerateAction.java Source code

Java tutorial

Introduction

Here is the source code for com.pureinfo.srm.project.action.CardIdGenerateAction.java

Source

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

package com.pureinfo.srm.project.action;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
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.force.exception.PureException;
import com.pureinfo.srm.SRMConstants;
import com.pureinfo.srm.SRMExceptionTypes;
import com.pureinfo.srm.org.domain.IOrganizationMgr;
import com.pureinfo.srm.org.model.Organization;
import com.pureinfo.srm.project.CardIdGenerator;

/**
 * <P>
 * Created on 2006-5-12 15:48:45<BR>
 * Last modified on 2006-5-12
 * </P>
 * TODO describe CardIdGenerateAction here ...
 * 
 * @author elmar.chen
 * @version 1.0, 2006-5-12
 * @since Command 1.0 
 */
public class CardIdGenerateAction extends ActionBase {

    private String m_sCardId;
    private String m_sMessage;

    /**
     * @see com.pureinfo.ark.interaction.ActionBase#executeAction()
     */
    public ActionForward executeAction() throws PureException {

        try {
            getCardId();
            Document xDoc = makeResponseXML();
            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) {
                response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            }
        } catch (RuntimeException e) {
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        }
        return null;
    }

    /**
     * @return
     */
    private Document makeResponseXML() {
        Document doc = DocumentHelper.createDocument();
        Element xRoot = doc.addElement("responseXML");
        Element xValue = xRoot.addElement("value");
        xValue.setText(m_sCardId);
        Element xMSG = xRoot.addElement("msg");
        xMSG.setText(m_sMessage);
        return doc;
    }

    private void getCardId() throws PureException {
        String sCampus = null;
        String sOrgCode = null;
        String sPrjSort = null;
        String sAS = null;
        try {
            sCampus = request.getRequiredParameter("campus", "");

            sAS = request.getRequiredParameter("administerSection", "");
            int nOrgId = request.getRequiredInt("department", "");
            try {
                IOrganizationMgr mgr = (IOrganizationMgr) ArkContentHelper.getContentMgrOf(Organization.class);
                Organization org = (Organization) mgr.lookupById(nOrgId);
                if (org == null) {
                    m_sCardId = "ERROR";
                    m_sMessage = "";
                    return;
                }
                sOrgCode = org.getCode();
                sOrgCode = StringUtils.rightPad(sOrgCode, 5, '0');
            } catch (PureException ex2) {
                // TODO Auto-generated catch block
                ex2.printStackTrace(System.err);
            }
            sPrjSort = request.getRequiredParameter("prjSortId", "");
        } catch (RuntimeException ex) {
            m_sCardId = "ERROR";
            m_sMessage = "";
            return;
        }
        CardIdGenerator cg = CardIdGenerator.instance(sCampus, sOrgCode, sPrjSort, sAS);
        try {
            m_sCardId = cg.getNextId();
            if ("true".equals(request.getParameter("short")) && SRMConstants.ADMIN_SECTION_DIQI.equals(sAS)) {
                m_sCardId = m_sCardId.substring(0, m_sCardId.length() - 2);
            }
            int nRound = cg.getRound();
            m_sMessage = (nRound == 0 ? "" : "" + (nRound + 1) + "");
        } catch (PureException ex1) {
            if (ex1.getErrNo() == SRMExceptionTypes.PRJ_CARD_ID_FULL) {
                m_sCardId = "OVERFLOW";
                m_sMessage = ex1.getMessage();
            } else {
                m_sCardId = "ERROR";
                m_sMessage = "";
            }
        }
    }

}