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) 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.NE) 105 { 106 NotQuery.appendNegation(query); 107 } 108 if (count > 1) 109 { 110 query.append('('); 111 } 112 113 boolean first = true; 114 for (String id : _ids) 115 { 116 if (!first) 117 { 118 query.append(" OR "); 119 } 120 query.append(SolrFieldNames.ALL_MIXIN_TYPES).append(':').append(id); 121 first = false; 122 } 123 124 if (count > 1) 125 { 126 query.append(')'); 127 } 128 129 return query.toString(); 130 } 131 132 @Override 133 public int hashCode() 134 { 135 final int prime = 31; 136 int result = 1; 137 result = prime * result + ((_ids == null) ? 0 : _ids.hashCode()); 138 result = prime * result + ((_operator == null) ? 0 : _operator.hashCode()); 139 return result; 140 } 141 142 @Override 143 public boolean equals(Object obj) 144 { 145 if (this == obj) 146 { 147 return true; 148 } 149 if (obj == null) 150 { 151 return false; 152 } 153 if (getClass() != obj.getClass()) 154 { 155 return false; 156 } 157 MixinTypeQuery other = (MixinTypeQuery) obj; 158 if (_ids == null) 159 { 160 if (other._ids != null) 161 { 162 return false; 163 } 164 } 165 else if (!_ids.equals(other._ids)) 166 { 167 return false; 168 } 169 if (_operator != other._operator) 170 { 171 return false; 172 } 173 return true; 174 } 175}