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.Calendar;
023import java.util.GregorianCalendar;
024import java.util.HashMap;
025import java.util.List;
026import java.util.Map;
027
028import org.nuxeo.ecm.core.api.CoreSession;
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.core.api.DocumentModelList;
031import org.nuxeo.ecm.core.api.IdRef;
032import org.nuxeo.ecm.core.api.event.DocumentEventTypes;
033import org.nuxeo.ecm.platform.audit.api.AuditReader;
034import org.nuxeo.ecm.platform.audit.api.FilterMapEntry;
035import org.nuxeo.ecm.platform.audit.api.LogEntry;
036import org.nuxeo.runtime.api.Framework;
037
038/**
039 * Audit log stores event related to the "live" DocumentModel. This means that when retrieving the Audit Log for a
040 * version or a proxy, we must merge part of the "live" document history with the history of the proxy or version. This
041 * helper class fetches the additional parameters that must be used to retrieve history of a version or of a proxy.
042 *
043 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a>
044 */
045public class DocumentAuditHelper {
046
047    @SuppressWarnings({ "unchecked", "boxing" })
048    public static AdditionalDocumentAuditParams getAuditParamsForUUID(String uuid, CoreSession session) {
049
050        IdRef ref = new IdRef(uuid);
051        if (!session.exists(ref)) {
052            return null;
053        }
054        DocumentModel doc = session.getDocument(ref);
055        if (!doc.isProxy() && !doc.isVersion()) {
056            return null;
057        }
058        SourceDocumentResolver resolver = new SourceDocumentResolver(session, doc);
059        resolver.runUnrestricted();
060        if (resolver.sourceDocument == null) {
061            return null;
062        }
063        String targetUUID = resolver.sourceDocument.getId();
064        // now get from Audit Logs the creation date of
065        // the version / proxy
066        AuditReader reader = Framework.getLocalService(AuditReader.class);
067        FilterMapEntry filter = new FilterMapEntry();
068        filter.setColumnName("eventId");
069        filter.setOperator("=");
070        filter.setQueryParameterName("eventId");
071        filter.setObject(DocumentEventTypes.DOCUMENT_CREATED);
072        Map<String, FilterMapEntry> filters = new HashMap<String, FilterMapEntry>();
073        filters.put("eventId", filter);
074        List<LogEntry> entries = reader.getLogEntriesFor(uuid, filters, false);
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            StringBuilder queryString = new StringBuilder();
095            queryString.append("from LogEntry log where log.docUUID in (");
096            queryString.append("'" + targetUUID + "'");
097            if (doc.isVersion()) {
098                DocumentModelList proxies = session.getProxies(doc.getRef(), null);
099                for (DocumentModel proxy : proxies) {
100                    queryString.append(",'" + proxy.getId() + "'");
101                }
102            }
103            queryString.append(",'" + doc.getId() + "'");
104            queryString.append(") AND log.eventId IN (");
105            queryString.append("'" + DocumentEventTypes.DOCUMENT_CREATED + "'");
106            queryString.append(",'" + DocumentEventTypes.DOCUMENT_CHECKEDIN + "'");
107            queryString.append(") AND log.eventDate >= :minDate ");
108            queryString.append(" order by log.eventId asc");
109
110            estimatedDate.add(Calendar.MILLISECOND, -500);
111            Map<String, Object> params = new HashMap<String, Object>();
112            params.put("minDate", estimatedDate.getTime());
113
114            List<LogEntry> dateEntries = (List<LogEntry>) reader.nativeQuery(queryString.toString(),
115                    params, 0, 20);
116            if (dateEntries.size() > 0) {
117                result.targetUUID = targetUUID;
118                Calendar maxDate = new GregorianCalendar();
119                maxDate.setTime(dateEntries.get(0).getEventDate());
120                maxDate.add(Calendar.MILLISECOND, -500);
121                result.maxDate = maxDate.getTime();
122            } else {
123                // no other choice : use the VCS TS
124                // results may be truncated in some DB config
125                result.targetUUID = targetUUID;
126                result.maxDate = ((Calendar) doc.getPropertyValue("dc:modified")).getTime();
127            }
128        }
129        return result;
130    }
131
132}