git_sitools_idoc / sitools-idoc / hesiod / javaExt / src / fr / ias / sitools / filters / basic / DateBeginEndFilter.java @ 779bac69
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 |
|
26 |
import org.restlet.Request; |
27 |
import org.restlet.data.Form; |
28 |
import org.restlet.ext.wadl.ParameterInfo; |
29 |
import org.restlet.ext.wadl.ParameterStyle; |
30 |
|
31 |
import fr.cnes.sitools.common.validator.ConstraintViolation; |
32 |
import fr.cnes.sitools.common.validator.Validator; |
33 |
import fr.cnes.sitools.dataset.DataSetApplication; |
34 |
import fr.cnes.sitools.dataset.filter.business.AbstractFilter; |
35 |
import fr.cnes.sitools.dataset.model.Column; |
36 |
import fr.cnes.sitools.dataset.model.DataSet; |
37 |
import fr.cnes.sitools.dataset.model.Operator; |
38 |
import fr.cnes.sitools.dataset.model.Predicat; |
39 |
import fr.cnes.sitools.util.SQLUtils; |
40 |
|
41 |
/**
|
42 |
* Filter defined for Date Between Component
|
43 |
*
|
44 |
*
|
45 |
* @author d.arpin
|
46 |
* <a href="https://sourceforge.net/tracker/?func=detail&atid=2158259&aid=3411383&group_id=531341">[3411383]</a><br/>
|
47 |
* 2011/09/19 d.arpin {add quotes arround date Value}
|
48 |
*/
|
49 |
public final class DateBeginEndFilter extends AbstractFilter { |
50 |
/**
|
51 |
* The index of TYPE
|
52 |
*/
|
53 |
private static final int TYPE = 0; |
54 |
/**
|
55 |
* The index of COLUMN
|
56 |
*/
|
57 |
private static final int COLUMN = 1; |
58 |
/**
|
59 |
* The index of Values
|
60 |
*/
|
61 |
private static final int VALUES = 2; |
62 |
|
63 |
/** The TEMPLATE_PARAM */
|
64 |
private static final String TEMPLATE_PARAM = "p[#]"; |
65 |
/**
|
66 |
* the values from to
|
67 |
*/
|
68 |
private String[] values; |
69 |
|
70 |
/**
|
71 |
* The list of component that uses this filter
|
72 |
*/
|
73 |
private enum TYPE_COMPONENT { |
74 |
/** DefaultType */
|
75 |
DATE_BEGIN_END |
76 |
} |
77 |
|
78 |
/**
|
79 |
* Default constructor
|
80 |
*/
|
81 |
public DateBeginEndFilter() {
|
82 |
|
83 |
super();
|
84 |
this.setName("DateBeginEndFilter"); |
85 |
this.setDescription("Required when using Date Between Components between 2 columns"); |
86 |
|
87 |
this.setClassAuthor("HUSSON@IAS"); |
88 |
this.setClassAuthor("HUSSON@IAS"); |
89 |
this.setClassOwner("IAS"); |
90 |
this.setClassVersion("0.1"); |
91 |
this.setDefaultFilter(true); |
92 |
|
93 |
HashMap<String, ParameterInfo> rpd = new HashMap<String, ParameterInfo>(); |
94 |
|
95 |
ParameterInfo paramInfo; |
96 |
paramInfo = new ParameterInfo("p[#]", false, "xs:string", ParameterStyle.QUERY, |
97 |
"DATE_BEGIN_END|columnFrom,columnTo|valueFrom|valueTo");
|
98 |
rpd.put("0", paramInfo);
|
99 |
this.setRequestParamsDescription(rpd);
|
100 |
//
|
101 |
|
102 |
} |
103 |
|
104 |
@Override
|
105 |
public List<Predicat> createPredicats(Request request, List<Predicat> predicats) throws Exception { |
106 |
// Get the dataset
|
107 |
DataSetApplication dsApplication = null;
|
108 |
DataSet ds = null;
|
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 |
if (trouve) {
|
127 |
if (dsApplication == null) { |
128 |
dsApplication = (DataSetApplication) getContext().getAttributes().get("DataSetApplication");
|
129 |
ds = dsApplication.getDataSet(); |
130 |
} |
131 |
String columnfrom = null; |
132 |
String columnto = null; |
133 |
if (parameters.length >= VALUES) {
|
134 |
columnfrom = parameters[COLUMN].split(",")[0]; |
135 |
columnto = parameters[COLUMN].split(",")[1]; |
136 |
|
137 |
Column colfrom = ds.findByColumnAlias(columnfrom); |
138 |
Column colto = ds.findByColumnAlias(columnto); |
139 |
|
140 |
if (colfrom != null && colfrom.getFilter() != null && colfrom.getFilter() && checkValues(parameters) && colto != null && colto.getFilter() != null && colto.getFilter()) { |
141 |
// escape the values to avoid SQL injection
|
142 |
String valuefrom = "'" + SQLUtils.escapeString(values[0]) + "'"; |
143 |
String valueto = "'" + SQLUtils.escapeString(values[1]) + "'"; |
144 |
|
145 |
Predicat predicat = new Predicat();
|
146 |
predicat.setLeftAttribute(colfrom); |
147 |
predicat.setNbOpenedParanthesis(1);
|
148 |
predicat.setNbClosedParanthesis(0);
|
149 |
predicat.setCompareOperator(Operator.GTE); |
150 |
predicat.setRightValue(valuefrom); |
151 |
predicats.add(predicat); |
152 |
predicat = new Predicat();
|
153 |
predicat.setLeftAttribute(colto); |
154 |
predicat.setNbOpenedParanthesis(0);
|
155 |
predicat.setNbClosedParanthesis(1);
|
156 |
predicat.setCompareOperator(Operator.LTE); |
157 |
predicat.setRightValue(valueto); |
158 |
predicats.add(predicat); |
159 |
} |
160 |
|
161 |
} |
162 |
} |
163 |
} |
164 |
|
165 |
else {
|
166 |
filterExists = false;
|
167 |
} |
168 |
} |
169 |
|
170 |
return predicats;
|
171 |
} |
172 |
|
173 |
/**
|
174 |
* Check values of the form
|
175 |
*
|
176 |
* @param parameters
|
177 |
* the parameters of the filter
|
178 |
* @return true if values agree
|
179 |
*/
|
180 |
private boolean checkValues(String[] parameters) { |
181 |
values = Arrays.copyOfRange(parameters, VALUES, parameters.length);
|
182 |
if (values.length == 2) { |
183 |
return true; |
184 |
} |
185 |
return false; |
186 |
} |
187 |
|
188 |
/**
|
189 |
* Gets the validator for this Filter
|
190 |
*
|
191 |
* @return the validator for the filter
|
192 |
*/
|
193 |
@Override
|
194 |
public Validator<AbstractFilter> getValidator() { |
195 |
return new Validator<AbstractFilter>() { |
196 |
@Override
|
197 |
public Set<ConstraintViolation> validate(AbstractFilter item) { |
198 |
return null; |
199 |
} |
200 |
}; |
201 |
} |
202 |
|
203 |
} |