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