com.pureinfo.srm.outlay.action.OutlayPrintEntryAction.java Source code

Java tutorial

Introduction

Here is the source code for com.pureinfo.srm.outlay.action.OutlayPrintEntryAction.java

Source

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

package com.pureinfo.srm.outlay.action;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForward;

import com.pureinfo.ark.content.ArkContentHelper;
import com.pureinfo.ark.interaction.ActionBase;
import com.pureinfo.ark.interaction.ArkWebConstants;
import com.pureinfo.force.exception.ErrorBean;
import com.pureinfo.force.exception.PureException;
import com.pureinfo.force.lang.RMBFormatter;
import com.pureinfo.force.lang.StrConvertor;
import com.pureinfo.srm.auth.model.SRMUser;
import com.pureinfo.srm.org.model.Organization;
import com.pureinfo.srm.project.OutlayUnitHelper;
import com.pureinfo.srm.project.domain.IOutlayUnitMgr;
import com.pureinfo.srm.project.model.OutlayUnit;
import com.pureinfo.srm.project.model.Project;

/**
 * <P>
 * Created on 2006-9-11 17:04:40 <BR>
 * Last modified on 2006-9-11
 * </P>
 * TODO describe OutlayPrintEntryAction here ...
 * 
 * @author elmar.chen
 * @version 1.0, 2006-9-11
 * @since Command 1.0
 */
public class OutlayPrintEntryAction extends ActionBase {

    public ActionForward executeAction() throws PureException {
        try {
            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);

            OutlayUnit unit = getOutlayUnit();
            Project prj = null;
            SRMUser admin = null;
            if (unit != null) {
                prj = unit.getProject();
                if (prj != null) {
                    admin = prj.getAdministrator();
                }
            }
            request.setAttribute("project", prj);
            request.setAttribute("unit", unit);
            request.setAttribute("admin", admin);
            if (prj != null) {
                request.setAttribute("department", getDepartmentName(unit.getDepartment()));
                request.setAttribute("prjCode", prj.getProjectNumber());
                request.setAttribute("prjName", prj.getProjectName());
                request.setAttribute("contract", prj.getContractId());
                request.setAttribute("prjAdmin", prj.getAdministratorName());
                request.setAttribute("cardId", prj.getCardId());
                request.setAttribute("prjOutlay", getFormattedNum(prj.getTotalFee()));
                request.setAttribute("prjHardOutlay", getFormattedNum(prj.getHardwareFee()));
                request.setAttribute("arrivedOutlay", getFormattedNum(prj.getArrivedOutlay()));
            }
            request.setAttribute("soureUnit", unit.getSourceUnit());
            request.setAttribute("sum", getFormattedNum(unit.getSum()));

            // request.setAttribute("arriveDate", unit.getArriveDate());
            request.setAttribute("arriveDate", unit.getArriveSchoolDate());

            request.setAttribute("unitNum", unit.getUnitId());
            unit.update();

            request.setAttribute("deductRatioHuman", getFormattedPercent(unit.getDeductRatioHF()));
            request.setAttribute("deductFee", getFormattedNum(unit.getSum() * unit.getDeductRatioHF() / 100));
            request.setAttribute("deductFeeCap",
                    RMBFormatter.format(unit.getSum() * unit.getDeductRatioHF() / 100));

            request.setAttribute("deductRatioManage", getFormattedPercent(unit.getDeductRatioMF()));

            request.setAttribute("deductRatioHuman", getFormattedPercent(unit.getDeductRatioHF()));

            request.setAttribute("deductRatioFromPrj", getFormattedPercent(unit.getDeductRatioFromPrj()));
            request.setAttribute("deductRatioFromH", getFormattedPercent(unit.getDeductRatioFromTransverse()));
            request.setAttribute("deductFeeFromPrj",
                    getFormattedNum(unit.getSum() * unit.getDeductRatioFromPrj() / 100));
            request.setAttribute("deductFeeFromH",
                    getFormattedNum(unit.getSum() * unit.getDeductRatioFromTransverse() / 100));

            request.setAttribute("todayStr", getTodayStr());

            logger.debug(unit.getDeductRatio());

            int printType = OutlayUnitHelper.printTypeOf(unit);
            //
            String tpye = request.getParameter("pringtype");

            SRMUser user = (SRMUser) loginUser;
            user.getType();
            if (user.getType() == 4) {
                tpye = "4";
            }

            if (StringUtils.isNotEmpty(tpye)) {
                printType = StrConvertor.strToInt(tpye, "printType", 0);
            }

            if (unit.isFromFormal()) {
                unit.setIsPrinted(true);
                IOutlayUnitMgr mgr = (IOutlayUnitMgr) ArkContentHelper.getContentMgrOf(OutlayUnit.class);
                mgr.save(unit);
            }

            logger.debug("the printType:" + printType);

            switch (printType) {
            case OutlayUnitHelper.PRINT_TYPE_HUMAN:
                return mapping.findForward("print-human");
            case OutlayUnitHelper.PRINT_TYPE_WHITE:
                return mapping.findForward("print-white");
            case OutlayUnitHelper.PRINT_TYPE_REDUCE:
                return mapping.findForward("print-reduce");
            case OutlayUnitHelper.PRINT_TYPE_NEW:
                return mapping.findForward("print-new");
            default:
                return mapping.findForward("print-none");
            }
        } catch (Exception e) {
            ErrorBean error = new ErrorBean(e);
            request.setAttribute(ArkWebConstants.REQ_ERROR, error);
            return mapping.findForward("error");
        }
    }

    private String getDepartmentName(Organization _department) {
        return _department == null ? "" : _department.getName();
    }

    /**
     * @return
     */
    private Object getTodayStr() {
        DateFormat format = new SimpleDateFormat("yyyy  MM  dd ");
        return format.format(new Date());
    }

    private String getFormattedNum(double _num) {
        NumberFormat format = new DecimalFormat("#,###.######");
        return format.format(_num / 10000);
    }

    private String getFormattedPercent(double _num) {
        NumberFormat format = new DecimalFormat("#.##");
        return format.format(_num);
    }

    private OutlayUnit getOutlayUnit() throws PureException {
        int nId = request.getRequiredInt("id", "ID");
        IOutlayUnitMgr mgr = (IOutlayUnitMgr) ArkContentHelper.getContentMgrOf(OutlayUnit.class);
        return (OutlayUnit) mgr.lookupById4Edit(nId);
    }
}