001/**
002 *
003 * Copyright the original author or authors, 2020 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.pubsub.form;
018
019import java.net.URL;
020import java.util.Collection;
021import java.util.Collections;
022import java.util.List;
023
024import org.jivesoftware.smackx.pubsub.AccessModel;
025import org.jivesoftware.smackx.pubsub.ChildrenAssociationPolicy;
026import org.jivesoftware.smackx.pubsub.ConfigureNodeFields;
027import org.jivesoftware.smackx.pubsub.ItemReply;
028import org.jivesoftware.smackx.pubsub.NodeType;
029import org.jivesoftware.smackx.pubsub.NotificationType;
030import org.jivesoftware.smackx.pubsub.PublishModel;
031import org.jivesoftware.smackx.xdata.FormField;
032import org.jivesoftware.smackx.xdata.form.FillableForm;
033import org.jivesoftware.smackx.xdata.packet.DataForm;
034
035import org.jxmpp.jid.Jid;
036
037public class FillableConfigureForm extends FillableForm implements ConfigureFormReader {
038
039    FillableConfigureForm(DataForm dataForm) {
040        super(dataForm);
041    }
042
043    /**
044     * Sets the value of access model.
045     *
046     * @param accessModel TODO javadoc me please
047     */
048    public void setAccessModel(AccessModel accessModel) {
049        FormField formField = FormField.listSingleBuilder(ConfigureNodeFields.access_model.getFieldName())
050                        .setValue(accessModel)
051                        .build();
052        write(formField);
053    }
054
055    /**
056     * Set the URL of an XSL transformation which can be applied to payloads in order to
057     * generate an appropriate message body element.
058     *
059     * @param bodyXslt The URL of an XSL
060     */
061    public void setBodyXSLT(String bodyXslt) {
062        FormField formField = FormField.listSingleBuilder(ConfigureNodeFields.body_xslt.getFieldName())
063                        .setValue(bodyXslt)
064                        .build();
065        write(formField);
066    }
067
068    /**
069     * Set the list of child node ids that are associated with a collection node.
070     *
071     * @param children TODO javadoc me please
072     */
073    public void setChildren(List<String> children) {
074        FormField formField = FormField.textMultiBuilder(ConfigureNodeFields.children.getFieldName())
075                        .addValues(children)
076                        .build();
077        write(formField);
078    }
079
080    /**
081     * Sets the policy that determines who may associate children with the node.
082     *
083     * @param policy The policy being set
084     */
085    public void setChildrenAssociationPolicy(ChildrenAssociationPolicy policy) {
086        FormField formField = FormField.listSingleBuilder(ConfigureNodeFields.children_association_policy.getFieldName())
087                        .setValue(policy)
088                        .build();
089        write(formField);
090    }
091
092    /**
093     * Set the JID's in the whitelist of users that can associate child nodes with the collection
094     * node.  This is only relevant if {@link #getChildrenAssociationPolicy()} is set to
095     * {@link ChildrenAssociationPolicy#whitelist}.
096     *
097     * @param whitelist The list of JID's
098     */
099    public void setChildrenAssociationWhitelist(List<? extends Jid> whitelist) {
100        FormField formField = FormField.jidMultiBuilder(ConfigureNodeFields.children_association_whitelist.getFieldName())
101                        .addValues(whitelist)
102                        .build();
103        write(formField);
104    }
105
106    /**
107     * Set the maximum number of child nodes that can be associated with a collection node.
108     *
109     * @param max The maximum number of child nodes.
110     */
111    public void setChildrenMax(int max) {
112        FormField formField = FormField.textSingleBuilder(ConfigureNodeFields.children_max.getFieldName())
113                        .setValue(max)
114                        .build();
115        write(formField);
116    }
117
118    /**
119     * Sets the collection node which the node is affiliated with.
120     *
121     * @param collection The node id of the collection node
122     */
123    public void setCollection(String collection) {
124        setCollections(Collections.singletonList(collection));
125    }
126
127    public void setCollections(Collection<String> collections) {
128        FormField formField = FormField.textMultiBuilder(ConfigureNodeFields.collection.getFieldName())
129                        .addValues(collections)
130                        .build();
131        write(formField);
132    }
133
134    /**
135     * Sets the URL of an XSL transformation which can be applied to the payload
136     * format in order to generate a valid Data Forms result that the client could
137     * display using a generic Data Forms rendering engine.
138     *
139     * @param url The URL of an XSL transformation
140     */
141    public void setDataformXSLT(URL url) {
142        FormField formField = FormField.textSingleBuilder(ConfigureNodeFields.dataform_xslt.getFieldName())
143                        .setValue(url)
144                        .build();
145        write(formField);
146    }
147
148    /**
149     * Sets whether the node will deliver payloads with event notifications.
150     *
151     * @param deliver true if the payload will be delivered, false otherwise
152     */
153    public void setDeliverPayloads(boolean deliver) {
154        writeBoolean(ConfigureNodeFields.deliver_payloads.getFieldName(), deliver);
155    }
156
157    /**
158     * Sets who should get the replies to items.
159     *
160     * @param reply Defines who should get the reply
161     */
162    public void setItemReply(ItemReply reply) {
163        FormField formField = FormField.listSingleBuilder(ConfigureNodeFields.itemreply.getFieldName())
164                        .setValue(reply)
165                        .build();
166        write(formField);
167    }
168
169    /**
170     * Set the maximum number of items to persisted to this node if {@link #isPersistItems()} is
171     * true.
172     *
173     * @param max The maximum number of items to persist
174     */
175    public void setMaxItems(int max) {
176        FormField formField = FormField.textSingleBuilder(ConfigureNodeFields.max_items.getFieldName())
177                        .setValue(max)
178                        .build();
179        write(formField);
180    }
181
182    /**
183     * Sets the maximum payload size in bytes.
184     *
185     * @param max The maximum payload size
186     */
187    public void setMaxPayloadSize(int max) {
188        FormField formField = FormField.textSingleBuilder(ConfigureNodeFields.max_payload_size.getFieldName())
189                        .setValue(max)
190                        .build();
191        write(formField);
192    }
193
194    /**
195     * Sets the node type.
196     *
197     * @param type The node type
198     */
199    public void setNodeType(NodeType type) {
200        FormField formField = FormField.listSingleBuilder(ConfigureNodeFields.node_type.getFieldName())
201                        .setValue(type)
202                        .build();
203        write(formField);
204    }
205
206    /**
207     * Sets whether subscribers should be notified when the configuration changes.
208     *
209     * @param notify true if subscribers should be notified, false otherwise
210     */
211    public void setNotifyConfig(boolean notify) {
212        writeBoolean(ConfigureNodeFields.notify_config.getFieldName(), notify);
213    }
214
215    /**
216     * Sets whether subscribers should be notified when the node is deleted.
217     *
218     * @param notify true if subscribers should be notified, false otherwise
219     */
220    public void setNotifyDelete(boolean notify)  {
221        writeBoolean(ConfigureNodeFields.notify_delete.getFieldName(), notify);
222    }
223
224
225    /**
226     * Sets whether subscribers should be notified when items are deleted
227     * from the node.
228     *
229     * @param notify true if subscribers should be notified, false otherwise
230     */
231    public void setNotifyRetract(boolean notify)  {
232        writeBoolean(ConfigureNodeFields.notify_retract.getFieldName(), notify);
233    }
234
235    /**
236     * Sets the NotificationType for the node.
237     *
238     * @param notificationType The enum representing the possible options
239     * @since 4.3
240     */
241    public void setNotificationType(NotificationType notificationType) {
242        FormField formField = FormField.listSingleBuilder(ConfigureNodeFields.notification_type.getFieldName())
243                        .setValue(notificationType)
244                        .build();
245        write(formField);
246    }
247
248    /**
249     * Sets whether items should be persisted in the node.
250     *
251     * @param persist true if items should be persisted, false otherwise
252     */
253    public void setPersistentItems(boolean persist) {
254        writeBoolean(ConfigureNodeFields.persist_items.getFieldName(), persist);
255    }
256
257    /**
258     * Sets whether to deliver notifications to available users only.
259     *
260     * @param presenceBased true if user must be available, false otherwise
261     */
262    public void setPresenceBasedDelivery(boolean presenceBased) {
263        writeBoolean(ConfigureNodeFields.presence_based_delivery.getFieldName(), presenceBased);
264    }
265
266
267    /**
268     * Sets the publishing model for the node, which determines who may publish to it.
269     *
270     * @param publish The enum representing the possible options for the publishing model
271     */
272    public void setPublishModel(PublishModel publish) {
273        FormField formField = FormField.listSingleBuilder(ConfigureNodeFields.publish_model.getFieldName())
274                        .setValue(publish)
275                        .build();
276        write(formField);
277    }
278
279    /**
280     * Sets the roster groups that are allowed to subscribe and retrieve items.
281     *
282     * @param groups The roster groups
283     */
284    public void setRosterGroupsAllowed(List<? extends CharSequence> groups) {
285        writeListMulti(ConfigureNodeFields.roster_groups_allowed.getFieldName(), groups);
286    }
287
288    /**
289     * Sets whether subscriptions are allowed.
290     *
291     * @param subscribe true if they are, false otherwise
292     */
293    public void setSubscribe(boolean subscribe) {
294        writeBoolean(ConfigureNodeFields.subscribe.getFieldName(), subscribe);
295    }
296
297    /**
298     * Sets a human readable title for the node.
299     *
300     * @param title The node title
301     */
302    public void setTitle(CharSequence title)  {
303        writeTextSingle(ConfigureNodeFields.title.getFieldName(), title);
304    }
305
306    /**
307     * Sets the type of node data, usually specified by the namespace of the payload (if any).
308     *
309     * @param type The type of node data
310     */
311    public void setDataType(CharSequence type)  {
312        writeTextSingle(ConfigureNodeFields.type.getFieldName(), type);
313    }
314}