001 /*
002 * Created on Aug 29, 2007
003 */
004 package com.x8ing.mc.bp;
005
006 import java.text.NumberFormat;
007 import java.util.Calendar;
008 import java.util.List;
009
010 import com.x8ing.lsm4j.Action;
011 import com.x8ing.lsm4j.Condition;
012 import com.x8ing.lsm4j.StateContext;
013 import com.x8ing.lsm4j.state.ProcessableState;
014
015 /**
016 * Provides some functionality which is useful in context of business processes.
017 * <p>
018 * Controls the simulation time.
019 * <p>
020 *
021 * @author Patrick Heusser
022 *
023 */
024 public abstract class AbstractBusinessAction implements Action {
025
026 private NumberFormat numberFormat = Constants.createCurrencyFormater();
027
028 /**
029 * subclasses might use this.
030 */
031 protected BusinessContext businessContext = null;
032
033 protected ProcessableState currentState = null;
034
035 private boolean lazyInitDone = false;
036
037 /**
038 * @see com.x8ing.lsm4j.Action#execute(com.x8ing.lsm4j.state.ProcessableState, com.x8ing.lsm4j.StateContext,
039 * com.x8ing.lsm4j.Condition, java.util.List)
040 */
041 final public void execute(ProcessableState currentState, StateContext stateContext, Condition previousCondition,
042 List lastVisitedStatesHistory) {
043
044 this.businessContext = (BusinessContext) stateContext;
045 this.currentState = currentState;
046
047 // create an init hook, after we have the business context!
048 if (!lazyInitDone) {
049 lazyInit();
050 lazyInitDone = true;
051 }
052
053 execute(currentState, businessContext, previousCondition, lastVisitedStatesHistory);
054
055 moveSimulationTimeForwardBy(24);
056
057 }
058
059 /**
060 * hook just before the execute will be executed. will just be called once in the lifetime of an action.
061 */
062 protected abstract void lazyInit();
063
064 /**
065 * move the simulation time forward for a certain time. this depends on the action that must be taken within the
066 * concrete implementing action. the value reflects the real time that would be needed in reality.
067 *
068 * @param hours
069 */
070 private void moveSimulationTimeForwardBy(int hours) {
071
072 businessContext.getCurrentDate().add(Calendar.HOUR, hours);
073
074 }
075
076 /**
077 * abstract to force implementors to implement that method.
078 *
079 * @param currentState
080 * @param businessContext
081 * @param previousCondition
082 * @param lastVisitedStatesHistory
083 */
084 public abstract void execute(ProcessableState currentState, BusinessContext businessContext, Condition previousCondition,
085 List lastVisitedStatesHistory);
086
087 /**
088 * convenience method.
089 *
090 * @param text
091 */
092 protected void addLogBookEntry(String text) {
093
094 Calendar currentDate = businessContext.getCurrentDate();
095
096 text += (" [" + currentState.getDescription() + "]");
097
098 businessContext.getLogBook().addLogEntry(currentDate, text);
099
100 }
101
102 protected void addBalanceSheetTransaction(String text, double amount, BalanceAccount balanceAccount) {
103
104 Calendar currentDate = businessContext.getCurrentDate();
105 businessContext.getBalanceSheet().addTransaction(text, amount, currentDate, balanceAccount);
106 businessContext.getLogBook().addLogEntry(currentDate, "balanceSheet: " + text + " amount=" + numberFormat.format(amount));
107
108 }
109
110 }
|