001/*
002 * (C) Copyright 2014-2018 Nuxeo (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" + //
041            "  \"bool\" : {\n" + //
042            "    \"must\" : {\n" + //
043            "      \"term\" : {\n" + //
044            "        \"docUUID\" : \"?\"\n" + //
045            "      }\n" + //
046            "    }\n" + //
047            "  }\n" + //
048            "}\n";
049
050    protected static String complexQuery = "{\n" +  //
051            "  \"bool\": {\n" +  //
052            "    \"should\": [\n" +  //
053            "      {\n" +  //
054            "        \"term\": {\n" +  //
055            "          \"docUUID\": \"?\"\n" +  //
056            "        }\n" +  //
057            "      },\n" +  //
058            "      {\n" +  //
059            "        \"bool\": {\n" +  //
060            "          \"must\": [\n" +  //
061            "            {\n" +  //
062            "              \"term\": {\n" +  //
063            "                \"docUUID\": \"?\"\n" +  //
064            "              }\n" +  //
065            "            },\n" +  //
066            "            {\n" +  //
067            "              \"range\": {\n" +  //
068            "                \"eventDate\": {\n" +  //
069            "                  \"lte\": \"?\"\n" +  //
070            "                }\n" +  //
071            "              }\n" +  //
072            "            }\n" +  //
073            "          ]\n" +  //
074            "        }\n" +  //
075            "      }\n" +  //
076            "    ]\n" +  //
077            "  }\n" +  //
078            "}\n";
079
080    @Override
081    protected String getFixedPart() {
082        if (getParameters().length == 3) {
083            return complexQuery;
084        } else {
085            return singleQuery;
086        }
087    }
088
089    @Override
090    public List<SortInfo> getSortInfos() {
091
092        List<SortInfo> sort = super.getSortInfos();
093        if (sort == null || sort.size() == 0) {
094            sort = new ArrayList<>();
095            sort.add(new SortInfo("eventDate", true));
096            sort.add(new SortInfo("id", true));
097        }
098        return sort;
099    }
100
101    @Override
102    public Object[] getParameters() {
103        if (newParams == null) {
104            Object[] params = super.getParameters();
105            if (params.length != 1) {
106                log.error(this.getClass().getSimpleName()
107                        + " Expect only one parameter the document uuid, unexpected behavior may occur");
108            }
109            CoreSession session;
110            String uuid;
111            if (params[0] instanceof DocumentModel) {
112                DocumentModel doc = (DocumentModel) params[0];
113                uuid = doc.getId();
114                session = doc.getCoreSession();
115            } else {
116                session = (CoreSession) getProperties().get(CORE_SESSION_PROPERTY);
117                uuid = params[0].toString();
118            }
119            if (session != null) {
120                AdditionalDocumentAuditParams additionalParams = DocumentAuditHelper.getAuditParamsForUUID(uuid,
121                        session);
122                if (additionalParams != null) {
123                    newParams = new Object[] { uuid, additionalParams.getTargetUUID(), additionalParams.getMaxDate() };
124                } else {
125                    newParams = new Object[] { uuid };
126                }
127            } else {
128                log.warn("No core session found: cannot compute all info to get complete audit entries");
129                return params;
130            }
131        }
132        return newParams;
133    }
134
135    @Override
136    public boolean hasChangedParameters(Object[] parameters) {
137        return getParametersChanged(this.parameters, parameters);
138    }
139
140}