001
014
015 package com.liferay.portal.kernel.audit;
016
017 import com.liferay.portal.kernel.json.JSONException;
018 import com.liferay.portal.kernel.json.JSONFactoryUtil;
019 import com.liferay.portal.kernel.json.JSONObject;
020 import com.liferay.portal.kernel.util.DateFormatFactoryUtil;
021 import com.liferay.portal.kernel.util.GetterUtil;
022
023 import java.io.Serializable;
024
025 import java.text.DateFormat;
026
027 import java.util.Date;
028
029
034 public class AuditMessage implements Serializable {
035
036 public AuditMessage(String message) throws JSONException {
037 JSONObject jsonObject = JSONFactoryUtil.createJSONObject(message);
038
039 _additionalInfoJSONObject = jsonObject.getJSONObject(_ADDITIONAL_INFO);
040 _className = jsonObject.getString(_CLASS_NAME);
041 _classPK = jsonObject.getString(_CLASS_PK);
042
043 if (jsonObject.has(_CLIENT_HOST)) {
044 _clientHost = jsonObject.getString(_CLIENT_HOST);
045 }
046
047 if (jsonObject.has(_CLIENT_IP)) {
048 _clientIP = jsonObject.getString(_CLIENT_IP);
049 }
050
051 _companyId = jsonObject.getLong(_COMPANY_ID);
052 _eventType = jsonObject.getString(_EVENT_TYPE);
053 _message = jsonObject.getString(_MESSAGE);
054
055 if (jsonObject.has(_SERVER_NAME)) {
056 _serverName = jsonObject.getString(_SERVER_NAME);
057 }
058
059 if (jsonObject.has(_SERVER_PORT)) {
060 _serverPort = jsonObject.getInt(_SERVER_PORT);
061 }
062
063 if (jsonObject.has(_SESSION_ID)) {
064 _sessionID = jsonObject.getString(_SESSION_ID);
065 }
066
067 _timestamp = GetterUtil.getDate(
068 jsonObject.getString(_TIMESTAMP), _getDateFormat());
069 _userId = jsonObject.getLong(_USER_ID);
070 _userName = jsonObject.getString(_USER_NAME);
071 }
072
073 public AuditMessage(
074 String eventType, long companyId, long userId, String userName) {
075
076 this(
077 eventType, companyId, userId, userName, null, null, null, null,
078 null);
079 }
080
081 public AuditMessage(
082 String eventType, long companyId, long userId, String userName,
083 String className, String classPK) {
084
085 this(
086 eventType, companyId, userId, userName, className, classPK, null,
087 null, null);
088 }
089
090 public AuditMessage(
091 String eventType, long companyId, long userId, String userName,
092 String className, String classPK, String message) {
093
094 this(
095 eventType, companyId, userId, userName, className, classPK, message,
096 null, null);
097 }
098
099 public AuditMessage(
100 String eventType, long companyId, long userId, String userName,
101 String className, String classPK, String message, Date timestamp,
102 JSONObject additionalInfoJSONObject) {
103
104 _eventType = eventType;
105 _companyId = companyId;
106 _userId = userId;
107 _userName = userName;
108 _className = className;
109 _classPK = classPK;
110 _message = message;
111
112 AuditRequestThreadLocal auditRequestThreadLocal =
113 AuditRequestThreadLocal.getAuditThreadLocal();
114
115 _clientHost = auditRequestThreadLocal.getClientHost();
116 _clientIP = auditRequestThreadLocal.getClientIP();
117 _serverName = auditRequestThreadLocal.getServerName();
118 _serverPort = auditRequestThreadLocal.getServerPort();
119 _sessionID = auditRequestThreadLocal.getSessionID();
120
121 _timestamp = timestamp;
122
123 if (_timestamp == null) {
124 _timestamp = new Date();
125 }
126
127 _additionalInfoJSONObject = additionalInfoJSONObject;
128
129 if (_additionalInfoJSONObject == null) {
130 JSONFactoryUtil.createJSONObject();
131 }
132 }
133
134 public AuditMessage(
135 String eventType, long companyId, long userId, String userName,
136 String className, String classPK, String message,
137 JSONObject additionalInfoJSONObject) {
138
139 this(
140 eventType, companyId, userId, userName, className, classPK, message,
141 null, additionalInfoJSONObject);
142 }
143
144 public JSONObject getAdditionalInfo() {
145 return _additionalInfoJSONObject;
146 }
147
148 public String getClassName() {
149 return _className;
150 }
151
152 public String getClassPK() {
153 return _classPK;
154 }
155
156 public String getClientHost() {
157 return _clientHost;
158 }
159
160 public String getClientIP() {
161 return _clientIP;
162 }
163
164 public long getCompanyId() {
165 return _companyId;
166 }
167
168 public String getEventType() {
169 return _eventType;
170 }
171
172 public String getMessage() {
173 return _message;
174 }
175
176 public String getServerName() {
177 return _serverName;
178 }
179
180 public int getServerPort() {
181 return _serverPort;
182 }
183
184 public String getSessionID() {
185 return _sessionID;
186 }
187
188 public Date getTimestamp() {
189 return _timestamp;
190 }
191
192 public long getUserId() {
193 return _userId;
194 }
195
196 public String getUserName() {
197 return _userName;
198 }
199
200 public void setAdditionalInfo(JSONObject additionalInfoJSONObject) {
201 _additionalInfoJSONObject = additionalInfoJSONObject;
202 }
203
204 public void setClassName(String className) {
205 _className = className;
206 }
207
208 public void setClassPK(long classPK) {
209 _classPK = String.valueOf(classPK);
210 }
211
212 public void setClassPK(String classPK) {
213 _classPK = classPK;
214 }
215
216 public void setClientHost(String clientHost) {
217 _clientHost = clientHost;
218 }
219
220 public void setClientIP(String clientIP) {
221 _clientIP = clientIP;
222 }
223
224 public void setCompanyId(long companyId) {
225 _companyId = companyId;
226 }
227
228 public void setEventType(String eventType) {
229 _eventType = eventType;
230 }
231
232 public void setMessage(String message) {
233 _message = message;
234 }
235
236 public void setServerName(String serverName) {
237 _serverName = serverName;
238 }
239
240 public void setServerPort(int serverPort) {
241 _serverPort = serverPort;
242 }
243
244 public void setSessionID(String sessionID) {
245 _sessionID = sessionID;
246 }
247
248 public void setTimestamp(Date timestamp) {
249 _timestamp = timestamp;
250 }
251
252 public void setUserId(long userId) {
253 _userId = userId;
254 }
255
256 public void setUserName(String userName) {
257 _userName = userName;
258 }
259
260 public JSONObject toJSONObject() {
261 JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
262
263 jsonObject.put(_ADDITIONAL_INFO, _additionalInfoJSONObject);
264 jsonObject.put(_COMPANY_ID, _companyId);
265 jsonObject.put(_CLASS_PK, _classPK);
266 jsonObject.put(_CLASS_NAME, _className);
267 jsonObject.put(_CLIENT_HOST, _clientHost);
268 jsonObject.put(_CLIENT_IP, _clientIP);
269 jsonObject.put(_MESSAGE, _message);
270 jsonObject.put(_SERVER_PORT, _serverPort);
271 jsonObject.put(_SERVER_NAME, _serverName);
272 jsonObject.put(_SESSION_ID, _sessionID);
273 jsonObject.put(_TIMESTAMP, _getDateFormat().format(new Date()));
274 jsonObject.put(_EVENT_TYPE, _eventType);
275 jsonObject.put(_USER_ID, _userId);
276 jsonObject.put(_USER_NAME, _userName);
277
278 return jsonObject;
279 }
280
281 private DateFormat _getDateFormat() {
282 return DateFormatFactoryUtil.getSimpleDateFormat(_DATE_FORMAT);
283 }
284
285 private static final String _ADDITIONAL_INFO = "additionalInfo";
286
287 private static final String _CLASS_NAME = "className";
288
289 private static final String _CLASS_PK = "classPK";
290
291 private static final String _CLIENT_HOST = "clientHost";
292
293 private static final String _CLIENT_IP = "clientIP";
294
295 private static final String _COMPANY_ID = "companyId";
296
297 private static final String _DATE_FORMAT = "yyyyMMddkkmmssSSS";
298
299 private static final String _EVENT_TYPE = "eventType";
300
301 private static final String _MESSAGE = "message";
302
303 private static final String _SERVER_NAME = "serverName";
304
305 private static final String _SERVER_PORT = "serverPort";
306
307 private static final String _SESSION_ID = "sessionID";
308
309 private static final String _TIMESTAMP = "timestamp";
310
311 private static final String _USER_ID = "userId";
312
313 private static final String _USER_NAME = "userName";
314
315 private JSONObject _additionalInfoJSONObject;
316 private String _className;
317 private String _classPK;
318 private String _clientHost;
319 private String _clientIP;
320 private long _companyId = -1;
321 private String _eventType;
322 private String _message;
323 private String _serverName;
324 private int _serverPort;
325 private String _sessionID;
326 private Date _timestamp;
327 private long _userId = -1;
328 private String _userName;
329
330 }