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.muclight.element;
018
019import java.util.HashMap;
020import java.util.Iterator;
021import java.util.Map;
022
023import org.jivesoftware.smack.packet.IQ;
024
025import org.jivesoftware.smackx.muclight.MUCLightAffiliation;
026import org.jivesoftware.smackx.muclight.MultiUserChatLight;
027import org.jivesoftware.smackx.muclight.element.MUCLightElements.UserWithAffiliationElement;
028
029import org.jxmpp.jid.Jid;
030
031/**
032 * MUCLight change affiliations IQ class.
033 *
034 * @author Fernando Ramirez
035 *
036 */
037public class MUCLightChangeAffiliationsIQ extends IQ {
038
039    public static final String ELEMENT = QUERY_ELEMENT;
040    public static final String NAMESPACE = MultiUserChatLight.NAMESPACE + MultiUserChatLight.AFFILIATIONS;
041
042    private HashMap<Jid, MUCLightAffiliation> affiliations;
043
044    /**
045     * MUCLight change affiliations IQ constructor.
046     *
047     * @param room TODO javadoc me please
048     * @param affiliations TODO javadoc me please
049     */
050    public MUCLightChangeAffiliationsIQ(Jid room, HashMap<Jid, MUCLightAffiliation> affiliations) {
051        super(ELEMENT, NAMESPACE);
052        this.setType(Type.set);
053        this.setTo(room);
054        this.affiliations = affiliations;
055    }
056
057    /**
058     * Get the affiliations.
059     *
060     * @return the affiliations
061     */
062    public HashMap<Jid, MUCLightAffiliation> getAffiliations() {
063        return affiliations;
064    }
065
066    @Override
067    protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
068        xml.rightAngleBracket();
069
070        if (affiliations != null) {
071            Iterator<Map.Entry<Jid, MUCLightAffiliation>> it = affiliations.entrySet().iterator();
072            while (it.hasNext()) {
073                Map.Entry<Jid, MUCLightAffiliation> pair = it.next();
074                xml.append(new UserWithAffiliationElement(pair.getKey(), pair.getValue()));
075            }
076        }
077
078        return xml;
079    }
080
081}