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

View differences:

JDBCStoreProvider.java
45 45
import org.gvsig.fmap.dal.DALLocator;
46 46
import org.gvsig.fmap.dal.DataManager;
47 47
import org.gvsig.fmap.dal.DataServerExplorer;
48
import org.gvsig.fmap.dal.DataStore;
48 49
import org.gvsig.fmap.dal.DataStoreNotification;
49 50
import org.gvsig.fmap.dal.DataTypes;
50 51
import org.gvsig.fmap.dal.exception.CloseException;
......
56 57
import org.gvsig.fmap.dal.feature.EditableFeatureType;
57 58
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
58 59
import org.gvsig.fmap.dal.feature.FeatureQuery;
59
import org.gvsig.fmap.dal.feature.FeatureStore;
60 60
import org.gvsig.fmap.dal.feature.FeatureType;
61 61
import org.gvsig.fmap.dal.feature.spi.AbstractFeatureStoreProvider;
62 62
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
......
66 66
import org.gvsig.fmap.dal.resource.exception.ResourceExecuteException;
67 67
import org.gvsig.fmap.dal.resource.spi.ResourceProvider;
68 68
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
69
import org.gvsig.fmap.dal.store.db.DBHelper;
70
import org.gvsig.fmap.dal.store.db.FeatureTypeHelper;
69 71
import org.gvsig.fmap.dal.store.jdbc.exception.InvalidResultSetIdException;
70 72
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCException;
71 73
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException;
72 74
import org.gvsig.fmap.geom.Geometry;
73 75
import org.gvsig.fmap.geom.operation.towkb.ToWKB;
74 76
import org.gvsig.fmap.geom.primitive.Envelope;
75
import org.gvsig.tools.ToolsLocator;
76
import org.gvsig.tools.dynobject.DynClass;
77
import org.gvsig.metadata.MetadataLocator;
78
import org.gvsig.metadata.MetadataManager;
77 79
import org.gvsig.tools.dynobject.DynObject;
78
import org.gvsig.tools.dynobject.DynObjectManager;
80
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException;
79 81
import org.gvsig.tools.exception.BaseException;
80 82
import org.slf4j.Logger;
81 83
import org.slf4j.LoggerFactory;
......
91 93
	final static private Logger logger = LoggerFactory
92 94
			.getLogger(JDBCStoreProvider.class);
93 95

  
94
	private List resulsetList;
96
	private List<ResultSetInfo> resulsetList;
95 97

  
96 98
	public static String NAME = "JDBC";
97
	private static final String DYNCLASS_NAME = "JDBCStore";
98 99
	public static String DESCRIPTION = "JDBC source";
99
	private static DynClass DYNCLASS = null;
100 100

  
101
	public static final String METADATA_DEFINITION_NAME = NAME;
102

  
101 103
	private long mlsecondsToZombie = 1000 * 60 * 10; // 10 Min
102 104

  
103 105
	protected JDBCHelper helper;
......
106 108

  
107 109
	private Long totalCount = null;
108 110

  
109

  
110 111
	public JDBCStoreProvider(JDBCStoreParameters params,
111 112
			DataStoreProviderServices storeServices) throws InitializeException {
112
		this(params, storeServices, ToolsLocator.getDynObjectManager()
113
				.createDynObject(DYNCLASS));
113
		this(
114
				params, 
115
				storeServices,
116
				DBHelper.newMetadataContainer(METADATA_DEFINITION_NAME)
117
		);
114 118
	}
