aboutsummaryrefslogtreecommitdiff
path: root/src/org/jivesoftware/smackx/pubsub/ItemsExtension.java
blob: c98d93af08b9e6a86748f311a37f6bf4a38a9dd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
 * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jivesoftware.smackx.pubsub;

import java.util.List;

import org.jivesoftware.smack.packet.PacketExtension;

/**
 * This class is used to for multiple purposes.  
 * <li>It can represent an event containing a list of items that have been published
 * <li>It can represent an event containing a list of retracted (deleted) items.
 * <li>It can represent a request to delete a list of items.
 * <li>It can represent a request to get existing items.
 * 
 * <p><b>Please note, this class is used for internal purposes, and is not required for usage of 
 * pubsub functionality.</b>
 * 
 * @author Robin Collier
 */
public class ItemsExtension extends NodeExtension implements EmbeddedPacketExtension
{
	protected ItemsElementType type;
	protected Boolean notify;
	protected List<? extends PacketExtension> items;

	public enum ItemsElementType
	{
		/** An items element, which has an optional <b>max_items</b> attribute when requesting items */
		items(PubSubElementType.ITEMS, "max_items"),
		
		/** A retract element, which has an optional <b>notify</b> attribute when publishing deletions */
		retract(PubSubElementType.RETRACT, "notify");
		
		private PubSubElementType elem;
		private String att;
		
		private ItemsElementType(PubSubElementType nodeElement, String attribute)
		{
			elem = nodeElement;
			att = attribute;
		}
		
		public PubSubElementType getNodeElement()
		{
			return elem;
		}

		public String getElementAttribute()
		{
			return att;
		}
	}

	/**
	 * Construct an instance with a list representing items that have been published or deleted.
	 * 
	 * <p>Valid scenarios are:
	 * <li>Request items from node - itemsType = {@link ItemsElementType#items}, items = list of {@link Item} and an
	 * optional value for the <b>max_items</b> attribute.
	 * <li>Request to delete items - itemsType = {@link ItemsElementType#retract}, items = list of {@link Item} containing
	 * only id's and an optional value for the <b>notify</b> attribute.
	 * <li>Items published event - itemsType = {@link ItemsElementType#items}, items = list of {@link Item} and 
	 * attributeValue = <code>null</code>
	 * <li>Items deleted event -  itemsType = {@link ItemsElementType#items}, items = list of {@link RetractItem} and 
	 * attributeValue = <code>null</code> 
	 * 
	 * @param itemsType Type of representation
	 * @param nodeId The node to which the items are being sent or deleted
	 * @param items The list of {@link Item} or {@link RetractItem}
	 * @param attributeValue The value of the <b>max_items</b>  
	 */
	public ItemsExtension(ItemsElementType itemsType, String nodeId, List<? extends PacketExtension> items)
	{
		super(itemsType.getNodeElement(), nodeId);
		type = itemsType;
		this.items = items;
	}
	
	/**
	 * Construct an instance with a list representing items that have been published or deleted.
	 * 
	 * <p>Valid scenarios are:
	 * <li>Request items from node - itemsType = {@link ItemsElementType#items}, items = list of {@link Item} and an
	 * optional value for the <b>max_items</b> attribute.
	 * <li>Request to delete items - itemsType = {@link ItemsElementType#retract}, items = list of {@link Item} containing
	 * only id's and an optional value for the <b>notify</b> attribute.
	 * <li>Items published event - itemsType = {@link ItemsElementType#items}, items = list of {@link Item} and 
	 * attributeValue = <code>null</code>
	 * <li>Items deleted event -  itemsType = {@link ItemsElementType#items}, items = list of {@link RetractItem} and 
	 * attributeValue = <code>null</code> 
	 * 
	 * @param itemsType Type of representation
	 * @param nodeId The node to which the items are being sent or deleted
	 * @param items The list of {@link Item} or {@link RetractItem}
	 * @param attributeValue The value of the <b>max_items</b>  
	 */
	public ItemsExtension(String nodeId, List<? extends PacketExtension> items, boolean notify)
	{
		super(ItemsElementType.retract.getNodeElement(), nodeId);
		type = ItemsElementType.retract;
		this.items = items; 
		this.notify = notify;
	}
	
	/**
	 * Get the type of element
	 * 
	 * @return The element type
	 */
	public ItemsElementType getItemsElementType()
	{
		return type;
	}
	
	public List<PacketExtension> getExtensions()
	{
		return (List<PacketExtension>)getItems();
	}
	
	/**
	 * Gets the items related to the type of request or event.
	 * 
	 * return List of {@link Item}, {@link RetractItem}, or null
	 */
	public List<? extends PacketExtension> getItems()
	{
		return items;
	}

	/**
	 * Gets the value of the optional attribute related to the {@link ItemsElementType}.
	 * 
	 * @return The attribute value
	 */
	public boolean getNotify()
	{
		return notify;
	}
	
	@Override
	public String toXML()
	{
		if ((items == null) || (items.size() == 0))
		{
			return super.toXML();
		}
		else
		{
			StringBuilder builder = new StringBuilder("<");
			builder.append(getElementName());
			builder.append(" node='");
			builder.append(getNode());
			
			if (notify != null)
			{
				builder.append("' ");
				builder.append(type.getElementAttribute());
				builder.append("='");
				builder.append(notify.equals(Boolean.TRUE) ? 1 : 0);
				builder.append("'>");
			}
			else
			{
				builder.append("'>");
				for (PacketExtension item : items)
				{
					builder.append(item.toXML());
				}
			}
			
			builder.append("</");
			builder.append(getElementName());
			builder.append(">");
			return builder.toString();
		}
	}

	@Override
	public String toString()
	{
		return getClass().getName() + "Content [" + toXML() + "]";
	}

}