Project

General

Profile

CadenceFilter.java

Guanji Wang, 03/03/2017 11:12

 
1
    /*******************************************************************************
2
 * Copyright 2010-2014 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.cnes.sitools.dataset.plugins.filters.core;
20

    
21
import java.util.HashMap;
22
import java.util.List;
23
import java.util.Map;
24
import java.util.Set;
25

    
26
import org.restlet.Request;
27
import org.restlet.data.Form;
28
import org.restlet.ext.wadl.ParameterInfo;
29
import org.restlet.ext.wadl.ParameterStyle;
30

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

    
41
/**
42
 * Filter defined for Single Value Component
43
 * 
44
 * 
45
 * @author WANG GUANJI
46
 */
47
public final class CadenceFilter extends AbstractFormFilter {
48

    
49
  /**
50
   * The list of component that uses this filter
51
   */
52
  private enum TYPE_COMPONENT {
53

    
54
    /** Cadence Checkbox type */
55
    CADENCE
56
  }
57

    
58
  /**
59
   * Default constructor
60
   */
61
  public CadenceFilter() {
62

    
63
    super();
64
    this.setName("CadenceFilter");
65
    this.setDescription("Required when using time interval function in sdo database ");
66

    
67
    this.setClassAuthor("WANG GUANJI");
68
    this.setClassAuthor("WANG GUANJI");
69
    this.setClassOwner("IAS");
70
    this.setClassVersion("0.1");
71
    this.setDefaultFilter(true);
72

    
73
    HashMap<String, ParameterInfo> rpd = new HashMap<String, ParameterInfo>();
74

    
75
    ParameterInfo paramInfo;
76
    paramInfo = new ParameterInfo("p[#]", false, "xs:string", ParameterStyle.QUERY, "CADENCE|columnAlias|value");
77
    rpd.put("10", paramInfo);
78

    
79
    paramInfo = new ParameterInfo("c[#]", false, "xs:string", ParameterStyle.QUERY,
80
    "CADENCE|dictionaryName,conceptName|value");
81
    rpd.put("11", paramInfo);
82
    this.setRequestParamsDescription(rpd);
83
    //
84

    
85
  }
86

    
87
  public Map<String, String> getMapCadence(Map<String, String> map){
88
 //   map.put("none", "000000000");
89
    map.put("12 sec", "000000000");
90
    map.put("1 min",  "000000001");
91
    map.put("2 min",  "000000011");
92
    map.put("10 min", "000000111");
93
    map.put("30 min", "000001111");    
94
    map.put("1 h",    "000011111");
95
    map.put("2 h",    "000111111");
96
    map.put("6 h",    "001111111");
97
    map.put("12 h",   "011111111");    
98
    map.put("1 day",  "111111111");
99
    return map;
100
  }
101
  
102
  public Map<String, String> getMapCadence_hmi(Map<String, String> map){
103
    //   map.put("none", "000000000");
104
       map.put("12 min","000000001");    
105
       map.put("1 h",   "000000011");
106
       map.put("2 h",   "000000111");
107
       map.put("6 h",   "000001111");
108
       map.put("12 h",  "000011111");       
109
       map.put("1 day", "000111111");
110
       return map;
111
     }
112
  
113
  public String getMaskCadence(String value, Map<String, String> map)
114
  {
115
    String id = "";
116
    id = map.get(value);
117
    return id;
118
  }
119
  
120
  @Override
121
  public List<Predicat> createPredicats(Request request, List<Predicat> predicats) throws Exception {
122
    DataSetApplication dsApplication = null;
123
    DataSet ds = null;
124
    boolean isConcept = true;
125
    Form params = request.getResourceRef().getQueryAsForm();
126
    boolean filterExists = true;
127
    int i = 0;
128
    // Build predicat for filters param
129
    while (filterExists) {
130
      // first check if the filter is applied on a Concept or not
131
      String index = TEMPLATE_PARAM_CONCEPT.replace("#", Integer.toString(i));
132
      String formParam = params.getFirstValue(index);
133
      if (formParam == null) {
134
        isConcept = false;
135
        index = TEMPLATE_PARAM.replace("#", Integer.toString(i));
136
        formParam = params.getFirstValue(index);
137
      }
138
      i++;
139
      if (formParam != null) {
140
        String[] parameters = formParam.split("\\|");
141
        TYPE_COMPONENT[] types = TYPE_COMPONENT.values();
142
        Boolean trouve = false;
143
        for (TYPE_COMPONENT typeCmp : types) {
144
          if (typeCmp.name().equals(parameters[TYPE])) {
145
            trouve = true;
146
          }
147
        }
148
        if (trouve) {
149
          if (dsApplication == null) {
150
            dsApplication = (DataSetApplication) getContext().getAttributes().get("DataSetApplication");
151
            ds = dsApplication.getDataSet();
152
          }
153
          String columnAlias = null;
154
          if (parameters.length >= VALUES) {
155

    
156
            columnAlias = getColumnAlias(isConcept, parameters, dsApplication);
157
            if (columnAlias != null) {
158
              Column col = ds.findByColumnAlias(columnAlias);
159
           //   System.out.println("col equals : " + col.getColumnAlias() + ' ' + col.toString()); //col equals : mask_cadence
160
              /**                                    @mask_cadence
161
               *                                     
162
               * fr.cnes.sitools.dataset.model.Column@38e5e12e for hmi
163
                 fr.cnes.sitools.dataset.model.Column@8b8c6538 for aia
164
                 
165
                 System.out.println("col equals : " + col.getTableAlias() + ' ' + col.getTableName() + ' ' + col.getDataIndex() );
166
                                                       @monitor_slony               @mask_cadence              @mask_cadence
167
               */
168

    
169
              if (col != null && col.getFilter() != null && col.getFilter()) {
170
                // get the value and escape it to avoid SQL injection
171
                String value = SQLUtils.escapeString(parameters[VALUES]);
172
                Map<String, String> map_cadence = new HashMap<String, String>();
173
               
174
                //Seperate the two types of data series name              
175
                if(col.toString().contains("38e5e12e")){
176
                  map_cadence = getMapCadence_hmi(map_cadence);
177
                }
178
                if(col.toString().contains("663c8d74a5a8")){
179
                  map_cadence = getMapCadence_hmi(map_cadence);
180
                }
181
                else if(col.toString().contains("8b8c6538")){                 
182
                  map_cadence = getMapCadence(map_cadence);
183
                }                
184
                String id_cadence = getMaskCadence(value, map_cadence);
185
                Predicat predicat = new Predicat();
186
                if (value != null) {
187
                  predicat.setLeftAttribute(col);
188
                  predicat.setNbOpenedParanthesis(0);
189
                  predicat.setNbClosedParanthesis(0);
190
                  predicat.setCompareOperator(Operator.GTE);
191
                  /*
192
                  if (parameters[TYPE].equals(TYPE_COMPONENT.CADENCE.name())) {                  
193
                    predicat.setCompareOperator(Operator.GTE);
194
                  }
195
*/
196
                  predicat.setRightValue("'" + id_cadence + "'");
197

    
198
                  predicats.add(predicat);
199
                }
200
              }
201
            }
202
          }
203
        }
204
      }
205

    
206
      else {
207
        filterExists = false;
208
      }
209
    }
210

    
211
    return predicats;
212
  }
213

    
214
  /**
215
   * Gets the validator for this Filter
216
   * 
217
   * @return the validator for the filter
218
   */
219
  @Override
220
  public Validator<AbstractFilter> getValidator() {
221
    return new Validator<AbstractFilter>() {
222
      @Override
223
      public Set<ConstraintViolation> validate(AbstractFilter item) {
224
        // TODO Auto-generated method stub
225
        return null;
226
      }
227
    };
228
  }
229

    
230
}