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