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