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