Project

General

Profile

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

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

1
/*******************************************************************************
2
 * Copyright 2011 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.Arrays;
22
import java.util.HashMap;
23
import java.util.List;
24
import java.util.Set;
25
import java.util.logging.Level;
26
import java.util.logging.Logger;
27

    
28
import org.restlet.Request;
29
import org.restlet.data.Form;
30
import org.restlet.ext.wadl.ParameterInfo;
31
import org.restlet.ext.wadl.ParameterStyle;
32

    
33
import fr.cnes.sitools.common.exception.SitoolsException;
34
import fr.cnes.sitools.common.validator.ConstraintViolation;
35
import fr.cnes.sitools.common.validator.Validator;
36
import fr.cnes.sitools.dataset.DataSetApplication;
37
import fr.cnes.sitools.dataset.filter.business.AbstractFilter;
38
import fr.cnes.sitools.dataset.model.DataSet;
39
import fr.cnes.sitools.dataset.model.Predicat;
40
import fr.cnes.sitools.util.SQLUtils;
41

    
42
/**
43
 * Filter defined for Single Value Component
44
 * 
45
 * 
46
 * @author d.arpin
47
 */
48
public final class NThreeVariability extends AbstractFilter {
49
  /**
50
   * The index of TYPE
51
   */
52
  private static final int TYPE = 0;
53
  /**
54
   * The index of COLUMN
55
   */
56
  private static final int COLUMN = 1;
57
  /**
58
   * The index of Values
59
   */
60
  private static final int VALUES = 2;
61
  
62
  private static final int NUMBER_OF_VALUES = 2;
63

    
64
  private String[] values;
65

    
66
  /**
67
   * The list of component that uses this filter
68
   */
69
  private enum TYPE_COMPONENT {
70
    /** Boolean Custom type */
71
    NTHREE_VARIABILITY
72
  }
73

    
74
  /** The TEMPLATE_PARAM */
75
  private static final String TEMPLATE_PARAM = "p[#]";
76
  
77
  private static final Logger LOGGER = Logger.getLogger(NThreeVariability.class.getName());
78

    
79
  /**
80
   * Colonnes de la base ou sont les probabilitées
81
   */
82
  private String probClass1Col = "prbclas1_from";
83
  private String probClass2Col = "prbclas2_from";
84
  private String probClass3Col = "prbclas3_from";
85
  
86
  /**
87
   * Colonnes de la base ou sont les class de variabilité associée aux probabilitées 
88
   */
89
  private String varClass1 = "varclas1_from";
90
  private String varClass2 = "varclas2_from";
91
  private String varClass3 = "varclas3_from";
92
  
93
  /**
94
   * Nome de la table
95
   */
96
  private String tableName = "exo_n2_view";
97
  
98
  private char doubleQuote = '"'; 
99
  
100
  /**
101
   * Default constructor
102
   */
103
  public NThreeVariability() {
104

    
105
    super();
106
    this.setName("NThreeVariability");
107
    this.setDescription("Required when using NThreeVariability Component");
108

    
109
    this.setClassAuthor("Marc NICOLAS");
110
    this.setClassAuthor("IAS");
111
    this.setClassOwner("MN@IAS");
112
    this.setClassVersion("1.0");
113
    this.setDefaultFilter(true);
114

    
115
    HashMap<String, ParameterInfo> rpd = new HashMap<String, ParameterInfo>();
116

    
117
    ParameterInfo paramInfo;
118
    paramInfo = new ParameterInfo("p[#]", false, "xs:string", ParameterStyle.QUERY, "NTHREE_VARIABILITY|columnAlias|value");
119
    rpd.put("0", paramInfo);
120
    this.setRequestParamsDescription(rpd);
121

    
122
  }
123

    
124
  @Override
125
  public List<Predicat> createPredicats(Request request, List<Predicat> predicats) throws Exception {
126
    DataSetApplication dsApplication = (DataSetApplication) getContext().getAttributes().get("DataSetApplication");
127
    DataSet ds = dsApplication.getDataSet();
128

    
129
    Form params = request.getResourceRef().getQueryAsForm();
130
    boolean filterExists = true;
131
    int i = 0;
132
    // Build predicat for filters param
133
    while (filterExists) {
134
      String index = TEMPLATE_PARAM.replace("#", Integer.toString(i++));
135
      String formParam = params.getFirstValue(index);
136
      if (formParam != null) {
137
        String[] parameters = formParam.split("\\|");
138
        TYPE_COMPONENT[] types = TYPE_COMPONENT.values();
139
        Boolean trouve = false;
140
        for (TYPE_COMPONENT typeCmp : types) {
141
          if (typeCmp.name().equals(parameters[TYPE])) {
142
            trouve = true;
143
          }
144
        }
145
        if(trouve){
146
          if (checkValues(parameters)) {
147
            
148
            String varClassSeek = null;
149
            Double probValue = null;
150
            try{
151
              varClassSeek = SQLUtils.escapeString(values[0]);
152
              probValue = Double.valueOf(values[1]);
153
            }catch(SitoolsException e){
154
              e.printStackTrace();
155
              LOGGER.log(Level.SEVERE,"Probleme lors de la récupération des valeurs dans le NThreeVariability, message d'erreur : "+e.getMessage());
156
            }
157
            
158
            Predicat predicat = new Predicat();
159
            predicat.setStringDefinition(" AND ("+doubleQuote+tableName+doubleQuote+"."+varClass1+" = "+"'"+varClassSeek+"'"+" AND "+doubleQuote+tableName+doubleQuote+"."+probClass1Col+" >= "+ probValue+") OR " +
160
                            "("+doubleQuote+tableName+doubleQuote+"."+varClass2+" = "+"'"+varClassSeek+"'"+" AND "+doubleQuote+tableName+doubleQuote+"."+probClass2Col+" >= "+ probValue+") OR ("+
161
                            doubleQuote+tableName+doubleQuote+"."+varClass3+" = "+"'"+varClassSeek+"'"+" AND "+doubleQuote+tableName+doubleQuote+"."+probClass3Col+" >= "+ probValue+")");
162
            predicats.add(predicat);
163
            
164
          }
165
        }
166
      }
167

    
168
      else {
169
        filterExists = false;
170
      }
171
    }
172

    
173
    return predicats;
174
  }
175
  
176
  /**
177
   * Check the number of values
178
   * 
179
   * @param parameters
180
   *          the values
181
   * @return true if the number of values is correct
182
   */
183
  private boolean checkValues(String[] parameters) {
184
    values = Arrays.copyOfRange(parameters, VALUES, VALUES + NUMBER_OF_VALUES);
185
    for(String value : values){
186
    }
187
    if (values.length == NUMBER_OF_VALUES) {
188
      return true;
189
    }
190
    return false;
191
  }
192

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

    
209
}