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.uuid.PortalUUIDUtil;
020    
021    import java.io.Serializable;
022    
023    import java.util.Objects;
024    
025    /**
026     * @author Edward Han
027     */
028    public class NotificationEvent implements Serializable {
029    
030            public NotificationEvent(
031                    long timestamp, String type, JSONObject payloadJSONObject) {
032    
033                    _timestamp = timestamp;
034                    _type = type;
035                    _payloadJSONObject = payloadJSONObject;
036            }
037    
038            @Override
039            public boolean equals(Object obj) {
040                    if (this == obj) {
041                            return true;
042                    }
043    
044                    if (!(obj instanceof NotificationEvent)) {
045                            return false;
046                    }
047    
048                    NotificationEvent notificationEvent = (NotificationEvent)obj;
049    
050                    if (Objects.equals(getUuid(), notificationEvent.getUuid())) {
051                            return true;
052                    }
053    
054                    return false;
055            }
056    
057            public long getDeliverBy() {
058                    return _deliverBy;
059            }
060    
061            public int getDeliveryType() {
062                    return _deliveryType;
063            }
064    
065            public JSONObject getPayload() {
066                    return _payloadJSONObject;
067            }
068    
069            public long getTimestamp() {
070                    return _timestamp;
071            }
072    
073            public String getType() {
074                    return _type;
075            }
076    
077            public String getUuid() {
078                    if (_uuid == null) {
079                            _uuid = PortalUUIDUtil.generate();
080                    }
081    
082                    return _uuid;
083            }
084    
085            @Override
086            public int hashCode() {
087                    String uuid = getUuid();
088    
089                    return uuid.hashCode();
090            }
091    
092            public boolean isArchived() {
093                    return _archived;
094            }
095    
096            public boolean isDeliveryRequired() {
097                    return _deliveryRequired;
098            }
099    
100            public void setArchived(boolean archived) {
101                    _archived = archived;
102            }
103    
104            public void setDeliverBy(long deliverBy) throws IllegalArgumentException {
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 setDeliveryType(int deliveryType) {
128                    _deliveryType = deliveryType;
129            }
130    
131            public void setTimestamp(long timestamp) {
132                    _timestamp = timestamp;
133            }
134    
135            public void setUuid(String uuid) {
136                    _uuid = uuid;
137            }
138    
139            public JSONObject toJSONObject() {
140                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
141    
142                    jsonObject.put(_KEY_ARCHIVED, _archived);
143                    jsonObject.put(_KEY_DELIVERY_BY, _deliverBy);
144                    jsonObject.put(_KEY_DELIVERY_REQUIRED, _deliveryRequired);
145                    jsonObject.put(_KEY_DELIVERY_TYPE, _deliveryType);
146                    jsonObject.put(_KEY_PAYLOAD, _payloadJSONObject);
147                    jsonObject.put(_KEY_TIMESTAMP, _timestamp);
148                    jsonObject.put(_KEY_TYPE, _type);
149                    jsonObject.put(_KEY_UUID, _uuid);
150    
151                    return jsonObject;
152            }
153    
154            private static final String _KEY_ARCHIVED = "archived";
155    
156            private static final String _KEY_DELIVERY_BY = "deliveryBy";
157    
158            private static final String _KEY_DELIVERY_REQUIRED = "deliveryRequired";
159    
160            private static final String _KEY_DELIVERY_TYPE = "deliveryType";
161    
162            private static final String _KEY_PAYLOAD = "payload";
163    
164            private static final String _KEY_TIMESTAMP = "timestamp";
165    
166            private static final String _KEY_TYPE = "type";
167    
168            private static final String _KEY_UUID = "uuid";
169    
170            private boolean _archived;
171            private long _deliverBy;
172            private boolean _deliveryRequired;
173            private int _deliveryType;
174            private final JSONObject _payloadJSONObject;
175            private long _timestamp;
176            private final String _type;
177            private String _uuid;
178    
179    }