001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nicolas Chapurlat <nchapurlat@nuxeo.com>
016 */
017
018package org.nuxeo.ecm.platform.audit.io;
019
020import static org.nuxeo.ecm.core.io.registry.reflect.Instantiations.SINGLETON;
021import static org.nuxeo.ecm.core.io.registry.reflect.Priorities.REFERENCE;
022
023import java.io.IOException;
024import java.io.Serializable;
025import java.util.Date;
026import java.util.Map;
027
028import org.codehaus.jackson.JsonGenerator;
029import org.joda.time.DateTime;
030import org.joda.time.format.DateTimeFormatter;
031import org.joda.time.format.ISODateTimeFormat;
032import org.nuxeo.ecm.core.io.marshallers.json.ExtensibleEntityJsonWriter;
033import org.nuxeo.ecm.core.io.marshallers.json.enrichers.AbstractJsonEnricher;
034import org.nuxeo.ecm.core.io.registry.reflect.Setup;
035import org.nuxeo.ecm.platform.audit.api.ExtendedInfo;
036import org.nuxeo.ecm.platform.audit.api.LogEntry;
037
038import com.thoughtworks.xstream.io.json.JsonWriter;
039
040/**
041 * Convert {@link LogEntry} to Json.
042 * <p>
043 * This marshaller is enrichable: register class implementing {@link AbstractJsonEnricher} and managing {@link LogEntry}
044 * .
045 * </p>
046 * <p>
047 * This marshaller is also extensible: extend it and simply override
048 * {@link ExtensibleEntityJsonWriter#extend(LogEntry, JsonWriter)}.
049 * </p>
050 * <p>
051 * Format is:
052 *
053 * <pre>
054 * {@code
055 * {
056 *   "entity-type":"logEntry",
057 *   "category": "LOG_ENTRY_CATEGORY",
058 *   "principalName": "LOG_ENTRY_PRINCIPAL",
059 *   "comment": "LOG_ENTRY_COMMENT",
060 *   "docLifeCycle": "DOC_LIFECYCLE",
061 *   "docPath": "DOC_PATH",
062 *   "docType": "DOC_TYPE",
063 *   "docUUID": "DOC_UUID",
064 *   "eventId": "EVENT_ID",
065 *   "repositoryId": "REPO_ID",
066 *   "eventDate": "LOG_EVENT_DATE",
067 *   "logDate": "LOG_DATE"
068 *             <-- contextParameters if there are enrichers activated
069 *             <-- additional property provided by extend() method
070 * }
071 * </pre>
072 *
073 * </p>
074 *
075 * @since 7.2
076 */
077@Setup(mode = SINGLETON, priority = REFERENCE)
078public class LogEntryJsonWriter extends ExtensibleEntityJsonWriter<LogEntry> {
079
080    public static final String ENTITY_TYPE = "logEntry";
081
082    public LogEntryJsonWriter() {
083        super(ENTITY_TYPE, LogEntry.class);
084    }
085
086    @Override
087    protected void writeEntityBody(LogEntry logEntry, JsonGenerator jg) throws IOException {
088        jg.writeNumberField("id", logEntry.getId());
089        jg.writeStringField("category", logEntry.getCategory());
090        jg.writeStringField("principalName", logEntry.getPrincipalName());
091        jg.writeStringField("comment", logEntry.getComment());
092        jg.writeStringField("docLifeCycle", logEntry.getDocLifeCycle());
093        jg.writeStringField("docPath", logEntry.getDocPath());
094        jg.writeStringField("docType", logEntry.getDocType());
095        jg.writeStringField("docUUID", logEntry.getDocUUID());
096        jg.writeStringField("eventId", logEntry.getEventId());
097        jg.writeStringField("repositoryId", logEntry.getRepositoryId());
098        DateTimeFormatter dateTime = ISODateTimeFormat.dateTime();
099        jg.writeStringField("eventDate", dateTime.print(new DateTime(logEntry.getEventDate())));
100        jg.writeStringField("logDate", dateTime.print(new DateTime(logEntry.getLogDate())));
101        writeExtendedInfos(jg, logEntry);
102    }
103
104    protected void writeExtendedInfos(JsonGenerator jg, LogEntry logEntry) throws IOException {
105        Map<String, ExtendedInfo> extended = logEntry.getExtendedInfos();
106        jg.writeObjectFieldStart("extended");
107        for (String key : extended.keySet()) {
108            ExtendedInfo ei = extended.get(key);
109            if (ei != null && ei.getSerializableValue() != null) {
110                writeExtendedInfo(jg, key, ei.getSerializableValue());
111            } else {
112                jg.writeNullField(key);
113            }
114        }
115        jg.writeEndObject();
116    }
117
118    protected void writeExtendedInfo(JsonGenerator jg, String key, Serializable value) throws IOException {
119        Class<?> clazz = value.getClass();
120        if (Long.class.isAssignableFrom(clazz)) {
121            jg.writeNumberField(key, (Long) value);
122        } else if (Integer.class.isAssignableFrom(clazz)) {
123            jg.writeNumberField(key, (Integer) value);
124        } else if (Double.class.isAssignableFrom(clazz)) {
125            jg.writeNumberField(key, (Double) value);
126        } else if (Date.class.isAssignableFrom(clazz)) {
127            jg.writeStringField(key, ISODateTimeFormat.dateTime().print(new DateTime(value)));
128        } else if (String.class.isAssignableFrom(clazz)) {
129            jg.writeStringField(key, (String) value);
130        } else if (Boolean.class.isAssignableFrom(clazz)) {
131            jg.writeBooleanField(key, (Boolean) value);
132        } else {
133            jg.writeStringField(key, value.toString());
134        }
135    }
136
137}