001/**
002 *
003 * Copyright the original author or authors
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.pubsub;
018
019import org.jivesoftware.smack.XMPPConnection;
020import org.jivesoftware.smack.packet.XmlElement;
021import org.jivesoftware.smack.util.Objects;
022import org.jivesoftware.smack.util.StringUtils;
023import org.jivesoftware.smack.util.XmlStringBuilder;
024
025import org.jivesoftware.smackx.pubsub.packet.PubSubNamespace;
026
027import org.jxmpp.jid.BareJid;
028
029/**
030 * Represents a affiliation between a user and a node, where the {@link Type} defines
031 * the type of affiliation.
032 *
033 * Affiliations are retrieved from the {@link PubSubManager#getAffiliations()} method, which
034 * gets affiliations for the calling user, based on the identity that is associated with
035 * the {@link XMPPConnection}.
036 *
037 * @author Robin Collier
038 */
039public class Affiliation implements XmlElement {
040    public static final String ELEMENT = "affiliation";
041
042    public enum AffiliationNamespace {
043        basic(PubSubElementType.AFFILIATIONS),
044        owner(PubSubElementType.AFFILIATIONS_OWNER),
045        ;
046        public final PubSubElementType type;
047
048        AffiliationNamespace(PubSubElementType type) {
049            this.type = type;
050        }
051
052        public static AffiliationNamespace fromXmlns(String xmlns) {
053            for (AffiliationNamespace affiliationsNamespace : AffiliationNamespace.values()) {
054                if (affiliationsNamespace.type.getNamespace().getXmlns().equals(xmlns)) {
055                    return affiliationsNamespace;
056                }
057            }
058            throw new IllegalArgumentException("Invalid affiliations namespace: " + xmlns);
059        }
060    }
061
062    private final BareJid jid;
063    private final String node;
064    private final Type affiliation;
065    private final AffiliationNamespace namespace;
066
067    public enum Type {
068        member, none, outcast, owner, publisher
069    }
070
071    /**
072     * Constructs an affiliation.
073     *
074     * @param node The node the user is affiliated with.
075     * @param affiliation the optional affiliation.
076     */
077    public Affiliation(String node, Type affiliation) {
078        this(node, affiliation, affiliation == null ? AffiliationNamespace.basic : AffiliationNamespace.owner);
079    }
080
081    /**
082     * Constructs an affiliation.
083     *
084     * @param node The node the user is affiliated with.
085     * @param affiliation the optional affiliation.
086     * @param namespace the affiliation's namespace.
087     */
088    public Affiliation(String node, Type affiliation, AffiliationNamespace namespace) {
089        this.node = StringUtils.requireNotNullNorEmpty(node, "node must not be null nor empty");
090        this.affiliation = affiliation;
091        this.jid = null;
092        this.namespace = Objects.requireNonNull(namespace);
093    }
094
095    /**
096     * Construct a affiliation modification request.
097     *
098     * @param jid TODO javadoc me please
099     * @param affiliation TODO javadoc me please
100     */
101    public Affiliation(BareJid jid, Type affiliation) {
102        this(jid, affiliation, AffiliationNamespace.owner);
103    }
104
105    public Affiliation(BareJid jid, Type affiliation, AffiliationNamespace namespace) {
106        this.jid = jid;
107        this.affiliation = affiliation;
108        this.node = null;
109        // This is usually the pubsub#owner namespace, but see xep60 example 208 where just 'pubsub' is used
110        // ("notification of affiliation change")
111        this.namespace = namespace;
112    }
113
114    /**
115     * Get the node.
116     *
117     * @return the node.
118     * @deprecated use {@link #getNode} instead.
119     */
120    @Deprecated
121    public String getNodeId() {
122        return getNode();
123    }
124
125    public String getNode() {
126        return node;
127    }
128
129    /**
130     * Get the type.
131     *
132     * @return the type.
133     * @deprecated use {@link #getAffiliation()} instead.
134     */
135    @Deprecated
136    public Type getType() {
137        return getAffiliation();
138    }
139
140    public Type getAffiliation() {
141        return affiliation;
142    }
143
144    public BareJid getJid() {
145        return jid;
146    }
147
148    @Override
149    public String getElementName() {
150        return ELEMENT;
151    }
152
153    @Override
154    public String getNamespace() {
155        return getPubSubNamespace().getXmlns();
156    }
157
158    public PubSubNamespace getPubSubNamespace() {
159        return namespace.type.getNamespace();
160    }
161
162    /**
163     * Check if this is an affiliation element to modify affiliations on a node.
164     *
165     * @return <code>true</code> if this is an affiliation element to modify affiliations on a node, <code>false</code> otherwise.
166     * @since 4.2
167     */
168    public boolean isAffiliationModification() {
169        if (jid != null && affiliation != null) {
170            assert node == null && namespace == AffiliationNamespace.owner;
171            return true;
172        }
173        return false;
174    }
175
176    @Override
177    public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
178        XmlStringBuilder xml = new XmlStringBuilder(this);
179        xml.optAttribute("node", node);
180        xml.optAttribute("jid", jid);
181        xml.optAttribute("affiliation", affiliation);
182        xml.closeEmptyElement();
183        return xml;
184    }
185}