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 static org.nuxeo.ecm.platform.audit.api.BuiltinLogEntryData.LOG_DOC_UUID; 023import static org.nuxeo.ecm.platform.audit.api.BuiltinLogEntryData.LOG_EVENT_DATE; 024import static org.nuxeo.ecm.platform.audit.api.BuiltinLogEntryData.LOG_EVENT_ID; 025 026import java.util.ArrayList; 027import java.util.Calendar; 028import java.util.GregorianCalendar; 029import java.util.List; 030 031import org.nuxeo.ecm.core.api.CoreSession; 032import org.nuxeo.ecm.core.api.DocumentModel; 033import org.nuxeo.ecm.core.api.IdRef; 034import org.nuxeo.ecm.core.api.event.DocumentEventTypes; 035import org.nuxeo.ecm.core.query.sql.model.OrderByExprs; 036import org.nuxeo.ecm.core.query.sql.model.Predicates; 037import org.nuxeo.ecm.core.query.sql.model.QueryBuilder; 038import org.nuxeo.ecm.platform.audit.api.AuditQueryBuilder; 039import org.nuxeo.ecm.platform.audit.api.AuditReader; 040import org.nuxeo.ecm.platform.audit.api.LogEntry; 041import org.nuxeo.runtime.api.Framework; 042 043/** 044 * Audit log stores event related to the "live" DocumentModel. This means that when retrieving the Audit Log for a 045 * version or a proxy, we must merge part of the "live" document history with the history of the proxy or version. This 046 * helper class fetches the additional parameters that must be used to retrieve history of a version or of a proxy. 047 * 048 * @author <a href="mailto:[email protected]">Tiry</a> 049 */ 050public class DocumentAuditHelper { 051 052 @SuppressWarnings({ "unchecked", "boxing" }) 053 public static AdditionalDocumentAuditParams getAuditParamsForUUID(String uuid, CoreSession session) { 054 055 IdRef ref = new IdRef(uuid); 056 if (!session.exists(ref)) { 057 return null; 058 } 059 DocumentModel doc = session.getDocument(ref); 060 if (!doc.isProxy() && !doc.isVersion()) { 061 return null; 062 } 063 SourceDocumentResolver resolver = new SourceDocumentResolver(session, doc); 064 resolver.runUnrestricted(); 065 if (resolver.sourceDocument == null) { 066 return null; 067 } 068 String targetUUID = resolver.sourceDocument.getId(); 069 // now get from Audit Logs the creation date of 070 // the version / proxy 071 QueryBuilder builder = new AuditQueryBuilder().predicate(Predicates.eq(LOG_DOC_UUID, uuid)) 072 .and(Predicates.eq(LOG_EVENT_ID, 073 DocumentEventTypes.DOCUMENT_CREATED)); 074 AuditReader reader = Framework.getService(AuditReader.class); 075 List<LogEntry> entries = reader.queryLogs(builder); 076 AdditionalDocumentAuditParams result; 077 if (entries != null && entries.size() > 0) { 078 result = new AdditionalDocumentAuditParams(); 079 result.maxDate = entries.get(0).getEventDate(); 080 result.targetUUID = targetUUID; 081 result.eventId = entries.get(0).getId(); 082 } else { 083 // we have no entry in audit log to get the maxDate 084 // fallback to repository timestamp 085 // this code is here only for compatibility so that it works before version events were added to 086 // the audit log 087 if (doc.getPropertyValue("dc:modified") == null) { 088 return null; 089 } 090 result = new AdditionalDocumentAuditParams(); 091 Calendar estimatedDate = ((Calendar) doc.getPropertyValue("dc:modified")); 092 093 // We can not directly use the repo timestamp because Audit and VCS can be in separated DB 094 // => try to find the matching TS in Audit 095 List<String> ids = new ArrayList<>(); 096 ids.add(targetUUID); 097 if (doc.isVersion()) { 098 session.getProxies(doc.getRef(), null).stream().map(DocumentModel::getId).forEach(ids::add); 099 } 100 estimatedDate.add(Calendar.MILLISECOND, -500); 101 102 QueryBuilder dateBuilder = new AuditQueryBuilder(); 103 dateBuilder.predicate(Predicates.in(LOG_DOC_UUID, ids)) 104 .and(Predicates.in(LOG_EVENT_ID, DocumentEventTypes.DOCUMENT_CREATED, 105 DocumentEventTypes.DOCUMENT_CHECKEDIN)) 106 .and(Predicates.gte(LOG_EVENT_DATE, estimatedDate.getTime())); 107 dateBuilder.order(OrderByExprs.asc(LOG_EVENT_ID)); 108 dateBuilder.offset(0).limit(20); 109 List<LogEntry> dateEntries = reader.queryLogs(dateBuilder); 110 if (dateEntries.size() > 0) { 111 result.targetUUID = targetUUID; 112 Calendar maxDate = new GregorianCalendar(); 113 maxDate.setTime(dateEntries.get(0).getEventDate()); 114 maxDate.add(Calendar.MILLISECOND, -500); 115 result.maxDate = maxDate.getTime(); 116 } else { 117 // no other choice : use the VCS TS 118 // results may be truncated in some DB config 119 result.targetUUID = targetUUID; 120 result.maxDate = ((Calendar) doc.getPropertyValue("dc:modified")).getTime(); 121 } 122 } 123 return result; 124 } 125 126}