001/*
002 *  Copyright 2016 Anyware Services
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 */
016package org.ametys.cms.search.query;
017
018import java.time.LocalDateTime;
019import java.util.List;
020
021import org.ametys.cms.content.indexing.solr.SolrFieldNames;
022import org.ametys.core.util.date.AdaptableDate;
023
024/**
025 * {@link Query} testing the existence of a particular step on the workflow history.
026 */
027public class HistoryStepQuery extends JoinQuery
028{
029    /**
030     * Create a new HistoryStepQuery.
031     * @param stepId the workflow step ID.
032     */
033    public HistoryStepQuery(int stepId)
034    {
035        this(stepId, (AdaptableDate) null, (AdaptableDate) null);
036    }
037    
038    /**
039     * Create a new HistoryStepQuery.
040     * @param stepId the workflow step ID.
041     * @param startAfter the beginning Date
042     * @param startBefore the ending Date
043     */
044    public HistoryStepQuery(int stepId, LocalDateTime startAfter, LocalDateTime startBefore)
045    {
046        this(stepId, AdaptableDate.fromDateTime(startAfter), AdaptableDate.fromDateTime(startBefore));
047    }
048    
049    /**
050     * Create a new HistoryStepQuery.
051     * @param stepId the workflow step ID.
052     * @param startAfter the beginning Date
053     * @param startBefore the ending Date
054     */
055    public HistoryStepQuery(int stepId, AdaptableDate startAfter, AdaptableDate startBefore)
056    {
057        super(_getSubQuery(stepId, startAfter, startBefore), List.of(SolrFieldNames.WORKFLOW_REF, SolrFieldNames.WORKFLOW_HISTORY_STEPS));
058    }
059    
060    private static Query _getSubQuery(int stepId, AdaptableDate startAfter, AdaptableDate startBefore)
061    {
062        Query stepQuery = () -> SolrFieldNames.WORKFLOW_STEP_ID + ":" + stepId;
063        
064        if (startAfter != null || startBefore != null)
065        {
066            StringBuilder query = new StringBuilder();
067            query.append(SolrFieldNames.WORKFLOW_STEP_STARTDATE).append(":[")
068                 .append(startAfter == null ? "*" : startAfter.resolveDateTime().min().format(AbstractDateOperatorQuery.DATE_FORMATTER))
069                 .append(" TO ")
070                 .append(startBefore == null ? "*" : startBefore.resolveDateTime().max().format(AbstractDateOperatorQuery.DATE_FORMATTER))
071                 .append("}");
072            
073            Query dateQuery = () -> query.toString();
074            
075            return new AndQuery(stepQuery, dateQuery);
076        }
077        
078        return stepQuery;
079    }
080}