⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fundaction.java

📁 EJB+Struts+Webservice实现的开放式基金管理系统
💻 JAVA
字号:
package com.fund.fund;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.fund.SuperAction;
import com.fund.sales.SalesDto;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.validator.DynaValidatorForm;

/**
 *
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class FundAction extends SuperAction {

    /**
     *
     * @param mapping ActionMapping
     * @param form ActionForm
     * @param request HttpServletRequest
     * @param response HttpServletResponse
     * @return ActionForward
     */
    public ActionForward doExecute(ActionMapping mapping, ActionForm form,
                                   HttpServletRequest request,
                                   HttpServletResponse response) {
        String parameter = mapping.getParameter();
        System.out.println("Parameter = ***** " + parameter);
        if (parameter.equals("add")) {
            return add(mapping, form, request, response);
        }  else if (parameter.equals("view")) {
            return view(mapping, form, request, response);
        }  else if (parameter.equals("edit")) {
            return edit(mapping, form, request, response);
        }  else {
            return null;
        }
    }

    /**
     *
     * @param mapping ActionMapping
     * @param form ActionForm
     * @param request HttpServletRequest
     * @param response HttpServletResponse
     * @return ActionForward
     */
    public ActionForward add(ActionMapping mapping, ActionForm form,
                             HttpServletRequest request,
                             HttpServletResponse response) {
        String target = "success";
        FundForm fund = (FundForm) form;
        try {
            //Adding new fund
            fund.getFundDto().setCreatedDate(new java.sql.Timestamp(new java.
                    util.Date().getTime()));
            fund.getFundDto().setStatus("正常");
            FundDelegate fd = new FundDelegate();
            fd.createFund(fund.getFundDto());
            fund.setFundDto(fd.fundFindByName(fund.getFundDto().getFundName().
                                              toUpperCase()));
            ActionErrors errors = new ActionErrors();
            errors.add("fundForm.add.success",
                       new ActionError("fundForm.add.success"));
            saveErrors(request, errors);

            FundDto fdto = fd.fundFindByName(fund.getFundDto().getFundName());
            SalesDto sdto = (SalesDto) request.getSession().getAttribute(
                    "salesDto");
            log.info(fdto.logData(
                    "FUND PRODUCT ADDED by sales having sales no="
                    +
                    sdto.getSalesNo()));

        } catch (Exception e) { //fundname already exists
            target = "failure";
            //Adding fundname already exists message
            ActionErrors errors = new ActionErrors();
            errors.add("fundForm.fundnameexists",
                       new ActionError("fundForm.fundnameexists"));
            saveErrors(request, errors);
        }

        return mapping.findForward(target);
    }

    /**
     *
     * @param mapping ActionMapping
     * @param form ActionForm
     * @param request HttpServletRequest
     * @param response HttpServletResponse
     * @return ActionForward
     */
    public ActionForward view(ActionMapping mapping, ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) {
        String target = "success";
        FundForm fundForm = new FundForm();
        String fundNoStr = request.getParameter("uFundNo");
        if (fundNoStr != null
            && (!fundNoStr.trim().equals(""))
            &&
            (!fundNoStr.trim().equals("0"))) {
            try {
                FundDelegate fd = new FundDelegate();
                fundForm.setFundDto(fd.fundFindByPrimaryKey(new Integer(
                        fundNoStr)));
                request.setAttribute("fundForm", fundForm);
            } catch (Exception e) {
                target = "failure";
                ActionErrors errors = new ActionErrors();
                errors.add("fundname",
                           new ActionError("fundForm.invalidfund"));
                saveErrors(request, errors);
                e.printStackTrace();

            }
        } else { //from search screen
            DynaValidatorForm searchForm = (DynaValidatorForm) form;
            String fundName = (String) searchForm.get("fundName");
            Integer fundNo = ((Integer) searchForm.get("fundNo"));
            if ((fundName == null || fundName.trim().equals(""))
                &&
                (fundNo == null || fundNo.intValue() == 0)) {
                target = "failure";
                ActionErrors errors = new ActionErrors();
                errors.add("emptyfund",
                           new ActionError("fundForm.emptyfields"));
                saveErrors(request, errors);
                return mapping.findForward(target);
            }

            try {
                //Retriving fundDto for the specified search
                FundDto fundDto = getFundDto(searchForm);
                if ((fundNo != null && fundNo.intValue() != 0)
                    && fundName != null
                    &&
                    !fundName.equals("")) {
                    if ((fundDto.getFundNo().intValue() != fundNo.intValue())
                        &&
                        !fundDto.getFundName().equalsIgnoreCase(fundName)) {
                        target = "failure";
                        ActionErrors errors = new ActionErrors();
                        errors.add("fundname",
                                   new ActionError("fundForm.invalidfund"));
                        saveErrors(request, errors);

                    }
                }
                fundForm.setFundDto(fundDto);
                request.setAttribute("fundForm", fundForm);
            } catch (Exception e) {
                //did not retrive the fundDto for specified fundname
                target = "failure";
                ActionErrors errors = new ActionErrors();
                errors.add("fundname",
                           new ActionError("fundForm.invalidfund"));
                saveErrors(request, errors);
                e.printStackTrace();
            }
        }
        return mapping.findForward(target);
    }

    /**
     *
     * @param mapping ActionMapping
     * @param form ActionForm
     * @param request HttpServletRequest
     * @param response HttpServletResponse
     * @return ActionForward
     */
    public ActionForward edit(ActionMapping mapping, ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) {
        String target = "success";
        FundForm fund = (FundForm) form;
        String fundNo = request.getParameter("fundNo");
        System.out.println(fundNo);
        if (fundNo != null) { //go to edit page
            FundDelegate fd = null;
            try {
                fd = new FundDelegate();
                fund.setFundDto(fd.fundFindByPrimaryKey(new Integer(fundNo)));
            } catch (Exception ex) {
                System.out.println(ex.getMessage());
                target = "failure";
            }
        } else { //edit the fund details
            try {
                FundDelegate fd = new FundDelegate();
                System.out.println("UPDATING THIS DTO" + fund.getFundDto());
                FundDto fund2 = null;
                try {
                    fund2 = fd.fundFindByName(fund.getFundDto().getFundName().
                                              toUpperCase());
                } catch (Exception e) { // no fund found with that name
                    e.printStackTrace();
                }
                if (fund2 != null
                    &&
                    ((fund2.getFundNo()).intValue()
                     !=
                     (fund.getFundDto().getFundNo().intValue()))) {
                    target = "failure";
                    ActionErrors errors = new ActionErrors();
                    errors.add("fundForm.fundnameexists",
                               new ActionError("fundForm.fundnameexists"));
                    saveErrors(request, errors);
                    return mapping.findForward(target);
                } else {
                    fd.updateFund(fund.getFundDto());
                    ActionErrors errors = new ActionErrors();
                    errors.add("fundForm.update.success",
                               new ActionError("fundForm.update.success"));
                    saveErrors(request, errors);

                    SalesDto sdto = (SalesDto) request.getSession().
                                    getAttribute("salesDto");
                    log.info(fund.getFundDto().logData(
                            "FUND PRODUCT UPDATED by sales having sales no="
                            +
                            sdto.getSalesNo()));

                }
            } catch (Exception e) {
                e.printStackTrace();
                target = "failure";
                ActionErrors errors = new ActionErrors();
                errors.add("fundForm.updatefailed",
                           new ActionError("fundForm.updatefailed"));
                saveErrors(request, errors);
            }
        }
        return mapping.findForward(target);
    }

    /**
     *
     * @param form DynaValidatorForm
     * @return FundDto
     * @throws Exception e
     */
    private FundDto getFundDto(DynaValidatorForm form) throws Exception {

        String fundName = (String) form.get("fundName");
        Integer fundNo = ((Integer) form.get("fundNo"));
        int intFundNo = fundNo.intValue();

        FundDelegate fd = new FundDelegate();
        if (intFundNo == 0 && !fundName.equals("")) {
            return fd.fundFindByName(fundName.toUpperCase());
        } else if (intFundNo != 0 && fundName.equals("")) {
            return fd.fundFindByPrimaryKey(fundNo);
        } else if (intFundNo != 0 && !fundName.equals("")) {
            FundDto fundDto = fd.fundFindByPrimaryKey(fundNo);
            if (fundDto.getFundName().equalsIgnoreCase(fundName)) {
                return fundDto;
            } else {
                throw new Exception("Fund Not Found");
            }
        } else {
            throw new Exception("Fund Not Found");
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -