001/* 002 * (C) Copyright 2019 Nuxeo (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 * Nour AL KOTOB 018 */ 019package org.nuxeo.ecm.platform.audit.io; 020 021import static org.nuxeo.ecm.core.io.registry.reflect.Instantiations.SINGLETON; 022import static org.nuxeo.ecm.core.io.registry.reflect.Priorities.REFERENCE; 023import static org.nuxeo.ecm.platform.audit.api.BuiltinLogEntryData.LOG_CATEGORY; 024import static org.nuxeo.ecm.platform.audit.api.BuiltinLogEntryData.LOG_COMMENT; 025import static org.nuxeo.ecm.platform.audit.api.BuiltinLogEntryData.LOG_DOC_LIFE_CYCLE; 026import static org.nuxeo.ecm.platform.audit.api.BuiltinLogEntryData.LOG_DOC_PATH; 027import static org.nuxeo.ecm.platform.audit.api.BuiltinLogEntryData.LOG_DOC_TYPE; 028import static org.nuxeo.ecm.platform.audit.api.BuiltinLogEntryData.LOG_DOC_UUID; 029import static org.nuxeo.ecm.platform.audit.api.BuiltinLogEntryData.LOG_EVENT_DATE; 030import static org.nuxeo.ecm.platform.audit.api.BuiltinLogEntryData.LOG_EVENT_ID; 031import static org.nuxeo.ecm.platform.audit.api.BuiltinLogEntryData.LOG_ID; 032import static org.nuxeo.ecm.platform.audit.api.BuiltinLogEntryData.LOG_LOG_DATE; 033import static org.nuxeo.ecm.platform.audit.api.BuiltinLogEntryData.LOG_PRINCIPAL_NAME; 034import static org.nuxeo.ecm.platform.audit.api.BuiltinLogEntryData.LOG_REPOSITORY_ID; 035import static org.nuxeo.ecm.platform.audit.io.LogEntryJsonWriter.ENTITY_TYPE; 036 037import java.io.IOException; 038import java.util.HashSet; 039import java.util.List; 040import java.util.Set; 041import java.util.stream.Collectors; 042 043import org.apache.commons.csv.CSVPrinter; 044import org.nuxeo.ecm.core.io.marshallers.csv.AbstractCSVWriter; 045import org.nuxeo.ecm.core.io.registry.context.RenderingContext; 046import org.nuxeo.ecm.core.io.registry.reflect.Setup; 047import org.nuxeo.ecm.platform.audit.api.LogEntry; 048 049/** 050 * Convert {@link LogEntry} to CSV only keeping default and fetched properties if any. 051 * 052 * @since 11.1 053 */ 054@Setup(mode = SINGLETON, priority = REFERENCE) 055public class LogEntryCSVWriter extends AbstractCSVWriter<LogEntry> { 056 057 public static final List<String> DEFAULT_PROPERTIES = List.of(LOG_ID, LOG_CATEGORY, LOG_PRINCIPAL_NAME, LOG_COMMENT, 058 LOG_DOC_LIFE_CYCLE, LOG_DOC_PATH, LOG_DOC_TYPE, LOG_DOC_UUID, LOG_EVENT_ID, LOG_REPOSITORY_ID, 059 LOG_EVENT_DATE, LOG_LOG_DATE); 060 061 @Override 062 protected void write(LogEntry entity, CSVPrinter printer) throws IOException { 063 Set<String> propertiesToFetch = new HashSet<>(getPropertiesToFetch(ctx)); 064 if (propertiesToFetch.contains(LOG_ID)) { 065 printer.print(entity.getId()); 066 } 067 if (propertiesToFetch.contains(LOG_CATEGORY)) { 068 printer.print(entity.getCategory()); 069 } 070 if (propertiesToFetch.contains(LOG_PRINCIPAL_NAME)) { 071 printer.print(entity.getPrincipalName()); 072 } 073 if (propertiesToFetch.contains(LOG_COMMENT)) { 074 printer.print(entity.getComment()); 075 } 076 if (propertiesToFetch.contains(LOG_DOC_LIFE_CYCLE)) { 077 printer.print(entity.getDocLifeCycle()); 078 } 079 if (propertiesToFetch.contains(LOG_DOC_PATH)) { 080 printer.print(entity.getDocPath()); 081 } 082 if (propertiesToFetch.contains(LOG_DOC_TYPE)) { 083 printer.print(entity.getDocType()); 084 } 085 if (propertiesToFetch.contains(LOG_DOC_UUID)) { 086 printer.print(entity.getDocUUID()); 087 } 088 if (propertiesToFetch.contains(LOG_EVENT_ID)) { 089 printer.print(entity.getEventId()); 090 } 091 if (propertiesToFetch.contains(LOG_REPOSITORY_ID)) { 092 printer.print(entity.getRepositoryId()); 093 } 094 if (propertiesToFetch.contains(LOG_EVENT_DATE)) { 095 printer.print(entity.getEventDate()); 096 } 097 if (propertiesToFetch.contains(LOG_LOG_DATE)) { 098 printer.print(entity.getLogDate()); 099 } 100 } 101 102 @Override 103 protected void writeHeader(LogEntry entity, CSVPrinter printer) throws IOException { 104 writeHeader(printer, ctx); 105 } 106 107 /** 108 * Gets the properties to fetch if specified. Otherwise returns all default properties. 109 * 110 * @return the list of properties to fetch 111 */ 112 protected static List<String> getPropertiesToFetch(RenderingContext ctx) { 113 Set<String> fetched = ctx.getFetched(ENTITY_TYPE); 114 List<String> propertiesToFetch = DEFAULT_PROPERTIES.stream() 115 .filter(fetched::contains) 116 .collect(Collectors.toList()); 117 // if no particular property to fetch, fetch all default properties 118 return propertiesToFetch.isEmpty() ? DEFAULT_PROPERTIES : propertiesToFetch; 119 } 120 121 public static void writeHeader(CSVPrinter printer, RenderingContext ctx) throws IOException { 122 List<String> properties = getPropertiesToFetch(ctx); 123 for (String property : properties) { 124 printer.print(property); 125 } 126 printer.println(); 127 } 128 129}