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.List;
021
022import org.jivesoftware.smack.packet.IQ;
023
024import org.jivesoftware.smackx.muclight.MUCLightAffiliation;
025import org.jivesoftware.smackx.muclight.MUCLightRoomConfiguration;
026import org.jivesoftware.smackx.muclight.MultiUserChatLight;
027import org.jivesoftware.smackx.muclight.element.MUCLightElements.ConfigurationElement;
028import org.jivesoftware.smackx.muclight.element.MUCLightElements.OccupantsElement;
029
030import org.jxmpp.jid.EntityJid;
031import org.jxmpp.jid.Jid;
032
033/**
034 * MUCLight create IQ class.
035 *
036 * @author Fernando Ramirez
037 *
038 */
039public class MUCLightCreateIQ extends IQ {
040
041    public static final String ELEMENT = QUERY_ELEMENT;
042    public static final String NAMESPACE = MultiUserChatLight.NAMESPACE + MultiUserChatLight.CREATE;
043
044    private MUCLightRoomConfiguration configuration;
045    private final HashMap<Jid, MUCLightAffiliation> occupants;
046
047    /**
048     * MUCLight create IQ constructor.
049     *
050     * @param room
051     * @param roomName
052     * @param subject
053     * @param customConfigs
054     * @param occupants
055     */
056    public MUCLightCreateIQ(EntityJid room, String roomName, String subject, HashMap<String, String> customConfigs,
057            List<Jid> occupants) {
058        super(ELEMENT, NAMESPACE);
059        this.configuration = new MUCLightRoomConfiguration(roomName, subject, customConfigs);
060
061        this.occupants = new HashMap<>();
062        for (Jid occupant : occupants) {
063            this.occupants.put(occupant, MUCLightAffiliation.member);
064        }
065
066        this.setType(Type.set);
067        this.setTo(room);
068    }
069
070    /**
071     * MUCLight create IQ constructor.
072     *
073     * @param room
074     * @param roomName
075     * @param occupants
076     */
077    public MUCLightCreateIQ(EntityJid room, String roomName, List<Jid> occupants) {
078        this(room, roomName, null, null, occupants);
079    }
080
081    /**
082     * Get the room configuration.
083     *
084     * @return the room configuration
085     */
086    public MUCLightRoomConfiguration getConfiguration() {
087        return configuration;
088    }
089
090    /**
091     * Get the room occupants.
092     *
093     * @return the room occupants
094     */
095    public HashMap<Jid, MUCLightAffiliation> getOccupants() {
096        return occupants;
097    }
098
099    @Override
100    protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
101        xml.rightAngleBracket();
102        xml.element(new ConfigurationElement(configuration));
103
104        if (!occupants.isEmpty()) {
105            xml.element(new OccupantsElement(occupants));
106        }
107
108        return xml;
109    }
110
111}