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