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.lang3.StringUtils;
025import org.apache.commons.logging.Log;
026import org.apache.commons.logging.LogFactory;
027import org.nuxeo.ecm.core.api.CoreSession;
028import org.nuxeo.ecm.core.api.DocumentModel;
029import org.nuxeo.ecm.core.api.SortInfo;
030import org.nuxeo.ecm.platform.audit.api.document.AdditionalDocumentAuditParams;
031import org.nuxeo.ecm.platform.audit.api.document.DocumentAuditHelper;
032
033public class ESDocumentHistoryPageProvider extends ESAuditPageProvider {
034
035    private static final long serialVersionUID = 1L;
036
037    protected Log log = LogFactory.getLog(ESDocumentHistoryPageProvider.class);
038
039    protected Object[] newParams;
040
041    protected static String singleQuery = "{\n" + //
042            "  \"bool\" : {\n" + //
043            "    \"must\" : {\n" + //
044            "      \"term\" : {\n" + //
045            "        \"docUUID\" : \"?\"\n" + //
046            "      }\n" + //
047            "    }\n" + //
048            "  }\n" + //
049            "}\n";
050
051    protected static String complexQuery = "{\n" +  //
052            "  \"bool\": {\n" +  //
053            "    \"should\": [\n" +  //
054            "      {\n" +  //
055            "        \"term\": {\n" +  //
056            "          \"docUUID\": \"?\"\n" +  //
057            "        }\n" +  //
058            "      },\n" +  //
059            "      {\n" +  //
060            "        \"bool\": {\n" +  //
061            "          \"must\": [\n" +  //
062            "            {\n" +  //
063            "              \"term\": {\n" +  //
064            "                \"docUUID\": \"?\"\n" +  //
065            "              }\n" +  //
066            "            },\n" +  //
067            "            {\n" +  //
068            "              \"range\": {\n" +  //
069            "                \"eventDate\": {\n" +  //
070            "                  \"lte\": \"?\"\n" +  //
071            "                }\n" +  //
072            "              }\n" +  //
073            "            }\n" +  //
074            "          ]\n" +  //
075            "        }\n" +  //
076            "      }\n" +  //
077            "    ]\n" +  //
078            "  }\n" +  //
079            "}\n";
080
081    @Override
082    protected String getFixedPart() {
083        if (getDefinition().getWhereClause() != null) {
084            //if the pp definition contains a fixed part, use it
085            String fixedPart = getDefinition().getWhereClause().getFixedPart();
086            if (StringUtils.isNotEmpty(fixedPart)) {
087                return fixedPart;
088            }
089        }
090
091        if (getParameters().length == 3) {
092            return complexQuery;
093        } else {
094            return singleQuery;
095        }
096    }
097
098    @Override
099    public List<SortInfo> getSortInfos() {
100
101        List<SortInfo> sort = super.getSortInfos();
102        if (sort == null || sort.size() == 0) {
103            sort = new ArrayList<>();
104            sort.add(new SortInfo("eventDate", true));
105            sort.add(new SortInfo("id", true));
106        }
107        return sort;
108    }
109
110    @Override
111    public Object[] getParameters() {
112        if (newParams == null) {
113            Object[] params = super.getParameters();
114            if (params.length != 1) {
115                log.error(this.getClass().getSimpleName()
116                        + " Expect only one parameter the document uuid, unexpected behavior may occur");
117            }
118            CoreSession session;
119            String uuid;
120            if (params[0] instanceof DocumentModel) {
121                DocumentModel doc = (DocumentModel) params[0];
122                uuid = doc.getId();
123                session = doc.getCoreSession();
124            } else {
125                session = (CoreSession) getProperties().get(CORE_SESSION_PROPERTY);
126                uuid = params[0].toString();
127            }
128            if (session != null) {
129                AdditionalDocumentAuditParams additionalParams = DocumentAuditHelper.getAuditParamsForUUID(uuid,
130                        session);
131                if (additionalParams != null) {
132                    newParams = new Object[] { uuid, additionalParams.getTargetUUID(), additionalParams.getMaxDate() };
133                } else {
134                    newParams = new Object[] { uuid };
135                }
136            } else {
137                log.warn("No core session found: cannot compute all info to get complete audit entries");
138                return params;
139            }
140        }
141        return newParams;
142    }
143
144    @Override
145    public boolean hasChangedParameters(Object[] parameters) {
146        return getParametersChanged(this.parameters, parameters);
147    }
148
149}