001/* 002 * Copyright (c) 2006-2012 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the Eclipse Public License v1.0 006 * which accompanies this distribution, and is available at 007 * http://www.eclipse.org/legal/epl-v10.html 008 * 009 * Contributors: 010 * Nuxeo - initial API and implementation 011 * 012 */ 013package org.nuxeo.ecm.platform.audit.api.document; 014 015import java.util.ArrayList; 016import java.util.List; 017 018import org.apache.commons.logging.Log; 019import org.apache.commons.logging.LogFactory; 020import org.nuxeo.ecm.core.api.CoreSession; 021import org.nuxeo.ecm.core.api.DocumentModel; 022import org.nuxeo.ecm.core.api.SortInfo; 023import org.nuxeo.ecm.platform.audit.api.AuditPageProvider; 024import org.nuxeo.ecm.platform.query.api.PageProvider; 025 026/** 027 * Page provider that is dedicated to fetching history of a Document. 028 * <p> 029 * Because of the way the Audit log is stored (i.e. mainly stores events related to the live document), retrieving 030 * history of a version or of a proxy requires some additional processing. 031 * <p> 032 * This {@link PageProvider} does not accept a fixed part in the whereclause because it is automatically build by the 033 * provider itself. This {@link PageProvider} expect to have : 034 * <ul> 035 * <li>DocumentModel or UUID as input parameter</li> 036 * <li>CoreSession as property (only used if input parameter is an uuid)</li> 037 * </ul> 038 * 039 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a> 040 */ 041public class DocumentHistoryPageProvider extends AuditPageProvider { 042 043 private static final long serialVersionUID = 1L; 044 045 protected Log log = LogFactory.getLog(DocumentHistoryPageProvider.class); 046 047 protected Object[] newParams; 048 049 @Override 050 protected String getFixedPart() { 051 if (getParameters().length == 3) { 052 return " ( log.docUUID = ? OR (log.docUUID = ? AND log.eventDate <= ?) ) "; 053 } else { 054 return " log.docUUID = ? "; 055 } 056 } 057 058 @Override 059 protected boolean allowSimplePattern() { 060 return false; 061 } 062 063 @Override 064 public List<SortInfo> getSortInfos() { 065 066 List<SortInfo> sort = super.getSortInfos(); 067 if (sort == null || sort.size() == 0) { 068 sort = new ArrayList<SortInfo>(); 069 sort.add(new SortInfo("log.eventDate", true)); 070 sort.add(new SortInfo("log.id", true)); 071 } 072 return sort; 073 } 074 075 @Override 076 public Object[] getParameters() { 077 if (newParams == null) { 078 Object[] params = super.getParameters(); 079 if (params.length != 1) { 080 log.error(this.getClass().getSimpleName() 081 + " Expect only one parameter the document uuid, unexpected behavior may occur"); 082 } 083 CoreSession session = null; 084 String uuid = null; 085 if (params[0] instanceof DocumentModel) { 086 DocumentModel doc = (DocumentModel) params[0]; 087 uuid = doc.getId(); 088 session = doc.getCoreSession(); 089 } else { 090 session = (CoreSession) getProperties().get(CORE_SESSION_PROPERTY); 091 uuid = params[0].toString(); 092 } 093 if (session != null) { 094 AdditionalDocumentAuditParams additionalParams = DocumentAuditHelper.getAuditParamsForUUID(uuid, 095 session); 096 if (additionalParams != null) { 097 newParams = new Object[] { uuid, additionalParams.targetUUID, additionalParams.maxDate }; 098 } else { 099 newParams = new Object[] { uuid }; 100 } 101 } else { 102 log.warn("No core session found: cannot compute all info to get complete audit entries"); 103 return params; 104 } 105 } 106 return newParams; 107 } 108 109 @Override 110 public boolean hasChangedParameters(Object[] parameters) { 111 return getParametersChanged(this.parameters, parameters); 112 } 113}