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    @SuppressWarnings("this-escape")
049    public MamQueryIQ(MamVersion version, String queryId) {
050        this(version, queryId, null, null);
051        setType(IQ.Type.get);
052    }
053
054    /**
055     * MAM query IQ constructor.
056     *
057     * @param version TODO javadoc me please
058     * @param form TODO javadoc me please
059     */
060    public MamQueryIQ(MamVersion version, DataForm form) {
061        this(version, null, null, form);
062    }
063
064    /**
065     * MAM query IQ constructor.
066     *
067     * @param version TODO javadoc me please
068     * @param queryId TODO javadoc me please
069     * @param form TODO javadoc me please
070     */
071    public MamQueryIQ(MamVersion version, String queryId, DataForm form) {
072        this(version, queryId, null, form);
073    }
074
075    /**
076     * MAM query IQ constructor.
077     *
078     * @param version TODO javadoc me please
079     * @param queryId TODO javadoc me please
080     * @param node TODO javadoc me please
081     * @param dataForm TODO javadoc me please
082     */
083    @SuppressWarnings("this-escape")
084    public MamQueryIQ(MamVersion version, String queryId, String node, DataForm dataForm) {
085        super(ELEMENT, version.getNamespace());
086        this.queryId = queryId;
087        this.node = node;
088        this.dataForm = dataForm;
089
090        if (dataForm != null) {
091            String formType = dataForm.getFormType();
092            if (formType == null) {
093                throw new IllegalArgumentException("If a data form is given it must posses a hidden form type field");
094            }
095            if (!formType.equals(version.getNamespace())) {
096                throw new IllegalArgumentException(
097                        "Value of the hidden form type field must be '" + version.getNamespace() + "'");
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}