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    /**
039     * the MAM query IQ NAMESPACE.
040     */
041    public static final String NAMESPACE = MamElements.NAMESPACE;
042
043    private final String queryId;
044    private final String node;
045    private final DataForm dataForm;
046
047    /**
048     * MAM query IQ constructor.
049     *
050     * @param queryId TODO javadoc me please
051     */
052    public MamQueryIQ(String queryId) {
053        this(queryId, null, null);
054        setType(IQ.Type.get);
055    }
056
057    /**
058     * MAM query IQ constructor.
059     *
060     * @param form TODO javadoc me please
061     */
062    public MamQueryIQ(DataForm form) {
063        this(null, null, form);
064    }
065
066    /**
067     * MAM query IQ constructor.
068     *
069     * @param queryId TODO javadoc me please
070     * @param form TODO javadoc me please
071     */
072    public MamQueryIQ(String queryId, DataForm form) {
073        this(queryId, null, form);
074    }
075
076    /**
077     * MAM query IQ constructor.
078     *
079     * @param queryId TODO javadoc me please
080     * @param node TODO javadoc me please
081     * @param dataForm TODO javadoc me please
082     */
083    public MamQueryIQ(String queryId, String node, DataForm dataForm) {
084        super(ELEMENT, NAMESPACE);
085        this.queryId = queryId;
086        this.node = node;
087        this.dataForm = dataForm;
088
089        if (dataForm != null) {
090            String formType = dataForm.getFormType();
091            if (formType == null) {
092                throw new IllegalArgumentException("If a data form is given it must posses a hidden form type field");
093            }
094            if (!formType.equals(MamElements.NAMESPACE)) {
095                throw new IllegalArgumentException(
096                        "Value of the hidden form type field must be '" + MamElements.NAMESPACE + "'");
097            }
098            addExtension(dataForm);
099        }
100    }
101
102    /**
103     * Get query id.
104     *
105     * @return the query id
106     */
107    public String getQueryId() {
108        return queryId;
109    }
110
111    /**
112     * Get the Node name.
113     *
114     * @return the node
115     */
116    public String getNode() {
117      return node;
118    }
119
120    /**
121     * Get the data form.
122     *
123     * @return the data form
124     */
125    public DataForm getDataForm() {
126        return dataForm;
127    }
128
129    @Override
130    protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
131        xml.optAttribute("queryid", queryId);
132        xml.optAttribute("node", node);
133        xml.rightAngleBracket();
134        return xml;
135    }
136
137}