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 */
019
020package org.apache.wiki.event;
021
022/**
023 * <p>
024 * WorkflowEvent indicates that a state change to a Workflow: started, running, waiting, completed, aborted. 
025 * These correspond exactly to the states described in the {@link org.apache.wiki.workflow.Workflow}. All events 
026 * are logged with priority INFO.
027 * </p>
028 * 
029 * @since 2.3.79
030 */
031public final class WorkflowEvent extends WikiEvent {
032
033    private static final long serialVersionUID = 1L;
034    
035    /** After Workflow instantiation. */
036    public static final int CREATED = 0;
037
038    /**
039     * After the Workflow has been instantiated, but before it has been started
040     * using the {@link org.apache.wiki.workflow.Workflow#start()} method.
041     */
042    public static final int STARTED = 10;
043
044    /**
045     * After the Workflow has been started (or re-started) using the
046     * {@link org.apache.wiki.workflow.Workflow#start()} method,
047     * but before it has finished processing all Steps.
048     */
049    public static final int RUNNING = 20;
050
051    /** When the Workflow has temporarily paused, for example because of a pending Decision. */
052    public static final int WAITING = 30;
053
054    /** After the Workflow has finished processing all Steps, without errors. */
055    public static final int COMPLETED = 40;
056
057    /** If a Step has elected to abort the Workflow. */
058    public static final int ABORTED = 50;
059
060    /** When the workflow wishes to add a Decision to the DecisionQueue */
061    public static final int DQ_ADDITION = 60;
062
063    /** When the workflow wishes to remove a Decision from the DecisionQueue */
064    public static final int DQ_REMOVAL = 70;
065
066    /** When the decision queue decides the outcome of a Decision */
067    public static final int DQ_DECIDE = 80;
068
069    /** When the decision queue reassigns a Decision */
070    public static final int DQ_REASSIGN = 90;
071
072    /**
073     * Constructs a new instance of this event type, which signals a security event has occurred. 
074     * The <code>source</code> parameter is required, and may not be <code>null</code>. When the 
075     * WikiSecurityEvent is constructed, the security logger {@link WikiSecurityEvent#log} is notified.
076     * 
077     * @param src the source of the event, which can be any object: a wiki page, group or authentication/authentication/group manager.
078     * @param type the type of event
079     */
080    public WorkflowEvent( final Object src, final int type ) {
081        super( src, type );
082        if( src == null ) {
083            throw new IllegalArgumentException( "Argument(s) cannot be null." );
084        }
085    }
086
087    /**
088     * Prints a String (human-readable) representation of this object.
089     * 
090     * @see java.lang.Object#toString()
091     */
092    public String toString() {
093        final StringBuilder msg = new StringBuilder();
094        msg.append( "WorkflowEvent." );
095        msg.append( eventName( getType() ) );
096        final Object obj = getSrc(); // cfr. https://forums.oracle.com/forums/thread.jspa?threadID=1184115
097        msg.append( " [source=" ).append( obj.toString() );
098        msg.append( "]" );
099        return msg.toString();
100    }
101
102    /**
103     * Returns a textual representation of an event type.
104     * 
105     * @param type the type
106     * @return the string representation
107     */
108    public String eventName( final int type ) {
109        switch( type ) {
110            case CREATED:    return "CREATED";
111            case ABORTED:    return "ABORTED";
112            case COMPLETED:  return "COMPLETED";
113            case RUNNING:    return "RUNNING";
114            case STARTED:    return "STARTED";
115            case WAITING:    return "WAITING";
116            case DQ_REMOVAL: return "DQ_REMOVAL";
117            default:         return super.eventName();
118        }
119    }
120
121}