001/**
002 *
003 * Copyright © 2016 Florian Schmaus and 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.mam.element;
018
019import org.jivesoftware.smack.packet.IQ;
020
021import org.jivesoftware.smackx.xdata.FormField;
022import org.jivesoftware.smackx.xdata.packet.DataForm;
023
024/**
025 * MAM Query IQ class.
026 *
027 * @see <a href="http://xmpp.org/extensions/xep-0313.html">XEP-0313: Message
028 *      Archive Management</a>
029 * @author Fernando Ramirez and Florian Schmaus
030 *
031 */
032public class MamQueryIQ extends IQ {
033
034    /**
035     * the MAM query IQ element.
036     */
037    public static final String ELEMENT = QUERY_ELEMENT;
038
039    /**
040     * the MAM query IQ NAMESPACE.
041     */
042    public static final String NAMESPACE = MamElements.NAMESPACE;
043
044    private final String queryId;
045    private final String node;
046    private final DataForm dataForm;
047
048    /**
049     * MAM query IQ constructor.
050     *
051     * @param queryId
052     */
053    public MamQueryIQ(String queryId) {
054        this(queryId, null, null);
055        setType(IQ.Type.get);
056    }
057
058    /**
059     * MAM query IQ constructor.
060     *
061     * @param form
062     */
063    public MamQueryIQ(DataForm form) {
064        this(null, null, form);
065    }
066
067    /**
068     * MAM query IQ constructor.
069     *
070     * @param queryId
071     * @param form
072     */
073    public MamQueryIQ(String queryId, DataForm form) {
074        this(queryId, null, form);
075    }
076
077    /**
078     * MAM query IQ constructor.
079     *
080     * @param queryId
081     * @param node
082     * @param dataForm
083     */
084    public MamQueryIQ(String queryId, String node, DataForm dataForm) {
085        super(ELEMENT, NAMESPACE);
086        this.queryId = queryId;
087        this.node = node;
088        this.dataForm = dataForm;
089
090        if (dataForm != null) {
091            FormField field = dataForm.getHiddenFormTypeField();
092            if (field == null) {
093                throw new IllegalArgumentException("If a data form is given it must posses a hidden form type field");
094            }
095            if (!field.getValues().get(0).equals(MamElements.NAMESPACE)) {
096                throw new IllegalArgumentException(
097                        "Value of the hidden form type field must be '" + MamElements.NAMESPACE + "'");
098            }
099            addExtension(dataForm);
100        }
101    }
102
103    /**
104     * Get query id.
105     *
106     * @return the query id
107     */
108    public String getQueryId() {
109        return queryId;
110    }
111
112    /**
113     * Get the Node name.
114     *
115     * @return the node
116     */
117    public String getNode() {
118      return node;
119    }
120
121    /**
122     * Get the data form.
123     *
124     * @return the data form
125     */
126    public DataForm getDataForm() {
127        return dataForm;
128    }
129
130    @Override
131    protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
132        xml.optAttribute("queryid", queryId);
133        xml.optAttribute("node", node);
134        xml.rightAngleBracket();
135        return xml;
136    }
137
138}