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;
024
025import org.codehaus.jackson.JsonGenerator;
026import org.joda.time.DateTime;
027import org.joda.time.format.DateTimeFormatter;
028import org.joda.time.format.ISODateTimeFormat;
029import org.nuxeo.ecm.core.io.marshallers.json.ExtensibleEntityJsonWriter;
030import org.nuxeo.ecm.core.io.marshallers.json.enrichers.AbstractJsonEnricher;
031import org.nuxeo.ecm.core.io.registry.reflect.Setup;
032import org.nuxeo.ecm.platform.audit.api.LogEntry;
033
034import com.thoughtworks.xstream.io.json.JsonWriter;
035
036/**
037 * Convert {@link LogEntry} to Json.
038 * <p>
039 * This marshaller is enrichable: register class implementing {@link AbstractJsonEnricher} and managing {@link LogEntry}
040 * .
041 * </p>
042 * <p>
043 * This marshaller is also extensible: extend it and simply override
044 * {@link ExtensibleEntityJsonWriter#extend(LogEntry, JsonWriter)}.
045 * </p>
046 * <p>
047 * Format is:
048 *
049 * <pre>
050 * {@code
051 * {
052 *   "entity-type":"logEntry",
053 *   "category": "LOG_ENTRY_CATEGORY",
054 *   "principalName": "LOG_ENTRY_PRINCIPAL",
055 *   "comment": "LOG_ENTRY_COMMENT",
056 *   "docLifeCycle": "DOC_LIFECYCLE",
057 *   "docPath": "DOC_PATH",
058 *   "docType": "DOC_TYPE",
059 *   "docUUID": "DOC_UUID",
060 *   "eventId": "EVENT_ID",
061 *   "repositoryId": "REPO_ID",
062 *   "eventDate": "LOG_EVENT_DATE",
063 *   "logDate": "LOG_DATE"
064 *             <-- contextParameters if there are enrichers activated
065 *             <-- additional property provided by extend() method
066 * }
067 * </pre>
068 *
069 * </p>
070 *
071 * @since 7.2
072 */
073@Setup(mode = SINGLETON, priority = REFERENCE)
074public class LogEntryJsonWriter extends ExtensibleEntityJsonWriter<LogEntry> {
075
076    public static final String ENTITY_TYPE = "logEntry";
077
078    public LogEntryJsonWriter() {
079        super(ENTITY_TYPE, LogEntry.class);
080    }
081
082    @Override
083    protected void writeEntityBody(LogEntry logEntry, JsonGenerator jg) throws IOException {
084        jg.writeNumberField("id", logEntry.getId());
085        jg.writeStringField("category", logEntry.getCategory());
086        jg.writeStringField("principalName", logEntry.getPrincipalName());
087        jg.writeStringField("comment", logEntry.getComment());
088        jg.writeStringField("docLifeCycle", logEntry.getDocLifeCycle());
089        jg.writeStringField("docPath", logEntry.getDocPath());
090        jg.writeStringField("docType", logEntry.getDocType());
091        jg.writeStringField("docUUID", logEntry.getDocUUID());
092        jg.writeStringField("eventId", logEntry.getEventId());
093        jg.writeStringField("repositoryId", logEntry.getRepositoryId());
094        DateTimeFormatter dateTime = ISODateTimeFormat.dateTime();
095        jg.writeStringField("eventDate", dateTime.print(new DateTime(logEntry.getEventDate())));
096        jg.writeStringField("logDate", dateTime.print(new DateTime(logEntry.getLogDate())));
097    }
098
099}