001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.db; 029 030import java.util.ArrayList; 031import java.util.List; 032 033/** 034 * Class for generating an SQL boolean expression.<p> 035 * 036 * @since 8.0.0 037 */ 038public class CmsSqlBooleanClause implements I_CmsQueryFragment { 039 040 /** The list of operands. */ 041 private List<I_CmsQueryFragment> m_fragments = new ArrayList<I_CmsQueryFragment>(); 042 043 /** The boolean operator. */ 044 private String m_operator; 045 046 /** 047 * Creates a new boolean clause.<p> 048 * 049 * @param operator the boolean operator 050 */ 051 public CmsSqlBooleanClause(String operator) { 052 053 m_operator = operator; 054 } 055 056 /** 057 * Creates a boolean "AND" expression.<p> 058 * 059 * @param fragments the operands of the "AND" 060 * 061 * @return the combined expression 062 */ 063 public static CmsSqlBooleanClause makeAnd(I_CmsQueryFragment... fragments) { 064 065 CmsSqlBooleanClause result = new CmsSqlBooleanClause("AND"); 066 for (I_CmsQueryFragment fragment : fragments) { 067 result.addCondition(fragment); 068 } 069 return result; 070 071 } 072 073 /** 074 * Creates a boolean "OR" expression.<p> 075 * 076 * @param fragments the operands of the "OR" 077 * 078 * @return the combined expressiong 079 */ 080 public static CmsSqlBooleanClause makeOr(I_CmsQueryFragment... fragments) { 081 082 CmsSqlBooleanClause result = new CmsSqlBooleanClause("OR"); 083 for (I_CmsQueryFragment fragment : fragments) { 084 result.addCondition(fragment); 085 } 086 return result; 087 } 088 089 /** 090 * Adds an operand to the boolean expression.<p> 091 * 092 * @param fragment the operand 093 * 094 * @return this object instance 095 */ 096 public CmsSqlBooleanClause addCondition(I_CmsQueryFragment fragment) { 097 098 m_fragments.add(fragment); 099 return this; 100 } 101 102 /** 103 * @see org.opencms.db.I_CmsQueryFragment#visit(org.opencms.db.CmsStatementBuilder) 104 */ 105 public void visit(CmsStatementBuilder builder) { 106 107 String connector = " " + m_operator + " "; 108 if (m_fragments.size() == 0) { 109 throw new IllegalStateException(); 110 } else if (m_fragments.size() == 1) { 111 m_fragments.get(0).visit(builder); 112 } else { 113 builder.add("("); 114 for (int i = 0; i < m_fragments.size(); i++) { 115 if (i != 0) { 116 builder.add(connector); 117 } 118 m_fragments.get(i).visit(builder); 119 } 120 builder.add(")"); 121 } 122 } 123 124}