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.Collections;
020import java.util.Date;
021import java.util.List;
022
023/**
024 * Represents an event generated by an item(s) being published to a node.
025 *
026 * @author Robin Collier
027 */
028public class ItemPublishEvent<T extends Item> extends SubscriptionEvent {
029    private List<T> items;
030    private Date originalDate;
031
032    /**
033     * Constructs an <tt>ItemPublishEvent</tt> with the provided list
034     * of {@link Item} that were published.
035     *
036     * @param nodeId The id of the node the event came from
037     * @param eventItems The list of {@link Item} that were published
038     */
039    public ItemPublishEvent(String nodeId, List<T> eventItems) {
040        super(nodeId);
041        items = eventItems;
042    }
043
044    /**
045     * Constructs an <tt>ItemPublishEvent</tt> with the provided list
046     * of {@link Item} that were published.  The list of subscription ids
047     * represents the subscriptions that matched the event, in the case
048     * of the user having multiple subscriptions.
049     *
050     * @param nodeId The id of the node the event came from
051     * @param eventItems The list of {@link Item} that were published
052     * @param subscriptionIds The list of subscriptionIds
053     */
054    public ItemPublishEvent(String nodeId, List<T> eventItems, List<String> subscriptionIds) {
055        super(nodeId, subscriptionIds);
056        items = eventItems;
057    }
058
059    /**
060     * Constructs an <tt>ItemPublishEvent</tt> with the provided list
061     * of {@link Item} that were published in the past.  The published
062     * date signifies that this is delayed event.  The list of subscription ids
063     * represents the subscriptions that matched the event, in the case
064     * of the user having multiple subscriptions.
065     *
066     * @param nodeId The id of the node the event came from
067     * @param eventItems The list of {@link Item} that were published
068     * @param subscriptionIds The list of subscriptionIds
069     * @param publishedDate date of publication.
070     */
071    public ItemPublishEvent(String nodeId, List<T> eventItems, List<String> subscriptionIds, Date publishedDate) {
072        super(nodeId, subscriptionIds);
073        items = eventItems;
074
075        if (publishedDate != null)
076            originalDate = publishedDate;
077    }
078
079    /**
080     * Get the list of {@link Item} that were published.
081     *
082     * @return The list of published {@link Item}
083     */
084    public List<T> getItems() {
085        return Collections.unmodifiableList(items);
086    }
087
088    /**
089     * Indicates whether this event was delayed.  That is, the items
090     * were published to the node at some time in the past.  This will
091     * typically happen if there is an item already published to the node
092     * before a user subscribes to it.  In this case, when the user
093     * subscribes, the server may send the last item published to the node
094     * with a delay date showing its time of original publication.
095     *
096     * @return true if the items are delayed, false otherwise.
097     */
098    public boolean isDelayed() {
099        return (originalDate != null);
100    }
101
102    /**
103     * Gets the original date the items were published.  This is only
104     * valid if {@link #isDelayed()} is true.
105     *
106     * @return date of publication.
107     */
108    public Date getPublishedDate() {
109        return originalDate;
110    }
111
112    @Override
113    public String toString() {
114        return getClass().getName() + "  [subscriptions: " + getSubscriptions() + "], [Delayed: " +
115            (isDelayed() ? originalDate.toString() : "false") + ']';
116    }
117
118}