サンプルプロジェクトはこちら fileS2DAOExam.zip
(ビルドや実行には Eclipse + m2plugin が必要です)

はじめに

app.dicon

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE components PUBLIC "-//SEASAR//DTD S2Container//EN"
"http://www.seasar.org/dtd/components.dtd">
<components>
  <include path="dao.dicon"/>
  
  <!-- DAOs -->
  <component name="CustomerDao" class="com.snail.exam.s2dao.CustomerDao">
    <aspect>dao.interceptor</aspect>
  </component>
  <component name="OrderDao" class="com.snail.exam.s2dao.OrderDao">
    <aspect>dao.interceptor</aspect>
  </component>
  <component name="ItemDao" class="com.snail.exam.s2dao.ItemDao">
    <aspect>dao.interceptor</aspect>
  </component>

  <!-- Models -->
  <component name="TradeModel" class="com.snail.exam.s2dao.TradeModel">
    <aspect>j2ee.requiredTx</aspect>
    <arg>CustomerDao</arg>
    <arg>ItemDao</arg>
    <arg>OrderDao</arg>
  </component>
   
  <!-- Services -->
  <component name="TradeService" class="com.snail.exam.s2dao.TradeService">
    <aspect>j2ee.requiresNewTx</aspect>
    <arg>TradeModel</arg>
  </component>
   
</components>

Model層(TradeModel?.java)

package com.snail.exam.s2dao;

import java.sql.Timestamp;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class TradeModel {

    private static final Log log = LogFactory.getLog(TradeModel.class);
    
    private CustomerDao pCustomerDao;
    private ItemDao pItemDao;
    private OrderDao pOrderDao;

    public TradeModel(CustomerDao customerDao,ItemDao itemDao,OrderDao orderDao) {
        super();
        
        pCustomerDao = customerDao;
        pItemDao = itemDao;
        pOrderDao = orderDao;
    }
    
    public boolean checkCreditFacility(long userId){
        
        int currentCreditFacility = pOrderDao.sumCreditSale(userId);
        log.debug("現在の顧客番号"+userId+"の売掛金は"+currentCreditFacility+"です。");
        
        CustomerBean customer = pCustomerDao.findCustomer(userId);
        int maxCreditFacility = customer.getCreditFacility();
        log.debug("現在の顧客番号"+userId+"の与信枠は"+maxCreditFacility+"です。");
        
        return currentCreditFacility < maxCreditFacility;
    }
    
    public boolean checkStock(long itemId,int amount ){
        
        ItemBean item = pItemDao.findItemByPK(itemId);
        
        log.debug(item.getName() + "の在庫:" + item.getStock());
        
        return amount < item.getStock();
    }
    
    public long setAside(long userId,long itemId,int amount){
        
        CustomerBean customer = pCustomerDao.findCustomer(userId);
        ItemBean item = pItemDao.findItemByPK(itemId);
        
        // 在庫を減らす
        item.setStock(item.getStock()-amount);
        pItemDao.update(item);
        
        // 受注をたてる
        OrderBean order = new OrderBean();
        order.setAmount(amount);
        order.setCustomerId(customer.getId());
        order.setItemId(item.getId());
        Timestamp now = new Timestamp(System.currentTimeMillis());
        order.setOnCreate(now);
        order.setOnUpdate(now);
        order.setStatus('0');
        order.setTradePrice((int)(item.getPrice() * (1.0 - customer.getDiscountRate())));
        pOrderDao.insert(order);
        
        return order.getId();
    }

}

Service層(TradeService?.java)

package com.snail.exam.s2dao;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class TradeService {

    private static final Log log = LogFactory.getLog(TradeService.class);    
    
    private TradeModel pTradeModel;
    
    public TradeService(TradeModel tradeModel) {
        super();
        
        pTradeModel = tradeModel;
    }
    
    /**
     * 受注処理を行います.
     * <pre>
     * 1.売掛金が与信枠を超えないかどうかを調べます.
     * 2.在庫があるかどうかを調べます
     * 3.在庫の引き当てを行います(Itemから在庫が減らされ、Orderテーブルに一行追加される)
     * 4.Order番号を返却します
     * </pre>
     * @param userId ユーザID
     * @param itemId 商品ID
     * @param amount 個数
     * @return Order番号
     * @throws Exception 受注処理が行えなかった
     */
    public long order(long userId,long itemId,int amount) throws Exception{
        
        if(!pTradeModel.checkCreditFacility(userId)){
            throw new Exception("売掛金が与信枠を超えています");
        }
        
        log.debug("与信チェックOK");
        
        if(!pTradeModel.checkStock(itemId, amount)){
            throw new Exception("在庫が足りません");            
        }
        
        log.debug("在庫チェックOK");
        
        return pTradeModel.setAside(userId, itemId, amount);
    }

}

Client層(S2DAOExam4.java)

package com.snail.exam.s2dao;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.seasar.framework.container.S2Container;
import org.seasar.framework.container.factory.S2ContainerFactory;

public class S2DAOExam4 {

    private static Log logger = LogFactory.getLog(S2DAOExam.class);

    public static void main(String[] args) {
        S2Container container = null;
        try {
            container = S2ContainerFactory.create("app.dicon");

            TradeService service = (TradeService) container.getComponent(TradeService.class);

            long orderNo = service.order(1,1,10);
            
            logger.info("注文が入りました。注文番号:" + orderNo);
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (container != null) {
                container.destroy();
            }
        }

    }
}

実行結果

(log4jで出力したものを適宜編集しています)


Java#Seasar2


添付ファイル: files2exam.png 2553件 [詳細] fileS2DAOExam.zip 2855件 [詳細]

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS   sitemap
Last-modified: 2009-01-04 (日) 02:48:38 (5825d)
Short-URL:
ISBN10
ISBN13
9784061426061