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