Revision 44411 trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.exportto/org.gvsig.exportto.lib/org.gvsig.exportto.lib.impl/src/main/java/org/gvsig/export/impl/service/DefaultExportAttributes.java

View differences:

DefaultExportAttributes.java
16 16
import org.gvsig.fmap.dal.feature.EditableFeatureType;
17 17
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
18 18
import org.gvsig.fmap.dal.feature.FeatureType;
19
import org.gvsig.tools.ToolsLocator;
20
import org.gvsig.tools.dynobject.DynStruct;
21
import org.gvsig.tools.persistence.PersistenceManager;
22
import org.gvsig.tools.persistence.PersistentState;
23
import org.gvsig.tools.persistence.exception.PersistenceException;
19 24
import org.slf4j.Logger;
20 25
import org.slf4j.LoggerFactory;
21 26

  
22 27
public final class DefaultExportAttributes implements ExportAttributes {
23 28

  
24
	private List<ExportAttribute> exportAttributes;
25
	private AttributeNamesTranslator namesTranslator = null;
26
	private FeatureType sourceFeatureType;
27
	static final Logger LOGGER = LoggerFactory.getLogger(DefaultExportAttributes.class);
28
	private boolean active;
29
    private List<ExportAttribute> exportAttributes;
30
    private AttributeNamesTranslator namesTranslator = null;
31
    private FeatureType sourceFeatureType;
32
    static final Logger LOGGER = LoggerFactory.getLogger(DefaultExportAttributes.class);
33
    private boolean active;
29 34

  
30
	public DefaultExportAttributes() { //FeatureType ftype) {
31
//        this.setSourceFeatureType(ftype);
32
		this.namesTranslator = new DummyAttributeNamesTranslator();
33
		this.active = false;
34
	}
35
    public DefaultExportAttributes() {
36
        this.namesTranslator = new DummyAttributeNamesTranslator();
37
        this.active = false;
38
    }
35 39

  
36
	public void fillExportAttributes(FeatureType ftype) {
37
		ArrayList attrs = new ArrayList();
38
		if (ftype != null) {
39
			for (FeatureAttributeDescriptor fad : ftype) {
40
				DefaultExportAttribute exportAttribute = new DefaultExportAttribute(fad);
41
				String newName = this.getNamesTranslator().getNameSuggestion(fad.getName());
42
				exportAttribute.setNewName(newName);
43
				exportAttribute.setNewType(fad.getDataType().getType());
44
				exportAttribute.setSize(fad.getSize());
45
				exportAttribute.setExported(true);
46
				attrs.add(exportAttribute);
47
			}
48
		}
49
		this.setExportAttributes(attrs);
50
		this.fixAttributeNames();
40
    public void fillExportAttributes(FeatureType ftype) {
41
        ArrayList attrs = new ArrayList();
42
        if (ftype != null) {
43
            for (FeatureAttributeDescriptor fad : ftype) {
44
                DefaultExportAttribute exportAttribute = new DefaultExportAttribute(fad);
45
                String newName = this.getNamesTranslator().getNameSuggestion(fad.getName());
46
                exportAttribute.setNewName(newName);
47
                exportAttribute.setNewType(fad.getDataType().getType());
48
                exportAttribute.setSize(fad.getSize());
49
                exportAttribute.setExported(true);
50
                attrs.add(exportAttribute);
51
            }
52
        }
53
        this.setExportAttributes(attrs);
54
        this.fixAttributeNames();
51 55

  
52
	}
56
    }
53 57

  
54
	@Override
55
	public void setNamesTranslator(AttributeNamesTranslator namesTranslator) {
56
		this.namesTranslator = namesTranslator;
57
		if (this.sourceFeatureType != null) {
58
			this.fillExportAttributes(sourceFeatureType);
59
		}
60
	}
58
    @Override
59
    public void setNamesTranslator(AttributeNamesTranslator namesTranslator) {
60
        if (this.namesTranslator != namesTranslator) {
61
            this.namesTranslator = namesTranslator;
62
            if (this.sourceFeatureType != null) {
63
                this.fillExportAttributes(sourceFeatureType);
64
            }
65
        }
66
    }
61 67

  
62
	@Override
63
	public AttributeNamesTranslator getNamesTranslator() {
64
		return this.namesTranslator;
65
	}
68
    @Override
69
    public AttributeNamesTranslator getNamesTranslator() {
70
        return this.namesTranslator;
71
    }
66 72

  
67
	@Override
68
	public List<ExportAttribute> toList() {
69
		return this.exportAttributes;
70
	}
73
    @Override
74
    public List<ExportAttribute> toList() {
75
        return this.exportAttributes;
76
    }
71 77

  
72
	@Override
73
	public boolean isEmpty() {
74
		return this.exportAttributes.isEmpty();
75
	}
78
    @Override
79
    public boolean isEmpty() {
80
        return this.exportAttributes.isEmpty();
81
    }
76 82

  
77
	@Override
78
	public int size() {
79
		return this.exportAttributes.size();
80
	}
83
    @Override
84
    public int size() {
85
        return this.exportAttributes.size();
86
    }
81 87

  
82
	@Override
83
	public Iterator<ExportAttribute> iterator() {
84
		return this.exportAttributes.iterator();
85
	}
88
    @Override
89
    public Iterator<ExportAttribute> iterator() {
90
        return this.exportAttributes.iterator();
91
    }
86 92

  
87
	@Override
88
	public ExportAttribute get(int position) {
89
		return this.exportAttributes.get(position);
90
	}
93
    @Override
94
    public ExportAttribute get(int position) {
95
        return this.exportAttributes.get(position);
96
    }
91 97

  
92
	@Override
93
	public ExportAttribute getExportAttribute(String name) {
94
		for (ExportAttribute exportAttribute : this.exportAttributes) {
95
			if (StringUtils.equals(exportAttribute.getName(), name)) {
96
				return exportAttribute;
97
			}
98
		}
99
		return null;
100
	}
98
    @Override
99
    public ExportAttribute getExportAttribute(String name) {
100
        for (ExportAttribute exportAttribute : this.exportAttributes) {
101
            if (StringUtils.equals(exportAttribute.getName(), name)) {
102
                return exportAttribute;
103
            }
104
        }
105
        return null;
106
    }
101 107

  
102
	@Override
103
	public FeatureType getTargetFeatureType() {
104
		if (!this.isAttributeNamesValid()) {
105
			this.fixAttributeNames();
106
			LOGGER.warn("An extra fix attributes names in the feature type has been made");
107
		}
108
		EditableFeatureType targetFeatureType = this.getSourceFeatureType().getCopy().getEditable();
109
		for (FeatureAttributeDescriptor attrSource : this.getSourceFeatureType()) {
110
			FeatureAttributeDescriptor attr = targetFeatureType.getAttributeDescriptor(attrSource.getName());
111
			String name = attr.getName();
112
			ExportAttribute exportAttr = this.getExportAttribute(name);
113
			if (!exportAttr.isExported()) {
114
				targetFeatureType.remove(name);
115
				continue;
116
			}
108
    @Override
109
    public FeatureType getTargetFeatureType() {
110
        if (!this.isAttributeNamesValid()) {
111
            this.fixAttributeNames();
112
            LOGGER.warn("An extra fix attributes names in the feature type has been made");
113
        }
114
        EditableFeatureType targetFeatureType = this.getSourceFeatureType().getCopy().getEditable();
115
        for (FeatureAttributeDescriptor attrSource : this.getSourceFeatureType()) {
116
            FeatureAttributeDescriptor attr = targetFeatureType.getAttributeDescriptor(attrSource.getName());
117
            String name = attr.getName();
118
            ExportAttribute exportAttr = this.getExportAttribute(name);
119
            if (!exportAttr.isExported()) {
120
                targetFeatureType.remove(name);
121
                continue;
122
            }
117 123

  
118
			EditableFeatureAttributeDescriptor eAttr = targetFeatureType.getEditableAttributeDescriptor(name);
119
			if (this.getTargetName(name) == null ? name != null : !this.getTargetName(name).equals(name)) {
120
				eAttr.setName(this.getTargetName(name));
121
			}
122
			int type = attr.getDataType().getType();
123
			if (type != this.getTargetType(name)) {
124
				eAttr.setDataType(this.getTargetType(name));
125
				eAttr.setDefaultValue(null); // TODO: delete default value
126
			}
127
			int size = exportAttr.getSize();
128
			eAttr.setSize(size);
129
		}
130
		return targetFeatureType.getNotEditableCopy();
131
	}
124
            EditableFeatureAttributeDescriptor eAttr = targetFeatureType.getEditableAttributeDescriptor(name);
125
            if (this.getTargetName(name) == null ? name != null : !this.getTargetName(name).equals(name)) {
126
                eAttr.setName(this.getTargetName(name));
127
            }
128
            int type = attr.getDataType().getType();
129
            if (type != this.getTargetType(name)) {
130
                eAttr.setDataType(this.getTargetType(name));
131
                eAttr.setDefaultValue(null); // TODO: delete default value
132
            }
133
            int size = exportAttr.getSize();
134
            eAttr.setSize(size);
135
        }
136
        return targetFeatureType.getNotEditableCopy();
137
    }
132 138

  
133
	@Override
134
	public void setSourceFeatureType(FeatureType sourceFeatureType) {
135
		if (sourceFeatureType != this.sourceFeatureType) {
136
			this.sourceFeatureType = sourceFeatureType;
137
			this.fillExportAttributes(this.sourceFeatureType);
138
		}
139
	}
139
    @Override
140
    public void setSourceFeatureType(FeatureType sourceFeatureType) {
141
        if (!sourceFeatureType.equals(this.sourceFeatureType)) {
142
            this.sourceFeatureType = sourceFeatureType;
143
            this.fillExportAttributes(this.sourceFeatureType);
144
        }
145
    }
140 146

  
141
	@Override
142
	public FeatureType getSourceFeatureType() {
143
		return this.sourceFeatureType;
144
	}
147
    @Override
148
    public FeatureType getSourceFeatureType() {
149
        return this.sourceFeatureType;
150
    }
145 151

  
146
	@Override
147
	public String getTargetName(String name) {
148
		for (int i = 0; i < exportAttributes.size(); i++) {
149
			ExportAttribute exportAttribute = exportAttributes.get(i);
150
			if (StringUtils.equals(exportAttribute.getName(), name)) {
151
				String newName = exportAttribute.getNewName();
152
    @Override
153
    public String getTargetName(String name) {
154
        for (int i = 0; i < exportAttributes.size(); i++) {
155
            ExportAttribute exportAttribute = exportAttributes.get(i);
156
            if (StringUtils.equals(exportAttribute.getName(), name)) {
157
                String newName = exportAttribute.getNewName();
152 158
//TODO return value or fixed value?
153 159
//                if (!this.namesTranslator.isValidName(this, i, newName)) {
154 160
//                    newName = this.namesTranslator.getNameSuggestion(this, i, newName);
155 161
//                    //exportAttribute.setNewName(newName); //Change the name in the exportAttribute after chacking
156 162
//                }
157
				return newName;
158
			}
159
		}
160
		return null;
161
	}
163
                return newName;
164
            }
165
        }
166
        return null;
167
    }
162 168

  
163
	@Override
164
	public String getSourceName(String name) {
165
		for (ExportAttribute exportAttribute : this.exportAttributes) {
166
			if (StringUtils.equalsIgnoreCase(exportAttribute.getNewName(), name)) {
167
				return exportAttribute.getName();
168
			}
169
		}
170
		return null;
171
	}
169
    @Override
170
    public String getSourceName(String name) {
171
        for (ExportAttribute exportAttribute : this.exportAttributes) {
172
            if (StringUtils.equalsIgnoreCase(exportAttribute.getNewName(), name)) {
173
                return exportAttribute.getName();
174
            }
175
        }
176
        return null;
177
    }
172 178

  
173
	@Override
174
	public int getTargetType(String name) {
175
		for (ExportAttribute exportAttribute : this.exportAttributes) {
176
			if (StringUtils.equals(exportAttribute.getName(), name)) {
177
				return exportAttribute.getNewDataType();
178
			}
179
		}
180
		return 0;
181
	}
179
    @Override
180
    public int getTargetType(String name) {
181
        for (ExportAttribute exportAttribute : this.exportAttributes) {
182
            if (StringUtils.equals(exportAttribute.getName(), name)) {
183
                return exportAttribute.getNewDataType();
184
            }
185
        }
186
        return 0;
187
    }
182 188

  
183
	@Override
184
	public int getSourceType(String name) {
185
		for (ExportAttribute exportAttribute : this.exportAttributes) {
186
			if (StringUtils.equals(exportAttribute.getNewName(), name)) {
187
				return exportAttribute.getDataType();
188
			}
189
		}
190
		return 0;
191
	}
189
    @Override
190
    public int getSourceType(String name) {
191
        for (ExportAttribute exportAttribute : this.exportAttributes) {
192
            if (StringUtils.equals(exportAttribute.getNewName(), name)) {
193
                return exportAttribute.getDataType();
194
            }
195
        }
196
        return 0;
197
    }
192 198

  
193
	@Override
194
	public boolean isAttributeNamesValid() {
195
		if (this.namesTranslator == null) {
196
			return true;
197
		}
198
		for (int i = 0; i < exportAttributes.size(); i++) {
199
			ExportAttribute attr = exportAttributes.get(i);
200
			if (!this.namesTranslator.isValidName(this, i, attr.getNewName())) {
201
				return false;
202
			}
203
		}
204
		return true;
205
	}
199
    @Override
200
    public boolean isAttributeNamesValid() {
201
        if (this.namesTranslator == null) {
202
            return true;
203
        }
204
        for (int i = 0; i < exportAttributes.size(); i++) {
205
            ExportAttribute attr = exportAttributes.get(i);
206
            if (!this.namesTranslator.isValidName(this, i, attr.getNewName())) {
207
                return false;
208
            }
209
        }
210
        return true;
211
    }
206 212

  
207
	@Override
208
	public void fixAttributeNames() {
209
		if (this.isAttributeNamesValid() == true) {
210
			return;
211
		}
212
		if (this.namesTranslator == null) {
213
			return;
214
		}
215
		int n = 0;
216
		while (!this.isAttributeNamesValid()) {
217
			for (int i = 0; i < exportAttributes.size(); i++) {
218
				ExportAttribute attr = exportAttributes.get(i);
219
				String newName = attr.getNewName();
220
				if (!this.namesTranslator.isValidName(this, i, newName)) {
221
					String sug = this.namesTranslator.getNameSuggestion(this, i, newName);
222
					attr.setNewName(sug);
223
				}
224
			}
225
			if (n > 5000) {
226
				LOGGER.warn("Not been able to fix attribute field names, it will require a manual operation");
227
				break;
228
			}
229
			n += 1;
230
		}
213
    @Override
214
    public void fixAttributeNames() {
215
        if (this.isAttributeNamesValid() == true) {
216
            return;
217
        }
218
        if (this.namesTranslator == null) {
219
            return;
220
        }
221
        int n = 0;
222
        while (!this.isAttributeNamesValid()) {
223
            for (int i = 0; i < exportAttributes.size(); i++) {
224
                ExportAttribute attr = exportAttributes.get(i);
225
                String newName = attr.getNewName();
226
                if (!this.namesTranslator.isValidName(this, i, newName)) {
227
                    String sug = this.namesTranslator.getNameSuggestion(this, i, newName);
228
                    attr.setNewName(sug);
229
                }
230
            }
231
            if (n > 5000) {
232
                LOGGER.warn("Not been able to fix attribute field names, it will require a manual operation");
233
                break;
234
            }
235
            n += 1;
236
        }
231 237

  
232
	}
238
    }
233 239

  
234
	@Override
235
	public ExportAttributes clone() throws CloneNotSupportedException {
236
		DefaultExportAttributes clone = (DefaultExportAttributes) super.clone();
237
		this.namesTranslator.clone();
238
		clone.setNamesTranslator(this.namesTranslator.clone());
240
    @Override
241
    public ExportAttributes clone() throws CloneNotSupportedException {
242
        DefaultExportAttributes clone = (DefaultExportAttributes) super.clone();
243
        this.namesTranslator.clone();
244
        clone.setNamesTranslator(this.namesTranslator.clone());
239 245

  
240
		List cloneListAttribute = new ArrayList();
241
		for (ExportAttribute exportAttribute : exportAttributes) {
242
			cloneListAttribute.add(exportAttribute.clone());
243
		}
244
		clone.setExportAttributes(cloneListAttribute);
246
        List cloneListAttribute = new ArrayList();
247
        for (ExportAttribute exportAttribute : exportAttributes) {
248
            cloneListAttribute.add(exportAttribute.clone());
249
        }
250
        clone.setExportAttributes(cloneListAttribute);
245 251

  
246
		return clone;
247
	}
252
        return clone;
253
    }
248 254

  
249
	@Override
250
	public void setExportAttributes(List<ExportAttribute> exportAttributes) {
251
		this.exportAttributes = exportAttributes;
252
	}
255
    @Override
256
    public void setExportAttributes(List<ExportAttribute> exportAttributes) {
257
        this.exportAttributes = exportAttributes;
258
    }
253 259

  
254
	@Override
255
	public void setActive(boolean active) {
256
		this.active = active;
257
	}
260
    @Override
261
    public void setActive(boolean active) {
262
        this.active = active;
263
    }
258 264

  
259
	@Override
260
	public boolean isActive() {
261
		return this.active;
262
	}
265
    @Override
266
    public boolean isActive() {
267
        return this.active;
268
    }
263 269

  
270
    public static void registerPersistence() {
271
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
272
        if (manager.getDefinition("ExportAttributes") == null) {
273
            DynStruct definition = manager.addDefinition(DefaultExportAttributes.class,
274
                    "ExportAttributes", "ExportAttributes persistence definition", null, null);
275
            definition.addDynFieldList("exportAttributes").setClassOfItems(ExportAttribute.class);
276
            definition.addDynFieldObject("namesTranslator").setClassOfValue(AttributeNamesTranslator.class);
277
            definition.addDynFieldObject("sourceFeatureType").setClassOfValue(FeatureType.class);
278
            definition.addDynFieldBoolean("active");
279
        }
280
    }
281

  
282
    @Override
283
    public void saveToState(PersistentState state) throws PersistenceException {
284
        state.set("exportAttributes", this.exportAttributes.iterator());
285
        state.set("namesTranslator", this.namesTranslator);
286
        state.set("sourceFeatureType", this.sourceFeatureType);
287
        state.set("active", this.active);
288
    }
289

  
290
    @Override
291
    public void loadFromState(PersistentState state) throws PersistenceException {
292
        Iterator it = state.getIterator("exportAttributes");
293
        List<ExportAttribute> data = new ArrayList<ExportAttribute>();
294
        while (it.hasNext()) {
295
            ExportAttribute ref = (ExportAttribute) it.next();
296
            data.add(ref);
297
        }
298
        this.exportAttributes = data;
299

  
300
        AttributeNamesTranslator nameTranslatorState = (AttributeNamesTranslator) state.get("namesTranslator");
301
        if (nameTranslatorState != null) {
302
            this.namesTranslator = nameTranslatorState;
303
        }
304
        this.sourceFeatureType = (FeatureType) state.get("sourceFeatureType");
305
        this.active = state.getBoolean("active");
306
    }
307

  
264 308
}

Also available in: Unified diff