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         */
073        public ItemPublishEvent(String nodeId, List<T> eventItems, List<String> subscriptionIds, Date publishedDate)
074        {
075                super(nodeId, subscriptionIds);
076                items = eventItems;
077                
078                if (publishedDate != null)
079                        originalDate = publishedDate;
080        }
081        
082        /**
083         * Get the list of {@link Item} that were published.
084         * 
085         * @return The list of published {@link Item}
086         */
087        public List<T> getItems()
088        {
089                return Collections.unmodifiableList(items);
090        }
091        
092        /**
093         * Indicates whether this event was delayed.  That is, the items
094         * were published to the node at some time in the past.  This will 
095         * typically happen if there is an item already published to the node
096         * before a user subscribes to it.  In this case, when the user 
097         * subscribes, the server may send the last item published to the node
098         * with a delay date showing its time of original publication.
099         * 
100         * @return true if the items are delayed, false otherwise.
101         */
102        public boolean isDelayed()
103        {
104                return (originalDate != null);
105        }
106        
107        /**
108         * Gets the original date the items were published.  This is only 
109         * valid if {@link #isDelayed()} is true.
110         * 
111         */
112        public Date getPublishedDate()
113        {
114                return originalDate;
115        }
116
117        @Override
118        public String toString()
119        {
120                return getClass().getName() + "  [subscriptions: " + getSubscriptions() + "], [Delayed: " + 
121                        (isDelayed() ? originalDate.toString() : "false") + ']';
122        }
123        
124}