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.ExtensionElement;
021
022/**
023 * Represents a affiliation between a user and a node, where the {@link #type} defines 
024 * the type of affiliation.
025 * 
026 * Affiliations are retrieved from the {@link PubSubManager#getAffiliations()} method, which 
027 * gets affiliations for the calling user, based on the identity that is associated with 
028 * the {@link XMPPConnection}.
029 * 
030 * @author Robin Collier
031 */
032public class Affiliation implements ExtensionElement
033{
034    public static final String ELEMENT = "affiliation";
035
036        protected String node;
037        protected Type type;
038        
039        public enum Type
040        {
041                member, none, outcast, owner, publisher
042        }
043
044        /**
045         * Constructs an affiliation.
046         * 
047         * @param nodeId The node the user is affiliated with.
048         * @param affiliation The type of affiliation.
049         */
050        public Affiliation(String nodeId, Type affiliation)
051        {
052                node = nodeId;
053                type = affiliation;
054        }
055        
056        public String getNodeId()
057        {
058                return node;
059        }
060        
061        public Type getType()
062        {
063                return type;
064        }
065        
066        public String getElementName()
067        {
068                return ELEMENT;
069        }
070
071        public String getNamespace()
072        {
073                return null;
074        }
075
076        public String toXML()
077        {
078                StringBuilder builder = new StringBuilder("<");
079                builder.append(getElementName());
080                appendAttribute(builder, "node", node);
081                appendAttribute(builder, "affiliation", type.toString());
082                
083                builder.append("/>");
084                return builder.toString();
085        }
086
087        private void appendAttribute(StringBuilder builder, String att, String value)
088        {
089                builder.append(" ");
090                builder.append(att);
091                builder.append("='");
092                builder.append(value);
093                builder.append("'");
094        }
095}