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.packet.Message;
020import org.jivesoftware.smackx.pubsub.provider.ItemProvider;
021
022/**
023 * This class represents an item that has been, or will be published to a
024 * pubsub node.  An <tt>Item</tt> has several properties that are dependent
025 * on the configuration of the node to which it has been or will be published.
026 * 
027 * <h3>An Item received from a node (via {@link LeafNode#getItems()} or {@link LeafNode#addItemEventListener(org.jivesoftware.smackx.pubsub.listener.ItemEventListener)}</h3>
028 * <ul>
029 * <li>Will always have an id (either user or server generated) unless node configuration has both
030 * {@link ConfigureForm#isPersistItems()} and {@link ConfigureForm#isDeliverPayloads()}set to false.
031 * <li>Will have a payload if the node configuration has {@link ConfigureForm#isDeliverPayloads()} set 
032 * to true, otherwise it will be null.
033 * </ul>
034 * 
035 * <h3>An Item created to send to a node (via {@link LeafNode#send()} or {@link LeafNode#publish()}</h3>
036 * <ul>
037 * <li>The id is optional, since the server will generate one if necessary, but should be used if it is 
038 * meaningful in the context of the node.  This value must be unique within the node that it is sent to, since
039 * resending an item with the same id will overwrite the one that already exists if the items are persisted.
040 * <li>Will require payload if the node configuration has {@link ConfigureForm#isDeliverPayloads()} set
041 * to true.
042 * </ul>
043 * 
044 * <p>
045 * To customise the payload object being returned from the {@link PayloadItem#getPayload()} method, you can
046 * add a custom parser as explained in {@link ItemProvider}.
047 * </p>
048 * 
049 * @author Robin Collier
050 */
051public class Item extends NodeExtension
052{
053    private String id;
054
055    /**
056     * Create an empty <tt>Item</tt> with no id.  This is a valid item for nodes which are configured
057     * so that {@link ConfigureForm#isDeliverPayloads()} is false.  In most cases an id will be generated by the server.
058     * For nodes configured with {@link ConfigureForm#isDeliverPayloads()} and {@link ConfigureForm#isPersistItems()}
059     * set to false, no <tt>Item</tt> is sent to the node, you have to use {@link LeafNode#send()} or {@link LeafNode#publish()}
060     * methods in this case.
061     */
062    public Item()
063    {
064        super(PubSubElementType.ITEM);
065    }
066
067    /**
068     * Create an <tt>Item</tt> with an id but no payload.  This is a valid item for nodes which are configured
069     * so that {@link ConfigureForm#isDeliverPayloads()} is false.
070     *
071     * @param itemId The id if the item.  It must be unique within the node unless overwriting and existing item.
072     * Passing null is the equivalent of calling {@link #Item()}.
073     */
074    public Item(String itemId)
075    {
076        // The element type is actually irrelevant since we override getNamespace() to return null
077        super(PubSubElementType.ITEM);
078        id = itemId;
079    }
080
081    /**
082     * Create an <tt>Item</tt> with an id and a node id.
083     * <p>
084     * <b>Note:</b> This is not valid for publishing an item to a node, only receiving from
085     * one as part of {@link Message}.  If used to create an Item to publish
086     * (via {@link LeafNode#publish(Item)}, the server <i>may</i> return an
087     * error for an invalid packet.
088     *
089     * @param itemId The id of the item.
090     * @param nodeId The id of the node which the item was published to.
091     */
092    public Item(String itemId, String nodeId)
093    {
094        super(PubSubElementType.ITEM_EVENT, nodeId);
095        id = itemId;
096    }
097
098    /**
099     * Get the item id.  Unique to the node it is associated with.
100     *
101     * @return The id
102     */
103    public String getId()
104    {
105        return id;
106    }
107
108    @Override
109    public String getNamespace()
110    {
111        return null;
112    }
113
114    @Override
115    public String toXML()
116    {
117        StringBuilder builder = new StringBuilder("<item");
118
119        if (id != null)
120        {
121            builder.append(" id='");
122            builder.append(id);
123            builder.append('\'');
124        }
125
126        if (getNode() != null) {
127            builder.append(" node='");
128            builder.append(getNode());
129            builder.append('\'');
130        }
131        builder.append("/>");
132
133        return builder.toString();
134    }
135
136    @Override
137    public String toString()
138    {
139        return getClass().getName() + " | Content [" + toXML() + "]";
140    }
141}