Project

General

Profile

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

git_sitools_idoc / sitools-idoc / hesiod / javaExt / src / fr / ias / sitools / filters / basic / TextFieldSQLLikeFilter.java @ 779bac69

1
package fr.ias.sitools.filters.basic;
2

    
3
import java.util.HashMap;
4
import java.util.List;
5
import java.util.logging.Level;
6

    
7
import org.restlet.Request;
8
import org.restlet.data.Form;
9
import org.restlet.ext.wadl.ParameterInfo;
10
import org.restlet.ext.wadl.ParameterStyle;
11

    
12
import fr.cnes.sitools.common.validator.Validator;
13
import fr.cnes.sitools.dataset.DataSetApplication;
14
import fr.cnes.sitools.dataset.filter.business.AbstractFilter;
15
import fr.cnes.sitools.dataset.model.Column;
16
import fr.cnes.sitools.dataset.model.DataSet;
17
import fr.cnes.sitools.dataset.model.Operator;
18
import fr.cnes.sitools.dataset.model.Predicat;
19
import fr.cnes.sitools.util.SQLUtils;
20
import fr.cnes.sitools.dataset.plugins.filters.core.AbstractFormFilter;
21

    
22
public class TextFieldSQLLikeFilter extends AbstractFormFilter {
23

    
24
  /**
25
   * The list of component that uses this filter
26
   */
27
  private enum TYPE_COMPONENT {
28
    /** DefaultType */
29
    TEXTFIELDSQLLIKEINSENSITIVE
30
  } 
31

    
32
  public TextFieldSQLLikeFilter() {
33
    super();
34
    this.setName("TextFieldSQLLikeFilter");
35
    this.setDescription("Required when using TextField with a sql like request");
36
    this.setClassAuthor("MNICOLAS");
37
    this.setClassOwner("IAS");
38
    this.setClassVersion("0.1");
39
    this.setDefaultFilter(true);
40
    
41
    HashMap<String, ParameterInfo> rpd = new HashMap<String, ParameterInfo>();
42

    
43
     ParameterInfo param1 = new ParameterInfo("p[#]", false, "xs:string", ParameterStyle.QUERY, "TEXTFIELDSQLLIKEINSENSITIVE|columnAlias|value");
44
     rpd.put("0", param1);
45
     
46
     param1 = new ParameterInfo("c[#]", false, "xs:string", ParameterStyle.QUERY,
47
     "TEXTFIELDSQLLIKEINSENSITIVE|dictionaryName,conceptName|value");
48
     rpd.put("2", param1);
49
  
50
     this.setRequestParamsDescription(rpd);
51
  }
52

    
53
  @Override
54
  public List<Predicat> createPredicats(Request request, List<Predicat> predicats) throws Exception {
55
    DataSetApplication dsApplication = null;
56
    getContext().getLogger().log(Level.CONFIG,"*********************** Je rentre dans le createPredicats du TEXTFIELDSQLLIKEINSENSITIVE");
57
    DataSet ds = null;
58
    boolean isConcept = true;
59
    Form params = request.getResourceRef().getQueryAsForm();
60
    boolean filterExists = true;
61
    int i = 0;
62
    
63
    while (filterExists) {
64
      // first check if the filter is applied on a Concept or not
65
      String index = TEMPLATE_PARAM_CONCEPT.replace("#", Integer.toString(i));
66
      String formParam = params.getFirstValue(index);
67
      if (formParam == null) {
68
        isConcept = false;
69
        index = TEMPLATE_PARAM.replace("#", Integer.toString(i));
70
        formParam = params.getFirstValue(index);
71
      }
72
      i++;
73
      if (formParam != null) {
74
        String[] parameters = formParam.split("\\|");
75
        TYPE_COMPONENT[] types = TYPE_COMPONENT.values();
76
        Boolean trouve = false;
77
        for (TYPE_COMPONENT typeCmp : types) {
78
          if (typeCmp.name().equals(parameters[TYPE])) {
79
            trouve = true;
80
          }
81
        }
82
        if (trouve) {
83
          if (dsApplication == null) {
84
            dsApplication = (DataSetApplication) getContext().getAttributes().get("DataSetApplication");
85
            ds = dsApplication.getDataSet();
86
          }
87
          String columnAlias = null;
88
          if (parameters.length >= VALUES) {
89

    
90
           columnAlias = getColumnAlias(isConcept, parameters, dsApplication);
91
            if (columnAlias != null) {
92
              Column col = ds.findByColumnAlias(columnAlias);
93
              if (col != null && col.getFilter() != null && col.getFilter()) {
94
                // get the value and escape it to avoid SQL injection
95
                String value = SQLUtils.escapeString(parameters[VALUES]);
96
                
97
                Predicat predicat = new Predicat();
98
                if (value != null) {
99
                  predicat.setLeftString("UPPER("+col.getColumnAlias()+")");
100
                  predicat.setNbOpenedParanthesis(0);
101
                  predicat.setNbClosedParanthesis(0);
102
                  value = value.replace("*", "%");
103
                  
104
                  predicat.setCompareOperator(Operator.LIKE);
105
                  predicat.setRightValue("'" + value.toUpperCase() + "'");
106

    
107
                  predicats.add(predicat);
108
                }
109
              }
110
            }
111
          }
112
        }
113
      }
114

    
115
      else {
116
        filterExists = false;
117
      }
118
    }
119
    
120
    return predicats;
121
  }
122

    
123
  @Override
124
  public Validator<AbstractFilter> getValidator() {
125
    // TODO Auto-generated method stub
126
    return null;
127
  }
128
}