Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.swing / org.gvsig.fmap.dal.swing.impl / src / main / java / org / gvsig / fmap / dal / swing / impl / expressionevaluator / suggestions / CheckTableFieldsSuggestionProviderFactory.java @ 44855

History | View | Annotate | Download (4.63 KB)

1
package org.gvsig.fmap.dal.swing.impl.expressionevaluator.suggestions;
2

    
3
import java.util.Objects;
4
import org.apache.commons.lang.StringUtils;
5
import org.apache.commons.lang3.mutable.MutableObject;
6
import org.apache.commons.text.similarity.LevenshteinDistance;
7
import org.gvsig.expressionevaluator.Code;
8
import org.gvsig.expressionevaluator.ExpressionBuilder;
9
import org.gvsig.expressionevaluator.ExpressionUtils;
10
import org.gvsig.expressionevaluator.swing.SuggestionProvider;
11
import org.gvsig.fmap.dal.exception.DataException;
12
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
13
import org.gvsig.fmap.dal.feature.FeatureStore;
14
import org.gvsig.fmap.dal.feature.FeatureType;
15
import org.gvsig.tools.ToolsLocator;
16
import org.gvsig.tools.i18n.I18nManager;
17
import org.gvsig.tools.util.Factory;
18
import org.gvsig.tools.visitor.VisitCanceledException;
19

    
20
/**
21
 *
22
 * @author jjdelcerro
23
 */
24
@SuppressWarnings("UseSpecificCatch")
25
public class CheckTableFieldsSuggestionProviderFactory implements Factory {
26

    
27
  private final FeatureStore store;
28
  private FeatureType featureType;
29

    
30
  public CheckTableFieldsSuggestionProviderFactory(FeatureStore store) {
31
    this.store = store;
32
    try {
33
      this.featureType = store.getDefaultFeatureType();
34
    } catch (DataException ex) {
35
      this.featureType = null;
36
    }            
37
  }
38

    
39
  @Override
40
  public String getName() {
41
    return "CheckTableFieldsSuggestion";
42
  }
43

    
44
  @Override
45
  public Object create(Object... parameters) {
46
    SuggestionProvider suggestionProvider = new CheckTableFieldsSuggestionProvider();
47
    return suggestionProvider;
48

    
49
  }
50

    
51
  private class CheckTableFieldsSuggestionProvider implements SuggestionProvider {
52

    
53
    @Override
54
    public String getSuggestion(String expression) {
55
      if ( StringUtils.isBlank(expression)) {
56
        return null;
57
      }
58
      MutableObject<String> suggestion = new MutableObject<>();
59
      try {
60
        Code code = ExpressionUtils.compile(expression);
61
        if (code == null) {
62
          return null;
63
        }
64
        final String storename = store.getName();
65
        code.accept((Object obj) -> {
66
          if (obj instanceof Code.Callable ) {
67
            Code.Callable callable = (Code.Callable)obj;
68
            if( StringUtils.equalsIgnoreCase(callable.name(),ExpressionBuilder.FUNCTION_GETATTR) ) {
69
              Code arg0 = callable.parameters().get(0);
70
              Code arg1 = callable.parameters().get(1);
71
              if( arg0 instanceof Code.Identifier && arg1 instanceof Code.Constant ) {
72
                String theStoreName = ((Code.Identifier)arg0).name();
73
                if( StringUtils.equalsIgnoreCase(storename, theStoreName) ) {
74
                  String attrname = Objects.toString(((Code.Constant)arg1).value(),"");
75
                  if( featureType.getAttributeDescriptor(attrname)==null ) {
76
                    String suggestedAttrName = getSuggestedAttrName(attrname,3);
77
                    I18nManager i18n = ToolsLocator.getI18nManager();
78
                    // _The_indicated_field_does_not_exist_in_the_table
79
                    // The indicated field does not exist in the table.
80
                    // El campo indicado no existe en la tabla.
81
                    if( StringUtils.isBlank(suggestedAttrName) ) {
82
                      suggestion.setValue(
83
                              i18n.getTranslation("_The_indicated_field_does_not_exist_in_the_table")+
84
                                      String.format(" (%s.%s)", storename, attrname)
85
                      );
86
                    } else {
87
                      suggestion.setValue(
88
                              i18n.getTranslation("_The_indicated_field_does_not_exist_in_the_table")+
89
                                      String.format("\n(%s.%s --> %s.%s)", storename, attrname, storename, suggestedAttrName)
90
                      );
91
                    }
92
                    throw new VisitCanceledException();
93
                  }
94
                }
95
              }
96
            }
97
          }
98
        });
99
      } catch (Throwable th) {
100
      }
101
      return suggestion.getValue();
102
    }
103

    
104
  }
105

    
106
  private String getSuggestedAttrName(String nameSearched, int maxdistance) {
107
    LevenshteinDistance comparator = LevenshteinDistance.getDefaultInstance();
108
    nameSearched = nameSearched.toLowerCase();
109
    String found_name = null;
110
    int found_distance = Integer.MAX_VALUE;
111
    for (FeatureAttributeDescriptor attr : featureType) {
112
      String name = attr.getName();
113
      int distance = comparator.apply(name.toLowerCase(), nameSearched);
114
      if( distance<found_distance ) {
115
        found_name = name;
116
        found_distance = distance;
117
      }
118
    }
119
    if( found_distance<=maxdistance ) {
120
      return found_name;
121
    }
122
    return null;
123
  }
124
}