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.util.Collections;
020import java.util.List;
021
022import org.jivesoftware.smackx.pubsub.AccessModel;
023import org.jivesoftware.smackx.pubsub.ChildrenAssociationPolicy;
024import org.jivesoftware.smackx.pubsub.ConfigureNodeFields;
025import org.jivesoftware.smackx.pubsub.ItemReply;
026import org.jivesoftware.smackx.pubsub.NodeType;
027import org.jivesoftware.smackx.pubsub.NotificationType;
028import org.jivesoftware.smackx.pubsub.PublishModel;
029import org.jivesoftware.smackx.pubsub.packet.PubSub;
030import org.jivesoftware.smackx.xdata.FormField;
031import org.jivesoftware.smackx.xdata.JidMultiFormField;
032import org.jivesoftware.smackx.xdata.form.FormReader;
033
034import org.jxmpp.jid.Jid;
035
036public interface ConfigureFormReader extends FormReader {
037
038    String FORM_TYPE = PubSub.NAMESPACE + "#node_config";
039
040    /**
041     * Get the currently configured {@link AccessModel}, null if it is not set.
042     *
043     * @return The current {@link AccessModel}
044     */
045    default AccessModel getAccessModel() {
046        String value = readFirstValue(ConfigureNodeFields.access_model.getFieldName());
047        if (value == null) {
048            return null;
049        }
050        return AccessModel.valueOf(value);
051    }
052
053    /**
054     * Returns the URL of an XSL transformation which can be applied to payloads in order to
055     * generate an appropriate message body element.
056     *
057     * @return URL to an XSL
058     */
059    default String getBodyXSLT() {
060        return readFirstValue(ConfigureNodeFields.body_xslt.getFieldName());
061    }
062
063    /**
064     * The id's of the child nodes associated with a collection node (both leaf and collection).
065     *
066     * @return list of child nodes.
067     */
068    default List<String> getChildren() {
069        return readStringValues(ConfigureNodeFields.children.getFieldName());
070    }
071
072    /**
073     * Returns the policy that determines who may associate children with the node.
074     *
075     * @return The current policy
076     */
077    default ChildrenAssociationPolicy getChildrenAssociationPolicy() {
078        String value = readFirstValue(ConfigureNodeFields.children_association_policy.getFieldName());
079        if (value == null) {
080            return null;
081        }
082        return ChildrenAssociationPolicy.valueOf(value);
083    }
084
085    /**
086     * List of JID's that are on the whitelist that determines who can associate child nodes
087     * with the collection node.  This is only relevant if {@link #getChildrenAssociationPolicy()} is set to
088     * {@link ChildrenAssociationPolicy#whitelist}.
089     *
090     * @return List of the whitelist
091     */
092    default List<Jid> getChildrenAssociationWhitelist() {
093        FormField formField = getField(ConfigureNodeFields.children_association_whitelist.getFieldName());
094        if (formField == null) {
095            Collections.emptyList();
096        }
097        JidMultiFormField jidMultiFormField = formField.ifPossibleAs(JidMultiFormField.class);
098        return jidMultiFormField.getValues();
099    }
100
101    /**
102     * Gets the maximum number of child nodes that can be associated with the collection node.
103     *
104     * @return The maximum number of child nodes
105     */
106    default Integer getChildrenMax() {
107        return readInteger(ConfigureNodeFields.children_max.getFieldName());
108    }
109
110    /**
111     * Gets the collection node which the node is affiliated with.
112     *
113     * @return The collection node id
114     */
115    default List<? extends CharSequence> getCollection() {
116        return readValues(ConfigureNodeFields.collection.getFieldName());
117    }
118
119    /**
120     * Gets the URL of an XSL transformation which can be applied to the payload
121     * format in order to generate a valid Data Forms result that the client could
122     * display using a generic Data Forms rendering engine.
123     *
124     * @return The URL of an XSL transformation
125     */
126    default String getDataformXSLT() {
127        return readFirstValue(ConfigureNodeFields.dataform_xslt.getFieldName());
128    }
129
130    /**
131     * Does the node deliver payloads with event notifications.
132     *
133     * @return true if it does, false otherwise
134     */
135    default Boolean isDeliverPayloads() {
136        return readBoolean(ConfigureNodeFields.deliver_payloads.getFieldName());
137    }
138
139    /**
140     * Determines who should get replies to items.
141     *
142     * @return Who should get the reply
143     */
144    default ItemReply getItemReply() {
145        String value = readFirstValue(ConfigureNodeFields.itemreply.getFieldName());
146        if (value == null) {
147            return null;
148        }
149        return ItemReply.valueOf(value);
150    }
151
152    /**
153     * Gets the maximum number of items to persisted to this node if {@link #isPersistItems()} is
154     * true.
155     *
156     * @return The maximum number of items to persist
157     */
158    default Integer getMaxItems() {
159        return readInteger(ConfigureNodeFields.max_items.getFieldName());
160    }
161
162    /**
163     * Gets the maximum payload size in bytes.
164     *
165     * @return The maximum payload size
166     */
167    default Integer getMaxPayloadSize() {
168        return readInteger(ConfigureNodeFields.max_payload_size.getFieldName());
169    }
170
171    /**
172     * Gets the node type.
173     *
174     * @return The node type
175     */
176    default NodeType getNodeType() {
177        String value = readFirstValue(ConfigureNodeFields.node_type.getFieldName());
178        if (value == null) {
179            return null;
180        }
181        return NodeType.valueOf(value);
182    }
183
184    /**
185     * Determines if subscribers should be notified when the configuration changes.
186     *
187     * @return true if they should be notified, false otherwise
188     */
189    default Boolean isNotifyConfig() {
190        return readBoolean(ConfigureNodeFields.notify_config.getFieldName());
191    }
192
193    /**
194     * Determines whether subscribers should be notified when the node is deleted.
195     *
196     * @return true if subscribers should be notified, false otherwise
197     */
198    default Boolean isNotifyDelete() {
199        return readBoolean(ConfigureNodeFields.notify_delete.getFieldName());
200    }
201
202    /**
203     * Determines whether subscribers should be notified when items are deleted
204     * from the node.
205     *
206     * @return true if subscribers should be notified, false otherwise
207     */
208    default Boolean isNotifyRetract() {
209        return readBoolean(ConfigureNodeFields.notify_retract.getFieldName());
210    }
211
212    /**
213     * Determines the type of notifications which are sent.
214     *
215     * @return NotificationType for the node configuration
216     * @since 4.3
217     */
218    default NotificationType getNotificationType() {
219        String value = readFirstValue(ConfigureNodeFields.notification_type.getFieldName());
220        if (value == null) {
221            return null;
222        }
223        return NotificationType.valueOf(value);
224    }
225
226    /**
227     * Determines whether items should be persisted in the node.
228     *
229     * @return true if items are persisted
230     */
231    default boolean isPersistItems() {
232        return readBoolean(ConfigureNodeFields.persist_items.getFieldName());
233    }
234
235    /**
236     * Determines whether to deliver notifications to available users only.
237     *
238     * @return true if users must be available
239     */
240    default boolean isPresenceBasedDelivery() {
241        return readBoolean(ConfigureNodeFields.presence_based_delivery.getFieldName());
242    }
243
244    /**
245     * Gets the publishing model for the node, which determines who may publish to it.
246     *
247     * @return The publishing model
248     */
249    default PublishModel getPublishModel() {
250        String value = readFirstValue(ConfigureNodeFields.publish_model.getFieldName());
251        if (value == null) {
252            return null;
253        }
254        return PublishModel.valueOf(value);
255    }
256
257    /**
258     * Gets the roster groups that are allowed to subscribe and retrieve items.
259     *
260     * @return The roster groups
261     */
262    default List<String> getRosterGroupsAllowed() {
263        return readStringValues(ConfigureNodeFields.roster_groups_allowed.getFieldName());
264    }
265
266    /**
267     * Determines if subscriptions are allowed.
268     *
269     * @return true if subscriptions are allowed, false otherwise
270     */
271    default boolean isSubscribe() {
272        return readBoolean(ConfigureNodeFields.subscribe.getFieldName());
273    }
274
275    /**
276     * Gets the human readable node title.
277     *
278     * @return The node title
279     */
280    default String getTitle() {
281        return readFirstValue(ConfigureNodeFields.title.getFieldName());
282    }
283
284    /**
285     * The type of node data, usually specified by the namespace of the payload (if any).
286     *
287     * @return The type of node data
288     */
289    default String getDataType() {
290        return readFirstValue(ConfigureNodeFields.type.getFieldName());
291    }
292}