/
LogEntryImpl.java
392 lines (336 loc) · 11.5 KB
/
LogEntryImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
* (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Contributors:
* Julien Anguenot
* Thierry Delprat
* Florent Guillaume
*/
package org.nuxeo.ecm.platform.audit.impl;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.MapKey;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.nuxeo.ecm.core.api.DocumentModel;
import org.nuxeo.ecm.core.api.DocumentRef;
import org.nuxeo.ecm.platform.audit.api.ExtendedInfo;
import org.nuxeo.ecm.platform.audit.api.LogEntry;
import org.nuxeo.ecm.platform.audit.api.comment.UIAuditComment;
import org.nuxeo.ecm.platform.audit.io.ExtendedInfoDeserializer;
import org.nuxeo.ecm.platform.audit.io.ExtendedInfoSerializer;
/**
* Log entry implementation.
*/
@Entity(name = "LogEntry")
@NamedQueries({
@NamedQuery(name = "LogEntry.removeByEventIdAndPath", query = "delete LogEntry log where log.eventId = :eventId and log.docPath like :pathPattern"),
@NamedQuery(name = "LogEntry.findByDocument", query = "from LogEntry log where log.docUUID=:docUUID ORDER BY log.eventDate DESC"),
@NamedQuery(name = "LogEntry.findByDocumentAndRepository", query = "from LogEntry log where log.docUUID=:docUUID and log.repositoryId=:repositoryId ORDER BY log.eventDate DESC"),
@NamedQuery(name = "LogEntry.findAll", query = "from LogEntry log order by log.eventDate DESC"),
@NamedQuery(name = "LogEntry.findByEventIdAndPath", query = "from LogEntry log where log.eventId=:eventId and log.docPath LIKE :pathPattern"),
@NamedQuery(name = "LogEntry.findByHavingExtendedInfo", query = "from LogEntry log where log.extendedInfos['one'] is not null order by log.eventDate DESC"),
@NamedQuery(name = "LogEntry.countEventsById", query = "select count(log.eventId) from LogEntry log where log.eventId=:eventId"),
@NamedQuery(name = "LogEntry.findEventIds", query = "select distinct log.eventId from LogEntry log") })
@Table(name = "NXP_LOGS")
public class LogEntryImpl implements LogEntry {
@JsonProperty("entity-type")
protected String entityType;
private static final long serialVersionUID = 3037187381843636097L;
private long id;
private String principalName;
private String eventId;
private Date eventDate;
private Date logDate;
private String docUUID;
private String docType;
private String docPath;
private String category;
private String comment;
private String docLifeCycle;
private String repositoryId;
protected transient UIAuditComment uiComment;
private Map<String, ExtendedInfoImpl> extendedInfos = new HashMap<String, ExtendedInfoImpl>();
/**
* @return the log identifier
*/
@Override
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "LOG_ID", nullable = false, columnDefinition = "integer")
public long getId() {
return id;
}
@Override
public void setId(long id) {
this.id = id;
}
/**
* Returns the name of the principal who originated the log entry.
*
* @return the name of the principal who originated the log entry
*/
@Override
@Column(name = "LOG_PRINCIPAL_NAME")
public String getPrincipalName() {
return principalName;
}
@Override
public void setPrincipalName(String principalName) {
this.principalName = principalName;
}
/**
* Returns the identifier of the event that originated the log entry.
*
* @return the identifier of the event that originated the log entry
*/
@Override
@Column(name = "LOG_EVENT_ID", nullable = false)
@MapKey(name = "logKey")
public String getEventId() {
return eventId;
}
@Override
public void setEventId(String eventId) {
this.eventId = eventId;
}
/**
* Returns the date of the event that originated the log entry.
*
* @return the date of the event that originated the log entry
*/
@Override
@Temporal(TemporalType.TIMESTAMP)
@JsonSerialize(using = DateSerializer.class)
@Column(name = "LOG_EVENT_DATE")
public Date getEventDate() {
return eventDate;
}
@Override
public void setEventDate(Date eventDate) {
this.eventDate = eventDate;
}
/**
* @return the date of the log insertion: this up to max transaction timeout later than eventDate. This date is
* useful for services such as Nuxeo Drive that need fine grained incremental near-monotonic access to the
* audit log.
* @since 5.7
* @since 5.6-HF16
*/
@Override
@Temporal(TemporalType.TIMESTAMP)
@JsonSerialize(using = DateSerializer.class)
@Column(name = "LOG_DATE")
public Date getLogDate() {
return logDate;
}
@Override
public void setLogDate(Date logDate) {
this.logDate = logDate;
}
/**
* Returns the doc UUID related to the log entry.
* <p>
* It might be null if the event that originated the event is noe bound to any document.
*
* @return the doc UUID related to the log entry.
*/
@Override
@Column(name = "LOG_DOC_UUID")
public String getDocUUID() {
return docUUID;
}
@Override
public void setDocUUID(String docUUID) {
this.docUUID = docUUID;
}
@Override
public void setDocUUID(DocumentRef docRef) {
switch (docRef.type()) {
case DocumentRef.ID:
docUUID = (String) docRef.reference();
break;
case DocumentRef.INSTANCE:
docUUID = ((DocumentModel) docRef.reference()).getId();
break;
default:
throw new IllegalArgumentException("not an id reference " + docRef);
}
}
/**
* Returns the doc path related to the log entry.
* <p>
* It might be null if the event that originated the event is noe bound to any document.
*
* @return the doc path related to the log entry.
*/
@Override
@Column(name = "LOG_DOC_PATH", length = 1024)
public String getDocPath() {
return docPath;
}
@Override
public void setDocPath(String docPath) {
this.docPath = docPath;
}
/**
* Returns the doc type related to the log entry.
* <p>
* It might be null if the event that originated the event is not bound to any document.
*
* @return the doc type related to the log entry.
*/
@Override
@Column(name = "LOG_DOC_TYPE")
public String getDocType() {
return docType;
}
@Override
public void setDocType(String docType) {
this.docType = docType;
}
/**
* Returns the category for this log entry.
* <p>
* This is defined at client level. Categories are not restricted in any ways.
*
* @return the category for this log entry.
*/
@Override
@Column(name = "LOG_EVENT_CATEGORY")
public String getCategory() {
return category;
}
@Override
public void setCategory(String category) {
this.category = category;
}
/**
* Returns the associated comment for this log entry.
*
* @return the associated comment for this log entry
*/
@Override
@Column(name = "LOG_EVENT_COMMENT", length = 1024)
public String getComment() {
return comment;
}
@Override
public void setComment(String comment) {
this.comment = comment;
}
/**
* Return the life cycle if the document related to the log entry.
* <p>
* It might be null if the event that originated the event is noe bound to any document.
*
* @return the life cycle if the document related to the log entry.
*/
@Override
@Column(name = "LOG_DOC_LIFE_CYCLE")
public String getDocLifeCycle() {
return docLifeCycle;
}
@Override
public void setDocLifeCycle(String docLifeCycle) {
this.docLifeCycle = docLifeCycle;
}
/**
* Returns the repository id related to the log entry.
*
* @return the repository id
*/
@Override
@Column(name = "LOG_REPO_ID")
public String getRepositoryId() {
return repositoryId;
}
@Override
public void setRepositoryId(String repositoryId) {
this.repositoryId = repositoryId;
}
// public Map<String, ExtendedInfoImpl> getExtendedInfosImpl() {
// return extendedInfos;
// }
//
// public void setExtendedInfosImpl(Map<String, ExtendedInfoImpl> infos) {
// extendedInfos = infos;
// }
@Override
@JsonProperty("extended")
@JsonSerialize(keyAs = String.class, contentUsing = ExtendedInfoSerializer.class)
@OneToMany(cascade = CascadeType.ALL, targetEntity = ExtendedInfoImpl.class)
@JoinTable(name = "NXP_LOGS_MAPEXTINFOS", joinColumns = { @JoinColumn(name = "LOG_FK") }, inverseJoinColumns = {
@JoinColumn(name = "INFO_FK") })
@org.hibernate.annotations.MapKey(columns = { @Column(name = "mapkey", nullable = false) })
public Map<String, ExtendedInfo> getExtendedInfos() {
return (Map) extendedInfos;
// return (Map)getExtendedInfosImpl();
}
@Override
@JsonDeserialize(keyAs = String.class, contentUsing = ExtendedInfoDeserializer.class)
public void setExtendedInfos(Map<String, ExtendedInfo> infos) {
extendedInfos = (Map) infos;
// setExtendedInfosImpl((Map)infos);
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
@Transient
@Override
public UIAuditComment getPreprocessedComment() {
return uiComment;
}
@Override
public void setPreprocessedComment(UIAuditComment uiComment) {
this.uiComment = uiComment;
}
/**
* Specific date serializer to have compliant dates with both current and 8.10 Elasticsearch mapping
*
* @since 9.3
*/
static class DateSerializer extends JsonSerializer<Date> {
public DateSerializer() {
}
@Override
public void serialize(Date date, JsonGenerator jg, SerializerProvider serializers) throws IOException {
jg.writeObject(date.toInstant().toString());
}
}
}