001/**
002 *
003 * Copyright © 2016 Fernando Ramirez
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.push_notifications.element;
018
019import java.util.HashMap;
020import java.util.Iterator;
021import java.util.Map;
022
023import org.jivesoftware.smack.packet.IQ;
024
025import org.jivesoftware.smackx.pubsub.packet.PubSub;
026import org.jivesoftware.smackx.xdata.FormField;
027import org.jivesoftware.smackx.xdata.packet.DataForm;
028
029import org.jxmpp.jid.Jid;
030
031/**
032 * Enable Push Notifications IQ.
033 *
034 * @see <a href="http://xmpp.org/extensions/xep-0357.html">XEP-0357: Push
035 *      Notifications</a>
036 * @author Fernando Ramirez
037 *
038 */
039public class EnablePushNotificationsIQ extends IQ {
040
041    /**
042     * enable element.
043     */
044    public static final String ELEMENT = "enable";
045
046    /**
047     * the IQ NAMESPACE.
048     */
049    public static final String NAMESPACE = PushNotificationsElements.NAMESPACE;
050
051    private final Jid jid;
052    private final String node;
053    private final HashMap<String, String> publishOptions;
054
055    public EnablePushNotificationsIQ(Jid jid, String node, HashMap<String, String> publishOptions) {
056        super(ELEMENT, NAMESPACE);
057        this.jid = jid;
058        this.node = node;
059        this.publishOptions = publishOptions;
060        this.setType(Type.set);
061    }
062
063    public EnablePushNotificationsIQ(Jid jid, String node) {
064        this(jid, node, null);
065    }
066
067    /**
068     * Get the JID.
069     *
070     * @return the JID
071     */
072    public Jid getJid() {
073        return jid;
074    }
075
076    /**
077     * Get the node.
078     *
079     * @return the node
080     */
081    public String getNode() {
082        return node;
083    }
084
085    /**
086     * Get the publish options.
087     *
088     * @return the publish options
089     */
090    public HashMap<String, String> getPublishOptions() {
091        return publishOptions;
092    }
093
094    @Override
095    protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
096        xml.attribute("jid", jid);
097        xml.attribute("node", node);
098        xml.rightAngleBracket();
099
100        if (publishOptions != null) {
101            DataForm dataForm = new DataForm(DataForm.Type.submit);
102
103            FormField formTypeField = new FormField("FORM_TYPE");
104            formTypeField.addValue(PubSub.NAMESPACE + "#publish-options");
105            dataForm.addField(formTypeField);
106
107            Iterator<Map.Entry<String, String>> publishOptionsIterator = publishOptions.entrySet().iterator();
108            while (publishOptionsIterator.hasNext()) {
109                Map.Entry<String, String> pairVariableValue = publishOptionsIterator.next();
110                FormField field = new FormField(pairVariableValue.getKey());
111                field.addValue(pairVariableValue.getValue());
112                dataForm.addField(field);
113            }
114
115            xml.element(dataForm);
116        }
117
118        return xml;
119    }
120
121}