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;
019
020import org.ametys.cms.content.indexing.solr.SolrFieldNames;
021import org.ametys.core.util.date.AdaptableDate;
022
023/**
024 * {@link Query} testing the existence of a particular step on the workflow history.
025 */
026public class HistoryStepQuery implements Query
027{
028    private int _stepId;
029    private AdaptableDate _startBefore;
030    private AdaptableDate _startAfter;
031
032    /**
033     * Create a new HistoryStepQuery.
034     * @param stepId the workflow step ID.
035     */
036    public HistoryStepQuery(int stepId)
037    {
038        this(stepId, (AdaptableDate) null, (AdaptableDate) null);
039    }
040    
041    /**
042     * Create a new HistoryStepQuery.
043     * @param stepId the workflow step ID.
044     * @param startAfter the beginning Date
045     * @param startBefore the ending Date
046     */
047    public HistoryStepQuery(int stepId, LocalDateTime startAfter, LocalDateTime startBefore)
048    {
049        this(stepId, AdaptableDate.fromDateTime(startAfter), AdaptableDate.fromDateTime(startBefore));
050    }
051    
052    /**
053     * Create a new HistoryStepQuery.
054     * @param stepId the workflow step ID.
055     * @param startAfter the beginning Date
056     * @param startBefore the ending Date
057     */
058    public HistoryStepQuery(int stepId, AdaptableDate startAfter, AdaptableDate startBefore)
059    {
060        _stepId = stepId;
061        _startBefore = startBefore;
062        _startAfter = startAfter;
063    }
064    
065    @Override
066    public String build()
067    {
068        StringBuilder query = new StringBuilder();
069        
070        // {!ametys join=workflowRef->wfHistorySteps q="wfStepId:x AND wfStepStartDate:[yyy TO zzz]"}
071        query.append("{!ametys join=").append(SolrFieldNames.WORKFLOW_REF)
072             .append("->").append(SolrFieldNames.WORKFLOW_HISTORY_STEPS).append(" q=\"")
073             .append(SolrFieldNames.WORKFLOW_STEP_ID).append(":").append(_stepId);
074        
075        if (_startAfter != null || _startBefore != null)
076        {
077            query.append(" AND ").append(SolrFieldNames.WORKFLOW_STEP_STARTDATE).append(":[")
078                 .append(_startAfter == null ? "*" : _startAfter.resolveDateTime().format(DateQuery.DATE_FORMATTER))
079                 .append(" TO ")
080                 .append(_startBefore == null ? "*" : _startBefore.resolveDateTime().format(DateQuery.DATE_FORMATTER))
081                 .append("}");
082        }
083        
084        query.append("\"}");
085        
086        return query.toString();
087    }
088
089    @Override
090    public int hashCode()
091    {
092        final int prime = 31;
093        int result = 1;
094        result = prime * result + ((_startAfter == null) ? 0 : _startAfter.hashCode());
095        result = prime * result + ((_startBefore == null) ? 0 : _startBefore.hashCode());
096        result = prime * result + _stepId;
097        return result;
098    }
099
100    @Override
101    public boolean equals(Object obj)
102    {
103        if (this == obj)
104        {
105            return true;
106        }
107        if (obj == null)
108        {
109            return false;
110        }
111        if (getClass() != obj.getClass())
112        {
113            return false;
114        }
115        HistoryStepQuery other = (HistoryStepQuery) obj;
116        if (_startAfter == null)
117        {
118            if (other._startAfter != null)
119            {
120                return false;
121            }
122        }
123        else if (!_startAfter.equals(other._startAfter))
124        {
125            return false;
126        }
127        if (_startBefore == null)
128        {
129            if (other._startBefore != null)
130            {
131                return false;
132            }
133        }
134        else if (!_startBefore.equals(other._startBefore))
135        {
136            return false;
137        }
138        if (_stepId != other._stepId)
139        {
140            return false;
141        }
142        return true;
143    }
144}