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