/*
 * This software is distributed under following license based on modified BSD
 * style license.
 * ----------------------------------------------------------------------
 * 
 * Copyright 2009 The Nimbus2 Project. All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer. 
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE NIMBUS PROJECT ``AS IS'' AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
 * NO EVENT SHALL THE NIMBUS PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * The views and conclusions contained in the software and documentation are
 * those of the authors and should not be interpreted as representing official
 * policies, either expressed or implied, of the Nimbus2 Project.
 */
package jp.ossc.nimbus.service.scheduler;

import java.util.Date;

import jp.ossc.nimbus.core.*;
import jp.ossc.nimbus.service.beanflow.*;


/**
 * BeanFlow条件スケジュール作成サービス。<p>
 * スケジュールの作成有無の判定をBeanFlowに委譲する。<br>
 *
 * @author M.Takata
 */
public class BeanFlowConditionScheduleMakerService
 extends DefaultScheduleMakerService
 implements BeanFlowConditionScheduleMakerServiceMBean{
    
    private static final long serialVersionUID = 9128789021473711234L;
    protected ServiceName beanFlowFactoryServiceName;
    protected BeanFlowFactory beanFlowFactory;
    
    protected String flowName;
    
    // BeanFlowConditionScheduleMakerServiceMBeanのJavaDoc
    public void setBeanFlowFactoryServiceName(ServiceName name){
        beanFlowFactoryServiceName = name;
    }
    // BeanFlowConditionScheduleMakerServiceMBeanのJavaDoc
    public ServiceName getBeanFlowFactoryServiceName(){
        return beanFlowFactoryServiceName;
    }
    
    // BeanFlowConditionScheduleMakerServiceMBeanのJavaDoc
    public void setFlowName(String name){
        flowName = name;
    }
    // BeanFlowConditionScheduleMakerServiceMBeanのJavaDoc
    public String getFlowName(){
        return flowName;
    }
    
    /**
     * サービスの開始処理を行う。<p>
     *
     * @exception Exception サービスの開始処理に失敗した場合
     */
    public void startService() throws Exception{
        
        if(beanFlowFactoryServiceName != null){
            beanFlowFactory = (BeanFlowFactory)ServiceManagerFactory
                .getServiceObject(beanFlowFactoryServiceName);
        }
        if(beanFlowFactory == null){
            throw new IllegalArgumentException("BeanFlowFactory is null.");
        }
    }
    
    /**
     * {@link BeanFlowFactory}を設定する。<p>
     *
     * @param factory BeanFlowFactory
     */
    public void setBeanFlowFactory(BeanFlowFactory factory){
        beanFlowFactory = factory;
    }
    
    /**
     * {@link BeanFlowFactory}を取得する。<p>
     *
     * @return BeanFlowFactory
     */
    public BeanFlowFactory getBeanFlowFactory(){
        return beanFlowFactory;
    }
    
    /**
     * この日付で、スケジュールを作成する必要があるかどうかを判定する。<p>
     * {@link #setFlowName(String)}で設定されたBeanFlow名または、{@link ScheduleMaster#getScheduleType()}で取得されるスケジュール種別名のBeanFlowを実行して、その戻り値でスケジュールの作成有無を判断する。<br>
     * BeanFlowの引数には、配列で、このメソッドの引数であるdateとmasterを渡す。<br>
     *
     * @param date 作成日
     * @param master スケジュールマスタ
     * @return trueの場合、作る必要がある
     * @exception ScheduleMakeException 判定に失敗した場合
     */
    protected boolean isNecessaryMake(Date date, ScheduleMaster master)
     throws ScheduleMakeException{
        final String conditionFlowName = flowName == null ? master.getScheduleType() : flowName;
        BeanFlow flow = null;
        try{
            flow = beanFlowFactory.createFlow(
                conditionFlowName
            );
        }catch(BeanFlowException e){
            throw new ScheduleMakeException(e);
        }
        
        boolean result = false;
        try{
            Object ret = flow.execute(new Object[]{date, master});
            if(ret instanceof Boolean){
                result = ((Boolean)ret).booleanValue();
            }else{
                throw new ScheduleMakeException("Return of BeanFlow is not boolean : " + ret);
            }
        }catch(Exception e){
            throw new ScheduleMakeException(e);
        }
        return result;
    }
}