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 java.util.ArrayList; 019import java.util.Arrays; 020import java.util.Collection; 021import java.util.Collections; 022import java.util.List; 023 024import org.ametys.cms.content.indexing.solr.SolrFieldNames; 025 026/** 027 * Represents a {@link Query} testing the mixin types of contents. 028 */ 029public class MixinTypeQuery implements Query 030{ 031 032 private Operator _operator; 033 private List<String> _ids; 034 035 /** 036 * Build a MixinTypeQuery. 037 * @param ids the mixin type ids. 038 */ 039 public MixinTypeQuery(String... ids) 040 { 041 this(Operator.EQ, ids); 042 } 043 044 /** 045 * Build a MixinTypeQuery. 046 * @param ids the mixin type ids. 047 */ 048 public MixinTypeQuery(Collection<String> ids) 049 { 050 this(Operator.EQ, ids); 051 } 052 053 /** 054 * Build a MixinTypeQuery. 055 * @param operator the operator. 056 * @param ids the mixin type ids. 057 */ 058 public MixinTypeQuery(Operator operator, String... ids) 059 { 060 this(operator, Arrays.asList(ids)); 061 } 062 063 /** 064 * Build a MixinTypeQuery. 065 * @param operator the operator. 066 * @param ids the mixin type ids. 067 */ 068 public MixinTypeQuery(Operator operator, Collection<String> ids) 069 { 070 if (Operator.EQ != operator && Operator.NE != operator && Operator.EXISTS != operator) 071 { 072 throw new IllegalArgumentException("Test operator '" + operator + "' is unknown for test's expression."); 073 } 074 075 _operator = operator; 076 _ids = new ArrayList<>(ids); 077 } 078 079 /** 080 * Get the operator. 081 * @return the operator 082 */ 083 public Operator getOperator() 084 { 085 return _operator; 086 } 087 088 /** 089 * Get the mixin type ids. 090 * @return the mixin type ids. 091 */ 092 public List<String> getIds() 093 { 094 return Collections.unmodifiableList(_ids); 095 } 096 097 @Override 098 public String build() throws QuerySyntaxException 099 { 100 int count = _ids.size(); 101 102 StringBuilder query = new StringBuilder(); 103 104 if (_operator == Operator.EXISTS) 105 { 106 query.append(SolrFieldNames.ALL_MIXIN_TYPES).append(":").append(QueryHelper.EXISTS_VALUE); 107 108 return query.toString(); 109 } 110 111 if (_operator == Operator.NE) 112 { 113 NotQuery.appendNegation(query); 114 } 115 if (count > 1) 116 { 117 query.append('('); 118 } 119 120 boolean first = true; 121 for (String id : _ids) 122 { 123 if (!first) 124 { 125 query.append(" OR "); 126 } 127 query.append(SolrFieldNames.ALL_MIXIN_TYPES).append(':').append(id); 128 first = false; 129 } 130 131 if (count > 1) 132 { 133 query.append(')'); 134 } 135 136 return query.toString(); 137 } 138 139 @Override 140 public int hashCode() 141 { 142 final int prime = 31; 143 int result = 1; 144 result = prime * result + ((_ids == null) ? 0 : _ids.hashCode()); 145 result = prime * result + ((_operator == null) ? 0 : _operator.hashCode()); 146 return result; 147 } 148 149 @Override 150 public boolean equals(Object obj) 151 { 152 if (this == obj) 153 { 154 return true; 155 } 156 if (obj == null) 157 { 158 return false; 159 } 160 if (getClass() != obj.getClass()) 161 { 162 return false; 163 } 164 MixinTypeQuery other = (MixinTypeQuery) obj; 165 if (_ids == null) 166 { 167 if (other._ids != null) 168 { 169 return false; 170 } 171 } 172 else if (!_ids.equals(other._ids)) 173 { 174 return false; 175 } 176 if (_operator != other._operator) 177 { 178 return false; 179 } 180 return true; 181 } 182}