summaryrefslogtreecommitdiffstats
blob: b9aedb88ceabc3de394ac6b8097c279837758be6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/*
 * Copyright (c) 2012 Isode Limited, London, England.
 * All rights reserved.
 */
/*
 * Copyright (c) 2010 Kevin Smith
 * All rights reserved.
 */

package com.isode.stroke.elements;

import java.util.ArrayList;
import java.util.List;

/**
 * Ad-Hoc Command (XEP-0050).
 */
public class Command extends Payload {
    /**
     * Attribute "node"
     */
    public static final String COMMAND_ATTRIBUTE_NODE = "node";

    /**
     * Attribute "sessionid"
     */
    public static final String COMMAND_ATTRIBUTE_SESSION_ID = "sessionid";

    /**
     * Attribute "status"
     */
    public static final String COMMAND_ATTRIBUTE_STATUS = "status";

    /**
     * Attribute "action"
     */
    public static final String COMMAND_ATTRIBUTE_ACTION = "action";

    /**
     * Element "actions"
     */
    public static final String COMMAND_ELEMENT_ACTIONS = "actions";

    /**
     * Attribute "execute"
     */
    public static final String COMMAND_ATTRIBUTE_EXECUTE = "execute";

    /**
     * Element "note"
     */
    public static final String COMMAND_ELEMENT_NOTE = "note";

    /**
     * Attribute "type"
     */
    public static final String COMMAND_ATTRIBUTE_TYPE = "type";

    /**
     * Current status of the command.
     */
    public enum Status {
        /**
         * The command is being executed.
         */
        EXECUTING("executing"),
        /**
         * The command has completed. The command session has ended.
         */
        COMPLETED("completed"),
        /**
         * The command has been cancelled. The command session has ended.
         */
        CANCELED("canceled"),
        /**
         * No status has been set for the command.
         */
        NO_STATUS("");

        private String stringForm_;

        private Status(String stringForm) {
            stringForm_ = stringForm;
        }

        /**
         * Get status from its string form.
         *
         * @param stringForm String form of status, can be null
         *
         * @return Corresponding status if match found, otherwise
         *         {@link #NO_STATUS}. Will never be null.
         */
        public static Status getStatus(String stringForm) {
            if (stringForm != null) {
                for (Status status : Status.values()) {
                    if (status.stringForm_.equals(stringForm)) {
                        return status;
                    }
                }
            }

            return NO_STATUS;
        }

        /**
         * @return String form of status, will never be null
         */
        public String getStringForm() {
            return stringForm_;
        }
    };

    /**
     * The action to undertake with the given command.
     */
    public enum Action {
        /**
         * The command should be cancelled.
         */
        CANCEL("cancel"),
        /**
         * The command should be executed or continue to be executed.
         */
        EXECUTE("execute"),
        /**
         * The command should be completed (if possible).
         */
        COMPLETE("complete"),
        /**
         * The command should digress to the previous stage of execution.
         */
        PREV("prev"),
        /**
         * The command should progress to the next stage of execution.
         */
        NEXT("next"),
        /**
         * No action is available for the command.
         */
        NO_ACTION("");

        private String stringForm_;

        private Action(String stringForm) {
            stringForm_ = stringForm;
        }

        /**
         * Get action from its string form.
         *
         * @param stringForm String form of action, can be null
         *
         * @return Corresponding action if match found, otherwise
         *         {@link Action#NO_ACTION}. Will never be null.
         */
        public static Action getAction(String stringForm) {
            if (stringForm != null) {
                for (Action action : Action.values()) {
                    if (action.stringForm_.equals(stringForm)) {
                        return action;
                    }
                }
            }

            return NO_ACTION;
        }

        /**
         * @return String form of action, will never be null
         */
        public String getStringForm() {
            return stringForm_;
        }
    };

