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