001/* 002 * (C) Copyright 2014 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.elasticsearch.audit.pageprovider; 020 021import java.util.ArrayList; 022import java.util.List; 023 024import org.apache.commons.logging.Log; 025import org.apache.commons.logging.LogFactory; 026import org.nuxeo.ecm.core.api.CoreSession; 027import org.nuxeo.ecm.core.api.DocumentModel; 028import org.nuxeo.ecm.core.api.SortInfo; 029import org.nuxeo.ecm.platform.audit.api.document.AdditionalDocumentAuditParams; 030import org.nuxeo.ecm.platform.audit.api.document.DocumentAuditHelper; 031 032public class ESDocumentHistoryPageProvider extends ESAuditPageProvider { 033 034 private static final long serialVersionUID = 1L; 035 036 protected Log log = LogFactory.getLog(ESDocumentHistoryPageProvider.class); 037 038 protected Object[] newParams; 039 040 protected static String singleQuery = " {\n" + " \"bool\" : {\n" 041 + " \"must\" : {\n" + " \"match\" : {\n" 042 + " \"docUUID\" : {\n" + " \"query\" : \"?\",\n" 043 + " \"type\" : \"boolean\"\n" + " }\n" 044 + " }\n" + " }\n" + " }\n" 045 + " } \n" + ""; 046 047 protected static String complexQuery = "{\n" + " \"filtered\" : {\n" + " \"query\" : {\n" 048 + " \"match_all\" : { }\n" + " },\n" + " \"filter\" : {\n" 049 + " \"or\" : [\n" + " {\n" 050 + " \"term\" : { \"docUUID\" : \"?\" }\n" + " },\n" 051 + " {\n" + " \"bool\" : {\n" + " \"must\" : [{\n" 052 + " \"term\" : { \"docUUID\" : \"?\" }\n" + " },\n" 053 + " {\n" + " \"range\" : {\n" 054 + " \"eventDate\" : { \"lte\" : \"?\"}\n" + " }\n" 055 + " }]\n" + " }\n" + " }\n" + " ]\n" 056 + " }\n" + " }\n" + "}\n" + "\n" + ""; 057 058 @Override 059 protected String getFixedPart() { 060 if (getParameters().length == 3) { 061 return complexQuery; 062 } else { 063 return singleQuery; 064 } 065 } 066 067 @Override 068 public List<SortInfo> getSortInfos() { 069 070 List<SortInfo> sort = super.getSortInfos(); 071 if (sort == null || sort.size() == 0) { 072 sort = new ArrayList<SortInfo>(); 073 sort.add(new SortInfo("eventDate", true)); 074 sort.add(new SortInfo("id", true)); 075 } 076 return sort; 077 } 078 079 @Override 080 public Object[] getParameters() { 081 if (newParams == null) { 082 Object[] params = super.getParameters(); 083 if (params.length != 1) { 084 log.error(this.getClass().getSimpleName() 085 + " Expect only one parameter the document uuid, unexpected behavior may occur"); 086 } 087 CoreSession session; 088 String uuid; 089 if (params[0] instanceof DocumentModel) { 090 DocumentModel doc = (DocumentModel) params[0]; 091 uuid = doc.getId(); 092 session = doc.getCoreSession(); 093 } else { 094 session = (CoreSession) getProperties().get(CORE_SESSION_PROPERTY); 095 uuid = params[0].toString(); 096 } 097 if (session != null) { 098 AdditionalDocumentAuditParams additionalParams = DocumentAuditHelper.getAuditParamsForUUID(uuid, 099 session); 100 if (additionalParams != null) { 101 newParams = new Object[] { uuid, additionalParams.getTargetUUID(), additionalParams.getMaxDate() }; 102 } else { 103 newParams = new Object[] { uuid }; 104 } 105 } else { 106 log.warn("No core session found: cannot compute all info to get complete audit entries"); 107 return params; 108 } 109 } 110 return newParams; 111 } 112 113 @Override 114 public boolean hasChangedParameters(Object[] parameters) { 115 return getParametersChanged(this.parameters, parameters); 116 } 117 118}