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.Iterator;
020import java.util.Map;
021
022import org.jivesoftware.smack.packet.IQ;
023
024import org.jivesoftware.smackx.muclight.MultiUserChatLight;
025import org.jivesoftware.smackx.muclight.element.MUCLightElements.BlockingElement;
026
027import org.jxmpp.jid.Jid;
028
029/**
030 * MUC Light blocking IQ class.
031 *
032 * @author Fernando Ramirez
033 *
034 */
035public class MUCLightBlockingIQ extends IQ {
036
037    public static final String ELEMENT = QUERY_ELEMENT;
038    public static final String NAMESPACE = MultiUserChatLight.NAMESPACE + MultiUserChatLight.BLOCKING;
039
040    private final Map<Jid, Boolean> rooms;
041    private final Map<Jid, Boolean> users;
042
043    /**
044     * MUC Light blocking IQ constructor.
045     *
046     * @param rooms TODO javadoc me please
047     * @param users TODO javadoc me please
048     */
049    public MUCLightBlockingIQ(Map<Jid, Boolean> rooms, Map<Jid, Boolean> users) {
050        super(ELEMENT, NAMESPACE);
051        this.rooms = rooms;
052        this.users = users;
053    }
054
055    /**
056     * Get rooms JIDs with booleans (true if allow, false if deny).
057     *
058     * @return the rooms JIDs with booleans (true if allow, false if deny)
059     */
060    public Map<Jid, Boolean> getRooms() {
061        return rooms;
062    }
063
064    /**
065     * Get users JIDs with booleans (true if allow, false if deny).
066     *
067     * @return the users JIDs with booleans (true if allow, false if deny)
068     */
069    public Map<Jid, Boolean> getUsers() {
070        return users;
071    }
072
073    @Override
074    protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
075        xml.rightAngleBracket();
076
077        if (rooms != null) {
078            parseBlocking(xml, rooms, true);
079        }
080
081        if (users != null) {
082            parseBlocking(xml, users, false);
083        }
084
085        return xml;
086    }
087
088    private static void parseBlocking(IQChildElementXmlStringBuilder xml, Map<Jid, Boolean> map, boolean isRoom) {
089        Iterator<Map.Entry<Jid, Boolean>> it = map.entrySet().iterator();
090        while (it.hasNext()) {
091            Map.Entry<Jid, Boolean> pair = it.next();
092            xml.append(new BlockingElement(pair.getKey(), pair.getValue(), isRoom));
093        }
094    }
095
096}