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.cms.tag.jcr.TaggableAmetysObjectHelper; 019import org.ametys.plugins.repository.AmetysRepositoryException; 020import org.ametys.plugins.repository.query.expression.Expression; 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 private String _metadataName; 054 055 /** 056 * Creates the expression. 057 * @param operator the operator to make the comparison (only Operator.EQ and Operator.NE allowed) 058 * @param value the tag value 059 */ 060 public TagExpression(Operator operator, String value) 061 { 062 this(operator, value, TaggableAmetysObjectHelper.DEFAULT_METADATA_TAGS); 063 } 064 065 /** 066 * Creates the expression. 067 * @param operator the operator to make the comparison (only Operator.EQ and Operator.NE allowed) 068 * @param value the tag value 069 * @param metadataName the metadata holding the tags 070 */ 071 public TagExpression(Operator operator, String value, String metadataName) 072 { 073 if (Operator.EQ != operator && Operator.NE != operator) 074 { 075 throw new AmetysRepositoryException("Test operator '" + "' is unknown for test's expression."); 076 } 077 078 _operator = operator; 079 _values = new String[]{value}; 080 _metadataName = metadataName; 081 } 082 083 084 /** 085 * Creates the expression. 086 * @param operator the operator to make the comparison (only Operator.EQ and Operator.NE allowed) 087 * @param values the tags value in a array 088 * @param logicalOperator the logical operator to use for given value 089 */ 090 public TagExpression(Operator operator, String[] values, LogicalOperator logicalOperator) 091 { 092 this(operator, values, logicalOperator, TaggableAmetysObjectHelper.DEFAULT_METADATA_TAGS); 093 } 094 095 /** 096 * Creates the expression. 097 * @param operator the operator to make the comparison (only Operator.EQ and Operator.NE allowed) 098 * @param values the tags value in a array 099 * @param logicalOperator the logical operator to use for given value 100 * @param metadataName the metadata holding the tags 101 */ 102 public TagExpression(Operator operator, String[] values, LogicalOperator logicalOperator, String metadataName) 103 { 104 if (Operator.EQ != operator && Operator.NE != operator) 105 { 106 throw new AmetysRepositoryException("Test operator '" + "' is unknown for test's expression."); 107 } 108 109 _operator = operator; 110 _values = values; 111 _logicalOperator = logicalOperator; 112 _metadataName = metadataName; 113 } 114 115 @Override 116 public String build() 117 { 118 StringBuffer sb = new StringBuffer(); 119 if (_values.length > 1) 120 { 121 sb.append("("); 122 } 123 for (int i = 0; i < _values.length; i++) 124 { 125 if (i != 0) 126 { 127 sb.append(_logicalOperator); 128 } 129 sb.append("@" + _metadataName + " " + _operator + "'" + _values[i].replaceAll("'", "''") + "'"); 130 } 131 if (_values.length > 1) 132 { 133 sb.append(")"); 134 } 135 136 return sb.toString(); 137 } 138}