git_sitools_idoc / sitools-idoc / hesiod / javaExt / src / fr / ias / sitools / filters / basic / SingleValueLikeFilter.java @ 779bac69
1 |
/*******************************************************************************
|
---|---|
2 |
* Copyright 2010-2013 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.HashMap; |
22 |
import java.util.List; |
23 |
import java.util.Set; |
24 |
|
25 |
import org.restlet.Request; |
26 |
import org.restlet.data.Form; |
27 |
import org.restlet.ext.wadl.ParameterInfo; |
28 |
import org.restlet.ext.wadl.ParameterStyle; |
29 |
|
30 |
import fr.cnes.sitools.common.validator.ConstraintViolation; |
31 |
import fr.cnes.sitools.common.validator.Validator; |
32 |
import fr.cnes.sitools.dataset.DataSetApplication; |
33 |
import fr.cnes.sitools.dataset.filter.business.AbstractFilter; |
34 |
import fr.cnes.sitools.dataset.model.Column; |
35 |
import fr.cnes.sitools.dataset.model.DataSet; |
36 |
import fr.cnes.sitools.dataset.model.Operator; |
37 |
import fr.cnes.sitools.dataset.model.Predicat; |
38 |
import fr.cnes.sitools.dataset.plugins.filters.core.AbstractFormFilter; |
39 |
import static fr.cnes.sitools.dataset.plugins.filters.core.AbstractFormFilter.TEMPLATE_PARAM; |
40 |
import static fr.cnes.sitools.dataset.plugins.filters.core.AbstractFormFilter.TEMPLATE_PARAM_CONCEPT; |
41 |
import static fr.cnes.sitools.dataset.plugins.filters.core.AbstractFormFilter.VALUES; |
42 |
import fr.cnes.sitools.util.SQLUtils; |
43 |
|
44 |
/**
|
45 |
* Filter defined for Single Value Component
|
46 |
*
|
47 |
*
|
48 |
* @author d.arpin
|
49 |
*/
|
50 |
public final class SingleValueLikeFilter extends AbstractFormFilter { |
51 |
|
52 |
/**
|
53 |
* The list of component that uses this filter
|
54 |
*/
|
55 |
private enum TYPE_COMPONENT { |
56 |
/** Hd Number field type */
|
57 |
HDNUMBERFIELD, |
58 |
} |
59 |
|
60 |
/**
|
61 |
* Default constructor
|
62 |
*/
|
63 |
public SingleValueLikeFilter() {
|
64 |
|
65 |
super();
|
66 |
this.setName("SingleValueLikeFilter"); |
67 |
this.setDescription("Required when using Hd Number Text Field Component"); |
68 |
|
69 |
this.setClassAuthor("MNICOLAS"); |
70 |
this.setClassOwner("IAS"); |
71 |
this.setClassVersion("0.1"); |
72 |
this.setDefaultFilter(true); |
73 |
|
74 |
HashMap<String, ParameterInfo> rpd = new HashMap<String, ParameterInfo>(); |
75 |
|
76 |
ParameterInfo paramInfo; |
77 |
paramInfo = new ParameterInfo("p[#]", false, "xs:string", ParameterStyle.QUERY, "HDNUMBERFIELD|columnAlias|value"); |
78 |
rpd.put("0", paramInfo);
|
79 |
paramInfo = new ParameterInfo("c[#]", false, "xs:string", ParameterStyle.QUERY, |
80 |
"HDNUMBERFIELD|dictionaryName,conceptName|value");
|
81 |
rpd.put("1", paramInfo);
|
82 |
|
83 |
this.setRequestParamsDescription(rpd);
|
84 |
//
|
85 |
|
86 |
} |
87 |
|
88 |
@Override
|
89 |
public List<Predicat> createPredicats(Request request, List<Predicat> predicats) throws Exception { |
90 |
DataSetApplication dsApplication = null;
|
91 |
DataSet ds = null;
|
92 |
boolean isConcept = true; |
93 |
Form params = request.getResourceRef().getQueryAsForm(); |
94 |
boolean filterExists = true; |
95 |
int i = 0; |
96 |
// Build predicat for filters param
|
97 |
while (filterExists) {
|
98 |
// first check if the filter is applied on a Concept or not
|
99 |
String index = TEMPLATE_PARAM_CONCEPT.replace("#", Integer.toString(i)); |
100 |
String formParam = params.getFirstValue(index);
|
101 |
if (formParam == null) { |
102 |
isConcept = false;
|
103 |
index = TEMPLATE_PARAM.replace("#", Integer.toString(i)); |
104 |
formParam = params.getFirstValue(index); |
105 |
} |
106 |
i++; |
107 |
if (formParam != null) { |
108 |
String[] parameters = formParam.split("\\|"); |
109 |
TYPE_COMPONENT[] types = TYPE_COMPONENT.values();
|
110 |
Boolean trouve = false; |
111 |
for (TYPE_COMPONENT typeCmp : types) {
|
112 |
if (typeCmp.name().equals(parameters[TYPE])) {
|
113 |
trouve = true;
|
114 |
} |
115 |
} |
116 |
if (trouve) {
|
117 |
if (dsApplication == null) { |
118 |
dsApplication = (DataSetApplication) getContext().getAttributes().get("DataSetApplication");
|
119 |
ds = dsApplication.getDataSet(); |
120 |
} |
121 |
String columnAlias = null; |
122 |
if (parameters.length >= VALUES) {
|
123 |
|
124 |
columnAlias = getColumnAlias(isConcept, parameters, dsApplication); |
125 |
if (columnAlias != null) { |
126 |
Column col = ds.findByColumnAlias(columnAlias); |
127 |
if (col != null && col.getFilter() != null && col.getFilter()) { |
128 |
// get the value and escape it to avoid SQL injection
|
129 |
String value = SQLUtils.escapeString(parameters[VALUES]);
|
130 |
Predicat predicat = new Predicat();
|
131 |
if (value != null) { |
132 |
predicat.setLeftAttribute(col); |
133 |
predicat.setNbOpenedParanthesis(0);
|
134 |
predicat.setNbClosedParanthesis(0);
|
135 |
if (parameters[TYPE].equals(TYPE_COMPONENT.HDNUMBERFIELD.name())) {
|
136 |
if(value.contains("HD")){ |
137 |
if(value.charAt(2) != ' '){ |
138 |
value = value.substring(2);
|
139 |
value = "%"+value+"%"; |
140 |
} |
141 |
|
142 |
}else{
|
143 |
value = "%"+value+"%"; |
144 |
} |
145 |
predicat.setCompareOperator(Operator.LIKE); |
146 |
}else {
|
147 |
predicat.setCompareOperator(Operator.EQ); |
148 |
} |
149 |
predicat.setRightValue("'" + value + "'"); |
150 |
|
151 |
predicats.add(predicat); |
152 |
} |
153 |
} |
154 |
} |
155 |
} |
156 |
} |
157 |
} |
158 |
|
159 |
else {
|
160 |
filterExists = false;
|
161 |
} |
162 |
} |
163 |
|
164 |
return predicats;
|
165 |
} |
166 |
|
167 |
/**
|
168 |
* Gets the validator for this Filter
|
169 |
*
|
170 |
* @return the validator for the filter
|
171 |
*/
|
172 |
@Override
|
173 |
public Validator<AbstractFilter> getValidator() { |
174 |
return new Validator<AbstractFilter>() { |
175 |
@Override
|
176 |
public Set<ConstraintViolation> validate(AbstractFilter item) { |
177 |
// TODO Auto-generated method stub
|
178 |
return null; |
179 |
} |
180 |
}; |
181 |
} |
182 |
|
183 |
} |