1/**
2 * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
3 * you may not use this file except in compliance with the License.
4 * You may obtain a copy of the License at
5 *
6 *     http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 */
14package org.jivesoftware.smackx.pubsub;
15
16import org.jivesoftware.smack.Connection;
17import org.jivesoftware.smack.packet.PacketExtension;
18
19/**
20 * Represents a affiliation between a user and a node, where the {@link #type} defines
21 * the type of affiliation.
22 *
23 * Affiliations are retrieved from the {@link PubSubManager#getAffiliations()} method, which
24 * gets affiliations for the calling user, based on the identity that is associated with
25 * the {@link Connection}.
26 *
27 * @author Robin Collier
28 */
29public class Affiliation implements PacketExtension
30{
31	protected String jid;
32	protected String node;
33	protected Type type;
34
35	public enum Type
36	{
37		member, none, outcast, owner, publisher
38	}
39
40	/**
41	 * Constructs an affiliation.
42	 *
43	 * @param jid The JID with affiliation.
44	 * @param affiliation The type of affiliation.
45	 */
46	public Affiliation(String jid, Type affiliation)
47	{
48		this(jid, null, affiliation);
49	}
50
51	/**
52	 * Constructs an affiliation.
53	 *
54	 * @param jid The JID with affiliation.
55	 * @param node The node with affiliation.
56	 * @param affiliation The type of affiliation.
57	 */
58	public Affiliation(String jid, String node, Type affiliation)
59	{
60		this.jid = jid;
61		this.node = node;
62		type = affiliation;
63	}
64
65	public String getJid()
66	{
67		return jid;
68	}
69
70	public String getNode()
71	{
72		return node;
73	}
74
75	public Type getType()
76	{
77		return type;
78	}
79
80	public String getElementName()
81	{
82		return "affiliation";
83	}
84
85	public String getNamespace()
86	{
87		return null;
88	}
89
90	public String toXML()
91	{
92		StringBuilder builder = new StringBuilder("<");
93		builder.append(getElementName());
94		if (node != null)
95			appendAttribute(builder, "node", node);
96		appendAttribute(builder, "jid", jid);
97		appendAttribute(builder, "affiliation", type.toString());
98
99		builder.append("/>");
100		return builder.toString();
101	}
102
103	private void appendAttribute(StringBuilder builder, String att, String value)
104	{
105		builder.append(" ");
106		builder.append(att);
107		builder.append("='");
108		builder.append(value);
109		builder.append("'");
110	}
111}
112