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