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

📄 fundsessionfacadebean.java

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

import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.ejb.CreateException;
import com.fund.ServiceLocator;
import com.fund.ServiceLocatorException;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Collection;
import java.util.Random;
import javax.ejb.EJBException;
/**
 *
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class FundSessionFacadeBean implements SessionBean {
    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = 1L;
    /**
     * sessionContext
     */
    SessionContext sessionContext;
    /**
     * fundHome
     */
    private FundHome fundHome;
    /**
     * ejbCreate
     * @throws CreateException e
     */
    public void ejbCreate() throws CreateException {
    }

    /**
     * ejbRemove
     */
    public void ejbRemove() {
    }

    /**
     * ejbActivate
     */
    public void ejbActivate() {
    }

    /**
     * ejbPassivate
     */
    public void ejbPassivate() {
    }

    /**
     * setSessionContext
     * @param sessionContext SessionContext
     */
    public void setSessionContext(SessionContext sessionContext) {
    this.sessionContext = sessionContext;
        try {
            findFundHome();
        } catch (Exception e) {
            throw new EJBException(e.getMessage());
        }
    }

    /**
     * createFund
     * @param fundDto FundDto
     * @throws EJBException e
     */
    public void createFund(FundDto fundDto) throws EJBException {

        try {
            Fund fund = fundHome.create(fundDto.getFundNo());
            setFundFromFundDto(fund, fundDto);
        } catch (Exception e) {
            throw new EJBException(e.getMessage());
        }
    }

    /**
     * removeFund
     * @param fundNo Integer
     * @throws EJBException e
     */
    public void removeFund(Integer fundNo) throws EJBException {
    try {
            fundHome.remove(fundNo);
        } catch (Exception e) {
            throw new EJBException(e.getMessage());
        }
    }

    /**
     * removeFund
     * @param fundDto FundDto
     * @throws EJBException e
     */
    public void removeFund(FundDto fundDto) throws EJBException {
    if (fundDto != null) {
            Integer fundNo = fundDto.getFundNo();
            removeFund(fundNo);
        }
    }

    /**
     * updateFund
     * @param fundDto FundDto
     * @throws EJBException e
     */
    public void updateFund(FundDto fundDto) throws EJBException {
    if (fundDto != null) {
            Integer fundNo = fundDto.getFundNo();
            try {
                Fund fund = fundHome.findByPrimaryKey(fundNo);
                setFundFromFundDto(fund, fundDto);
            } catch (Exception e) {
                throw new EJBException(e.getMessage());
            }
        }
    }

    /**
     *
     * @param fundDtos FundDto[]
     * @throws EJBException e
     */
    public void updateFunds(FundDto[] fundDtos) throws EJBException {
    if (fundDtos != null) {
            for (int i = 0; i < fundDtos.length; i++) {
                updateFund(fundDtos[i]);
            }
        }
    }

    /**
     * fundFindByPrimaryKey
     * @param fundNo Integer
     * @return FundDto
     * @throws EJBException e
     */
    public FundDto fundFindByPrimaryKey(Integer fundNo) throws EJBException {
    try {
            return assembleFundDto(fundHome.findByPrimaryKey(fundNo));
        } catch (Exception e) {
            throw new EJBException(e.getMessage());
        }
    }

    /**
     * fundFindByName
     * @param fundName String
     * @return FundDto
     * @throws EJBException e
     */
    public FundDto fundFindByName(String fundName) throws EJBException {
    try {
            return assembleFundDto(fundHome.findByName(fundName));
        } catch (Exception e) {
            throw new EJBException(e.getMessage());
        }
    }

    /**
     * setFundFromFundDto
     * @param fund Fund
     * @param fundDto FundDto
     */
    private void setFundFromFundDto(Fund fund, FundDto fundDto) {
    fund.setFundName(fundDto.getFundName());
        fund.setPrice(fundDto.getPrice());
        fund.setDescription(fundDto.getDescription());
        fund.setStatus(fundDto.getStatus());
        fund.setCreatedDate(fundDto.getCreatedDate());
    }

    /**
     * findFundHome
     * @throws EJBException e
     */
    private void findFundHome() throws EJBException {
     String ENTITY_NAME = "java:comp/env/ejb/fund";
        if (fundHome == null) {
            try {
                ServiceLocator locator = ServiceLocator.getInstance();
                fundHome = (FundHome) locator.getEjbLocalHome(ENTITY_NAME);
            } catch (ServiceLocatorException e) {
                throw new EJBException(e.getMessage());
            }
        }
    }

    /**
     *
     * @param fund Fund
     * @return FundDto
     */
    private FundDto assembleFundDto(Fund fund) {
    return FundDtoAssembler.createDto(fund);
    }

    /**
     *
     * @param funds Collection
     * @return FundDto[]
     */
    private FundDto[] assembleFundDtos(Collection funds) {
    List list = new ArrayList();
        if (funds != null) {
            Iterator iterator = funds.iterator();
            while (iterator.hasNext()) {
                Fund fund = (Fund) iterator.next();
                list.add(assembleFundDto(fund));
            }
        }
        FundDto[] returnArray = new FundDto[list.size()];
        return (FundDto[]) list.toArray(returnArray);
    }

    /**
     *
     * @param fundNo Integer
     * @return Double
     */
    public Double getCurrentPrice(Integer fundNo) {
    FundDto fundDto = fundFindByPrimaryKey(fundNo);
        double price = fundDto.getPrice().doubleValue();
        int randomNo = pickNumberInRange(-10, 10);
        double currentPrice = price * (1 + (double) randomNo / (double) 100);
        return new Double(currentPrice);
    }

    /**
     *
     * @param aLowerLimit int
     * @param aUpperLimit int
     * @return int
     */
    private int pickNumberInRange(int aLowerLimit, int aUpperLimit) {
    Random generator = new Random();
        long range = (long) aUpperLimit - (long) aLowerLimit + 1;
        long fraction = (long) (range * generator.nextDouble());
        return (int) (fraction + aLowerLimit);
    }

    /**
     *
     * @return FundDto[]
     * @throws EJBException e
     */
    public FundDto[] fundFindGetNormallFunds() throws EJBException {
    try {
            return assembleFundDtos(fundHome.findGetNormallFunds());
        } catch (Exception e) {
            throw new EJBException(e.getMessage());
        }
    }
}

⌨️ 快捷键说明

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