001/* 002 * Copyright 2010 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.web.tags; 017 018import org.ametys.plugins.repository.AmetysRepositoryException; 019import org.ametys.plugins.repository.query.expression.Expression; 020import org.ametys.web.repository.content.jcr.TaggableAmetysObjectHelper; 021 022/** 023 * Constructs an {@link Expression} corresponding to a tag comparison. 024 */ 025public class TagExpression implements Expression 026{ 027 /** The logical operator to use in xpath query */ 028 public enum LogicalOperator 029 { 030 /** Logical operator AND */ 031 AND 032 { 033 @Override 034 public String toString() 035 { 036 return " and "; 037 } 038 }, 039 /** Logical operator OR */ 040 OR 041 { 042 @Override 043 public String toString() 044 { 045 return " or "; 046 } 047 } 048 } 049 050 private Operator _operator; 051 private String[] _values; 052 private LogicalOperator _logicalOperator; 053 054 /** 055 * Creates the expression. 056 * @param operator the operator to make the comparison (only Operator.EQ and Operator.NE allowed) 057 * @param value the tag value 058 */ 059 public TagExpression(Operator operator, String value) 060 { 061 if (Operator.EQ != operator && Operator.NE != operator) 062 { 063 throw new AmetysRepositoryException("Test operator '" + "' is unknown for test's expression."); 064 } 065 066 _operator = operator; 067 _values = new String[]{value}; 068 } 069 070 /** 071 * Creates the expression. 072 * @param operator the operator to make the comparison (only Operator.EQ and Operator.NE allowed) 073 * @param values the tags value in a array 074 * @param logicalOperator the logical operator to use for given value 075 */ 076 public TagExpression(Operator operator, String[] values, LogicalOperator logicalOperator) 077 { 078 if (Operator.EQ != operator && Operator.NE != operator) 079 { 080 throw new AmetysRepositoryException("Test operator '" + "' is unknown for test's expression."); 081 } 082 083 _operator = operator; 084 _values = values; 085 _logicalOperator = logicalOperator; 086 } 087 088 089 @Override 090 public String build() 091 { 092 StringBuffer sb = new StringBuffer(); 093 if (_values.length > 1) 094 { 095 sb.append("("); 096 } 097 for (int i = 0; i < _values.length; i++) 098 { 099 if (i != 0) 100 { 101 sb.append(_logicalOperator); 102 } 103 sb.append("@" + TaggableAmetysObjectHelper.METADATA_TAGS + " " + _operator + "'" + _values[i].replaceAll("'", "''") + "'"); 104 } 105 if (_values.length > 1) 106 { 107 sb.append(")"); 108 } 109 110 return sb.toString(); 111 } 112}