1 |
779bac69
|
Marc NICOLAS
|
|
2 |
|
|
|
3 |
|
|
|
4 |
|
|
|
5 |
|
|
|
6 |
|
|
|
7 |
|
|
|
8 |
|
|
|
9 |
|
|
|
10 |
|
|
|
11 |
|
|
|
12 |
|
|
|
13 |
|
|
|
14 |
|
|
|
15 |
|
|
|
16 |
|
|
|
17 |
|
|
|
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 |
|
|
|
43 |
|
|
|
44 |
|
|
|
45 |
|
|
|
46 |
|
|
|
47 |
|
|
|
48 |
|
|
|
49 |
|
|
public final class DateBeginEndFilter extends AbstractFilter {
|
50 |
|
|
|
51 |
|
|
|
52 |
|
|
|
53 |
|
|
private static final int TYPE = 0;
|
54 |
|
|
|
55 |
|
|
|
56 |
|
|
|
57 |
|
|
private static final int COLUMN = 1;
|
58 |
|
|
|
59 |
|
|
|
60 |
|
|
|
61 |
|
|
private static final int VALUES = 2;
|
62 |
|
|
|
63 |
|
|
|
64 |
|
|
private static final String TEMPLATE_PARAM = "p[#]";
|
65 |
|
|
|
66 |
|
|
|
67 |
|
|
|
68 |
|
|
private String[] values;
|
69 |
|
|
|
70 |
|
|
|
71 |
|
|
|
72 |
|
|
|
73 |
|
|
private enum TYPE_COMPONENT {
|
74 |
|
|
|
75 |
|
|
DATE_BEGIN_END
|
76 |
|
|
}
|
77 |
|
|
|
78 |
|
|
|
79 |
|
|
|
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 |
|
|
|
107 |
|
|
DataSetApplication dsApplication = null;
|
108 |
|
|
DataSet ds = null;
|
109 |
|
|
|
110 |
|
|
Form params = request.getResourceRef().getQueryAsForm();
|
111 |
|
|
boolean filterExists = true;
|
112 |
|
|
int i = 0;
|
113 |
|
|
|
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 |
|
|
|
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 |
|
|
|
175 |
|
|
|
176 |
|
|
|
177 |
|
|
|
178 |
|
|
|
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 |
|
|
|
190 |
|
|
|
191 |
|
|
|
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 |
|
|
}
|