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