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 java.util.List;
020
021import org.jivesoftware.smack.packet.ExtensionElement;
022import org.jivesoftware.smack.packet.NamedElement;
023import org.jivesoftware.smack.util.XmlStringBuilder;
024
025/**
026 * This class is used for multiple purposes.
027 * <ul>
028 * <li>It can represent an event containing a list of items that have been published
029 * <li>It can represent an event containing a list of retracted (deleted) items.
030 * <li>It can represent a request to delete a list of items.
031 * <li>It can represent a request to get existing items.
032 * </ul>
033 *
034 * <p><b>Please note, this class is used for internal purposes, and is not required for usage of
035 * pubsub functionality.</b></p>
036 *
037 * @author Robin Collier
038 */
039public class ItemsExtension extends NodeExtension implements EmbeddedPacketExtension {
040    protected ItemsElementType type;
041    protected Boolean notify;
042    protected List<? extends NamedElement> items;
043
044    public enum ItemsElementType {
045        /** An items element, which has an optional <b>max_items</b> attribute when requesting items. */
046        items(PubSubElementType.ITEMS, "max_items"),
047
048        /** A retract element, which has an optional <b>notify</b> attribute when publishing deletions. */
049        retract(PubSubElementType.RETRACT, "notify");
050
051        private final PubSubElementType elem;
052        private final String att;
053
054        ItemsElementType(PubSubElementType nodeElement, String attribute) {
055            elem = nodeElement;
056            att = attribute;
057        }
058
059        public PubSubElementType getNodeElement() {
060            return elem;
061        }
062
063        public String getElementAttribute() {
064            return att;
065        }
066    }
067
068    /**
069     * Construct an instance with a list representing items that have been published or deleted.
070     *
071     * <p>Valid scenarios are:</p>
072     * <ul>
073     * <li>Request items from node - itemsType = {@link ItemsElementType#items}, items = list of {@link Item} and an
074     * optional value for the <b>max_items</b> attribute.
075     * <li>Request to delete items - itemsType = {@link ItemsElementType#retract}, items = list of {@link Item} containing
076     * only id's and an optional value for the <b>notify</b> attribute.
077     * <li>Items published event - itemsType = {@link ItemsElementType#items}, items = list of {@link Item} and
078     * attributeValue = <code>null</code>
079     * <li>Items deleted event -  itemsType = {@link ItemsElementType#items}, items = list of {@link RetractItem} and
080     * attributeValue = <code>null</code>
081     * </ul>
082     *
083     * @param itemsType Type of representation
084     * @param nodeId The node to which the items are being sent or deleted
085     * @param items The list of {@link Item} or {@link RetractItem}
086     */
087    public ItemsExtension(ItemsElementType itemsType, String nodeId, List<? extends NamedElement> items) {
088        super(itemsType.getNodeElement(), nodeId);
089        type = itemsType;
090        this.items = items;
091    }
092
093    /**
094     * Construct an instance with a list representing items that have been published or deleted.
095     *
096     * <p>Valid scenarios are:</p>
097     * <ul>
098     * <li>Request items from node - itemsType = {@link ItemsElementType#items}, items = list of {@link Item} and an
099     * optional value for the <b>max_items</b> attribute.
100     * <li>Request to delete items - itemsType = {@link ItemsElementType#retract}, items = list of {@link Item} containing
101     * only id's and an optional value for the <b>notify</b> attribute.
102     * <li>Items published event - itemsType = {@link ItemsElementType#items}, items = list of {@link Item} and
103     * attributeValue = <code>null</code>
104     * <li>Items deleted event -  itemsType = {@link ItemsElementType#items}, items = list of {@link RetractItem} and
105     * attributeValue = <code>null</code>
106     * </ul>
107     *
108     * @param nodeId The node to which the items are being sent or deleted
109     * @param items The list of {@link Item} or {@link RetractItem}
110     * @param notify TODO javadoc me please
111     */
112    public ItemsExtension(String nodeId, List<? extends ExtensionElement> items, boolean notify) {
113        super(ItemsElementType.retract.getNodeElement(), nodeId);
114        type = ItemsElementType.retract;
115        this.items = items;
116        this.notify = notify;
117    }
118
119    /**
120     * Get the type of element.
121     *
122     * @return The element type
123     */
124    public ItemsElementType getItemsElementType() {
125        return type;
126    }
127
128    @Override
129    @SuppressWarnings("unchecked")
130    public List<ExtensionElement> getExtensions() {
131        return (List<ExtensionElement>) getItems();
132    }
133
134    /**
135     * Gets the items related to the type of request or event.
136     *
137     * @return List of {@link Item}, {@link RetractItem}, or null
138     */
139    // TODO: Shouldn't this return List<Item>? Why is RetractItem not a subtype of item?
140    public List<? extends NamedElement> getItems() {
141        return items;
142    }
143
144    /**
145     * Gets the value of the optional attribute related to the {@link ItemsElementType}.
146     *
147     * @return The attribute value
148     */
149    public boolean getNotify() {
150        return notify;
151    }
152
153    @Override
154    protected void addXml(XmlStringBuilder xml) {
155        if ((items == null) || (items.size() == 0)) {
156            xml.closeEmptyElement();
157            return;
158        }
159
160        if (notify != null) {
161            xml.attribute(type.getElementAttribute(), notify);
162            xml.rightAngleBracket();
163        } else {
164            xml.rightAngleBracket();
165            xml.append(items);
166        }
167
168        xml.closeElement(this);
169    }
170
171    @Override
172    public String toString() {
173        return getClass().getName() + "Content [" + toXML() + "]";
174    }
175
176}