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