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 / WrongNameFieldSuggestionProviderFactory.java @ 44855
History | View | Annotate | Download (3.83 KB)
1 |
package org.gvsig.fmap.dal.swing.impl.expressionevaluator.suggestions; |
---|---|
2 |
|
3 |
import java.util.HashMap; |
4 |
import java.util.Map; |
5 |
import org.apache.commons.lang.StringUtils; |
6 |
import org.apache.commons.text.similarity.LevenshteinDistance; |
7 |
import org.gvsig.expressionevaluator.Code; |
8 |
import org.gvsig.expressionevaluator.ExpressionUtils; |
9 |
import org.gvsig.expressionevaluator.swing.SuggestionProvider; |
10 |
import org.gvsig.fmap.dal.exception.DataException; |
11 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
12 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
13 |
import org.gvsig.fmap.dal.feature.FeatureType; |
14 |
import org.gvsig.tools.ToolsLocator; |
15 |
import org.gvsig.tools.i18n.I18nManager; |
16 |
import org.gvsig.tools.util.Factory; |
17 |
|
18 |
/**
|
19 |
*
|
20 |
* @author jjdelcerro
|
21 |
*/
|
22 |
@SuppressWarnings("UseSpecificCatch") |
23 |
public class WrongNameFieldSuggestionProviderFactory implements Factory { |
24 |
|
25 |
private final FeatureStore store; |
26 |
private FeatureType featureType;
|
27 |
|
28 |
public WrongNameFieldSuggestionProviderFactory(FeatureStore store) {
|
29 |
this.store = store;
|
30 |
try {
|
31 |
this.featureType = store.getDefaultFeatureType();
|
32 |
} catch (DataException ex) {
|
33 |
this.featureType = null; |
34 |
} |
35 |
} |
36 |
|
37 |
@Override
|
38 |
public String getName() { |
39 |
return "WrongNameFieldSuggestion"; |
40 |
} |
41 |
|
42 |
@Override
|
43 |
public Object create(Object... parameters) { |
44 |
SuggestionProvider suggestionProvider = new WrongNameFieldSuggestionProvider();
|
45 |
return suggestionProvider;
|
46 |
|
47 |
} |
48 |
|
49 |
private class WrongNameFieldSuggestionProvider implements SuggestionProvider { |
50 |
|
51 |
@Override
|
52 |
public String getSuggestion(String expression) { |
53 |
if ( StringUtils.isBlank(expression)) {
|
54 |
return null; |
55 |
} |
56 |
final Map<String,String> similarFields = new HashMap<>(); |
57 |
try {
|
58 |
Code code = ExpressionUtils.compile(expression); |
59 |
if (code == null) { |
60 |
return null; |
61 |
} |
62 |
code.accept((Object obj) -> {
|
63 |
if (obj instanceof Code.Identifier) { |
64 |
String varname = ((Code.Identifier) obj).name().toLowerCase();
|
65 |
if( varname.length()<3 || featureType.get(varname)!=null ) { |
66 |
return;
|
67 |
} |
68 |
if( similarFields.containsKey(varname) ) {
|
69 |
return;
|
70 |
} |
71 |
String suggestedName = getSuggestedAttrName(varname, 3); |
72 |
if( suggestedName!=null ) { |
73 |
similarFields.put(varname, suggestedName); |
74 |
} |
75 |
} |
76 |
}); |
77 |
} catch (Throwable th) { |
78 |
} |
79 |
if( similarFields.isEmpty() ) {
|
80 |
return null; |
81 |
} |
82 |
// Perhaps you wanted to use a similar field.
|
83 |
// Quiz?s quiso usar un campo parecido.
|
84 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
85 |
StringBuilder suggestion = new StringBuilder(); |
86 |
suggestion.append(i18n.getTranslation("_Perhaps_you_wanted_to_use_a_similar_field"));
|
87 |
suggestion.append("\n");
|
88 |
boolean needAComma = false; |
89 |
for(Map.Entry<String, String> entry : similarFields.entrySet() ) { |
90 |
if( needAComma ) {
|
91 |
suggestion.append(", ");
|
92 |
} else {
|
93 |
needAComma = true;
|
94 |
} |
95 |
suggestion.append(entry.getKey()); |
96 |
suggestion.append(" --> ");
|
97 |
suggestion.append(entry.getValue()); |
98 |
} |
99 |
return suggestion.toString();
|
100 |
} |
101 |
|
102 |
} |
103 |
|
104 |
private String getSuggestedAttrName(String nameSearched, int maxdistance) { |
105 |
LevenshteinDistance comparator = LevenshteinDistance.getDefaultInstance(); |
106 |
nameSearched = nameSearched.toLowerCase(); |
107 |
String found_name = null; |
108 |
int found_distance = Integer.MAX_VALUE; |
109 |
for (FeatureAttributeDescriptor attr : featureType) {
|
110 |
String name = attr.getName();
|
111 |
int distance = comparator.apply(name.toLowerCase(), nameSearched);
|
112 |
if( distance<found_distance ) {
|
113 |
found_name = name; |
114 |
found_distance = distance; |
115 |
} |
116 |
} |
117 |
if( found_distance<=maxdistance ) {
|
118 |
return found_name;
|
119 |
} |
120 |
return null; |
121 |
} |
122 |
} |