Revision 6212 trunk/libraries/libFMap/src/com/iver/cit/gvsig/fmap/edition/writers/dbf/DbfWriter.java

View differences:

DbfWriter.java
34 34

  
35 35
	private Object[] record;
36 36

  
37
	private ArrayList fieldCommands = new ArrayList();
38

  
37 39
	// private FLyrVect lyrVect;
38 40
	private FBitSet selection = null;
39 41

  
......
70 72
		// de las cabeceras. Luego en el postprocess los escribiremos
71 73
		// correctamente, con el fullExtent y el numero de
72 74
		// registros que tocan.
75
		alterTable();
73 76
		if (selection == null) {
74 77

  
75 78
			try {
......
89 92

  
90 93
	public void process(IRowEdited row) throws EditionException {
91 94
		IRow rowEdit = (IRow) row.getLinkedRow();
92
		System.err.println("DBFWriter: " + row.getStatus() + " numRows = " + numRows);
93
		switch (row.getStatus())
94
		{
95
		System.err.println("DBFWriter: " + row.getStatus() + " numRows = "
96
				+ numRows);
97
		switch (row.getStatus()) {
95 98
		case IRowEdited.STATUS_ADDED:
96 99
		case IRowEdited.STATUS_ORIGINAL:
97 100
		case IRowEdited.STATUS_MODIFIED:
98 101
			try {
99
				
102

  
100 103
				for (int i = 0; i < record.length; i++)
101 104
					record[i] = rowEdit.getAttribute(i);
102 105
				dbfWrite.write(record);
......
106 109
				e.printStackTrace();
107 110
				throw new EditionException(e);
108 111
			}
109
			
112

  
110 113
		}
111 114

  
112

  
113 115
	}
114 116

  
115 117
	public void postProcess() throws EditionException {
......
118 120
			dbfWrite = new DbaseFileWriterNIO(myHeader,
119 121
					(FileChannel) getWriteChannel(dbfPath));
120 122

  
121
			
122 123
		} catch (IOException e) {
123 124
			e.printStackTrace();
124 125
			throw new EditionException(e);
......
146 147
		case Types.CHAR:
147 148
		case Types.LONGVARCHAR:
148 149
			return true; // TODO: Revisar esto, porque no creo que admita
149
							// campos muy grandes
150
		// campos muy grandes
150 151

  
151 152
		}
152 153

  
......
169 170
		return originalFields;
170 171
	}
171 172

  
172
public void alterTable(FieldCommand[] fieldCommands) throws EditionException {
173
	public boolean alterTable() throws EditionException {
173 174
		ArrayList fields = new ArrayList();
174 175
		boolean bNeedRewrite = false;
175
		for (int i=0; i < fieldCommands.length; i++)
176
		{
177
			if (fieldCommands[i] instanceof AddFieldCommand)
178
			{
179
				AddFieldCommand addFC = (AddFieldCommand) fieldCommands[i];
176
		for (int i = 0; i < fieldCommands.size(); i++) {
177
			FieldCommand fc = (FieldCommand) fieldCommands.get(i);
178
			if (fc instanceof AddFieldCommand) {
179
				AddFieldCommand addFC = (AddFieldCommand) fc;
180 180
				bNeedRewrite = true;
181 181
				fields.add(addFC.getFieldDesc());
182 182
			}
183
			if (fieldCommands[i] instanceof RemoveFieldCommand)
184
			{
183
			if (fc instanceof RemoveFieldCommand) {
185 184
				bNeedRewrite = true;
186
				RemoveFieldCommand deleteFC = (RemoveFieldCommand) fieldCommands[i];
187
//				for (int j=0; j < myHeader.getNumFields(); j++)
188
//				{
189
//					if (myHeader.getFieldName(j).compareTo(deleteFC.getFieldName()) == 0)
190
//					{
191
						myHeader.removeColumn(deleteFC.getFieldName());
192
//						break;
193
//					}
194
//				}
195
				
185
				RemoveFieldCommand deleteFC = (RemoveFieldCommand) fc;
186
				// for (int j=0; j < myHeader.getNumFields(); j++)
187
				// {
188
				// if
189
				// (myHeader.getFieldName(j).compareTo(deleteFC.getFieldName())
190
				// == 0)
191
				// {
192
				myHeader.removeColumn(deleteFC.getFieldName());
193
				// break;
194
				// }
195
				// }
196

  
196 197
			}
197
			if (fieldCommands[i] instanceof RenameFieldCommand)
198
			{
199
				RenameFieldCommand renFC = (RenameFieldCommand) fieldCommands[i];
200
				for (int j=0; j < myHeader.getNumFields(); j++)
201
				{
202
					if (myHeader.getFieldName(j).compareTo(renFC.getAntName()) == 0)
203
					{
198
			if (fc instanceof RenameFieldCommand) {
199
				RenameFieldCommand renFC = (RenameFieldCommand) fc;
200
				for (int j = 0; j < myHeader.getNumFields(); j++) {
201
					if (myHeader.getFieldName(j).compareTo(renFC.getAntName()) == 0) {
204 202
						myHeader.setFieldName(j, renFC.getNewName());
205 203
						break;
206 204
					}
207 205
				}
208
			}			
209
			
206
			}
207

  
210 208
		}
211
		FieldDescription[] fieldsDesc = (FieldDescription[]) fields.toArray(new FieldDescription[0]);
212
		
209
		FieldDescription[] fieldsDesc = (FieldDescription[]) fields
210
				.toArray(new FieldDescription[0]);
211

  
213 212
		myHeader = DbaseFileHeaderNIO.createDbaseHeader(fieldsDesc);
214 213
		try {
215 214
			dbfWrite = new DbaseFileWriterNIO(myHeader,
216 215
					(FileChannel) getWriteChannel(dbfPath));
217
			if (bNeedRewrite)
218
			{
219
				int aux = (int)(Math.random() * 1000);
220
				File fTemp = new File(System.getProperty("java.io.tmpdir") + "/tmpDbf" + aux + ".dbf");
221
				
222
				// TODO: TERMINAR!!
223
				
224
			}
216
//			if (bNeedRewrite) {
217
//				int aux = (int) (Math.random() * 1000);
218
//				File fTemp = new File(System.getProperty("java.io.tmpdir")
219
//						+ "/tmpDbf" + aux + ".dbf");
220
//
221
//				// TODO: TERMINAR!!
222
//
223
//			}
225 224
		} catch (IOException e) {
226 225
			throw new EditionException(e);
227 226
		}
228
	
229
	}}
227
		return bNeedRewrite;
228
	}
229

  
230
	public void addField(FieldDescription fieldDesc) {
231
		AddFieldCommand c = new AddFieldCommand(fieldDesc);
232
		fieldCommands.add(c);
233
	}
234

  
235
	public FieldDescription removeField(String fieldName) {
236
		RemoveFieldCommand c = new RemoveFieldCommand(fieldName);
237
		FieldDescription[] act = getFields();
238
		FieldDescription found = null;
239
		for (int i=0; i < act.length; i++)
240
		{
241
			if (act[i].getFieldAlias().compareToIgnoreCase(fieldName) == 0)
242
			{
243
				found = act[i];
244
				break;
245
			}
246
		}
247
		fieldCommands.add(c);
248
		return found;
249
	}
250

  
251
	public void renameField(String antName, String newName) {
252
		RenameFieldCommand c = new RenameFieldCommand(antName, newName);
253
		fieldCommands.add(c);
254
		
255
	}
256

  
257
	public FieldDescription[] getFields() {
258
		ArrayList aux = new ArrayList();
259
		for (int i=0; i < aux.size(); i++)
260
		{
261
			aux.add(getOriginalFields()[i]);
262
		}
263
		// procesamos comandos para devolver los campos reales.
264
		for (int j=0; j < fieldCommands.size(); j++)
265
		{
266
			FieldCommand fc = (FieldCommand) fieldCommands.get(j);
267
			if (fc instanceof AddFieldCommand)
268
			{
269
				AddFieldCommand ac = (AddFieldCommand) fc;
270
				aux.add(ac.getFieldDesc());
271
			}
272
			if (fc instanceof RemoveFieldCommand)
273
			{
274
				RemoveFieldCommand rc = (RemoveFieldCommand) fc;
275
				for (int k = 0; k < aux.size(); k++) {
276
					FieldDescription fAux = (FieldDescription) aux.get(k);
277
					if (fAux.getFieldAlias().compareTo(rc.getFieldName()) == 0) {
278
						aux.remove(k);
279
						break;
280
					}
281
				}
282
			}
283
			if (fc instanceof RenameFieldCommand)
284
			{
285
				RenameFieldCommand renc = (RenameFieldCommand) fc;
286
				for (int k = 0; k < aux.size(); k++) {
287
					FieldDescription fAux = (FieldDescription) aux.get(k);
288
					if (fAux.getFieldAlias().compareTo(renc.getAntName()) == 0) {
289
						fAux.setFieldAlias(renc.getNewName());
290
						break;
291
					}
292
				}
293

  
294
			}			
295
			
296
		}
297
		return (FieldDescription[]) aux.toArray(new FieldDescription[0]);
298
	}
299

  
300
}

Also available in: Unified diff