Revision 43504 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.dbf/src/main/java/org/gvsig/exportto/swing/prov/dbf/ExporttoDBFService.java
ExporttoDBFService.java | ||
---|---|---|
27 | 27 |
|
28 | 28 |
import org.gvsig.exportto.ExporttoService; |
29 | 29 |
import org.gvsig.exportto.ExporttoServiceException; |
30 |
import org.gvsig.exportto.impl.AttributeNamesTranslator; |
|
30 | 31 |
import org.gvsig.exportto.swing.prov.file.AbstractExporttoFileService; |
31 | 32 |
import org.gvsig.fmap.dal.exception.DataException; |
33 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
|
34 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
|
35 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
|
36 |
import org.gvsig.fmap.dal.feature.Feature; |
|
37 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
|
32 | 38 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
39 |
import org.gvsig.fmap.dal.feature.FeatureType; |
|
33 | 40 |
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters; |
34 | 41 |
import org.gvsig.fmap.dal.feature.OpenFeatureStoreParameters; |
35 | 42 |
|
... | ... | |
41 | 48 |
public class ExporttoDBFService extends AbstractExporttoFileService implements |
42 | 49 |
ExporttoService { |
43 | 50 |
|
51 |
public static final int MAX_FIELD_NAME_LENGTH = 10; |
|
52 |
|
|
44 | 53 |
private String encoding; |
54 |
private AttributeNamesTranslator nameTranslator; |
|
45 | 55 |
|
46 | 56 |
protected ExporttoDBFService(File dbfFile, FeatureStore featureStore, |
47 |
String encoding) { |
|
57 |
String encoding) throws ExporttoServiceException {
|
|
48 | 58 |
super(dbfFile, featureStore); |
49 |
if( dbfFile == null ) { |
|
50 |
throw new IllegalArgumentException("file can't be null."); |
|
51 |
} |
|
52 |
if( featureStore == null ) { |
|
53 |
throw new IllegalArgumentException("Store can't be null."); |
|
54 |
} |
|
55 | 59 |
this.encoding = encoding; |
56 |
try { |
|
57 |
this.open(); |
|
58 |
} catch (ExporttoServiceException e) { |
|
59 |
throw new RuntimeException("Can't prepare the export to dbf. Target="+dbfFile.getAbsolutePath()+", Source="+featureStore.getFullName()+".", e); |
|
60 |
} |
|
60 |
this.open(); |
|
61 | 61 |
} |
62 | 62 |
|
63 | 63 |
@Override |
... | ... | |
65 | 65 |
NewFeatureStoreParameters newStoreParameters) |
66 | 66 |
throws DataException { |
67 | 67 |
newStoreParameters.setDynValue("Encoding", encoding); |
68 |
newStoreParameters.setDefaultFeatureType(featureStore |
|
69 |
.getDefaultFeatureType().getEditable()); |
|
68 |
|
|
69 |
FeatureType ft = featureStore.getDefaultFeatureType(); |
|
70 |
this.nameTranslator = new AttributeNamesTranslator(ft, MAX_FIELD_NAME_LENGTH); |
|
71 |
if( nameTranslator.hasTranslations() ) { |
|
72 |
// Obtenemos una copia del ft desbinculada del store. |
|
73 |
ft = ft.getCopy(); |
|
74 |
EditableFeatureType eft = ft.getEditable(); |
|
75 |
for( int i=0; i<eft.size(); i++) { |
|
76 |
EditableFeatureAttributeDescriptor attr = eft.getEditableAttributeDescriptor(i); |
|
77 |
String name = attr.getName(); |
|
78 |
if( nameTranslator.hasTranslation(name) ) { |
|
79 |
attr.setName(nameTranslator.getTargetName(name)); |
|
80 |
} |
|
81 |
} |
|
82 |
// Volvemos a obtener una copia "limpia" con los cambios consolidados |
|
83 |
ft = eft.getNotEditableCopy(); |
|
84 |
} |
|
85 |
newStoreParameters.setDefaultFeatureType(ft.getEditable()); |
|
70 | 86 |
} |
71 | 87 |
|
72 | 88 |
@Override |
... | ... | |
80 | 96 |
public String getFileExtension() { |
81 | 97 |
return "dbf"; |
82 | 98 |
} |
99 |
|
|
100 |
@Override |
|
101 |
protected void copyValues(Feature source, EditableFeature target) { |
|
102 |
FeatureType sourceType= source.getType(); |
|
103 |
FeatureType targetType = target.getType(); |
|
104 |
for( FeatureAttributeDescriptor targetAttr : targetType) { |
|
105 |
String targetAttrName = targetAttr.getName(); |
|
106 |
String sourceAttrName = nameTranslator.getSourceName(targetAttrName); |
|
107 |
FeatureAttributeDescriptor sourceAttr = sourceType.getAttributeDescriptor(sourceAttrName); |
|
108 |
if( sourceAttr!=null ) { |
|
109 |
Object value = source.get(sourceAttrName); |
|
110 |
if( value == null ) { |
|
111 |
if( targetAttr.allowNull() ) { |
|
112 |
target.set(targetAttrName, null); |
|
113 |
} |
|
114 |
} else { |
|
115 |
target.set(targetAttrName, value); |
|
116 |
} |
|
117 |
} |
|
118 |
} |
|
119 |
} |
|
120 |
|
|
121 |
|
|
83 | 122 |
} |
Also available in: Unified diff