001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Vladimir Pasquier <vpasquier@nuxeo.com>
018 */
019
020package org.nuxeo.salesforce;
021
022import static org.nuxeo.ecm.core.io.registry.reflect.Instantiations.SINGLETON;
023import static org.nuxeo.ecm.core.io.registry.reflect.Priorities.REFERENCE;
024
025import java.io.IOException;
026import java.text.SimpleDateFormat;
027import java.util.Date;
028import java.util.GregorianCalendar;
029
030import org.codehaus.jackson.JsonGenerator;
031import org.nuxeo.ecm.core.api.CoreInstance;
032import org.nuxeo.ecm.core.api.CoreSession;
033import org.nuxeo.ecm.core.api.DocumentModel;
034import org.nuxeo.ecm.core.api.IdRef;
035import org.nuxeo.ecm.core.api.NuxeoPrincipal;
036import org.nuxeo.ecm.core.io.marshallers.json.enrichers.AbstractJsonEnricher;
037import org.nuxeo.ecm.core.io.registry.reflect.Setup;
038import org.nuxeo.ecm.platform.audit.api.LogEntry;
039import org.nuxeo.ecm.platform.usermanager.UserManager;
040import org.nuxeo.runtime.api.Framework;
041
042import com.google.common.base.Strings;
043
044/**
045 * Enricher for Audit LogEntries.
046 *
047 * @since 7.10
048 */
049@Setup(mode = SINGLETON, priority = REFERENCE)
050public class LogEntryEnricher extends AbstractJsonEnricher<LogEntry> {
051
052    public static final String NAME = "sfLogEntry";
053
054    public LogEntryEnricher() {
055        super(NAME);
056    }
057
058    @Override
059    public void write(JsonGenerator jsonGenerator, LogEntry logEntry) throws IOException {
060        CoreSession session = CoreInstance.openCoreSession(logEntry.getRepositoryId());
061        DocumentModel targetDocument = session.getDocument(new IdRef(logEntry.getDocUUID()));
062        Date creationDate = ((GregorianCalendar) targetDocument.getPropertyValue("dc:created")).getTime();
063        Date modificationDate = ((GregorianCalendar) targetDocument.getPropertyValue("dc:modified")).getTime();
064
065        jsonGenerator.writeObjectFieldStart(NAME);
066        jsonGenerator.writeStringField("contact", getPrincipalName(logEntry));
067        jsonGenerator.writeStringField("creation", new SimpleDateFormat("yyyy-MM-dd HH:mm a").format(creationDate));
068        jsonGenerator.writeStringField("modification", new SimpleDateFormat("yyyy-MM-dd HH:mm a").format(modificationDate));
069        jsonGenerator.writeEndObject();
070    }
071
072    protected String getPrincipalName(LogEntry logEntry) {
073        UserManager userManager = Framework.getLocalService(UserManager.class);
074        String principalName = logEntry.getPrincipalName();
075        NuxeoPrincipal principal = userManager.getPrincipal(principalName);
076        if (principal == null) {
077            return principalName;
078        }
079        String firstName = principal.getFirstName();
080        String lastName = principal.getLastName();
081        if (Strings.isNullOrEmpty(firstName) && Strings.isNullOrEmpty(lastName)) {
082            return principal.getName();
083        } else {
084            return firstName + " " + lastName;
085        }
086    }
087}