001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     bstefanescu
011 */
012package org.nuxeo.ecm.automation.core.operations.services;
013
014import java.security.Principal;
015import java.util.ArrayList;
016import java.util.Collections;
017import java.util.Date;
018import java.util.List;
019
020import org.nuxeo.ecm.automation.OperationContext;
021import org.nuxeo.ecm.automation.core.Constants;
022import org.nuxeo.ecm.automation.core.annotations.Context;
023import org.nuxeo.ecm.automation.core.annotations.Operation;
024import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
025import org.nuxeo.ecm.automation.core.annotations.Param;
026import org.nuxeo.ecm.core.api.DocumentModel;
027import org.nuxeo.ecm.core.api.DocumentModelList;
028import org.nuxeo.ecm.core.api.NuxeoPrincipal;
029import org.nuxeo.ecm.platform.audit.api.AuditLogger;
030import org.nuxeo.ecm.platform.audit.api.LogEntry;
031
032/**
033 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
034 */
035@Operation(id = AuditLog.ID, category = Constants.CAT_SERVICES, label = "Log Event In Audit", description = "Log events into audit for each of the input document. The operation accept as input one ore more documents that are returned back as the output.", aliases = { "Audit.Log" })
036public class AuditLog {
037
038    public static final String ID = "Audit.LogEvent";
039
040    @Context
041    protected AuditLogger logger;
042
043    @Context
044    protected OperationContext ctx;
045
046    @Param(name = "event", widget = Constants.W_AUDIT_EVENT)
047    protected String event;
048
049    @Param(name = "category", required = false, values = { "Automation" })
050    protected String category = "Automation";
051
052    @Param(name = "comment", required = false, widget = Constants.W_MULTILINE_TEXT)
053    protected String comment = "";
054
055    @OperationMethod
056    public DocumentModel run(DocumentModel doc) {
057        Principal principal = ctx.getPrincipal();
058        String uname = getPrincipalName(principal);
059        LogEntry entry = newEntry(doc, uname, new Date());
060        logger.addLogEntries(Collections.singletonList(entry));
061        return doc;
062    }
063
064    @OperationMethod
065    public DocumentModelList run(DocumentModelList docs) {
066        List<LogEntry> entries = new ArrayList<LogEntry>();
067        Date date = new Date();
068        Principal principal = ctx.getPrincipal();
069        String uname = getPrincipalName(principal);
070        for (DocumentModel doc : docs) {
071            entries.add(newEntry(doc, uname, date));
072        }
073        logger.addLogEntries(entries);
074        return docs;
075    }
076
077    protected LogEntry newEntry(DocumentModel doc, String principal, Date date) {
078        LogEntry entry = logger.newLogEntry();
079        entry.setEventId(event);
080        entry.setEventDate(new Date());
081        entry.setCategory(category);
082        entry.setDocUUID(doc.getId());
083        entry.setDocPath(doc.getPathAsString());
084        entry.setComment(comment);
085        entry.setPrincipalName(principal);
086        entry.setDocType(doc.getType());
087        entry.setRepositoryId(doc.getRepositoryName());
088        entry.setDocLifeCycle(doc.getCurrentLifeCycleState());
089        return entry;
090    }
091
092    private String getPrincipalName(Principal principal) {
093        String uname;
094        if (principal instanceof NuxeoPrincipal) {
095            uname = ((NuxeoPrincipal) principal).getActingUser();
096        } else {
097            uname = principal.getName();
098        }
099        return uname;
100    }
101
102}