001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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                    if (_uuid == null) {
074                            _uuid = PortalUUIDUtil.generate();
075                    }
076    
077                    return _uuid;
078            }
079    
080            @Override
081            public int hashCode() {
082                    if (_uuid != null) {
083                            return _uuid.hashCode();
084                    }
085                    else {
086                            return 0;
087                    }
088            }
089    
090            public boolean isArchived() {
091                    return _archived;
092            }
093    
094            public boolean isDeliveryRequired() {
095                    return _deliveryRequired;
096            }
097    
098            public void setArchived(boolean archived) {
099                    _archived = archived;
100            }
101    
102            public void setDeliverBy(long deliverBy)
103                    throws IllegalArgumentException {
104    
105                    if ((deliverBy < 0) && _deliveryRequired) {
106                            throw new IllegalArgumentException(
107                                    "Deliver by must be greater than or equal to 0 if delivery " +
108                                            "is required");
109                    }
110    
111                    _deliverBy = deliverBy;
112            }
113    
114            public void setDeliveryRequired(long deliverBy)
115                    throws IllegalArgumentException {
116    
117                    if (deliverBy < 0) {
118                            throw new IllegalArgumentException(
119                                    "Deliver by must be greater than or equal to 0 if delivery " +
120                                            "is required");
121                    }
122    
123                    _deliverBy = deliverBy;
124                    _deliveryRequired = true;
125            }
126    
127            public void setTimestamp(long timestamp) {
128                    _timestamp = timestamp;
129            }
130    
131            public void setUuid(String uuid) {
132                    _uuid = uuid;
133            }
134    
135            public JSONObject toJSONObject() {
136                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
137    
138                    jsonObject.put(_KEY_DELIVERY_REQUIRED, _deliveryRequired);
139                    jsonObject.put(_KEY_PAYLOAD, _payloadJSONObject);
140                    jsonObject.put(_KEY_TIMESTAMP, _timestamp);
141                    jsonObject.put(_KEY_TYPE, _type);
142                    jsonObject.put(_KEY_UUID, _uuid);
143    
144                    return jsonObject;
145            }
146    
147            private static final String _KEY_DELIVERY_REQUIRED = "deliveryRequired";
148    
149            private static final String _KEY_PAYLOAD = "payload";
150    
151            private static final String _KEY_TIMESTAMP = "timestamp";
152    
153            private static final String _KEY_TYPE = "type";
154    
155            private static final String _KEY_UUID = "uuid";
156    
157            private boolean _archived;
158            private long _deliverBy;
159            private boolean _deliveryRequired;
160            private JSONObject _payloadJSONObject;
161            private long _timestamp;
162            private String _type;
163            private String _uuid;
164    
165    }