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