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