Project

General

Profile

Download (7.19 KB) Statistics
| Branch: | Revision:

git_sitools_idoc / hesiod / javaExt / src / fr / ias / sitools / filters / basic / ListBoxLumClassCorot.java @ 6552a8ce

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.TYPE;
44
import static fr.cnes.sitools.dataset.plugins.filters.core.AbstractFormFilter.VALUES;
45
import java.util.logging.Level;
46

    
47
/**
48
 * Filter defined for Multiple Value Component
49
 * 
50
 * 
51
 * @author d.arpin
52
 */
53
public final class ListBoxLumClassCorot extends AbstractFormFilter {
54

    
55
  /**
56
   * The list of component that uses this filter
57
   */
58
  private enum TYPE_COMPONENT {
59
    /** List box Lum class for Corot */
60
    LISTBOXLUMCLASSCOROT
61
  }
62
  
63
  /*
64
    Pour EXO : 
65
    ""
66
    "I"
67
    "II"
68
    "III"
69
    "III:"
70
    "IV"
71
    "null"
72
    "V"
73
    ""
74
  
75
    Pour Astero : 
76
    ""
77
    "I"
78
    "II"
79
    "III"
80
    "III-IV"
81
    "IV"
82
    "V"
83

84
  */
85

    
86
  /**
87
   * Default constructor
88
   */
89
  public ListBoxLumClassCorot() {
90

    
91
    super();
92
    this.setName("ListBoxLumClassCorot");
93
    this.setDescription("Required when using List Box Lum Class for Corot Components");
94

    
95
    this.setClassAuthor("Mnicolas");
96
    this.setClassOwner("IAS");
97
    this.setClassVersion("0.1");
98
    this.setDefaultFilter(true);
99

    
100
    HashMap<String, ParameterInfo> rpd = new HashMap<String, ParameterInfo>();
101

    
102
    ParameterInfo paramInfo;
103
    paramInfo = new ParameterInfo("p[#]", false, "xs:string", ParameterStyle.QUERY,
104
        "LISTBOXLUMCLASSCOROT|columnAlias|value1|...|value n");
105
    rpd.put("0", paramInfo);
106
    this.setRequestParamsDescription(rpd);
107

    
108
  }
109

    
110
  @Override
111
  public List<Predicat> createPredicats(Request request, List<Predicat> predicats) throws Exception {
112
    // Get the dataset
113
    DataSetApplication dsApplication = null;
114
    DataSet ds = null;
115
    boolean isConcept = true;
116
    Form params = request.getResourceRef().getQueryAsForm();
117
    boolean filterExists = true;
118
    int i = 0;
119
    // Build predicat for filters param
120
    while (filterExists) {
121
      // first check if the filter is applied on a Concept or not
122
      String index = TEMPLATE_PARAM_CONCEPT.replace("#", Integer.toString(i));
123
      String formParam = params.getFirstValue(index);
124
      if (formParam == null) {
125
        isConcept = false;
126
        index = TEMPLATE_PARAM.replace("#", Integer.toString(i));
127
        formParam = params.getFirstValue(index);
128
      }
129
      i++;
130
      if (formParam != null) {
131
        String[] parameters = formParam.split("\\|");
132
        TYPE_COMPONENT[] types = TYPE_COMPONENT.values();
133
        Boolean trouve = false;
134
        for (TYPE_COMPONENT typeCmp : types) {
135
          if (typeCmp.name().equals(parameters[TYPE])) {
136
            trouve = true;
137
          }
138
        }
139
        if (trouve) {
140
          if (dsApplication == null) {
141
            dsApplication = (DataSetApplication) getContext().getAttributes().get("DataSetApplication");
142
            ds = dsApplication.getDataSet();
143
          }
144
          String columnAlias = null;
145
          if (parameters.length >= VALUES) {
146

    
147
            /*
148
             * columnsAlias = parameters[COLUMN].split(","); ArrayList<Column> columns = new ArrayList<Column>(); for
149
             * (String columnAlias : columnsAlias) { Column col = ds.findByColumnAlias(columnAlias); if (col != null) {
150
             * columns.add(col); }
151
             * 
152
             * }
153
             */
154
            columnAlias = getColumnAlias(isConcept, parameters, dsApplication);
155
            if (columnAlias != null) {
156
              Column col = ds.findByColumnAlias(columnAlias);
157
              List<String> lumClassSeeked = new ArrayList<String>();
158

    
159
              if (col != null && col.getFilter() != null && col.getFilter()) {
160
                String[] values = Arrays.copyOfRange(parameters, VALUES, parameters.length);
161
                Predicat predicat = new Predicat();
162
                if (values != null) {
163
                  predicat.setLeftAttribute(col);
164
                  predicat.setNbOpenedParanthesis(0);
165
                  predicat.setNbClosedParanthesis(0);
166
                  predicat.setCompareOperator(Operator.IN);
167

    
168
                  boolean all = false;
169
                  for (String value : values) {
170
                      if(value.equalsIgnoreCase("All")){
171
                        all = true;
172
                      }else{
173
                          if(value.equalsIgnoreCase(" ")){
174
                              lumClassSeeked.add(value);
175
                              lumClassSeeked.add("null");
176
                          }else if(value.equalsIgnoreCase("III")){
177
                              lumClassSeeked.add(value);
178
                              lumClassSeeked.add("III:");
179
                          }else{
180
                            lumClassSeeked.add(value);
181
                          }
182
                      }
183
                  }
184
                  getContext().getLogger().log(Level.INFO, "lumClassSeeked : "+lumClassSeeked.toArray().toString());
185
                  if(!lumClassSeeked.isEmpty() && !all) {
186
                    predicat.setRightValue(lumClassSeeked);
187
                    predicats.add(predicat);
188
                  }
189
                }
190
              }
191
            }
192

    
193
          }
194
        }
195
      }else {
196
        filterExists = false;
197
      }
198
    }
199

    
200
    return predicats;
201
  }
202

    
203
  /**
204
   * Gets the validator for this Filter
205
   * 
206
   * @return the validator for the filter
207
   */
208
  @Override
209
  public Validator<AbstractFilter> getValidator() {
210
    return new Validator<AbstractFilter>() {
211
      @Override
212
      public Set<ConstraintViolation> validate(AbstractFilter item) {
213
        // TODO Auto-generated method stub
214
        return null;
215
      }
216
    };
217
  }
218

    
219
}