SessionData.java 4.68 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/**
 * $Revision: 3023 $
 * $Date: 2005-11-02 18:00:15 -0300 (Wed, 02 Nov 2005) $
 *
 * Copyright (C) 2005 Jive Software. All rights reserved.
 *
 * This software is published under the terms of the GNU Public License (GPL),
 * a copy of which is included in this distribution.
 */

package org.jivesoftware.wildfire.commands;

13 14
import org.xmpp.packet.JID;

15 16 17 18 19 20 21 22 23 24 25 26
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * A SessionData instance is responsible for keeping information gathered during the many stages
 * of the command being executed. Each session data is associated with the <tt>sessionid</tt>
 * attribute included in the <tt>command</tt> child element of the IQ packet.
 *
 * @author Gaston Dombiak
 */
27
public class SessionData {
28 29 30 31

    private long creationStamp;

    private String id;
32
    private JID owner;
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

    /**
     * Map that keeps the association of variables and values obtained in each stage.
     * Note: Key=stage number, Value=Map with key=variable name and value=variable values.
     */
    private Map<Integer, Map<String, List<String>>> stagesData = new HashMap<Integer, Map<String, List<String>>>();

    /**
     * Keeps the default execution action to follow if the command requester does not include
     * an action in his command.
     */
    private AdHocCommand.Action executeAction;

    private List<AdHocCommand.Action> allowedActions = new ArrayList<AdHocCommand.Action>();

    /**
     * Indicates the current stage where the requester is located. Stages are numbered from 0.
     */
    private int stage;

53
    SessionData(String sessionid, JID owner) {
54 55
        this.id = sessionid;
        this.creationStamp = System.currentTimeMillis();
56 57
        this.stage = -1;
        this.owner = owner;
58 59 60 61 62 63
    }

    public String getId() {
        return id;
    }

64 65 66 67 68 69 70 71 72
    /**
     * Returns the JID of the entity that is executing the command.
     *
     * @return the JID of the entity that is executing the command.
     */
    public JID getOwner() {
        return owner;
    }

73 74 75 76
    public long getCreationStamp() {
        return creationStamp;
    }

77
    AdHocCommand.Action getExecuteAction() {
78 79 80
        return executeAction;
    }

81
    void setExecuteAction(AdHocCommand.Action executeAction) {
82 83 84 85 86 87 88 89
        this.executeAction = executeAction;
    }

    /**
     * Sets the valid actions that the user can follow from the current stage.
     *
     * @param allowedActions list of valid actions.
     */
90
    void setAllowedActions(List<AdHocCommand.Action> allowedActions) {
91 92 93 94 95 96 97 98 99 100 101 102 103
        if (allowedActions == null) {
            allowedActions = new ArrayList<AdHocCommand.Action>();
        }
        this.allowedActions = allowedActions;
    }

    /**
     * Returns true if the specified action is valid in the current stage. The action should have
     * previously been offered to the user.
     *
     * @param actionName the name of the action to validate.
     * @return true if the specified action is valid in the current stage.
     */
104
    boolean isValidAction(String actionName) {
105 106 107 108 109 110 111 112
        for (AdHocCommand.Action action : allowedActions) {
            if (actionName.equals(action.name())) {
                return true;
            }
        }
        return false;
    }

113
    void addStageForm(Map<String, List<String>> data) {
114 115 116 117 118 119 120 121 122 123
        stagesData.put(stage, data);
    }

    /**
     * Returns a Map with all the variables and values obtained during all the command stages.
     *
     * @return a Map with all the variables and values obtained during all the command stages.
     */
    public Map<String, List<String>> getData() {
        Map<String, List<String>> data = new HashMap<String, List<String>>();
124 125 126
        for (Map<String, List<String>> stageData : stagesData.values()) {
            data.putAll(stageData);
        }
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
        return data;
    }

    /**
     * Returns the current stage where the requester is located. Stages are numbered from 0. A
     * stage with value 0 means that a command request has just been received and no data form
     * has been sent to the requester yet. The first sent data form of the first stage would be
     * represented as stage 1.
     *
     * @return the current stage where the requester is located.
     */
    public int getStage() {
        return stage;
    }

    /**
     * Sets the current stage where the requester is located. Stages are numbered from 0. A
     * stage with value 0 means that a command request has just been received and no data form
     * has been sent to the requester yet. The first sent data form of the first stage would be
     * represented as stage 1.
     *
     * @param stage the current stage where the requester is located.
     */
150
    void setStage(int stage) {
151 152 153 154
        this.stage = stage;
    }

}