001/*
002 * (C) Copyright 2012 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 *     Thierry Delprat
018 */
019package org.nuxeo.template.context.extensions;
020
021import java.util.ArrayList;
022import java.util.List;
023import java.util.Locale;
024import java.util.Map;
025import java.util.MissingResourceException;
026
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029import org.nuxeo.common.utils.i18n.I18NUtils;
030import org.nuxeo.ecm.core.api.CoreSession;
031import org.nuxeo.ecm.core.api.DocumentModel;
032import org.nuxeo.ecm.platform.audit.api.DocumentHistoryReader;
033import org.nuxeo.ecm.platform.audit.api.LogEntry;
034import org.nuxeo.ecm.platform.audit.api.comment.CommentProcessorHelper;
035import org.nuxeo.runtime.api.Framework;
036import org.nuxeo.template.api.context.ContextExtensionFactory;
037import org.nuxeo.template.api.context.DocumentWrapper;
038
039public class AuditExtensionFactory implements ContextExtensionFactory {
040
041    public static List<LogEntry> testAuditEntries;
042
043    protected static final Log log = LogFactory.getLog(AuditExtensionFactory.class);
044
045    @Override
046    public Object getExtension(DocumentModel currentDocument, DocumentWrapper wrapper, Map<String, Object> ctx) {
047        // add audit context info
048        DocumentHistoryReader historyReader = Framework.getLocalService(DocumentHistoryReader.class);
049        List<LogEntry> auditEntries = null;
050        if (historyReader != null) {
051            auditEntries = historyReader.getDocumentHistory(currentDocument, 0, 1000);
052        } else {
053            if (Framework.isTestModeSet() && testAuditEntries != null) {
054                auditEntries = testAuditEntries;
055            } else {
056                auditEntries = new ArrayList<LogEntry>();
057                log.warn("Can not add Audit info to rendering context");
058            }
059        }
060        if (auditEntries != null) {
061            try {
062                auditEntries = preprocessAuditEntries(auditEntries, currentDocument.getCoreSession(), "en");
063            } catch (MissingResourceException e) {
064                log.warn("Unable to preprocess Audit entries : " + e.getMessage());
065            }
066            ctx.put("auditEntries", wrapper.wrap(auditEntries));
067        }
068        return null;
069    }
070
071    protected List<LogEntry> preprocessAuditEntries(List<LogEntry> auditEntries, CoreSession session, String lang)
072            throws MissingResourceException {
073        CommentProcessorHelper helper = new CommentProcessorHelper(session);
074        for (LogEntry entry : auditEntries) {
075            String comment = helper.getLogComment(entry);
076            if (comment == null) {
077                comment = "";
078            } else {
079                String i18nComment = I18NUtils.getMessageString("messages", comment, null, new Locale(lang));
080                if (i18nComment != null) {
081                    comment = i18nComment;
082                }
083            }
084            String eventId = entry.getEventId();
085            String i18nEventId = I18NUtils.getMessageString("messages", eventId, null, new Locale(lang));
086            if (i18nEventId != null) {
087                entry.setEventId(i18nEventId);
088            }
089            entry.setComment(comment);
090        }
091        return auditEntries;
092    }
093}