001/* 
002    Licensed to the Apache Software Foundation (ASF) under one
003    or more contributor license agreements.  See the NOTICE file
004    distributed with this work for additional information
005    regarding copyright ownership.  The ASF licenses this file
006    to you under the Apache License, Version 2.0 (the
007    "License"); you may not use this file except in compliance
008    with the License.  You may obtain a copy of the License at
009
010       http://www.apache.org/licenses/LICENSE-2.0
011
012    Unless required by applicable law or agreed to in writing,
013    software distributed under the License is distributed on an
014    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015    KIND, either express or implied.  See the License for the
016    specific language governing permissions and limitations
017    under the License.  
018 */
019package org.apache.wiki.workflow;
020
021import org.apache.wiki.api.exceptions.WikiException;
022import org.apache.wiki.event.WikiEventEmitter;
023import org.apache.wiki.event.WorkflowEvent;
024
025import java.io.Serializable;
026import java.security.Principal;
027import java.util.ArrayList;
028import java.util.Collections;
029import java.util.List;
030import java.util.Map;
031
032
033/**
034 * <p>
035 * AbstractStep subclass that asks an actor Principal to choose an Outcome on behalf of an owner (also a Principal). The actor
036 * "makes the decision" by calling the {@link #decide(Outcome)} method. When this method is called, it will set the Decision's Outcome to
037 * the one supplied. If the parent Workflow is in the {@link Workflow#WAITING} state, it will be re-started. Any checked WikiExceptions
038 * thrown by the workflow after re-start will be re-thrown to callers.
039 * </p>
040 * <p>
041 * When a Decision completes, its {@link #isCompleted()} method returns <code>true</code>. It also tells its parent WorkflowManager to
042 * remove it from the list of pending tasks by calling {@link DecisionQueue#remove(Decision)}.
043 * </p>
044 * <p>
045 * To enable actors to choose an appropriate Outcome, Decisions can store arbitrary key-value pairs called "facts." These facts can be
046 * presented by the user interface to show details the actor needs to know about. Facts are added by calling classes to the Decision,
047 * in order of expected presentation, by the {@link #addFact(Fact)} method. They can be retrieved, in order, via {@link #getFacts()}.
048 * </p>
049 *
050 * @since 2.5
051 */
052public abstract class Decision extends AbstractStep {
053
054    private static final long serialVersionUID = -6835601038263238062L;
055
056    private Principal m_actor;
057
058    private int m_id;
059
060    private final Outcome m_defaultOutcome;
061
062    private final List<Fact> m_facts;
063
064    /**
065     * Constructs a new Decision for a required "actor" Principal, having a default Outcome.
066     *
067     * @param workflowId the parent workflow id to set
068     * @param workflowContext the parent workflow context to set
069     * @param messageKey the i18n message key that represents the message the actor will see
070     * @param actor the Principal (<em>e.g.</em>, a WikiPrincipal, Role, GroupPrincipal) who is required to select an appropriate Outcome
071     * @param defaultOutcome the Outcome that the user interface will recommend as the default choice
072     */
073    public Decision( final int workflowId, final Map< String, Serializable > workflowContext, final String messageKey, final Principal actor, final Outcome defaultOutcome ) {
074        super( workflowId, workflowContext, messageKey );
075        m_actor = actor;
076        m_defaultOutcome = defaultOutcome;
077        m_facts = new ArrayList<>();
078        addSuccessor( defaultOutcome, null );
079    }
080
081    /**
082     * Appends a Fact to the list of Facts associated with this Decision.
083     * 
084     * @param fact the new fact to add
085     */
086    public final void addFact( final Fact fact )
087    {
088        m_facts.add( fact );
089    }
090
091    /**
092     * <p>
093     * Sets this Decision's outcome, and restarts the parent Workflow if it is
094     * in the {@link Workflow#WAITING} state and this Decision is its currently
095     * active Step. Any checked WikiExceptions thrown by the workflow after
096     * re-start will be re-thrown to callers.
097     * </p>
098     * <p>
099     * This method cannot be invoked if the Decision is not the current Workflow
100     * step; all other invocations will throw an IllegalStateException. If the
101     * Outcome supplied to this method is one one of the Outcomes returned by
102     * {@link #getAvailableOutcomes()}, an IllegalArgumentException will be
103     * thrown.
104     * </p>
105     * 
106     * @param outcome the Outcome of the Decision
107     * @throws WikiException if the act of restarting the Workflow throws an exception
108     */
109    public void decide( final Outcome outcome ) throws WikiException {
110        super.setOutcome( outcome );
111        WikiEventEmitter.fireWorkflowEvent( this, WorkflowEvent.DQ_REMOVAL );
112    }
113
114    /**
115     * Default implementation that always returns {@link Outcome#STEP_CONTINUE} if the current Outcome isn't a completion (which will be
116     * true if the {@link #decide(Outcome)} method hasn't been executed yet. This method will also add the Decision to the associated
117     * DecisionQueue.
118     * 
119     * @return the Outcome of the execution
120     * @throws WikiException never
121     */
122    public Outcome execute() throws WikiException {
123        if( getOutcome().isCompletion() ) {
124            return getOutcome();
125        }
126
127        // Put decision in the DecisionQueue
128        WikiEventEmitter.fireWorkflowEvent( this, WorkflowEvent.DQ_ADDITION );
129
130        // Indicate we are waiting for user input
131        return Outcome.STEP_CONTINUE;
132    }
133
134    /**
135     * {@inheritDoc}
136     */
137    public final Principal getActor() {
138        return m_actor;
139    }
140
141    /**
142     * Returns the default or suggested outcome, which must be one of those returned by {@link #getAvailableOutcomes()}. This method is
143     * guaranteed to return a non-<code>null</code> Outcome.
144     * 
145     * @return the default outcome.
146     */
147    public Outcome getDefaultOutcome() {
148        return m_defaultOutcome;
149    }
150
151    /**
152     * Returns the Facts associated with this Decision, in the order in which they were added.
153     * 
154     * @return the list of Facts
155     */
156    public final List< Fact > getFacts() {
157        return Collections.unmodifiableList( m_facts );
158    }
159
160    /**
161     * Returns the unique identifier for this Decision. Normally, this ID is programmatically assigned when the Decision is added to the
162     * DecisionQueue.
163     * 
164     * @return the identifier
165     */
166    public final int getId() {
167        return m_id;
168    }
169
170    /**
171     * Returns <code>true</code> if the Decision can be reassigned to another actor. This implementation always returns <code>true</code>.
172     *
173     * @return the result
174     */
175    public boolean isReassignable() {
176        return true;
177    }
178
179    /**
180     * Reassigns the Decision to a new actor (that is, provide an outcome). If the Decision is not reassignable, this method throws an
181     * IllegalArgumentException.
182     * 
183     * @param actor the actor to reassign the Decision to
184     */
185    public final synchronized void reassign( final Principal actor ) {
186        if( isReassignable() ) {
187            m_actor = actor;
188        } else {
189            throw new IllegalArgumentException( "Decision cannot be reassigned." );
190        }
191    }
192
193    /**
194     * Sets the unique identfier for this Decision.
195     * 
196     * @param id the identifier
197     */
198    public final void setId( final int id ) {
199        m_id = id;
200    }
201
202}