    /**
     * This class contains information about the current status of the command.
     * This is an immutable class.
     */
    public static class Note {
        /**
         * Severity of the note.
         */
        public enum Type {
            /**
             * The note is informational only. This is not really an exceptional
             * condition.
             */
            INFO("info"),
            /**
             * The note indicates a warning. Possibly due to illogical (yet
             * valid) data.
             */
            WARN("warn"),
            /**
             * The note indicates an error. The text should indicate the reason
             * for the error.
             */
            ERROR("error");

            private String stringForm_;

            private Type(String stringForm) {
                stringForm_ = stringForm;
            }

            /**
             * Get type from its string form.
             *
             * @param stringForm String form of type, can be null
             *
             * @return Corresponding type if match found, otherwise
             *         {@link Type#INFO}. Will never be null.
             */
            public static Type getType(String stringForm) {
                if (stringForm != null) {
                    for (Type type : Type.values()) {
                        if (type.stringForm_.equals(stringForm)) {
                            return type;
                        }
                    }
                }

                return INFO;
            }

            /**
             * @return String form of type, will never be null
             */
            public String getStringForm() {
                return stringForm_;
            }
        };

        /**
         * Create a note element for the Ad-Hoc command.
         *
         * @param note user-readable text, must not be null
         * @param type Severity of the note, must not be null
         */
        public Note(String note, Type type) {
            if (note == null) {
                throw new NullPointerException("'note' must not be null");
            }
            if (type == null) {
                throw new NullPointerException("'type' must not be null");
            }
            this.note = note;
            this.type = type;
        }

        /**
         * User-readable text, will never be null
         */
        public final String note;

        /**
         * Severity of the note, will never be null
         */
        public final Type type;
    };

    private String node_ = "";
    private String sessionID_ = "";
    private Action action_;
    private Status status_;
    private Action executeAction_;
    private List<Action> availableActions_ = new ArrayList<Action>();
    private List<Note> notes_ = new ArrayList<Note>();
    private Form form_ = null;

    private void assignData(String node, String sessionID, Action action,
            Status status) {
        setNode(node);
        setSessionID(sessionID);
        setAction(action);
        setStatus(status);
        setExecuteAction(Action.NO_ACTION);
    }

    /**
     * Create an Ad-Hoc command with the given node, session ID, status and
     * {@link Action#NO_ACTION} action.
     *
     * @param node Node, must not be null. Each command is identified by its
     *            'node' attribute. This matches its 'node' attribute from the
     *            service discovery.
     * @param sessionID The ID of the session within which the command exists,
     *            must not be null but can be empty if this is the first stage
     *            of the command and the client does not know it yet
     * @param status Status of the command, must not be null
     */
    public Command(String node, String sessionID, Status status) {
        assignData(node, sessionID, Action.NO_ACTION, status);
    }

    /**
     * Create an Ad-Hoc command with the given node, session ID, action and
     * {@link Status#NO_STATUS} status. This can be used when submitting further
     * actions of an ongoing Ad-Hoc session.
     * 
     * @param node Node, must not be null. Each command is identified by its
     *            'node' attribute. This matches its 'node' attribute from the
     *            service discovery.
     * @param sessionID The ID of the session within which the command exists,
     *            must not be null but can be empty if this is the first stage
     *            of the command and the client does not know it yet
     * @param action action of the command, must not be null
     */
    public Command(String node, String sessionID, Action action) {
        assignData(node, sessionID, action, Status.NO_STATUS);
    }

    /**
     * Create an Ad-Hoc command with the given node, empty session ID,
     * {@link Action#EXECUTE} action and {@link Status#NO_STATUS} status. This
     * can be used when initiating an Ad-Hoc command request.
     * 
     * @param node Node, must not be null. Each command is identified by its
     *            'node' attribute. This matches its 'node' attribute from the
     *            service discovery.
     */
    public Command(String node) {
        this(node, "", Action.EXECUTE);
    }

