001/*
002 * (C) Copyright 2013 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 *     Vladimir Pasquier <vpasquier@nuxeo.com>
016 */
017package org.nuxeo.ecm.automation.jaxrs.io.audit;
018
019import java.io.IOException;
020import java.io.Serializable;
021import java.util.Date;
022import java.util.Map;
023
024import javax.ws.rs.Produces;
025import javax.ws.rs.ext.Provider;
026
027import org.codehaus.jackson.JsonGenerator;
028import org.joda.time.DateTime;
029import org.joda.time.format.ISODateTimeFormat;
030import org.nuxeo.ecm.automation.jaxrs.io.EntityWriter;
031import org.nuxeo.ecm.platform.audit.api.ExtendedInfo;
032import org.nuxeo.ecm.platform.audit.api.LogEntry;
033import org.nuxeo.ecm.webengine.jaxrs.coreiodelegate.JsonCoreIODelegate;
034
035/**
036 * @since 5.7.3 - LogEntry Writer for Audit.
037 * @deprecated since 7.10 this marshaller was migrated to org.nuxeo.ecm.platform.audit.io.LogEntryJsonWriter. To use it
038 *             in JAX-RS, register the {@link JsonCoreIODelegate} to forward the JAX-RS marshalling to nuxeo-core-io.
039 */
040@Deprecated
041@Provider
042@Produces({ "application/json+nxentity", "application/json" })
043public class LogEntryWriter extends EntityWriter<LogEntry> {
044
045    public static final String ENTITY_TYPE = "logEntry";
046
047    @Override
048    protected String getEntityType() {
049        return ENTITY_TYPE;
050    }
051
052    @Override
053    protected void writeEntityBody(JsonGenerator jg, LogEntry logEntry) throws IOException {
054        jg.writeStringField("entity-type", "logEntry");
055        jg.writeStringField("category", logEntry.getCategory());
056        jg.writeStringField("principalName", logEntry.getPrincipalName());
057        jg.writeStringField("comment", logEntry.getComment());
058        jg.writeStringField("docLifeCycle", logEntry.getDocLifeCycle());
059        jg.writeStringField("docPath", logEntry.getDocPath());
060        jg.writeStringField("docType", logEntry.getDocType());
061        jg.writeStringField("docUUID", logEntry.getDocUUID());
062        jg.writeStringField("eventId", logEntry.getEventId());
063        jg.writeStringField("repositoryId", logEntry.getRepositoryId());
064        jg.writeStringField("eventDate", ISODateTimeFormat.dateTime().print(new DateTime(logEntry.getEventDate())));
065        jg.writeNumberField("id", logEntry.getId());
066        jg.writeStringField("logDate", ISODateTimeFormat.dateTime().print(new DateTime(logEntry.getLogDate())));
067        writeExtendedInfos(jg, logEntry);
068    }
069
070    protected void writeExtendedInfos(JsonGenerator jg, LogEntry logEntry) throws IOException {
071        Map<String, ExtendedInfo> extended = logEntry.getExtendedInfos();
072        jg.writeObjectFieldStart("extended");
073        for (String key : extended.keySet()) {
074            ExtendedInfo ei = extended.get(key);
075            if (ei != null && ei.getSerializableValue() != null) {
076                writeExtendedInfo(jg, key, ei.getSerializableValue());
077            } else {
078                jg.writeNullField(key);
079            }
080        }
081        jg.writeEndObject();
082    }
083
084    protected void writeExtendedInfo(JsonGenerator jg, String key, Serializable value) throws IOException {
085        Class<?> clazz = value.getClass();
086        if (Long.class.isAssignableFrom(clazz)) {
087            jg.writeNumberField(key, (Long) value);
088        } else if (Integer.class.isAssignableFrom(clazz)) {
089            jg.writeNumberField(key, (Integer) value);
090        } else if (Double.class.isAssignableFrom(clazz)) {
091            jg.writeNumberField(key, (Double) value);
092        } else if (Date.class.isAssignableFrom(clazz)) {
093            jg.writeStringField(key, ISODateTimeFormat.dateTime().print(new DateTime(value)));
094        } else if (String.class.isAssignableFrom(clazz)) {
095            jg.writeStringField(key, (String) value);
096        } else if (Boolean.class.isAssignableFrom(clazz)) {
097            jg.writeBooleanField(key, (Boolean) value);
098        } else {
099            jg.writeStringField(key, value.toString());
100        }
101    }
102}