Revision 44860 trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.exportto/org.gvsig.exportto.swing/org.gvsig.exportto.swing.prov/org.gvsig.exportto.swing.prov.jdbc/src/main/java/org/gvsig/export/jdbc/service/ExportJDBCAttributeNamesTranslator.java
ExportJDBCAttributeNamesTranslator.java | ||
---|---|---|
5 | 5 |
*/ |
6 | 6 |
package org.gvsig.export.jdbc.service; |
7 | 7 |
|
8 |
import java.util.ArrayList; |
|
9 |
import java.util.Collection; |
|
10 |
import java.util.List; |
|
11 | 8 |
import org.apache.commons.lang3.StringUtils; |
12 |
import org.gvsig.export.ExportAttributes; |
|
13 |
import org.gvsig.export.spi.AttributeNamesTranslator; |
|
14 | 9 |
import org.gvsig.tools.ToolsLocator; |
15 | 10 |
import org.gvsig.tools.dynobject.DynStruct; |
11 |
import org.gvsig.tools.namestranslator.BaseNamesTranslator; |
|
12 |
import org.gvsig.tools.namestranslator.NamesTranslator; |
|
16 | 13 |
import org.gvsig.tools.persistence.PersistenceManager; |
17 | 14 |
import org.gvsig.tools.persistence.PersistentState; |
18 | 15 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
... | ... | |
21 | 18 |
* |
22 | 19 |
* @author osc |
23 | 20 |
*/ |
24 |
public class ExportJDBCAttributeNamesTranslator implements AttributeNamesTranslator {
|
|
21 |
public class ExportJDBCAttributeNamesTranslator extends BaseNamesTranslator {
|
|
25 | 22 |
|
26 | 23 |
private ExportJDBCParameters parameters; |
27 | 24 |
|
... | ... | |
42 | 39 |
if (manager.getDefinition("ExportJDBCAttributeNamesTranslator") == null) { |
43 | 40 |
DynStruct definition = manager.addDefinition(ExportJDBCAttributeNamesTranslator.class, |
44 | 41 |
"ExportJDBCAttributeNamesTranslator", "ExportJDBCAttributeNamesTranslator persistence definition", null, null); |
45 |
definition.addDynFieldInt("a");
|
|
42 |
definition.extend(manager.getDefinition("BaseNamesTranslator"));
|
|
46 | 43 |
} |
47 | 44 |
} |
48 | 45 |
|
49 | 46 |
@Override |
50 | 47 |
public void saveToState(PersistentState state) throws PersistenceException { |
51 |
state.set("a", 0);
|
|
48 |
super.saveToState(state);
|
|
52 | 49 |
} |
53 | 50 |
|
54 | 51 |
@Override |
55 | 52 |
public void loadFromState(PersistentState state) throws PersistenceException { |
56 |
int a = state.getInt("a");
|
|
53 |
super.loadFromState(state);
|
|
57 | 54 |
} |
58 | 55 |
|
59 |
private String buildTranslatedName(String source_name, Collection<String> current_names) { |
|
56 |
@Override |
|
57 |
public NamesTranslator clone() throws CloneNotSupportedException { |
|
58 |
ExportJDBCAttributeNamesTranslator clone = (ExportJDBCAttributeNamesTranslator) super.clone(); |
|
59 |
return clone; |
|
60 |
} |
|
60 | 61 |
|
61 |
String target_name = this.getNameSuggestion(source_name); |
|
62 |
if (!current_names.contains(target_name)) { |
|
62 |
protected String fixSyntax(String name) { |
|
63 |
String target_name = name; |
|
64 |
if (StringUtils.isBlank(target_name)) { |
|
65 |
target_name = DEFAULT_FIELD_NAME; |
|
66 |
} |
|
67 |
if (this.parameters.getRemoveSpacesInIdentifiers()) { |
|
68 |
//target_name = target_name.replaceAll("\\s",""); |
|
69 |
target_name = StringUtils.normalizeSpace(target_name).replace(" ", "_"); |
|
70 |
} |
|
71 |
if (this.parameters.getTranslateHyphens()) { |
|
72 |
target_name = target_name.replace("-", "_"); |
|
73 |
} |
|
74 |
if (this.parameters.getTranslateIdentifiersToLowerCase()) { |
|
75 |
target_name = target_name.toLowerCase(); |
|
76 |
} |
|
77 |
return target_name; |
|
78 |
} |
|
79 |
|
|
80 |
@Override |
|
81 |
public String getSuggestion(String name) { |
|
82 |
|
|
83 |
String target_name = fixSyntax(name); |
|
84 |
|
|
85 |
if (isValid(target_name)) { |
|
63 | 86 |
return target_name; |
64 | 87 |
} |
88 |
|
|
65 | 89 |
|
66 | 90 |
for (int i = 0; i < 255; i++) { |
67 |
target_name = source_name + i;
|
|
68 |
if (!current_names.contains(target_name)) {
|
|
69 |
return target_name; |
|
91 |
String new_target_name = target_name + i;
|
|
92 |
if (isValid(new_target_name)) {
|
|
93 |
return new_target_name;
|
|
70 | 94 |
} |
71 | 95 |
} |
72 |
return source_name.substring(0, 4) + "_" + (System.currentTimeMillis() % 1000000); |
|
96 |
|
|
97 |
return target_name.substring(0, 5) + (System.currentTimeMillis() % 1000000); |
|
98 |
|
|
73 | 99 |
} |
74 | 100 |
|
75 | 101 |
@Override |
76 |
public boolean isValidName(ExportAttributes attributes, int index, String name) { |
|
77 |
ExportAttributes.ExportAttribute selectedAttr = attributes.get(index); |
|
78 |
// |
|
79 |
String selfName = this.getNameSuggestion(attributes, index, name); |
|
80 |
if (!selfName.equals(name)) { |
|
102 |
public boolean isValid(String name) { |
|
103 |
if (!super.isValid(name)) { |
|
81 | 104 |
return false; |
82 | 105 |
} |
83 |
|
|
84 |
// check with others values |
|
85 |
boolean valid = true; |
|
86 |
for (int i = 0; i < attributes.size(); i++) { |
|
87 |
if (i == index) { |
|
88 |
continue; |
|
89 |
} |
|
90 |
ExportAttributes.ExportAttribute attr = attributes.get(i); |
|
91 |
String newName = attr.getNewName(); |
|
92 |
if (newName.equals(name)) { |
|
106 |
if (name.isEmpty()) { |
|
107 |
name = DEFAULT_FIELD_NAME; |
|
108 |
} |
|
109 |
if (this.parameters.getRemoveSpacesInIdentifiers()) { |
|
110 |
//if (newName.replaceAll("\\s","") != newName) { |
|
111 |
String s = StringUtils.normalizeSpace(name).replace(" ", "_"); |
|
112 |
if (!name.equals(s)) { |
|
93 | 113 |
return false; |
94 | 114 |
} |
95 |
if (newName.equals(selfName)) { |
|
115 |
} |
|
116 |
if (this.parameters.getTranslateHyphens()) { |
|
117 |
if (name.contains("-")) { |
|
96 | 118 |
return false; |
97 | 119 |
} |
98 |
if (newName.isEmpty()) { |
|
120 |
} |
|
121 |
if (this.parameters.getTranslateIdentifiersToLowerCase()) { |
|
122 |
if (!StringUtils.equals(name.toLowerCase(), name)) { |
|
99 | 123 |
return false; |
100 | 124 |
} |
101 |
if (this.parameters.getRemoveSpacesInIdentifiers()) { |
|
102 |
//if (newName.replaceAll("\\s","") != newName) { |
|
103 |
String s = StringUtils.normalizeSpace(newName).replace(" ", "_"); |
|
104 |
if (!newName.equals(s)) { |
|
105 |
return false; |
|
106 |
} |
|
107 |
} |
|
108 |
if (this.parameters.getTranslateHyphens()) { |
|
109 |
if (newName.contains("-")) { |
|
110 |
return false; |
|
111 |
} |
|
112 |
} |
|
113 |
if (this.parameters.getTranslateIdentifiersToLowerCase()) { |
|
114 |
if (newName.toLowerCase() != newName) { |
|
115 |
return false; |
|
116 |
} |
|
117 |
} |
|
118 | 125 |
} |
119 |
return valid;
|
|
126 |
return true;
|
|
120 | 127 |
} |
121 | 128 |
|
122 | 129 |
@Override |
123 |
public String getNameSuggestion(ExportAttributes attributes, int index, String name) { |
|
124 |
List names = new ArrayList(); |
|
125 |
boolean oneTime = true; |
|
126 |
for (ExportAttributes.ExportAttribute attribute : attributes) { |
|
127 |
if (attribute.getNewName().equals(name) && oneTime) { |
|
128 |
oneTime = false; |
|
129 |
continue; |
|
130 |
} |
|
131 |
names.add(attribute.getNewName()); |
|
132 |
} |
|
133 |
String target_name = buildTranslatedName(name, names); |
|
134 |
return target_name; |
|
130 |
public int setTranslation(String sourceName, String translatedName) { |
|
131 |
String target_name = fixSyntax(translatedName); |
|
132 |
return super.setTranslation(sourceName, target_name); |
|
135 | 133 |
} |
136 | 134 |
|
137 |
@Override |
|
138 |
public AttributeNamesTranslator clone() throws CloneNotSupportedException { |
|
139 |
ExportJDBCAttributeNamesTranslator clone = (ExportJDBCAttributeNamesTranslator) super.clone(); |
|
140 |
return clone; |
|
141 |
} |
|
142 |
|
|
143 |
@Override |
|
144 |
public String getNameSuggestion(String name) { |
|
145 |
String target_name = name; |
|
146 |
if (this.parameters.getRemoveSpacesInIdentifiers()) { |
|
147 |
//target_name = target_name.replaceAll("\\s",""); |
|
148 |
target_name = StringUtils.normalizeSpace(target_name).replace(" ", "_"); |
|
149 |
} |
|
150 |
if (this.parameters.getTranslateHyphens()) { |
|
151 |
target_name = target_name.replace("-", "_"); |
|
152 |
} |
|
153 |
if (this.parameters.getTranslateIdentifiersToLowerCase()) { |
|
154 |
target_name = target_name.toLowerCase(); |
|
155 |
} |
|
156 |
return target_name; |
|
157 |
|
|
158 |
} |
|
159 |
|
|
160 | 135 |
} |
Also available in: Unified diff