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

Java tutorial

Introduction

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

Source

/**
 * PureInfo Command-phase2
 * @(#)OutlayAssginCheckAction.java   1.0 2008-7-8
 * 
 * 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.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

import org.apache.commons.httpclient.util.DateUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForward;

import com.pureinfo.ark.ArkActionTypes;
import com.pureinfo.ark.ArkHelper;
import com.pureinfo.ark.auth.model.User;
import com.pureinfo.ark.auth2.Auth2Helper;
import com.pureinfo.ark.content.ArkContentHelper;
import com.pureinfo.ark.interaction.ActionBase;
import com.pureinfo.ark.interaction.ArkWebConstants;
import com.pureinfo.common.sender.SenderHelper;
import com.pureinfo.common.sender.model.ISender;
import com.pureinfo.common.sender.model.SentInfo;
import com.pureinfo.force.exception.PureException;
import com.pureinfo.force.lang.StrConvertor;
import com.pureinfo.srm.SRMConstants;
import com.pureinfo.srm.auth.model.SRMUser;
import com.pureinfo.srm.product.ProductConstants;
import com.pureinfo.srm.product.model.Product;
import com.pureinfo.srm.project.domain.IOutlayAssginDetailMgr;
import com.pureinfo.srm.project.domain.IOutlayAssginMgr;
import com.pureinfo.srm.project.domain.IProjectMgr;
import com.pureinfo.srm.project.model.OutlayAssgin;
import com.pureinfo.srm.project.model.OutlayAssginDetail;
import com.pureinfo.srm.project.model.Project;

public class OutlayAssginCheckAction extends ActionBase {

    public ActionForward executeAction() throws PureException {
        String sIds = request.getTrimedParameter(ArkWebConstants.REQ_OBJ_ID);
        boolean approve = request.getBoolean("approve", "true", true);

        if (StringUtils.isEmpty(sIds)) {
            logger.debug("the id is null.");
            return mapping.findForward("success");
        }

        IOutlayAssginMgr mgr = (IOutlayAssginMgr) ArkContentHelper.getContentMgrOf(OutlayAssgin.class);
        String[] ids = sIds.split(",");

        for (int i = 0; i < ids.length; i++) {
            int id = StrConvertor.strToInt(ids[i], "id", 0);
            OutlayAssgin oa = (OutlayAssgin) mgr.lookupTempById(id);
            if (oa == null || !Auth2Helper.getManager().hasRight(loginUser, oa, ArkActionTypes.AUDIT, false)) {
                logger.error("the outlayassgin is null or has no right to audit!." + oa.getCheckCode());
                continue;
            }
            oa.setAuditPersonName(((SRMUser) loginUser).getTrueName());
            oa.setAuditTime(new Date());

            if (approve) {
                oa.setStatus(SRMConstants.PRODUCT_STATUS_AUDITED);
                oa.setCheckSuggestion(null);
                mgr.tempToFormal(oa);

                IOutlayAssginDetailMgr detailmgr = (IOutlayAssginDetailMgr) ArkContentHelper
                        .getContentMgrOf(OutlayAssginDetail.class);
                List assgindetailList = detailmgr.findAllOfAssginDetail(id);
                request.setAttribute("obj", oa);
                request.setAttribute("assgindetailList", assgindetailList);
                return mapping.findForward("view");

            } else {
                oa.setStatus(SRMConstants.PRODUCT_STATUS_AUDIT_BACK1);
                String temp = request.getParameter("dp_checkSuggestion");
                try {
                    oa.setCheckSuggestion(new String(temp.getBytes("ISO-8859-1"), "utf-8"));
                } catch (UnsupportedEncodingException ex) {
                    // TODO Auto-generated catch block
                    ex.printStackTrace(System.err);
                }
                mgr.saveToTemp(oa, approve);
                //
                sendEMail2PrjAdmin(oa, false);
                IProjectMgr prjMgr = (IProjectMgr) ArkContentHelper.getContentMgrOf(Project.class);
                int projectId = oa.getProjectId();
                Project _project = (Project) prjMgr.lookupById(projectId);
                if (_project == null) {
                    _project = (Project) prjMgr.lookupTempById(projectId);
                }
                //
                sendSysMessage(_project, oa);
            }

        }
        return mapping.findForward("success");
    }

    protected ActionForward afterExecution() throws PureException {

        request.setAttribute("forward", "../project/outlayAssginAuthList.do");
        return super.afterExecution();
    }

    private void sendEMail2PrjAdmin(OutlayAssgin _oa, boolean _bApprove) {
        try {
            int projectId = _oa.getProjectId();
            IProjectMgr prjMgr = (IProjectMgr) ArkContentHelper.getContentMgrOf(Project.class);
            Project _project = (Project) prjMgr.lookupById(projectId);
            if (_project == null) {
                _project = (Project) prjMgr.lookupTempById(projectId);
            }
            IOutlayAssginMgr mgr = (IOutlayAssginMgr) ArkContentHelper.getContentMgrOf(OutlayAssgin.class);

            String sMsgContent = "";
            String title = "";
            if (_bApprove) {
                sMsgContent = messageContentForCheckEdit(_project, _oa, "", _bApprove);
                title = "-";
            } else {
                sMsgContent = messageContentForCheckEdit(_project, _oa, "", _bApprove);
                title = "-";

            }

            mgr.sendEmailPrjAdmin(_project, title, sMsgContent);

        } catch (Exception ex) {
            logger.error("", ex);
        }
    }

    /**
     * get message content
     * 
     * @param _project
     * @param _sFailed
     * @return
     * @throws PureException 
     */
    private String messageContentForCheckEdit(Project _project, OutlayAssgin _oa, String _sFailed,
            boolean _bApprove) throws PureException {
        StringBuffer buffer = new StringBuffer();
        Date date = new Date();
        SRMUser prjAdmin = _project.getAdministrator();
        SRMUser _loginuser = (SRMUser) ArkHelper.getLoginUser();
        String authorName = "";
        String checkName = "";
        if (prjAdmin != null) {
            authorName = prjAdmin.getTrueName();
        }
        if (_loginuser != null) {
            checkName = _loginuser.getTrueName();
        }
        String dateStr = DateUtil.formatDate(date, "yyyy-MM-dd HH:mm");
        String dateYMD = DateUtil.formatDate(date, "yyyy-MM-dd");
        try {
            buffer.append(authorName + ",:\n");
            if (_bApprove) {
                buffer.append("");
                buffer.append("");
                buffer.append(_project.getProjectName());
                buffer.append("");
                buffer.append(dateStr + " ");
                buffer.append(_sFailed);
            } else {
                buffer.append("");
                buffer.append(_project.getProjectName());
                buffer.append("\n");
                buffer.append(":");
                buffer.append(_oa.getCheckSuggestion());
            }
            buffer.append("\n");
            buffer.append(checkName);
            buffer.append("\n\r");
            buffer.append("\n");
            buffer.append(dateYMD);

            return buffer.toString();
        } finally {
            if (buffer != null) {
                buffer.setLength(0);
            }
        }
    }

    public void sendSysMessage(Project _project, OutlayAssgin _oa) throws PureException {

        SRMUser prjAdmin = _project.getAdministrator();
        SentInfo sentInfo = new SentInfo();
        sentInfo.setFromAddress(String.valueOf(loginUser.getId()));
        sentInfo.setFromPerson(((User) loginUser).getTrueName());
        sentInfo.setToAddress(String.valueOf((prjAdmin.getId())));
        sentInfo.setToPerson(prjAdmin.getTrueName());
        sentInfo.setSubject("");
        sentInfo.setContent("" + _project.getProjectName()
                + ",\n:" + _oa.getCheckSuggestion());
        ISender sysSender = SenderHelper.getSender(SenderHelper.TYPE_SYSTEM);
        sysSender.send(sentInfo);
    }

}