001    /*
002     * Copyright 2004-2005 the original author or authors.
003     */
004    package org.springframework.rules.closure.support;
005    
006    import org.springframework.rules.closure.Closure;
007    
008    /**
009     * Convenient class to implement workflow.
010     *
011     * @author Keith Donald
012     */
013    public abstract class AbstractElementGeneratorWorkflow extends AbstractElementGenerator {
014    
015            /**
016             * @see AbstractElementGenerator#AbstractElementGenerator()
017             */
018            protected AbstractElementGeneratorWorkflow() {
019                    super();
020            }
021    
022            /**
023             * @see AbstractElementGenerator#AbstractElementGenerator(boolean)
024             */
025            protected AbstractElementGeneratorWorkflow(boolean runOnce) {
026                    super(runOnce);
027            }
028    
029            /**
030             * {@inheritDoc}
031             *
032             * Defines the workflow sequence.
033             */
034            public final void run(Closure templateCallback) {
035                    reset();
036                    setRunning();
037                    doSetup();
038                    while (processing()) {
039                            templateCallback.call(doWork());
040                    }
041                    setCompleted();
042                    doCleanup();
043            }
044    
045            /**
046             * Setup the workflow.
047             */
048            protected void doSetup() {
049    
050            }
051    
052            /**
053             * @return <code>true</code> if ElementGenerator is still processing.
054             */
055            protected boolean processing() {
056                    return hasMoreWork() && !isStopped();
057            }
058    
059            /**
060             * @return <code>true</code> if more work has to be done.
061             */
062            protected abstract boolean hasMoreWork();
063    
064            /**
065             * @return the object to process (with the given closure).
066             */
067            protected abstract Object doWork();
068    
069            /**
070             * Clean up after workflow.
071             */
072            protected void doCleanup() {
073    
074            }
075    }