001    /**
002     * Copyright (c) 2000-present 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 (this == obj) {
040                            return true;
041                    }
042    
043                    if (!(obj instanceof NotificationEvent)) {
044                            return false;
045                    }
046    
047                    NotificationEvent notificationEvent = (NotificationEvent)obj;
048    
049                    if (Validator.equals(getUuid(), notificationEvent.getUuid())) {
050                            return true;
051                    }
052    
053                    return false;
054            }
055    
056            public long getDeliverBy() {
057                    return _deliverBy;
058            }
059    
060            public int getDeliveryType() {
061                    return _deliveryType;
062            }
063    
064            public JSONObject getPayload() {
065                    return _payloadJSONObject;
066            }
067    
068            public long getTimestamp() {
069                    return _timestamp;
070            }
071    
072            public String getType() {
073                    return _type;
074            }
075    
076            public String getUuid() {
077                    if (_uuid == null) {
078                            _uuid = PortalUUIDUtil.generate();
079                    }
080    
081                    return _uuid;
082            }
083    
084            @Override
085            public int hashCode() {
086                    String uuid = getUuid();
087    
088                    return uuid.hashCode();
089            }
090    
091            public boolean isArchived() {
092                    return _archived;
093            }
094    
095            public boolean isDeliveryRequired() {
096                    return _deliveryRequired;
097            }
098    
099            public void setArchived(boolean archived) {
100                    _archived = archived;
101            }
102    
103            public void setDeliverBy(long deliverBy) throws IllegalArgumentException {
104                    if ((deliverBy < 0) && _deliveryRequired) {
105                            throw new IllegalArgumentException(
106                                    "Deliver by must be greater than or equal to 0 if delivery " +
107                                            "is required");
108                    }
109    
110                    _deliverBy = deliverBy;
111            }
112    
113            public void setDeliveryRequired(long deliverBy)
114                    throws IllegalArgumentException {
115    
116                    if (deliverBy < 0) {
117                            throw new IllegalArgumentException(
118                                    "Deliver by must be greater than or equal to 0 if delivery " +
119                                            "is required");
120                    }
121    
122                    _deliverBy = deliverBy;
123                    _deliveryRequired = true;
124            }
125    
126            public void setDeliveryType(int deliveryType) {
127                    _deliveryType = deliveryType;
128            }
129    
130            public void setTimestamp(long timestamp) {
131                    _timestamp = timestamp;
132            }
133    
134            public void setUuid(String uuid) {
135                    _uuid = uuid;
136            }
137    
138            public JSONObject toJSONObject() {
139                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
140    
141                    jsonObject.put(_KEY_ARCHIVED, _archived);
142                    jsonObject.put(_KEY_DELIVERY_BY, _deliverBy);
143                    jsonObject.put(_KEY_DELIVERY_REQUIRED, _deliveryRequired);
144                    jsonObject.put(_KEY_DELIVERY_TYPE, _deliveryType);
145                    jsonObject.put(_KEY_PAYLOAD, _payloadJSONObject);
146                    jsonObject.put(_KEY_TIMESTAMP, _timestamp);
147                    jsonObject.put(_KEY_TYPE, _type);
148                    jsonObject.put(_KEY_UUID, _uuid);
149    
150                    return jsonObject;
151            }
152    
153            private static final String _KEY_ARCHIVED = "archived";
154    
155            private static final String _KEY_DELIVERY_BY = "deliveryBy";
156    
157            private static final String _KEY_DELIVERY_REQUIRED = "deliveryRequired";
158    
159            private static final String _KEY_DELIVERY_TYPE = "deliveryType";
160    
161            private static final String _KEY_PAYLOAD = "payload";
162    
163            private static final String _KEY_TIMESTAMP = "timestamp";
164    
165            private static final String _KEY_TYPE = "type";
166    
167            private static final String _KEY_UUID = "uuid";
168    
169            private boolean _archived;
170            private long _deliverBy;
171            private boolean _deliveryRequired;
172            private int _deliveryType;
173            private final JSONObject _payloadJSONObject;
174            private long _timestamp;
175            private final String _type;
176            private String _uuid;
177    
178    }