001/* 002 * Copyright 2016 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.plugins.repository.activities; 017 018import org.ametys.plugins.repository.AmetysRepositoryException; 019import org.ametys.plugins.repository.query.expression.Expression; 020import org.ametys.plugins.repository.query.expression.MetadataExpression; 021 022/** 023 * Constructs an {@link Expression} corresponding to a activity'type comparison. 024 */ 025public class ActivityTypeExpression implements Expression 026{ 027 private static final String __METADATA_EXPRESSION = new MetadataExpression(ActivityFactory.TYPE).build(); 028 private Operator _operator; 029 private String[] _values; 030 031 /** 032 * Creates the expression. 033 * @param operator the operator to make the comparison (only Operator.EQ and Operator.NE allowed) 034 * @param value the activity type id value 035 */ 036 public ActivityTypeExpression(Operator operator, String value) 037 { 038 if (Operator.EQ != operator && Operator.NE != operator) 039 { 040 throw new AmetysRepositoryException("Test operator '" + "' is unknown for test's expression."); 041 } 042 043 _operator = operator; 044 _values = new String[]{value}; 045 } 046 047 /** 048 * Creates the expression. 049 * @param operator the operator to make the comparison (only Operator.EQ and Operator.NE allowed) 050 * @param values the activity' type values 051 */ 052 public ActivityTypeExpression(Operator operator, String... values) 053 { 054 _operator = operator; 055 _values = values; 056 } 057 058 @Override 059 public String build() 060 { 061 StringBuilder xpath = new StringBuilder(); 062 063 if (_values.length == 1) 064 { 065 xpath.append(__METADATA_EXPRESSION) 066 .append(' ').append(_operator).append(" '").append(_values[0].replaceAll("'", "''")).append("'"); 067 068 return xpath.toString(); 069 } 070 071 xpath.append(Operator.EQ.equals(_operator) ? '(' : "not("); 072 073 for (int i = 0; i < _values.length; i++) 074 { 075 if (i > 0) 076 { 077 xpath.append(" or "); 078 } 079 080 xpath.append(__METADATA_EXPRESSION) 081 .append(" = '").append(_values[i].replaceAll("'", "''")).append("'"); 082 } 083 084 xpath.append(')'); 085 086 return xpath.toString(); 087 } 088}