001    /**
002     * Copyright (c) 2000-2012 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                    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) throws IllegalArgumentException {
103                    if ((deliverBy < 0) && _deliveryRequired) {
104                            throw new IllegalArgumentException(
105                                    "Deliver by must be greater than or equal to 0 if delivery " +
106                                            "is required");
107                    }
108    
109                    _deliverBy = deliverBy;
110            }
111    
112            public void setDeliveryRequired(long deliverBy)
113                    throws IllegalArgumentException {
114    
115                    if (deliverBy < 0) {
116                            throw new IllegalArgumentException(
117                                    "Deliver by must be greater than or equal to 0 if delivery " +
118                                            "is required");
119                    }
120    
121                    _deliverBy = deliverBy;
122                    _deliveryRequired = true;
123            }
124    
125            public void setTimestamp(long timestamp) {
126                    _timestamp = timestamp;
127            }
128    
129            public void setUuid(String uuid) {
130                    _uuid = uuid;
131            }
132    
133            public JSONObject toJSONObject() {
134                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
135    
136                    jsonObject.put(_KEY_DELIVERY_REQUIRED, _deliveryRequired);
137                    jsonObject.put(_KEY_PAYLOAD, _payloadJSONObject);
138                    jsonObject.put(_KEY_TIMESTAMP, _timestamp);
139                    jsonObject.put(_KEY_TYPE, _type);
140                    jsonObject.put(_KEY_UUID, _uuid);
141    
142                    return jsonObject;
143            }
144    
145            private static final String _KEY_DELIVERY_REQUIRED = "deliveryRequired";
146    
147            private static final String _KEY_PAYLOAD = "payload";
148    
149            private static final String _KEY_TIMESTAMP = "timestamp";
150    
151            private static final String _KEY_TYPE = "type";
152    
153            private static final String _KEY_UUID = "uuid";
154    
155            private boolean _archived;
156            private long _deliverBy;
157            private boolean _deliveryRequired;
158            private JSONObject _payloadJSONObject;
159            private long _timestamp;
160            private String _type;
161            private String _uuid;
162    
163    }