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