1d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen/**
2d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
3d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * you may not use this file except in compliance with the License.
4d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * You may obtain a copy of the License at
5d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen *
6d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen *     http://www.apache.org/licenses/LICENSE-2.0
7d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen *
8d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * Unless required by applicable law or agreed to in writing, software
9d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * distributed under the License is distributed on an "AS IS" BASIS,
10d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * See the License for the specific language governing permissions and
12d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * limitations under the License.
13d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen */
14d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenpackage org.jivesoftware.smackx.pubsub;
15d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen
16d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenimport org.jivesoftware.smack.packet.Message;
17d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenimport org.jivesoftware.smackx.pubsub.provider.ItemProvider;
18d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen
19d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen/**
20d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * This class represents an item that has been, or will be published to a
21d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * pubsub node.  An <tt>Item</tt> has several properties that are dependent
22d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * on the configuration of the node to which it has been or will be published.
23d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen *
24d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * <h1>An Item received from a node (via {@link LeafNode#getItems()} or {@link LeafNode#addItemEventListener(org.jivesoftware.smackx.pubsub.listener.ItemEventListener)}</b>
25d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * <li>Will always have an id (either user or server generated) unless node configuration has both
26d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * {@link ConfigureForm#isPersistItems()} and {@link ConfigureForm#isDeliverPayloads()}set to false.
27d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * <li>Will have a payload if the node configuration has {@link ConfigureForm#isDeliverPayloads()} set
28d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * to true, otherwise it will be null.
29d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen *
30d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * <h1>An Item created to send to a node (via {@link LeafNode#send()} or {@link LeafNode#publish()}</b>
31d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * <li>The id is optional, since the server will generate one if necessary, but should be used if it is
32d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * meaningful in the context of the node.  This value must be unique within the node that it is sent to, since
33d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * resending an item with the same id will overwrite the one that already exists if the items are persisted.
34d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * <li>Will require payload if the node configuration has {@link ConfigureForm#isDeliverPayloads()} set
35d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * to true.
36d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen *
37d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * <p>To customise the payload object being returned from the {@link #getPayload()} method, you can
38d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * add a custom parser as explained in {@link ItemProvider}.
39d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen *
40d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen * @author Robin Collier
41d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen */
42d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenpublic class Item extends NodeExtension
43d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen{
44d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen	private String id;
45d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen
46d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen	/**
47d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen	 * Create an empty <tt>Item</tt> with no id.  This is a valid item for nodes which are configured
48d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen	 * so that {@link ConfigureForm#isDeliverPayloads()} is false.  In most cases an id will be generated by the server.
49d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen	 * For nodes configured with {@link ConfigureForm#isDeliverPayloads()} and {@link ConfigureForm#isPersistItems()}
50d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen	 * set to false, no <tt>Item</tt> is sent to the node, you have to use {@link LeafNode#send()} or {@link LeafNode#publish()}
51d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen	 * methods in this case.
52d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen	 */
53d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen	public Item()
54d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen	{
55d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen		super(PubSubElementType.ITEM);
56d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen	}
57d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen
58d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen	/**
59d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen	 * Create an <tt>Item</tt> with an id but no payload.  This is a valid item for nodes which are configured
60d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen	 * so that {@link ConfigureForm#isDeliverPayloads()} is false.
61d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen	 *
62d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen	 * @param itemId The id if the item.  It must be unique within the node unless overwriting and existing item.
63d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen	 * Passing null is the equivalent of calling {@link #Item()}.
64d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen	 */
65d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen	public Item(String itemId)
66d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen	{
67d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen		// The element type is actually irrelevant since we override getNamespace() to return null
68d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen		super(PubSubElementType.ITEM);
69d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen		id = itemId;
70d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen	}
71d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen
72d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen	/**
73d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen	 * Create an <tt>Item</tt> with an id and a node id.
74d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen	 * <p>
75d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen	 * <b>Note:</b> This is not valid for publishing an item to a node, only receiving from
76d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen	 * one as part of {@link Message}.  If used to create an Item to publish
77d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen	 * (via {@link LeafNode#publish(Item)}, the server <i>may</i> return an
78d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen	 * error for an invalid packet.
79d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen	 *
80d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen	 * @param itemId The id of the item.
81d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen	 * @param nodeId The id of the node which the item was published to.
82d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen	 */
83d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen    public Item(String itemId, String nodeId)
84d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen    {
85d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen    	super(PubSubElementType.ITEM_EVENT, nodeId);
86d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen        id = itemId;
87d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen    }
88d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen
89d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen	/**
90d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen	 * Get the item id.  Unique to the node it is associated with.
91d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen	 *
92d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen	 * @return The id
93d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen	 */
94d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen	public String getId()
95d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen	{
96d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen		return id;
97d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen	}
98d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen
99d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen	@Override
100d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen	public String getNamespace()
101d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen	{
102d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen		return null;
103d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen	}
104d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen
105d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen	@Override
106d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen	public String toXML()
107d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen	{
108d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen		StringBuilder builder = new StringBuilder("<item");
109d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen
110d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen		if (id != null)
111d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen		{
112d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen			builder.append(" id='");
113d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen			builder.append(id);
114d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen			builder.append("'");
115d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen		}
116d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen
117d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen        if (getNode() != null) {
118d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen            builder.append(" node='");
119d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen            builder.append(getNode());
120d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen            builder.append("'");
121d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen        }
122d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen		builder.append("/>");
123d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen
124d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen		return builder.toString();
125d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen	}
126d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen
127d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen	@Override
128d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen	public String toString()
129d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen	{
130d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen		return getClass().getName() + " | Content [" + toXML() + "]";
131d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen	}
132d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen}
133