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