Project

General

Profile

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

git_sitools_idoc / sitools-idoc / hesiod / javaExt / src / fr / ias / sitools / filters / basic / ListBoxSpectralTypeCorot.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

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

    
53
  /**
54
   * The list of component that uses this filter
55
   */
56
  private enum TYPE_COMPONENT {
57
    /** List box Spectral Type for Corot */
58
    LISTBOXSPECTRALTYPECOROT
59
  }
60
  
61
  private final List<String> spectralTypeO = Arrays.asList("O5","O6","O8","O8.5","O9");
62
  private final List<String> spectralTypeB = Arrays.asList("B0","B1","B2","B3","B5","B6","B8","B9","B2.5","B.5");
63
  private final List<String> spectralTypeA = Arrays.asList("A0","A1","A2","A3","A4","A5","A6","A7","A8","A9");
64
  private final List<String> spectralTypeF = Arrays.asList("F0","F1","F2","F3","F4","F5","F6","F7","F8","F9");
65
  private final List<String> spectralTypeG = Arrays.asList("G0","G1","G2","G3","G4","G5","G6","G7","G8","G9");
66
  private final List<String> spectralTypeK = Arrays.asList("K0","K1","K2","K3","K4","K5","K6","K7","K8","K9");
67
  private final List<String> spectralTypeM = Arrays.asList("M","M0","M1","M2","M3","M4","M5","M6","M7","M8","M9");
68
  
69
  /**
70
   * Default constructor
71
   */
72
  public ListBoxSpectralTypeCorot() {
73

    
74
    super();
75
    this.setName("ListBoxSpectralTypeCorot");
76
    this.setDescription("Required when using List Box Spectral Type for Corot Components");
77

    
78
    this.setClassAuthor("Mnicolas");
79
    this.setClassOwner("IAS");
80
    this.setClassVersion("0.1");
81
    this.setDefaultFilter(true);
82

    
83
    HashMap<String, ParameterInfo> rpd = new HashMap<String, ParameterInfo>();
84

    
85
    ParameterInfo paramInfo;
86
    paramInfo = new ParameterInfo("p[#]", false, "xs:string", ParameterStyle.QUERY,
87
        "LISTBOXSPECTRALTYPECOROT|columnAlias|value1|...|value n");
88
    rpd.put("0", paramInfo);
89
    this.setRequestParamsDescription(rpd);
90

    
91
  }
92

    
93
  @Override
94
  public List<Predicat> createPredicats(Request request, List<Predicat> predicats) throws Exception {
95
    // Get the dataset
96
    DataSetApplication dsApplication = null;
97
    DataSet ds = null;
98
    boolean isConcept = true;
99
    Form params = request.getResourceRef().getQueryAsForm();
100
    boolean filterExists = true;
101
    int i = 0;
102
    // Build predicat for filters param
103
    while (filterExists) {
104
      // first check if the filter is applied on a Concept or not
105
      String index = TEMPLATE_PARAM_CONCEPT.replace("#", Integer.toString(i));
106
      String formParam = params.getFirstValue(index);
107
      if (formParam == null) {
108
        isConcept = false;
109
        index = TEMPLATE_PARAM.replace("#", Integer.toString(i));
110
        formParam = params.getFirstValue(index);
111
      }
112
      i++;
113
      if (formParam != null) {
114
        String[] parameters = formParam.split("\\|");
115
        TYPE_COMPONENT[] types = TYPE_COMPONENT.values();
116
        Boolean trouve = false;
117
        for (TYPE_COMPONENT typeCmp : types) {
118
          if (typeCmp.name().equals(parameters[TYPE])) {
119
            trouve = true;
120
          }
121
        }
122
        if (trouve) {
123
          if (dsApplication == null) {
124
            dsApplication = (DataSetApplication) getContext().getAttributes().get("DataSetApplication");
125
            ds = dsApplication.getDataSet();
126
          }
127
          String columnAlias = null;
128
          if (parameters.length >= VALUES) {
129

    
130
            /*
131
             * columnsAlias = parameters[COLUMN].split(","); ArrayList<Column> columns = new ArrayList<Column>(); for
132
             * (String columnAlias : columnsAlias) { Column col = ds.findByColumnAlias(columnAlias); if (col != null) {
133
             * columns.add(col); }
134
             * 
135
             * }
136
             */
137
            columnAlias = getColumnAlias(isConcept, parameters, dsApplication);
138
            if (columnAlias != null) {
139
              Column col = ds.findByColumnAlias(columnAlias);
140

    
141
              if (col != null && col.getFilter() != null && col.getFilter()) {
142
                String[] values = Arrays.copyOfRange(parameters, VALUES, parameters.length);
143
                Predicat predicat = new Predicat();
144
                if (values != null) {
145
                  predicat.setLeftAttribute(col);
146
                  predicat.setNbOpenedParanthesis(0);
147
                  predicat.setNbClosedParanthesis(0);
148
                  predicat.setCompareOperator(Operator.IN);
149

    
150
                  boolean all = false;
151
                  List<String> spectralTypeSeeked = new ArrayList<String>();
152
                  for (String value : values) {
153
                      if(value.equalsIgnoreCase("O")){
154
                          spectralTypeSeeked.addAll(this.spectralTypeO);
155
                      }else if(value.equalsIgnoreCase("B")){
156
                          spectralTypeSeeked.addAll(this.spectralTypeB);
157
                      }else if(value.equalsIgnoreCase("A")){
158
                          spectralTypeSeeked.addAll(this.spectralTypeA);
159
                      }else if(value.equalsIgnoreCase("F")){
160
                          spectralTypeSeeked.addAll(this.spectralTypeF);
161
                      }else if(value.equalsIgnoreCase("G")){
162
                          spectralTypeSeeked.addAll(this.spectralTypeG);
163
                      }else if(value.equalsIgnoreCase("K")){
164
                          spectralTypeSeeked.addAll(this.spectralTypeK);
165
                      }else if(value.equalsIgnoreCase("M")){
166
                          spectralTypeSeeked.addAll(this.spectralTypeM);
167
                      }else if(value.equalsIgnoreCase("All")){
168
                          all = true;
169
                      }
170
                      
171
                    //in.add(SQLUtils.escapeString(value));
172
                  }
173
                  if(!spectralTypeSeeked.isEmpty()&& !all) {
174
                    predicat.setRightValue(spectralTypeSeeked);
175
                    predicats.add(predicat);
176
                  }
177
                }
178
              }
179
            }
180

    
181
          }
182
        }
183
      }else {
184
        filterExists = false;
185
      }
186
    }
187

    
188
    return predicats;
189
  }
190

    
191
  /**
192
   * Gets the validator for this Filter
193
   * 
194
   * @return the validator for the filter
195
   */
196
  @Override
197
  public Validator<AbstractFilter> getValidator() {
198
    return new Validator<AbstractFilter>() {
199
      @Override
200
      public Set<ConstraintViolation> validate(AbstractFilter item) {
201
        // TODO Auto-generated method stub
202
        return null;
203
      }
204
    };
205
  }
206

    
207
}