Revision 32880 branches/v2_0_0_prep/libraries/libFMap_daldb/src/org/gvsig/fmap/dal/store/jdbc/JDBCStoreProviderWriter.java

View differences:

JDBCStoreProviderWriter.java
47 47
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
48 48
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices;
49 49
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
50
import org.gvsig.fmap.dal.store.db.FeatureTypeHelper;
50 51
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCExecutePreparedSQLException;
51 52
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCExecuteSQLException;
52 53
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCPreparingSQLException;
......
67 68

  
68 69

  
69 70
	protected String appendModeSql;
70
	protected List appendModeAttributes;
71
	protected List<FeatureAttributeDescriptor> appendModeAttributes;
71 72

  
72 73

  
73 74
	public JDBCStoreProviderWriter(JDBCStoreParameters params,
......
86 87

  
87 88
	protected void addToListFeatureValues(FeatureProvider featureProvider,
88 89
			FeatureAttributeDescriptor attrOfList,
89
			FeatureAttributeDescriptor attr, List values) throws DataException {
90
			FeatureAttributeDescriptor attr, List<Object> values) throws DataException {
90 91
		if (attr == null) {
91 92
			if (attrOfList.isPrimaryKey()) {
92
				// FIXME excepton
93 93
				throw new RuntimeException("pk attribute '"
94 94
						+ attrOfList.getName() + "' not found in feature");
95 95
			}
......
103 103
	}
104 104

  
105 105
	protected void addToListFeatureValues(FeatureProvider featureProvider,
106
			List attributes, List values) throws DataException {
106
			List<FeatureAttributeDescriptor> attributes, List<Object> values) throws DataException {
107 107
		FeatureAttributeDescriptor attr, attrOfList;
108 108
		FeatureType fType = featureProvider.getType();
109 109
		for (int i = 0; i < attributes.size(); i++) {
......
114 114
	}
115 115

  
116 116
	protected void appendToSQLPreparedPkWhereClause(StringBuilder sql,
117
			List pkAttributes) {
117
			List<FeatureAttributeDescriptor> pkAttributes) {
118 118
		sql.append(" Where ");
119 119
		FeatureAttributeDescriptor attr;
120 120
		for (int i = 0; i < pkAttributes.size() - 1; i++) {
......
129 129
	}
130 130

  
131 131
	protected void executeRemovePreparedStatement(Connection conn, String sql,
132
			List attributes, Iterator featureReferences) throws DataException {
132
			List<FeatureAttributeDescriptor> attributes, Iterator<FeatureReferenceProviderServices> featureReferences) throws DataException {
133 133
				PreparedStatement st;
134 134
				try {
135 135
					st = conn.prepareStatement(sql);
......
137 137
					throw new JDBCPreparingSQLException(sql, e);
138 138
				}
139 139
				try {
140
					List values = new ArrayList();
140
					List<Object> values = new ArrayList<Object>();
141 141
					FeatureReferenceProviderServices featureRef;
142
					FeatureType featureType;
142
//					FeatureType featureType;
143 143
					while (featureReferences.hasNext()) {
144 144
						st.clearParameters();
145
						featureRef = (FeatureReferenceProviderServices) featureReferences
146
								.next();
145
						featureRef = featureReferences.next();
147 146
						values.clear();
148
						featureType = this.getFeatureStore().getFeatureType(
149
						featureRef
150
								.getFeatureTypeId());
147
//						featureType = this.getFeatureStore()
148
//							.getFeatureType(featureRef.getFeatureTypeId());
151 149

  
152
						Iterator iter = attributes.iterator();
150
						Iterator<FeatureAttributeDescriptor> iter = attributes.iterator();
153 151
						FeatureAttributeDescriptor attr;
154 152
						while (iter.hasNext()) {
155
							attr = (FeatureAttributeDescriptor) iter.next();
156
							values.add(helper.dalValueToJDBC(attr, featureRef
153
							attr =  iter.next();
154
							values.add( helper.dalValueToJDBC(attr, featureRef
157 155
									.getKeyValue(attr.getName())));
158 156
						}
159 157

  
......
184 182
			}
185 183

  
186 184
	protected void executeUpdatePreparedStatement(Connection conn, String sql,
187
			List attributes, Iterator featureProviders) throws DataException {
185
			List<FeatureAttributeDescriptor> attributes, Iterator<FeatureProvider> featureProviders) throws DataException {
188 186
				PreparedStatement st;
189 187
				try {
190 188
					st = conn.prepareStatement(sql);
......
192 190
					throw new JDBCPreparingSQLException(sql, e);
193 191
				}
194 192
				try {
195
					List values = new ArrayList();
193
					List<Object> values = new ArrayList<Object>();
196 194
					FeatureProvider featureProvider;
197 195
					while (featureProviders.hasNext()) {
198 196
						st.clearParameters();
......
219 217

  
220 218
			}
221 219

  
222
	protected void performDeletes(Connection conn, Iterator deleteds, List pkAttributes)
220
	protected void performDeletes(Connection conn, Iterator<FeatureReferenceProviderServices> deleteds, List<FeatureAttributeDescriptor> pkAttributes)
223 221
			throws DataException {
224 222

  
225 223
				if (pkAttributes.size() < 0) {
226
					// FIXME Exception
227 224
					throw new RuntimeException("Operation requires missing pk");
228 225
				}
229 226

  
......
239 236
			}
240 237

  
241 238
	protected String getSqlStatementAddField(FeatureAttributeDescriptor attr,
242
			List additionalStatement) throws DataException {
239
			List<String> additionalStatement) throws DataException {
243 240
		StringBuilder strb = new StringBuilder();
244 241
		strb.append("ADD ");
245 242
		strb.append(this.helper.getSqlFieldDescription(attr));
246 243
		return strb.toString();
247 244
	}
248 245

  
249
	protected String getSqlStatementDropField(FeatureAttributeDescriptor attr,List additionalStatement) {
246
	protected String getSqlStatementDropField(FeatureAttributeDescriptor attr,List<String> additionalStatement) {
250 247
		// DROP [ COLUMN ] column
251 248
		return " DROP COLUMN "
252 249
				+ this.helper.escapeFieldName(attr.getName());
......
258 255
	}
259 256

  
260 257
	public void endAppend() throws DataException {
261
		this.loadMetadata();
262 258
		appendModeSql = null;
263 259
		appendModeAttributes = null;
264 260
	}
265 261

  
266
	protected List getSqlStatementAlterField(
262
	protected List<String> getSqlStatementAlterField(
267 263
			FeatureAttributeDescriptor attrOrg,
268
			FeatureAttributeDescriptor attrTrg, List additionalStatement)
264
			FeatureAttributeDescriptor attrTrg, List<String> additionalStatement)
269 265
			throws DataException {
270 266
		//
271
		List actions = new ArrayList();
267
		List<String> actions = new ArrayList<String>();
272 268
		StringBuilder strb;
273 269
		if (attrOrg.getDataType() != attrTrg.getDataType()) {
274 270
			// ALTER COLUMN {col} TYPE {type} character varying(35)
......
331 327
		 * ALTER TABLE [ ONLY ] name [ * ] action [, ... ]
332 328
		 */
333 329

  
334
		List toDrop = new ArrayList();
335
		List toAdd = new ArrayList();
336
		List toAlter = new ArrayList();
330
		List<String> toDrop = new ArrayList<String>();
331
		List<String> toAdd = new ArrayList<String>();
332
		List<String> toAlter = new ArrayList<String>();
337 333

  
338
		List additionalStatement = new ArrayList();
334
		List<String> additionalStatement = new ArrayList<String>();
339 335

  
340 336
		FeatureAttributeDescriptor attrOrg;
341 337
		FeatureAttributeDescriptor attrTrg;
342
		Iterator attrs = original.iterator();
338
		Iterator<FeatureAttributeDescriptor> attrs = FeatureTypeHelper.iterator(original);
343 339
		while (attrs.hasNext()) {
344 340
			attrOrg = (FeatureAttributeDescriptor) attrs.next();
345 341
			attrTrg = target.getAttributeDescriptor(attrOrg.getName());
......
347 343
				toDrop.add(getSqlStatementDropField(attrOrg,
348 344
						additionalStatement));
349 345
			} else {
350
				toAlter.addAll(getSqlStatementAlterField(attrOrg, attrTrg,
351
						additionalStatement));
346
				toAlter.addAll(getSqlStatementAlterField(attrOrg, attrTrg,additionalStatement));
352 347
			}
353 348

  
354 349
		}
355
		attrs = target.iterator();
350
		attrs = FeatureTypeHelper.iterator(target);
356 351
		while (attrs.hasNext()) {
357 352
			attrTrg = (FeatureAttributeDescriptor) attrs.next();
358 353
			if (original.getAttributeDescriptor(attrTrg.getName()) == null) {
359
				toAdd
360
						.add(getSqlStatementAddField(attrTrg,
354
				toAdd.add(getSqlStatementAddField(attrTrg,
361 355
								additionalStatement));
362 356
			}
363 357
		}
......
368 362
		sqlb.append(getJDBCParameters().tableID());
369 363
		sqlb.append(' ');
370 364

  
371
		List actions = new ArrayList();
365
		List<String> actions = new ArrayList<String>();
372 366
		actions.addAll(toDrop);
373 367
		actions.addAll(toAlter);
374 368
		actions.addAll(toAdd);
375 369

  
376
		Iterator it = actions.iterator();
370
		Iterator<String> it = actions.iterator();
377 371
		while (it.hasNext()) {
378 372
			if (it.next() == null) {
379 373
				it.remove();
......
404 398
		}
405 399
		try {
406 400
			st.execute(sql);
407
			Iterator iter = additionalStatement.iterator();
401
			Iterator<String> iter = additionalStatement.iterator();
408 402
			while (iter.hasNext()) {
409 403
				sql = (String) iter.next();
410 404
				st.execute(sql);
......
424 418

  
425 419

  
426 420
	private void perfomInsert(Connection conn, PreparedStatement insertSt,
427
			String sql, FeatureProvider feature, List attributes)
421
			String sql, FeatureProvider feature, List<FeatureAttributeDescriptor> attributes)
428 422
			throws DataException {
429 423

  
430 424
		try {
431 425
			insertSt.clearParameters();
432
			List values = new ArrayList();
426
			List<Object> values = new ArrayList<Object>();
433 427
			addToListFeatureValues(feature, attributes, values);
434
			FeatureAttributeDescriptor attr;
428
//			FeatureAttributeDescriptor attr;
435 429
			int j = 1;
436 430
			for (int i = 0; i < values.size(); i++) {
437 431
				insertSt.setObject(j, values.get(i));
......
490 484
	}
491 485

  
492 486
	protected void prepareAttributeForUpdate(FeatureAttributeDescriptor attr,
493
			List values) {
487
			List<String> values) {
494 488
		values.add(helper.escapeFieldName(attr.getName()) + " = ?");
495 489
	}
496 490

  
497 491
	protected void prepareAttributeForInsert(FeatureAttributeDescriptor attr,
498
			List fields, List values) {
492
			List<String> fields, List<String> values) {
499 493

  
500 494
		fields.add(helper.escapeFieldName(attr.getName()));
501 495
		values.add("?");
......
504 498

  
505 499

  
506 500
	protected void prepareSQLAndAttributeListForInsert(StringBuilder sqlb,
507
			List attributes) throws DataException {
501
			List<FeatureAttributeDescriptor> attributes) throws DataException {
508 502
		/*
509 503
		 * INSERT INTO table [ ( column [, ...] ) ] { DEFAULT VALUES | VALUES (
510 504
		 * { expression | DEFAULT } [, ...] ) [, ...] | query } [ RETURNING * |
......
518 512

  
519 513
		FeatureType type = this.getFeatureStore().getDefaultFeatureType();
520 514

  
521
		List fields = new ArrayList();
522
		List values = new ArrayList();
515
		List<String> fields = new ArrayList<String>();
516
		List<String> values = new ArrayList<String>();
523 517

  
524
		Iterator iter = type.iterator();
518
		Iterator<FeatureAttributeDescriptor> iter = FeatureTypeHelper.iterator(type);
525 519
		FeatureAttributeDescriptor attr;
526 520
		while (iter.hasNext()) {
527
			attr = (FeatureAttributeDescriptor) iter.next();
521
			attr = iter.next();
528 522
			if (attr.isAutomatic() || attr.isReadOnly()) {
529 523
				continue;
530 524
			}
......
533 527

  
534 528
		}
535 529
		if (attributes.size() < 1) {
536
			// FIXME exception
537 530
			throw new RuntimeException("no fields to set");
538 531
		}
539 532

  
......
547 540
	}
548 541

  
549 542

  
550
	protected void performInserts(Connection conn, Iterator inserteds)
543
	protected void performInserts(Connection conn, Iterator<FeatureProvider> inserteds)
551 544
			throws DataException {
552 545

  
553 546
		StringBuilder sqlb = new StringBuilder();
554
		List attrs = new ArrayList();
547
		List<FeatureAttributeDescriptor> attrs = new ArrayList<FeatureAttributeDescriptor>();
555 548

  
556 549
		prepareSQLAndAttributeListForInsert(sqlb, attrs);
557 550

  
......
564 557
		}
565 558
		try {
566 559
			while (inserteds.hasNext()) {
567
				perfomInsert(conn, st, sql, (FeatureProvider) inserteds.next(),
560
				perfomInsert(conn, st, sql, inserteds.next(),
568 561
						attrs);
569 562
			}
570 563
		} finally {
......
572 565
		}
573 566
	}
574 567

  
575
	protected void performUpdates(Connection conn, Iterator updateds,
576
			List pkAttributes) throws DataException {
568
	protected void performUpdates(Connection conn, Iterator<FeatureProvider> updateds,
569
			List<FeatureAttributeDescriptor> pkAttributes) throws DataException {
577 570
		/*
578 571
		 * UPDATE [ ONLY ] table [ [ AS ] alias ] SET { column = { expression |
579 572
		 * DEFAULT } | ( column [, ...] ) = ( { expression | DEFAULT } [, ...] )
......
582 575
		 */
583 576

  
584 577
		if (pkAttributes.size() < 0) {
585
			// FIXME Exception
586 578
			throw new RuntimeException("Operation requires missing pk");
587 579
		}
588 580

  
......
594 586

  
595 587
		sqlb.append(" SET ");
596 588

  
597
		List values = new ArrayList();
589
		List<String> values = new ArrayList<String>();
598 590

  
599 591
		FeatureType type = this.getFeatureStore().getDefaultFeatureType();
600 592

  
601
		Iterator iter = type.iterator();
593
		Iterator<FeatureAttributeDescriptor> iter = FeatureTypeHelper.iterator(type);
602 594
		FeatureAttributeDescriptor attr;
603
		List updateAttrs = new ArrayList();
595
		List<FeatureAttributeDescriptor> updateAttrs = new ArrayList<FeatureAttributeDescriptor>();
604 596
		while (iter.hasNext()) {
605
			attr = (FeatureAttributeDescriptor) iter.next();
597
			attr = iter.next();
606 598
			if (attr.isPrimaryKey() || attr.isAutomatic() || attr.isReadOnly()) {
607 599
				continue;
608 600
			}
......
611 603

  
612 604
		}
613 605
		if (updateAttrs.size() < 1) {
614
			// FIXME exception
615 606
			throw new RuntimeException("no fields to set");
616 607
		}
617 608

  
......
631 622

  
632 623
	public void beginAppend() throws DataException {
633 624
		StringBuilder sqlb = new StringBuilder();
634
		List attrs = new ArrayList();
625
		List<FeatureAttributeDescriptor> attrs = new ArrayList<FeatureAttributeDescriptor>();
635 626

  
636 627
		prepareSQLAndAttributeListForInsert(sqlb, attrs);
637 628

  
......
641 632

  
642 633

  
643 634
	protected TransactionalAction getPerformChangesAction(
644
			final Iterator deleteds, final Iterator inserteds,
645
			final Iterator updateds, final Iterator featureTypesChanged) {
635
			final Iterator<FeatureReferenceProviderServices> deleteds, 
636
			final Iterator<FeatureProvider> inserteds,
637
			final Iterator<FeatureProvider> updateds, 
638
			final Iterator<FeatureTypeChanged> featureTypesChanged) {
646 639

  
647 640
		TransactionalAction action = new TransactionalAction() {
648 641

  
......
650 643

  
651 644
				if (featureTypesChanged.hasNext()) {
652 645

  
653
					FeatureTypeChanged item = (FeatureTypeChanged) featureTypesChanged
654
							.next();
646
					FeatureTypeChanged item = featureTypesChanged.next();
655 647
					performUpdateTable(conn, item.getSource(), item.getTarget());
656 648
				}
657 649

  
658
				List pkAttributes = null;
650
				List<FeatureAttributeDescriptor> pkAttributes = null;
659 651
				if (deleteds.hasNext() || updateds.hasNext()) {
660 652
					pkAttributes = Arrays.asList(getFeatureStore()
661 653
							.getDefaultFeatureType()
......
687 679

  
688 680
	}
689 681

  
682
	@SuppressWarnings("unchecked")
690 683
	public void performChanges(Iterator deleteds, Iterator inserteds,
691 684
			Iterator updateds, Iterator featureTypesChanged)
692 685
			throws PerformEditingException {
......
725 718
				return false;
726 719
			}
727 720
			FeatureAttributeDescriptor attr;
728
			Iterator iter = ft.iterator();
721
			Iterator<FeatureAttributeDescriptor> iter = FeatureTypeHelper.iterator(ft);
729 722
			while (iter.hasNext()) {
730 723
				attr = (FeatureAttributeDescriptor) iter.next();
731 724
				if (attr.isPrimaryKey()) {

Also available in: Unified diff