115 119

  
116 120
	protected JDBCStoreProvider(JDBCStoreParameters params,
......
118 122
			throws InitializeException {
119 123
		super(params, storeServices, metadata);
120 124

  
121
		resulsetList = new ArrayList(10);
125
		resulsetList = new ArrayList<ResultSetInfo>(10);
122 126

  
123 127
		helper = createHelper();
124 128
		if (params.getSQL() != null && (params.getSQL()).trim().length() > 0) {
125 129
			directSQLMode = true;
126 130
		}
127 131

  
128

  
129
		this.setDynValue("DefaultSRS", null);
130
		this.setDynValue("Envelope", null);
131 132
		this.initFeatureType();
132 133
	}
133 134

  
135
	public Object getDynValue(String name) throws DynFieldNotFoundException {
136
		try {
137
			if( DataStore.METADATA_ENVELOPE.equalsIgnoreCase(name) ) {
138
				Envelope env = this.getEnvelope();
139
				if( env != null ) {
140
					return env;
141
				}
142
			} else if( DataStore.METADATA_CRS.equalsIgnoreCase(name) ) {
143
				IProjection proj;
144
				proj = this.getFeatureStore().getDefaultFeatureType().getDefaultSRS();
145
				if( proj != null ) {
146
					return proj;
147
				}
148
			}
149
		} catch (DataException e) {
150
			throw new RuntimeException(e);
151
		}
152
		return super.getDynValue(name);
153
	}
154
	
134 155

  
135

  
136 156
	protected JDBCStoreParameters getJDBCParameters() {
137 157
		return (JDBCStoreParameters) this.getParameters();
138 158
	}
139 159

  
140 160

  
141
	protected static void registerDynClass() {
142
		DynObjectManager dynman = ToolsLocator.getDynObjectManager();
143
		DynClass dynClass;
144
		if (DYNCLASS == null) {
145
			dynClass = dynman.add(DYNCLASS_NAME, DESCRIPTION);
146

  
147
			dynClass.extend(dynman.get(FeatureStore.DYNCLASS_NAME));
148
			DYNCLASS = dynClass;
149
		}
150
	}
151

  
152 161
	/**
153 162
	 * Load data form a resulset.<br>
154 163
	 *
......
169 178
			public Object run() throws Exception {
170 179
				ResultSet rs = getResultSet(resultsetID);
171 180
				FeatureAttributeDescriptor attr;
172
				Iterator iter = data.getType().iterator();
181
				Iterator<FeatureAttributeDescriptor> iter = FeatureTypeHelper.iterator(data.getType());
173 182
				while (iter.hasNext()) {
174
					attr = (FeatureAttributeDescriptor) iter.next();
183
					attr = iter.next();
175 184
					loadFeatureProviderValue(data, rs, attr);
176 185
				}
177 186
				return null;
......
179 188
		});
180 189
	}
181 190

  
182

  
183 191
	protected void loadFeatureProviderValue(FeatureProvider data, ResultSet rs,
184 192
			FeatureAttributeDescriptor attr) throws DataException {
185 193
		if (attr.getDataType() == DataTypes.GEOMETRY) {
......
288 296
			throws ResourceExecuteException,
289 297
			JDBCException {
290 298
		synchronized (this) {
299
			// FIXME: Esto no deberia funcionar. 
291 300
			Iterator iter = resulsetList.iterator();
292 301
			Integer rsID = null;
293 302
			while (iter.hasNext()) {
......
304 313

  
305 314
		}
306 315
	}
307

  
316
	
308 317
	protected final void forceCloseResultSet(int rsID)
309 318
			throws ResourceExecuteException, JDBCException,
310 319
			InvalidResultSetIdException {
......
317 326
		if (resultsetID >= resulsetList.size()) {
318 327
			throw new InvalidResultSetIdException(resultsetID);
319 328
		}
320
		ResultSetInfo rsInfo = (ResultSetInfo) resulsetList.get(resultsetID);
329
		ResultSetInfo rsInfo = resulsetList.get(resultsetID);
321 330
		if (rsInfo == null) {
322 331
			throw new InvalidResultSetIdException(resultsetID);
323 332
		}
......
393 402
		}
394 403
	}
395 404

  
396
	private int getResultsetOpenCount() {
397
		int count = 0;
398
		Iterator iter = resulsetList.iterator();
399
		while (iter.hasNext()) {
400
			if (iter.next() != null) {
401
				count++;
402
			}
403
		}
404
		return count;
405
	}
406

  
407 405
	protected void closeResulset(final ResultSet rs) throws JDBCException,
408 406
			ResourceExecuteException {
409 407
		getResource().execute(new ResourceAction() {
......
422 420
		});
423 421
	}
424 422

  
423
	private int getResultsetOpenCount() {
424
		int count = 0;
425
		Iterator<ResultSetInfo> iter = resulsetList.iterator();
426
		while (iter.hasNext()) {
427
			if (iter.next() != null) {
428
				count++;
429
			}
430
		}
431
		return count;
432
	}
433

  
425 434
	protected final int openResulsetCount() {
426 435
		int count = 0;
427
		Iterator iter = resulsetList.iterator();
436
		Iterator<ResultSetInfo> iter = resulsetList.iterator();
428 437
		while (iter.hasNext()) {
429 438
			if (iter.next() != null) {
430 439
				count++;
......
535 544
				getFeatureStore().getFeatureType(featureType.getId())
536 545
						.getPrimaryKey();
537 546

  
538
		List values = new ArrayList();
547
		List<Object> values = new ArrayList<Object>();
539 548

  
540 549
		int i;
541 550
		for (i = 0; i < pk.length - 1; i++) {
542
			values.add(helper.dalValueToJDBC(pk[i],
551
			values.add(
552
					helper.dalValueToJDBC(pk[i],
543 553
					reference.getKeyValue(pk[i].getName())));
544 554
			filter.append(helper.getSqlFieldName(pk[i]));
545 555
			filter.append(" = ? AND ");
......
555 565
		int rsId = createResultSet(sql, values.toArray(), 1);
556 566
		try {
557 567
			if (!resulsetNext(rsId)) {
558
				// FIXME Exception
559 568
				throw new RuntimeException("Reference Not found");
560 569
			}
561 570
			data = createFeatureProvider(featureType);
......
584 593
		}
585 594

  
586 595
		FeatureType defaultType = edFType.getNotEditableCopy();
587
		List types = Collections.singletonList(defaultType);
596
		List<FeatureType> types = Collections.singletonList(defaultType);
588 597
		this.getStoreServices().setFeatureTypes(types, defaultType);
589
		try {
590
			loadMetadata();
591
		} catch (DataException e) {
592
			throw new InitializeException(e);
593
		}
594 598
	}
595 599

  
596 600
	protected ResultSet createNewResultSet(final String sql,
......
619 623
											(byte[]) ((Geometry) value).invokeOperation(
620 624
													ToWKB.CODE, null);
621 625
								} catch (BaseException e) {
622
									// FIXME
623 626
									throw new InvalidParameterException();
624 627
								}
625 628
								st.setBytes(i + 1, bytes);
......
637 640
					}
638 641
					return rs;
639 642
				} catch (SQLException e) {
640
					// TODO throw exception ???
641 643
					try {
642 644
						rs.close();
643 645
					} catch (Exception e1) {
......
693 695

  
694 696
	protected void appendWhere(StringBuilder sql, String filter) {
695 697
		filter = fixFilter(filter);
696
		String initialFilter = getJDBCParameters().getInitialFilter();
698
		String initialFilter = getJDBCParameters().getBaseFilter();
697 699
		if ((initialFilter != null && initialFilter.length() != 0)
698 700
				|| (filter != null && filter.length() != 0)) {
699 701
			sql.append("where (");
......
717 719
		}
718 720
	}
719 721

  
720
	protected void loadMetadata() throws DataException {
721
		IProjection srs = getJDBCParameters().getSRS();
722

  
723
		if (srs == null) {
724
			srs = getFeatureStore().getDefaultFeatureType().getDefaultSRS();
725
		}
726

  
727
		this.setDynValue("DefaultSRS", srs);
728

  
729
		String defGeomName = this.getFeatureStore().getDefaultFeatureType()
730
				.getDefaultGeometryAttributeName();
731
		Envelope env = null;
732
		if (defGeomName != null && defGeomName.length() > 0) {
733
			env = this.helper.getFullEnvelopeOfField(this.getJDBCParameters(),
734
					defGeomName, this.getJDBCParameters().getWorkingArea());
735

  
736
		}
737
		this.setDynValue("Envelope", env);
738

  
739
	}
740

  
741 722
	public void closeDone() throws DataException {
742
		clearMetadata();
743

  
723
		// Do nothing
744 724
	}
745 725

  
746 726
	public void opendDone() throws DataException {
......
749 729

  
750 730
	public Envelope getEnvelope() throws DataException {
751 731
		this.open();
752
		return (Envelope) this.getDynValue("Envelope");
732
		String defaultGeometryAttributeName;
733
		defaultGeometryAttributeName = this.getFeatureStore()
734
			.getDefaultFeatureType()
735
				.getDefaultGeometryAttributeName();
736
		if( defaultGeometryAttributeName != null ) {
737
			return this.helper.getFullEnvelopeOfField(
738
					this.getJDBCParameters(),
739
					defaultGeometryAttributeName, 
740
					this.getJDBCParameters().getWorkingArea()
741
				);
742
		}
743
		return null;
753 744
	}
754 745

  
755 746
	public void resourceChanged(ResourceProvider resource) {
......
758 749
				resource);
759 750
	}
760 751

  
761
	protected void clearMetadata() {
762
		this.setDynValue("DefaultSRS", null);
763
		this.setDynValue("Envelope", null);
764
	}
765

  
766 752
	public boolean allowAutomaticValues() {
767 753
		return this.helper.allowAutomaticValues();
768 754
	}
......
784 770
			exParams.setSchema(params.getSchema());
785 771
			exParams.setJDBCDriverClassName(params.getJDBCDriverClassName());
786 772

  
787
			return manager.createServerExplorer(exParams);
773
			return manager.openServerExplorer(JDBCServerExplorer.NAME,exParams);
788 774
		} catch (DataException e) {
789 775
			throw new ReadException(this.getName(), e);
790 776
		} catch (ValidateDataParametersException e) {
791
			// TODO Auto-generated catch block
792 777
			throw new ReadException(this.getName(), e);
793 778
		}
794 779
	}
......
811 796
		JDBCStoreParameters params = getJDBCParameters();
812 797
		if (directSQLMode) {
813 798
			if (filter != null || order != null) {
814
				// FIXME Exception
815 799
				throw new UnsupportedOperationException();
816 800
			}
817 801
			sql.append(params.getSQL());
......
860 844
			appendWhere(sql, filter);
861 845

  
862 846
			// Order
863
			if ((params.getInitialOrder() != null && params.getInitialOrder()
847
			if ((params.getBaseOrder() != null && params.getBaseOrder()
864 848
					.length() != 0)
865 849
					|| (order != null && order.length() != 0)) {
866 850
				sql.append("order by ");
......
870 854
					sql.append(order);
871 855
				} else {
872 856
					// initial order
873
					sql.append(params.getInitialOrder());
857
					sql.append(params.getBaseOrder());
874 858
				}
875 859
				sql.append(' ');
876 860
			}

Also available in: Unified diff