001package org.nuxeo.template.context.extensions; 002 003import java.util.ArrayList; 004import java.util.List; 005import java.util.Locale; 006import java.util.Map; 007import java.util.MissingResourceException; 008 009import org.apache.commons.logging.Log; 010import org.apache.commons.logging.LogFactory; 011import org.nuxeo.common.utils.i18n.I18NUtils; 012import org.nuxeo.ecm.core.api.CoreSession; 013import org.nuxeo.ecm.core.api.DocumentModel; 014import org.nuxeo.ecm.platform.audit.api.DocumentHistoryReader; 015import org.nuxeo.ecm.platform.audit.api.LogEntry; 016import org.nuxeo.ecm.platform.audit.api.comment.CommentProcessorHelper; 017import org.nuxeo.runtime.api.Framework; 018import org.nuxeo.template.api.context.ContextExtensionFactory; 019import org.nuxeo.template.api.context.DocumentWrapper; 020 021public class AuditExtensionFactory implements ContextExtensionFactory { 022 023 public static List<LogEntry> testAuditEntries; 024 025 protected static final Log log = LogFactory.getLog(AuditExtensionFactory.class); 026 027 @Override 028 public Object getExtension(DocumentModel currentDocument, DocumentWrapper wrapper, Map<String, Object> ctx) { 029 // add audit context info 030 DocumentHistoryReader historyReader = Framework.getLocalService(DocumentHistoryReader.class); 031 List<LogEntry> auditEntries = null; 032 if (historyReader != null) { 033 auditEntries = historyReader.getDocumentHistory(currentDocument, 0, 1000); 034 } else { 035 if (Framework.isTestModeSet() && testAuditEntries != null) { 036 auditEntries = testAuditEntries; 037 } else { 038 auditEntries = new ArrayList<LogEntry>(); 039 log.warn("Can not add Audit info to rendering context"); 040 } 041 } 042 if (auditEntries != null) { 043 try { 044 auditEntries = preprocessAuditEntries(auditEntries, currentDocument.getCoreSession(), "en"); 045 } catch (MissingResourceException e) { 046 log.warn("Unable to preprocess Audit entries : " + e.getMessage()); 047 } 048 ctx.put("auditEntries", wrapper.wrap(auditEntries)); 049 } 050 return null; 051 } 052 053 protected List<LogEntry> preprocessAuditEntries(List<LogEntry> auditEntries, CoreSession session, String lang) 054 throws MissingResourceException { 055 CommentProcessorHelper helper = new CommentProcessorHelper(session); 056 for (LogEntry entry : auditEntries) { 057 String comment = helper.getLogComment(entry); 058 if (comment == null) { 059 comment = ""; 060 } else { 061 String i18nComment = I18NUtils.getMessageString("messages", comment, null, new Locale(lang)); 062 if (i18nComment != null) { 063 comment = i18nComment; 064 } 065 } 066 String eventId = entry.getEventId(); 067 String i18nEventId = I18NUtils.getMessageString("messages", eventId, null, new Locale(lang)); 068 if (i18nEventId != null) { 069 entry.setEventId(i18nEventId); 070 } 071 entry.setComment(comment); 072 } 073 return auditEntries; 074 } 075}