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 *     Vladimir Pasquier <vpasquier@nuxeo.com>
016 */
017
018package org.nuxeo.salesforce;
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.text.SimpleDateFormat;
025import java.util.Date;
026import java.util.GregorianCalendar;
027
028import org.codehaus.jackson.JsonGenerator;
029import org.nuxeo.ecm.core.api.CoreInstance;
030import org.nuxeo.ecm.core.api.CoreSession;
031import org.nuxeo.ecm.core.api.DocumentModel;
032import org.nuxeo.ecm.core.api.IdRef;
033import org.nuxeo.ecm.core.api.NuxeoPrincipal;
034import org.nuxeo.ecm.core.io.marshallers.json.enrichers.AbstractJsonEnricher;
035import org.nuxeo.ecm.core.io.registry.reflect.Setup;
036import org.nuxeo.ecm.platform.audit.api.LogEntry;
037import org.nuxeo.ecm.platform.usermanager.UserManager;
038import org.nuxeo.runtime.api.Framework;
039
040import com.google.common.base.Strings;
041
042/**
043 * Enricher for Audit LogEntries.
044 *
045 * @since 7.10
046 */
047@Setup(mode = SINGLETON, priority = REFERENCE)
048public class LogEntryEnricher extends AbstractJsonEnricher<LogEntry> {
049
050    public static final String NAME = "sfLogEntry";
051
052    public LogEntryEnricher() {
053        super(NAME);
054    }
055
056    @Override
057    public void write(JsonGenerator jsonGenerator, LogEntry logEntry) throws IOException {
058        CoreSession session = CoreInstance.openCoreSession(logEntry.getRepositoryId());
059        DocumentModel targetDocument = session.getDocument(new IdRef(logEntry.getDocUUID()));
060        Date creationDate = ((GregorianCalendar) targetDocument.getPropertyValue("dc:created")).getTime();
061        Date modificationDate = ((GregorianCalendar) targetDocument.getPropertyValue("dc:modified")).getTime();
062
063        jsonGenerator.writeObjectFieldStart(NAME);
064        jsonGenerator.writeObjectField("contact", getPrincipalName(logEntry));
065        jsonGenerator.writeObjectField("creation", new SimpleDateFormat("yyyy-MM-dd").format(creationDate));
066        jsonGenerator.writeObjectField("modification", new SimpleDateFormat("yyyy-MM-dd").format(modificationDate));
067        jsonGenerator.writeEndObject();
068    }
069
070    protected String getPrincipalName(LogEntry logEntry) {
071        UserManager userManager = Framework.getLocalService(UserManager.class);
072        NuxeoPrincipal principal = userManager.getPrincipal(logEntry.getPrincipalName());
073        String firstName = principal.getFirstName();
074        String lastName = principal.getLastName();
075        if (Strings.isNullOrEmpty(firstName) && Strings.isNullOrEmpty(lastName)) {
076            return principal.getName();
077        } else {
078            return firstName + " " + lastName;
079        }
080    }
081}