001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.notifications;
016    
017    import com.liferay.portal.kernel.json.JSONFactoryUtil;
018    import com.liferay.portal.kernel.json.JSONObject;
019    import com.liferay.portal.kernel.util.Validator;
020    import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
021    
022    import java.io.Serializable;
023    
024    /**
025     * @author Edward Han
026     */
027    public class NotificationEvent implements Serializable {
028    
029            public NotificationEvent(
030                    long timestamp, String type, JSONObject payloadJSONObject) {
031    
032                    _timestamp = timestamp;
033                    _type = type;
034                    _payloadJSONObject = payloadJSONObject;
035            }
036    
037            @Override
038            public boolean equals(Object obj) {
039                    if (obj == null) {
040                            return false;
041                    }
042    
043                    if (!(obj instanceof NotificationEvent)) {
044                            return false;
045                    }
046    
047                    NotificationEvent notificationEvent = (NotificationEvent)obj;
048    
049                    if (Validator.equals(_uuid, notificationEvent._uuid)) {
050                            return true;
051                    }
052    
053                    return false;
054            }
055    
056            public long getDeliverBy() {
057                    return _deliverBy;
058            }
059    
060            public JSONObject getPayload() {
061                    return _payloadJSONObject;
062            }
063    
064            public long getTimestamp() {
065                    return _timestamp;
066            }
067    
068            public String getType() {
069                    return _type;
070            }
071    
072            public String getUuid() {
073                    return _uuid;
074            }
075    
076            @Override
077            public int hashCode() {
078                    if (_uuid != null) {
079                            return _uuid.hashCode();
080                    }
081                    else {
082                            return 0;
083                    }
084            }
085    
086            public boolean isDeliveryRequired() {
087                    return _deliveryRequired;
088            }
089    
090            public void setDeliverBy(long deliverBy)
091                    throws IllegalArgumentException {
092    
093                    if ((deliverBy <= 0) && _deliveryRequired) {
094                            throw new IllegalArgumentException(
095                                    "Deliver by must be greater than 0 if delivery is required");
096                    }
097    
098                    _deliverBy = deliverBy;
099            }
100    
101            public void setDeliveryRequired(long deliverBy)
102                    throws IllegalArgumentException {
103    
104                    if (deliverBy <= 0) {
105                            throw new IllegalArgumentException(
106                                    "Deliver by must be greater than 0 if delivery is required");
107                    }
108    
109                    _deliverBy = deliverBy;
110                    _deliveryRequired = true;
111            }
112    
113            public void setTimestamp(long timestamp) {
114                    _timestamp = timestamp;
115            }
116    
117            public JSONObject toJSONObject() {
118                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
119    
120                    jsonObject.put(_KEY_DELIVERY_REQUIRED, _deliveryRequired);
121                    jsonObject.put(_KEY_PAYLOAD, _payloadJSONObject);
122                    jsonObject.put(_KEY_TIMESTAMP, _timestamp);
123                    jsonObject.put(_KEY_TYPE, _type);
124                    jsonObject.put(_KEY_UUID, _uuid);
125    
126                    return jsonObject;
127            }
128    
129            private static final String _KEY_DELIVERY_REQUIRED = "deliveryRequired";
130    
131            private static final String _KEY_PAYLOAD = "payload";
132    
133            private static final String _KEY_TIMESTAMP = "timestamp";
134    
135            private static final String _KEY_TYPE = "type";
136    
137            private static final String _KEY_UUID = "uuid";
138    
139            private long _deliverBy;
140            private boolean _deliveryRequired;
141            private JSONObject _payloadJSONObject;
142            private long _timestamp;
143            private String _type;
144            private String _uuid = PortalUUIDUtil.generate();
145    
146    }