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