001/*
002 * (C) Copyright 2006-2011 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 *     bstefanescu
018 */
019package org.nuxeo.ecm.automation.core.operations.services;
020
021import java.security.Principal;
022import java.util.ArrayList;
023import java.util.Collections;
024import java.util.Date;
025import java.util.List;
026
027import org.nuxeo.ecm.automation.OperationContext;
028import org.nuxeo.ecm.automation.core.Constants;
029import org.nuxeo.ecm.automation.core.annotations.Context;
030import org.nuxeo.ecm.automation.core.annotations.Operation;
031import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
032import org.nuxeo.ecm.automation.core.annotations.Param;
033import org.nuxeo.ecm.core.api.DocumentModel;
034import org.nuxeo.ecm.core.api.DocumentModelList;
035import org.nuxeo.ecm.core.api.NuxeoPrincipal;
036import org.nuxeo.ecm.platform.audit.api.AuditLogger;
037import org.nuxeo.ecm.platform.audit.api.LogEntry;
038
039/**
040 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
041 */
042@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" })
043public class AuditLog {
044
045    public static final String ID = "Audit.LogEvent";
046
047    @Context
048    protected AuditLogger logger;
049
050    @Context
051    protected OperationContext ctx;
052
053    @Param(name = "event", widget = Constants.W_AUDIT_EVENT)
054    protected String event;
055
056    @Param(name = "category", required = false, values = { "Automation" })
057    protected String category = "Automation";
058
059    @Param(name = "comment", required = false, widget = Constants.W_MULTILINE_TEXT)
060    protected String comment = "";
061
062    @OperationMethod
063    public DocumentModel run(DocumentModel doc) {
064        Principal principal = ctx.getPrincipal();
065        String uname = getPrincipalName(principal);
066        LogEntry entry = newEntry(doc, uname, new Date());
067        logger.addLogEntries(Collections.singletonList(entry));
068        return doc;
069    }
070
071    @OperationMethod
072    public DocumentModelList run(DocumentModelList docs) {
073        List<LogEntry> entries = new ArrayList<LogEntry>();
074        Date date = new Date();
075        Principal principal = ctx.getPrincipal();
076        String uname = getPrincipalName(principal);
077        for (DocumentModel doc : docs) {
078            entries.add(newEntry(doc, uname, date));
079        }
080        logger.addLogEntries(entries);
081        return docs;
082    }
083
084    protected LogEntry newEntry(DocumentModel doc, String principal, Date date) {
085        LogEntry entry = logger.newLogEntry();
086        entry.setEventId(event);
087        entry.setEventDate(new Date());
088        entry.setCategory(category);
089        entry.setDocUUID(doc.getId());
090        entry.setDocPath(doc.getPathAsString());
091        entry.setComment(comment);
092        entry.setPrincipalName(principal);
093        entry.setDocType(doc.getType());
094        entry.setRepositoryId(doc.getRepositoryName());
095        entry.setDocLifeCycle(doc.getCurrentLifeCycleState());
096        return entry;
097    }
098
099    private String getPrincipalName(Principal principal) {
100        String uname;
101        if (principal instanceof NuxeoPrincipal) {
102            uname = ((NuxeoPrincipal) principal).getActingUser();
103        } else {
104            uname = principal.getName();
105        }
106        return uname;
107    }
108
109}