001/**
002 *
003 * Copyright 2005-2008 Jive Software.
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.jivesoftware.smackx.commands.packet;
019
020import org.jivesoftware.smack.packet.IQ;
021import org.jivesoftware.smack.packet.ExtensionElement;
022import org.jivesoftware.smackx.commands.AdHocCommand;
023import org.jivesoftware.smackx.commands.AdHocCommand.Action;
024import org.jivesoftware.smackx.commands.AdHocCommand.SpecificErrorCondition;
025import org.jivesoftware.smackx.commands.AdHocCommandNote;
026import org.jivesoftware.smackx.xdata.packet.DataForm;
027import org.jxmpp.jid.Jid;
028
029import java.util.ArrayList;
030import java.util.List;
031
032/**
033 * Represents the state and the request of the execution of an adhoc command.
034 * 
035 * @author Gabriel Guardincerri
036 */
037public class AdHocCommandData extends IQ {
038
039    public static final String ELEMENT = "command";
040    public static final String NAMESPACE = "http://jabber.org/protocol/commands";
041
042    /* JID of the command host */
043    private Jid id;
044
045    /* Command name */
046    private String name;
047
048    /* Command identifier */
049    private String node;
050
051    /* Unique ID of the execution */
052    private String sessionID;
053
054    private List<AdHocCommandNote> notes = new ArrayList<AdHocCommandNote>();
055
056    private DataForm form;
057
058    /* Action request to be executed */
059    private AdHocCommand.Action action;
060
061    /* Current execution status */
062    private AdHocCommand.Status status;
063
064    private ArrayList<AdHocCommand.Action> actions = new ArrayList<AdHocCommand.Action>();
065
066    private AdHocCommand.Action executeAction;
067
068    public AdHocCommandData() {
069        super(ELEMENT, NAMESPACE);
070    }
071
072    @Override
073    protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
074        xml.attribute("node", node);
075        xml.optAttribute("sessionid", sessionID);
076        xml.optAttribute("status", status);
077        xml.optAttribute("action", action);
078        xml.rightAngleBracket();
079
080        if (getType() == Type.result) {
081            xml.halfOpenElement("actions");
082            xml.optAttribute("execute", executeAction);
083            if (actions.size() == 0) {
084                xml.closeEmptyElement();
085            } else {
086                xml.rightAngleBracket();
087
088                for (AdHocCommand.Action action : actions) {
089                    xml.emptyElement(action);
090                }
091                xml.closeElement("actions");
092            }
093        }
094
095        if (form != null) {
096            xml.append(form.toXML());
097        }
098
099        for (AdHocCommandNote note : notes) {
100            xml.halfOpenElement("note").attribute("type", note.getType().toString()).rightAngleBracket();
101            xml.append(note.getValue());
102            xml.closeElement("note");
103        }
104
105        // TODO ERRORS
106//        if (getError() != null) {
107//            buf.append(getError().toXML());
108//        }
109
110        return xml;
111    }
112
113    /**
114     * Returns the JID of the command host.
115     *
116     * @return the JID of the command host.
117     */
118    public Jid getId() {
119        return id;
120    }
121
122    public void setId(Jid id) {
123        this.id = id;
124    }
125
126    /**
127     * Returns the human name of the command.
128     *
129     * @return the name of the command.
130     */
131    public String getName() {
132        return name;
133    }
134
135    public void setName(String name) {
136        this.name = name;
137    }
138
139    /**
140     * Returns the identifier of the command.
141     *
142     * @return the node.
143     */
144    public String getNode() {
145        return node;
146    }
147
148    public void setNode(String node) {
149        this.node = node;
150    }
151
152    /**
153     * Returns the list of notes that the command has.
154     *
155     * @return the notes.
156     */
157    public List<AdHocCommandNote> getNotes() {
158        return notes;
159    }
160
161    public void addNote(AdHocCommandNote note) {
162        this.notes.add(note);
163    }
164
165    public void remveNote(AdHocCommandNote note) {
166        this.notes.remove(note);
167    }
168
169    /**
170     * Returns the form of the command.
171     *
172     * @return the data form associated with the command.
173     */
174    public DataForm getForm() {
175        return form;
176    }
177
178    public void setForm(DataForm form) {
179        this.form = form;
180    }
181
182    /**
183     * Returns the action to execute. The action is set only on a request.
184     *
185     * @return the action to execute.
186     */
187    public AdHocCommand.Action getAction() {
188        return action;
189    }
190
191    public void setAction(AdHocCommand.Action action) {
192        this.action = action;
193    }
194
195    /**
196     * Returns the status of the execution.
197     *
198     * @return the status.
199     */
200    public AdHocCommand.Status getStatus() {
201        return status;
202    }
203
204    public void setStatus(AdHocCommand.Status status) {
205        this.status = status;
206    }
207
208    public List<Action> getActions() {
209        return actions;
210    }
211
212    public void addAction(Action action) {
213        actions.add(action);
214    }
215
216    public void setExecuteAction(Action executeAction) {
217        this.executeAction = executeAction;
218    }
219
220    public Action getExecuteAction() {
221        return executeAction;
222    }
223
224    /**
225     * Set the 'sessionid' attribute of the command.
226     * <p>
227     * This value can be null or empty for the first command, but MUST be set for subsequent commands. See also <a
228     * href="http://xmpp.org/extensions/xep-0050.html#impl-session">XEP-0050 ยง 3.3 Session Lifetime</a>.
229     * </p>
230     *
231     * @param sessionID
232     */
233    public void setSessionID(String sessionID) {
234        this.sessionID = sessionID;
235    }
236
237    public String getSessionID() {
238        return sessionID;
239    }
240
241    public static class SpecificError implements ExtensionElement {
242
243        public static final String namespace = "http://jabber.org/protocol/commands";
244
245        public SpecificErrorCondition condition;
246
247        public SpecificError(SpecificErrorCondition condition) {
248            this.condition = condition;
249        }
250
251        @Override
252        public String getElementName() {
253            return condition.toString();
254        }
255        @Override
256        public String getNamespace() {
257            return namespace;
258        }
259
260        public SpecificErrorCondition getCondition() {
261            return condition;
262        }
263
264        @Override
265        public String toXML() {
266            StringBuilder buf = new StringBuilder();
267            buf.append('<').append(getElementName());
268            buf.append(" xmlns=\"").append(getNamespace()).append("\"/>");
269            return buf.toString();
270        }
271    }
272}