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