001/** 002 * Copyright 2015 Tampere University of Technology, Pori Department 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 core.tut.pori.dao.filter; 017 018import java.util.ArrayList; 019import java.util.Collection; 020import java.util.Iterator; 021import java.util.List; 022 023import org.apache.commons.lang3.ArrayUtils; 024import org.apache.log4j.Logger; 025 026/** 027 * Class for creating AND sub queries. 028 * 029 * The inner operation of the queries AND (Q1 [OR] Q2) depends on the type of the given query. In the aforementioned example the later query would have been an OR query. 030 * 031 */ 032public class AndSubQueryFilter implements AbstractQueryFilter { 033 private static final Logger LOGGER = Logger.getLogger(AndSubQueryFilter.class); 034 private List<AbstractQueryFilter> _filters = new ArrayList<>(); 035 036 /** 037 * 038 * @param filters 039 * @throws IllegalArgumentException on empty filter list 040 */ 041 public AndSubQueryFilter(AbstractQueryFilter[] filters) throws IllegalArgumentException{ 042 if(ArrayUtils.isEmpty(filters)){ 043 throw new IllegalArgumentException("Empty filter list."); 044 } 045 for(int i=0;i<filters.length;++i){ 046 _filters.add(filters[i]); 047 } 048 } 049 050 /** 051 * 052 * @param filters 053 * @throws IllegalArgumentException on empty filter list 054 */ 055 public AndSubQueryFilter(Collection<AbstractQueryFilter> filters) throws IllegalArgumentException{ 056 if(filters == null || filters.isEmpty()){ 057 throw new IllegalArgumentException("Empty filter list."); 058 } 059 _filters.addAll(filters); 060 } 061 062 /** 063 * initialize and empty sub query filter 064 */ 065 public AndSubQueryFilter() { 066 // nothing needed 067 } 068 069 /** 070 * 071 * @param filter filter to be added to this query 072 */ 073 public void addFilter(AbstractQueryFilter filter) { 074 if(filter == null){ 075 LOGGER.warn("Ignored null filter."); 076 }else{ 077 _filters.add(filter); 078 } 079 } 080 081 /** 082 * @throws IllegalStateException if the builder has not been initialized with sub-queries 083 */ 084 @Override 085 public void toFilterString(StringBuilder fq) throws IllegalStateException { 086 if(_filters.isEmpty()){ 087 throw new IllegalStateException("Attempted to convert to string, but no sub-queries are given."); 088 } 089 fq.append('('); 090 Iterator<AbstractQueryFilter> iter = _filters.iterator(); 091 iter.next().toFilterString(fq); 092 while(iter.hasNext()){ 093 AbstractQueryFilter f = iter.next(); 094 fq.append(f.getQueryType().toTypeString()); 095 f.toFilterString(fq); 096 } 097 fq.append(')'); 098 } 099 100 @Override 101 public QueryType getQueryType() { 102 return QueryType.AND; 103 } 104 105} // class AndSubQueryFilter