git_sitools_idoc / hesiod / javaExt / src / fr / ias / sitools / filters / basic / BooleanCustom.java @ 6552a8ce
1 |
/*******************************************************************************
|
---|---|
2 |
* Copyright 2011 CNES - CENTRE NATIONAL d'ETUDES SPATIALES
|
3 |
*
|
4 |
* This file is part of SITools2.
|
5 |
*
|
6 |
* SITools2 is free software: you can redistribute it and/or modify
|
7 |
* it under the terms of the GNU General Public License as published by
|
8 |
* the Free Software Foundation, either version 3 of the License, or
|
9 |
* (at your option) any later version.
|
10 |
*
|
11 |
* SITools2 is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with SITools2. If not, see <http://www.gnu.org/licenses/>.
|
18 |
******************************************************************************/
|
19 |
package fr.ias.sitools.filters.basic; |
20 |
|
21 |
import java.util.Arrays; |
22 |
import java.util.HashMap; |
23 |
import java.util.List; |
24 |
import java.util.Set; |
25 |
import java.util.logging.Logger; |
26 |
|
27 |
import org.restlet.Request; |
28 |
import org.restlet.data.Form; |
29 |
import org.restlet.ext.wadl.ParameterInfo; |
30 |
import org.restlet.ext.wadl.ParameterStyle; |
31 |
|
32 |
import fr.cnes.sitools.common.validator.ConstraintViolation; |
33 |
import fr.cnes.sitools.common.validator.Validator; |
34 |
import fr.cnes.sitools.dataset.DataSetApplication; |
35 |
import fr.cnes.sitools.dataset.filter.business.AbstractFilter; |
36 |
import fr.cnes.sitools.dataset.model.Column; |
37 |
import fr.cnes.sitools.dataset.model.DataSet; |
38 |
import fr.cnes.sitools.dataset.model.Operator; |
39 |
import fr.cnes.sitools.dataset.model.Predicat; |
40 |
import fr.cnes.sitools.util.SQLUtils; |
41 |
|
42 |
/**
|
43 |
* Filter defined for Single Value Component
|
44 |
*
|
45 |
*
|
46 |
* @author d.arpin
|
47 |
*/
|
48 |
public final class BooleanCustom extends AbstractFilter { |
49 |
/**
|
50 |
* The index of TYPE
|
51 |
*/
|
52 |
private static final int TYPE = 0; |
53 |
/**
|
54 |
* The index of COLUMN
|
55 |
*/
|
56 |
private static final int COLUMN = 1; |
57 |
/**
|
58 |
* The index of Values
|
59 |
*/
|
60 |
private static final int VALUES = 2; |
61 |
|
62 |
/**
|
63 |
* The number of values
|
64 |
*/
|
65 |
private static final int NUMBER_OF_VALUES = 1; |
66 |
|
67 |
/**
|
68 |
* The list of component that uses this filter
|
69 |
*/
|
70 |
private enum TYPE_COMPONENT { |
71 |
/** Boolean Custom type */
|
72 |
BOOLEAN_CUSTOM |
73 |
} |
74 |
|
75 |
private String[] values; |
76 |
|
77 |
private static final Logger LOGGER = Logger.getLogger(BooleanCustom.class.getName()); |
78 |
|
79 |
/** The TEMPLATE_PARAM */
|
80 |
private static final String TEMPLATE_PARAM = "p[#]"; |
81 |
|
82 |
/**
|
83 |
* Default constructor
|
84 |
*/
|
85 |
public BooleanCustom() {
|
86 |
|
87 |
super();
|
88 |
this.setName("BooleanCustom"); |
89 |
this.setDescription("Required when using Boolean Custom Component"); |
90 |
this.setClassAuthor("Marc NICOLAS"); |
91 |
this.setClassOwner("IAS"); |
92 |
this.setClassVersion("0.9"); |
93 |
this.setDefaultFilter(true); |
94 |
|
95 |
HashMap<String, ParameterInfo> rpd = new HashMap<String, ParameterInfo>(); |
96 |
|
97 |
ParameterInfo paramInfo; |
98 |
paramInfo = new ParameterInfo("p[#]", false, "xs:string", ParameterStyle.QUERY, "BOOLEAN_CUSTOM|columnAlias|value"); |
99 |
rpd.put("0", paramInfo);
|
100 |
this.setRequestParamsDescription(rpd);
|
101 |
|
102 |
} |
103 |
|
104 |
@Override
|
105 |
public List<Predicat> createPredicats(Request request, List<Predicat> predicats) throws Exception { |
106 |
|
107 |
DataSetApplication dsApplication = (DataSetApplication) getContext().getAttributes().get("DataSetApplication");
|
108 |
DataSet ds = dsApplication.getDataSet(); |
109 |
|
110 |
Form params = request.getResourceRef().getQueryAsForm(); |
111 |
boolean filterExists = true; |
112 |
int i = 0; |
113 |
// Build predicat for filters param
|
114 |
while (filterExists) {
|
115 |
String index = TEMPLATE_PARAM.replace("#", Integer.toString(i++)); |
116 |
String formParam = params.getFirstValue(index);
|
117 |
if (formParam != null) { |
118 |
String[] parameters = formParam.split("\\|"); |
119 |
TYPE_COMPONENT[] types = TYPE_COMPONENT.values();
|
120 |
Boolean trouve = false; |
121 |
for (TYPE_COMPONENT typeCmp : types) {
|
122 |
if (typeCmp.name().equals(parameters[TYPE])) {
|
123 |
trouve = true;
|
124 |
} |
125 |
} |
126 |
|
127 |
if (trouve) {
|
128 |
if (checkValues(parameters)) {
|
129 |
String columnAlias = null; |
130 |
if (parameters.length >= VALUES) {
|
131 |
|
132 |
columnAlias = parameters[COLUMN]; |
133 |
Column col = ds.findByColumnAlias(columnAlias); |
134 |
if (col != null && col.getFilter() != null && col.getFilter()) { |
135 |
// get the value and escape it to avoid SQL injection
|
136 |
String value = SQLUtils.escapeString(parameters[VALUES]);
|
137 |
Predicat predicat = new Predicat();
|
138 |
if (value != null) { |
139 |
predicat.setLeftAttribute(col); |
140 |
predicat.setNbOpenedParanthesis(0);
|
141 |
predicat.setNbClosedParanthesis(0);
|
142 |
predicat.setCompareOperator(Operator.EQ); |
143 |
predicat.setRightValue("'" + value + "'"); |
144 |
if(value.equalsIgnoreCase("false")){ |
145 |
predicats.add(predicat); |
146 |
} |
147 |
} |
148 |
} |
149 |
} |
150 |
} |
151 |
} |
152 |
} |
153 |
|
154 |
else {
|
155 |
filterExists = false;
|
156 |
} |
157 |
} |
158 |
|
159 |
return predicats;
|
160 |
} |
161 |
|
162 |
/**
|
163 |
* Check the number of values
|
164 |
*
|
165 |
* @param parameters
|
166 |
* the values
|
167 |
* @return true if the number of values is correct
|
168 |
*/
|
169 |
private boolean checkValues(String[] parameters) { |
170 |
if(parameters.length >= 3){ |
171 |
values = Arrays.copyOfRange(parameters, VALUES, VALUES + NUMBER_OF_VALUES);
|
172 |
if (values.length == NUMBER_OF_VALUES) {
|
173 |
return true; |
174 |
} |
175 |
return false; |
176 |
}else{
|
177 |
return false; |
178 |
} |
179 |
} |
180 |
|
181 |
|
182 |
/**
|
183 |
* Gets the validator for this Filter
|
184 |
*
|
185 |
* @return the validator for the filter
|
186 |
*/
|
187 |
@Override
|
188 |
public Validator<AbstractFilter> getValidator() { |
189 |
return new Validator<AbstractFilter>() { |
190 |
@Override
|
191 |
public Set<ConstraintViolation> validate(AbstractFilter item) { |
192 |
// TODO Auto-generated method stub
|
193 |
return null; |
194 |
} |
195 |
}; |
196 |
} |
197 |
|
198 |
} |