001/**
002 *
003 * Copyright 2023 Florian Schmaus
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 */
017package org.jivesoftware.smackx.commands.packet;
018
019import java.util.ArrayList;
020import java.util.EnumSet;
021import java.util.List;
022import java.util.Set;
023
024import org.jivesoftware.smack.XMPPConnection;
025import org.jivesoftware.smack.packet.AbstractIqBuilder;
026import org.jivesoftware.smack.packet.IQ;
027import org.jivesoftware.smack.packet.IqBuilder;
028import org.jivesoftware.smack.packet.IqData;
029import org.jivesoftware.smack.util.StringUtils;
030
031import org.jivesoftware.smackx.commands.AdHocCommandNote;
032import org.jivesoftware.smackx.commands.packet.AdHocCommandData.Action;
033import org.jivesoftware.smackx.commands.packet.AdHocCommandData.AllowedAction;
034import org.jivesoftware.smackx.commands.packet.AdHocCommandData.Status;
035import org.jivesoftware.smackx.xdata.packet.DataForm;
036
037public class AdHocCommandDataBuilder extends IqBuilder<AdHocCommandDataBuilder, AdHocCommandData> implements AdHocCommandDataView {
038
039    private final String node;
040
041    private String name;
042
043    private String sessionId;
044
045    private final List<AdHocCommandNote> notes = new ArrayList<>();
046
047    private DataForm form;
048
049    /* Action request to be executed */
050    private Action action;
051
052    /* Current execution status */
053    private Status status;
054
055    private final Set<AllowedAction> actions = EnumSet.noneOf(AllowedAction.class);
056
057    private AllowedAction executeAction;
058
059    AdHocCommandDataBuilder(String node, IqData iqCommon) {
060        super(iqCommon);
061        this.node = StringUtils.requireNotNullNorEmpty(node, "Ad-Hoc Command node must be set");
062    }
063
064    AdHocCommandDataBuilder(String node, String stanzaId) {
065        super(stanzaId);
066        this.node = StringUtils.requireNotNullNorEmpty(node, "Ad-Hoc Command node must be set");
067    }
068
069    AdHocCommandDataBuilder(String node, XMPPConnection connection) {
070        super(connection);
071        this.node = StringUtils.requireNotNullNorEmpty(node, "Ad-Hoc Command node must be set");
072    }
073
074    @Override
075    public String getNode() {
076        return node;
077    }
078
079    @Override
080    public String getName() {
081        return name;
082    }
083
084    public AdHocCommandDataBuilder setName(String name) {
085        this.name = name;
086        return getThis();
087    }
088
089    @Override
090    public String getSessionId() {
091        return sessionId;
092    }
093
094    public AdHocCommandDataBuilder setSessionId(String sessionId) {
095        this.sessionId = sessionId;
096        return getThis();
097    }
098
099    @Override
100    public List<AdHocCommandNote> getNotes() {
101        return notes;
102    }
103
104    public AdHocCommandDataBuilder addNote(AdHocCommandNote note) {
105        notes.add(note);
106        return getThis();
107    }
108
109    @Override
110    public DataForm getForm() {
111        return form;
112    }
113
114    public AdHocCommandDataBuilder setForm(DataForm form) {
115        this.form = form;
116        return getThis();
117    }
118
119    @Override
120    public Action getAction() {
121        return action;
122    }
123
124    public AdHocCommandDataBuilder setAction(AdHocCommandData.Action action) {
125        this.action = action;
126        return getThis();
127    }
128    @Override
129
130    public AdHocCommandData.Status getStatus() {
131        return status;
132    }
133
134    public AdHocCommandDataBuilder setStatus(AdHocCommandData.Status status) {
135        this.status = status;
136        return getThis();
137    }
138
139    public AdHocCommandDataBuilder setStatusCompleted() {
140        return setStatus(AdHocCommandData.Status.completed);
141    }
142
143    public enum PreviousStage {
144        exists,
145        none,
146    }
147
148    public enum NextStage {
149        isFinal,
150        nonFinal,
151    }
152
153    @SuppressWarnings("fallthrough")
154    public AdHocCommandDataBuilder setStatusExecuting(PreviousStage previousStage, NextStage nextStage) {
155        setStatus(AdHocCommandData.Status.executing);
156
157        switch (previousStage) {
158        case exists:
159            addAction(AllowedAction.prev);
160            break;
161        case none:
162            break;
163        }
164
165        setExecuteAction(AllowedAction.next);
166
167        switch (nextStage) {
168        case isFinal:
169            addAction(AllowedAction.complete);
170            // Override execute action of 'next'.
171            setExecuteAction(AllowedAction.complete);
172            // Deliberate fallthrough, we want 'next' to be added.
173        case nonFinal:
174            addAction(AllowedAction.next);
175            break;
176        }
177
178        return getThis();
179    }
180
181    @Override
182    public Set<AllowedAction> getActions() {
183        return actions;
184    }
185
186    public AdHocCommandDataBuilder addAction(AllowedAction action) {
187        actions.add(action);
188        return getThis();
189    }
190
191    @Override
192    public AllowedAction getExecuteAction() {
193        return executeAction;
194    }
195
196    public AdHocCommandDataBuilder setExecuteAction(AllowedAction action) {
197        this.executeAction = action;
198        return getThis();
199    }
200
201    @Override
202    public AdHocCommandData build() {
203        return new AdHocCommandData(this);
204    }
205
206    @Override
207    public AdHocCommandDataBuilder getThis() {
208        return this;
209    }
210
211    public static AdHocCommandDataBuilder buildResponseFor(AdHocCommandData request) {
212        return buildResponseFor(request, IQ.ResponseType.result);
213    }
214
215    public static AdHocCommandDataBuilder buildResponseFor(AdHocCommandData request, IQ.ResponseType responseType) {
216        AdHocCommandDataBuilder builder = new AdHocCommandDataBuilder(request.getNode(), AbstractIqBuilder.createResponse(request, responseType));
217        return builder;
218    }
219
220}