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