001/* 002 * Copyright 2014 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 org.ametys.cms.content.indexing.solr.SolrFieldNames; 019 020/** 021 * Represents a {@link Query} testing the current workflow step of a content. 022 * It can only be used to search on the current step. 023 */ 024public class WorkflowStepQuery implements Query 025{ 026 027 private Operator _operator; 028 private int[] _values; 029 private LogicalOperator _logicalOperator; 030 031 /** 032 * Build a WorkflowStepQuery. 033 * @param value the step id to test. 034 */ 035 public WorkflowStepQuery(int value) 036 { 037 this(new int[] {value}); 038 } 039 040 /** 041 * Build a WorkflowStepQuery. 042 * @param values the step IDs to test. 043 */ 044 public WorkflowStepQuery(int... values) 045 { 046 this(Operator.EQ, values); 047 } 048 049 /** 050 * Build a WorkflowStepQuery. 051 * @param operator the operator. 052 * @param value the step id to test. 053 */ 054 public WorkflowStepQuery(Operator operator, int value) 055 { 056 this(operator, new int[] {value}); 057 } 058 059 /** 060 * Build a WorkflowStepQuery. 061 * @param operator the operator. 062 * @param values the step IDs to test. 063 */ 064 public WorkflowStepQuery(Operator operator, int... values) 065 { 066 this(operator, LogicalOperator.OR, values); 067 } 068 069 /** 070 * Build a WorkflowStepQuery. 071 * @param operator the operator. 072 * @param logicalOperator the logical operator. 073 * @param values the step IDs to test. 074 */ 075 public WorkflowStepQuery(Operator operator, LogicalOperator logicalOperator, int... values) 076 { 077 if (Operator.EQ != operator && Operator.NE != operator) 078 { 079 throw new IllegalArgumentException("Test operator '" + operator + "' is unknown for test's expression."); 080 } 081 082 _operator = operator; 083 _logicalOperator = logicalOperator; 084 _values = values; 085 } 086 087 /** 088 * Get the operator. 089 * @return the operator 090 */ 091 public Operator getOperator() 092 { 093 return _operator; 094 } 095 096 /** 097 * Get the values. 098 * @return the values 099 */ 100 public int[] getValues() 101 { 102 return _values; 103 } 104 105 /** 106 * Get the logicalOperator. 107 * @return the logicalOperator 108 */ 109 public LogicalOperator getLogicalOperator() 110 { 111 return _logicalOperator; 112 } 113 114 @Override 115 public String build() throws QuerySyntaxException 116 { 117 StringBuilder query = new StringBuilder(); 118 119 if (_values.length > 0) 120 { 121 if (_values.length > 1) 122 { 123 query.append('('); 124 } 125 for (int i = 0; i < _values.length; i++) 126 { 127 if (i > 0) 128 { 129 query.append(' ').append(_logicalOperator).append(' '); 130 } 131 132 if (_operator == Operator.NE) 133 { 134 query.append('-'); 135 } 136 137 query.append(SolrFieldNames.WORKFLOW_STEP).append(':').append(_values[i]); 138 } 139 if (_values.length > 1) 140 { 141 query.append(')'); 142 } 143 } 144 145 return query.toString(); 146 } 147 148}