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