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.PacketExtension;
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 PacketExtension
033{
034        protected String node;
035        protected Type type;
036        
037        public enum Type
038        {
039                member, none, outcast, owner, publisher
040        }
041
042        /**
043         * Constructs an affiliation.
044         * 
045         * @param nodeId The node the user is affiliated with.
046         * @param affiliation The type of affiliation.
047         */
048        public Affiliation(String nodeId, Type affiliation)
049        {
050                node = nodeId;
051                type = affiliation;
052        }
053        
054        public String getNodeId()
055        {
056                return node;
057        }
058        
059        public Type getType()
060        {
061                return type;
062        }
063        
064        public String getElementName()
065        {
066                return "subscription";
067        }
068
069        public String getNamespace()
070        {
071                return null;
072        }
073
074        public String toXML()
075        {
076                StringBuilder builder = new StringBuilder("<");
077                builder.append(getElementName());
078                appendAttribute(builder, "node", node);
079                appendAttribute(builder, "affiliation", type.toString());
080                
081                builder.append("/>");
082                return builder.toString();
083        }
084
085        private void appendAttribute(StringBuilder builder, String att, String value)
086        {
087                builder.append(" ");
088                builder.append(att);
089                builder.append("='");
090                builder.append(value);
091                builder.append("'");
092        }
093}