001/**
002 *
003 * Copyright 2003-2007 Jive Software, 2014 Florian Schmaus
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.muc.packet;
018
019import org.jivesoftware.smack.packet.IQ;
020import org.jivesoftware.smack.packet.NamedElement;
021import org.jivesoftware.smack.util.XmlStringBuilder;
022import org.jivesoftware.smackx.muc.MUCAffiliation;
023import org.jivesoftware.smackx.muc.MUCRole;
024
025/**
026 * Item child that holds information about roles, affiliation, jids and nicks.
027 *
028 * @author Gaston Dombiak
029 */
030public class MUCItem implements NamedElement {
031    public static final String ELEMENT = IQ.ITEM;
032
033    private final MUCAffiliation affiliation;
034    private final MUCRole role;
035    private final String actor;
036    private final String reason;
037    private final String jid;
038    private final String nick;
039
040    public MUCItem(MUCAffiliation affiliation) {
041        this(affiliation, null, null, null, null, null);
042    }
043
044    public MUCItem(MUCRole role) {
045        this(null, role, null, null, null, null);
046    }
047
048    public MUCItem(MUCRole role, String nick) {
049        this(null, role, null, null, null, nick);
050    }
051
052    public MUCItem(MUCAffiliation affiliation, String jid, String reason) {
053        this(affiliation, null, null, reason, jid, null);
054    }
055
056    public MUCItem(MUCAffiliation affiliation, String jid) {
057        this(affiliation, null, null, null, jid, null);
058    }
059
060    public MUCItem(MUCRole role, String nick, String reason) {
061        this(null, role, null, reason, null, nick);
062    }
063
064    /**
065     * Creates a new item child.
066     * 
067     * @param affiliation the actor's affiliation to the room
068     * @param role the privilege level of an occupant within a room.
069     * @param actor
070     * @param reason
071     * @param jid
072     * @param nick
073     */
074    public MUCItem(MUCAffiliation affiliation, MUCRole role, String actor,
075                    String reason, String jid, String nick) {
076        this.affiliation = affiliation;
077        this.role = role;
078        this.actor = actor;
079        this.reason = reason;
080        this.jid = jid;
081        this.nick = nick;
082    }
083
084    /**
085     * Returns the actor (JID of an occupant in the room) that was kicked or banned.
086     * 
087     * @return the JID of an occupant in the room that was kicked or banned.
088     */
089    public String getActor() {
090        return actor;
091    }
092
093    /**
094     * Returns the reason for the item child. The reason is optional and could be used to explain
095     * the reason why a user (occupant) was kicked or banned.
096     * 
097     * @return the reason for the item child.
098     */
099    public String getReason() {
100        return reason;
101    }
102
103    /**
104     * Returns the occupant's affiliation to the room. The affiliation is a semi-permanent
105     * association or connection with a room. The possible affiliations are "owner", "admin",
106     * "member", and "outcast" (naturally it is also possible to have no affiliation). An
107     * affiliation lasts across a user's visits to a room.
108     * 
109     * @return the actor's affiliation to the room
110     */
111    public MUCAffiliation getAffiliation() {
112        return affiliation;
113    }
114
115    /**
116     * Returns the <room@service/nick> by which an occupant is identified within the context of a
117     * room. If the room is non-anonymous, the JID will be included in the item.
118     * 
119     * @return the room JID by which an occupant is identified within the room.
120     */
121    public String getJid() {
122        return jid;
123    }
124
125    /**
126     * Returns the new nickname of an occupant that is changing his/her nickname. The new nickname
127     * is sent as part of the unavailable presence.
128     * 
129     * @return the new nickname of an occupant that is changing his/her nickname.
130     */
131    public String getNick() {
132        return nick;
133    }
134
135    /**
136     * Returns the temporary position or privilege level of an occupant within a room. The possible
137     * roles are "moderator", "participant", "visitor" and "none" (it is also possible to have no defined
138     * role). A role lasts only for the duration of an occupant's visit to a room.
139     * 
140     * @return the privilege level of an occupant within a room.
141     */
142    public MUCRole getRole() {
143        return role;
144    }
145
146    public XmlStringBuilder toXML() {
147        XmlStringBuilder xml = new XmlStringBuilder(this);
148        xml.optAttribute("affiliation", getAffiliation());
149        xml.optAttribute("jid", getJid());
150        xml.optAttribute("nick", getNick());
151        xml.optAttribute("role", getRole());
152        xml.rightAngleBracket();
153        xml.optElement("reason", getReason());
154        if (getActor() != null) {
155            xml.halfOpenElement("actor").attribute("jid", getActor()).closeEmptyElement();
156        }
157        xml.closeElement(IQ.ITEM);
158        return xml;
159    }
160
161    @Override
162    public String getElementName() {
163        return ELEMENT;
164    }
165}