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.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 |
|
|
|
46 |
|
|
|
47 |
|
|
|
48 |
|
|
|
49 |
|
|
|
50 |
|
|
public final class SingleValueLikeFilter extends AbstractFormFilter {
|
51 |
|
|
|
52 |
|
|
|
53 |
|
|
|
54 |
|
|
|
55 |
|
|
private enum TYPE_COMPONENT {
|
56 |
|
|
|
57 |
|
|
HDNUMBERFIELD,
|
58 |
|
|
}
|
59 |
|
|
|
60 |
|
|
|
61 |
|
|
|
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 |
|
|
|
97 |
|
|
while (filterExists) {
|
98 |
|
|
|
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 |
|
|
|
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 |
|
|
|
169 |
|
|
|
170 |
|
|
|
171 |
|
|
|
172 |
|
|
@Override
|
173 |
|
|
public Validator<AbstractFilter> getValidator() {
|
174 |
|
|
return new Validator<AbstractFilter>() {
|
175 |
|
|
@Override
|
176 |
|
|
public Set<ConstraintViolation> validate(AbstractFilter item) {
|
177 |
|
|
|
178 |
|
|
return null;
|
179 |
|
|
}
|
180 |
|
|
};
|
181 |
|
|
}
|
182 |
|
|
|
183 |
|
|
} |