    /**
     * Create an Ad-Hoc command with an empty node, empty session ID,
     * {@link Action#EXECUTE} action and {@link Status#NO_STATUS} status. This
     * will need the attributes to be populated later as the object is not
     * usable in this form.
     */
    public Command() {
        this("", "", Action.EXECUTE);
    }

    /**
     * @return Node, will never be null
     */
    public String getNode() {
        return node_;
    }

    /**
     * Set command node.
     *
     * @param node Node, must not be null
     */
    public void setNode(String node) {
        if (node == null) {
            throw new NullPointerException("'node' must not be null");
        }

        node_ = node;
    }

    /**
     * @return The ID of the session within which the command exists, can be
     *         empty if this is the first stage of the command and the client
     *         does not know it yet, will never be null
     */
    public String getSessionID() {
        return sessionID_;
    }

    /**
     * Set session ID.
     *
     * @param sessionID The ID of the session within which the command exists,
     *            must not be null but can be empty if this is the first stage
     *            of the command and the client does not know it yet
     */
    public void setSessionID(String sessionID) {
        if (sessionID == null) {
            throw new NullPointerException("'sessionID' must not be null");
        }

        sessionID_ = sessionID;
    }

    /**
     * @return action of the command, will never be null
     */
    public Action getAction() {
        return action_;
    }

    /**
     * Set action of the command.
     *
     * @param action action of the command, must not be null
     */
    public void setAction(Action action) {
        if (action == null) {
            throw new NullPointerException("'action' must not be null");
        }

        action_ = action;
    }

    /**
     * @return execute action of the command, will never be null
     */
    public Action getExecuteAction() {
        return executeAction_;
    }

    /**
     * Set execute action of the command.
     *
     * @param action execute action of the command, must not be null
     */
    public void setExecuteAction(Action action) {
        if (action == null) {
            throw new NullPointerException("'action' must not be null");
        }

        executeAction_ = action;
    }

    /**
     * @return status of the command, will never be null
     */
    public Status getStatus() {
        return status_;
    }

    /**
     * Set status of the command.
     *
     * @param status Status of the command, must not be null
     */
    public void setStatus(Status status) {
        if (status == null) {
            throw new NullPointerException("'status' must not be null");
        }

        status_ = status;
    }

    /**
     * @return List of allowed actions for this stage of execution, will never
     *         be null. The instance of the list stored in the object is not
     *         returned, a copy is made.
     */
    public List<Action> getAvailableActions() {
        return new ArrayList<Action>(availableActions_);
    }

    /**
     * Add to the list of allowed actions for this stage of execution.
     *
     * @param action Action to add, must not be null
     */
    public void addAvailableAction(Action action) {
        if (action == null) {
            throw new NullPointerException("'action' must not be null");
        }

        availableActions_.add(action);
    }

    /**
     * @return List of information elements for the current status of the
     *         command, will never be null. The instance of the list stored in
     *         the object is not returned, a copy is made.
     */
    public List<Note> getNotes() {
        return new ArrayList<Note>(notes_);
    }

    /**
     * Add to the list of information elements for the current status of the
     * command.
     *
     * @param note Note to add, must not be null
     */
    public void addNote(Note note) {
        if (note == null) {
            throw new NullPointerException("'note' must not be null");
        }

        notes_.add(note);
    }

    /**
     * @return form of the command, can be null. The instance of the form stored
     *         in the object is returned, a copy is not made.
     */
    public Form getForm() {
        return form_;
    }

    /**
     * Set form for the command.
     *
     * @param payload Form for the command, can be null. The instance of the
     *            form is stored in the object, a copy is not made.
     */
    public void setForm(Form payload) {
        form_ = payload;
    }

    @Override
    public String toString() {
        return Command.class.getSimpleName() + "\nnode: " + node_
                + "\nsession ID: " + sessionID_ + "\naction: " + action_
                + "\nstatus: " + status_ + "\nexecute action: "
                + executeAction_;
    }
}