public void unsetDataManager(DataManager dataManager) { // Unlink try(Connection connection = dataManager.getDataSource().getConnection(); Statement st = connection.createStatement()) { if(isLocalH2DataBase(connection.getMetaData())) { st.execute("SET DATABASE_EVENT_LISTENER ''"); H2Trigger.setTriggerFactory(null); } } catch (SQLException ex) { // Ignore } }
public void unsetDataManager(DataManager dataManager) { // Unlink try(Connection connection = dataManager.getDataSource().getConnection(); Statement st = connection.createStatement()) { if(isLocalH2DataBase(connection.getMetaData())) { st.execute("SET DATABASE_EVENT_LISTENER ''"); H2Trigger.setTriggerFactory(null); } } catch (SQLException ex) { // Ignore } }
/** * Check if table exists * @return */ public boolean tableExists() { try(Connection connection = element.getDataManager().getDataSource().getConnection(); Statement st = connection.createStatement()) { return st.execute("SELECT COUNT(*) FROM "+element.getTableReference()); } catch (SQLException ex) { return false; } }
protected void doUndo(boolean callListeners) throws SQLException { if(primaryKey != null) { try(Connection connection = dataManager.getDataSource().getConnection(); PreparedStatement st = connection.prepareStatement("DELETE FROM "+tableLocation+" WHERE "+pkName+" = ?")) { st.setLong(1, primaryKey); st.execute(); primaryKey = null; } if(callListeners) { dataManager.fireTableEditHappened(new TableEditEvent(tableLocation.toString(isH2), TableModelEvent.ALL_COLUMNS, null, null, TableModelEvent.DELETE)); } } }
@Override public ResultSetProvider getResultSetProvider(ILayer layer, ProgressMonitor pm) { return new DefaultResultSetProvider(layer.getDataManager().getDataSource(), layer); }
private void doUpdate(Long pk, Object value, boolean callListeners) throws SQLException { try(Connection connection = dataManager.getDataSource().getConnection(); PreparedStatement st = connection.prepareStatement("UPDATE "+tableLocation+" SET "+TableLocation.quoteIdentifier(columnName, isH2)+" = ? WHERE "+pkName+" = ?")) { st.setObject(1, value); st.setLong(2, pk); st.execute(); if(callListeners) { try (Statement stat = connection.createStatement(); ResultSet rs = stat.executeQuery("SELECT * from " + tableLocation.toString(isH2) + " LIMIT 0")) { // Fire with the new PK Value Long pkToFire = pk; if( columnName.equals(pkName)) { pkToFire = Long.valueOf(value.toString()); } dataManager.fireTableEditHappened(new TableEditEvent(tableLocation.toString(isH2), JDBCUtilities.getFieldIndex(rs.getMetaData(), columnName), pkToFire, pkToFire, TableModelEvent.UPDATE)); } } } }
/** * Constructor * @param dataManager * @note Do not forget to call dispose() */ public SourceListModel(DataManager dataManager) { this.dataSource = dataManager.getDataSource(); try(Connection connection = dataSource.getConnection()) { isH2 = JDBCUtilities.isH2DataBase(connection.getMetaData()); } catch (SQLException ex) { LOGGER.error(ex.getLocalizedMessage(), ex); } //Install database listeners dataManager.addDatabaseProgressionListener(this, StateEvent.DB_STATES.STATE_STATEMENT_END); //Call readDatabase when a SourceManager fire an event onDataManagerChange(); }
private ILayer createFenceLayer(Geometry g) { try(Connection connection = mc.getDataManager().getDataSource().getConnection(); Statement st = connection.createStatement()) { boolean isH2 = JDBCUtilities.isH2DataBase(connection.getMetaData()); String fenceLayer = getFenceLayerName(isH2); if(isH2) { st.execute("CREATE TABLE IF NOT EXISTS " + fenceLayer + " (THE_GEOM POLYGON)"); } else { //PostGIS // TODO get SRID of the MapContext to build the fence st.execute("CREATE TABLE IF NOT EXISTS " + fenceLayer + " (THE_GEOM geometry(POLYGON,0))"); } st.execute("DELETE FROM " + fenceLayer); st.execute("INSERT INTO " + fenceLayer + " VALUES (ST_PolyFromText('" + g.toString() + "', 0))"); return mc.createLayer(fenceLayer); } catch (SQLException | LayerException e) { UILOGGER.error(i18n.tr("Cannot create fence layer"), e); } return null; }
@Override public boolean isVectorial() throws LayerException { if(getTableReference().isEmpty()) { return false; } try(Connection connection = dataManager.getDataSource().getConnection()) { try { return !SFSUtilities.getGeometryFields(connection,TableLocation.parse(getTableReference())).isEmpty(); } finally { connection.close(); } } catch (SQLException ex) { throw new LayerException(I18N.tr("Error while fetching source MetaData")); } }
private boolean hasTemporaryTables() { try { try (Connection connection = mapContext.getDataManager().getDataSource().getConnection()) { for (ILayer layer : mapContext.getLayers()) { String table = layer.getTableReference(); if (table != null && !table.isEmpty()) { if (JDBCUtilities.isTemporaryTable(connection, table)) { return true; } } } } } catch (SQLException ex) { LOGGER.error(I18N.tr("Error while checking temporary table")); } return false; }
public static boolean isActiveLayerEditable(MapContext vc) throws SQLException { // A primary key must be defined in the table ILayer activeLayer = vc.getActiveLayer(); if(activeLayer == null) { return false; } else { String table = activeLayer.getTableReference(); if(table!=null && !table.isEmpty()) { try(Connection connection = vc.getDataManager().getDataSource().getConnection()) { int pk = JDBCUtilities.getIntegerPrimaryKey(connection, activeLayer.getTableReference()); return pk>0; } } else { return false; } } }
@Override protected void rectangleDone(Rectangle2D rect, boolean smallerThanTolerance, MapContext vc, ToolManager tm) throws TransitionException { ILayer layer = vc.getSelectedLayers()[0]; double minx = rect.getMinX(); double miny = rect.getMinY(); double maxx = rect.getMaxX(); double maxy = rect.getMaxY(); BackgroundManager bm = Services.getService(BackgroundManager.class); bm.backgroundOperation(new DefaultJobId( "org.orbisgis.jobs.InfoTool"), new PopulateViewJob(new Envelope(minx, maxx, miny, maxy), vc.getDataManager().getDataSource(),layer)); }
/** * Builds the WizardPanel. The objects it will create will be linked to {@code l}. * @param l The parent layer. * @param m Needed for LegendContext implementation. */ public ThematicMapWizard(ILayer l, MapTransform m){ layer = l; try(Connection connection = l.getDataManager().getDataSource().getConnection()) { TableLocation tableLocation = TableLocation.parse(l.getTableReference()); int type = SFSUtilities.getGeometryType(connection, tableLocation, SFSUtilities.getGeometryFields(connection, tableLocation).get(0)); this.geometryType = SimpleGeometryType.getSimpleType(type); mt = m; } catch (SQLException e) { LOGGER.error("Error while reading the data source"); } }
/** * Obtains the DataSource attached to the LegendContext's layer. * * @param lc LegendContext from which to obtain the DataSource. */ public PnlProportional(LegendContext lc) { this.table = lc.getLayer().getTableReference(); this.ds = lc.getLayer().getDataManager().getDataSource(); } }
@Override public ILayer createLayer(String layerName, String tableRef) throws LayerException { try { try (Connection connection = dataManager.getDataSource().getConnection()) { List<String> geoFields = SFSUtilities.getGeometryFields(connection, TableLocation.parse(tableRef)); if (!geoFields.isEmpty()) { return new Layer(layerName, tableRef, dataManager); } else { throw new LayerException(I18N.tr("The source contains no spatial info")); } } } catch (SQLException ex) { throw new LayerException("Cannot retrieve spatial metadata",ex); } }
/** * Sets the DataSource to the LegendContext's layer's DataSource and keeps * a reference to the given legend. * * @param lc LegendContext * @param legend Legend */ public PnlAbstractTableAnalysis(LegendContext lc, MappedLegend<K, U> legend) { this.ds = lc.getLayer().getDataManager().getDataSource(); this.table = lc.getLayer().getTableReference(); this.legend = legend; }
@Override public void open(ProgressMonitor progressMonitor) throws UnsupportedOperationException, EditableElementException { if(rowSet == null) { try(Connection connection = dataManager.getDataSource().getConnection()) { boolean isH2 = JDBCUtilities.isH2DataBase(connection.getMetaData()); String pkName = MetaData.getPkName(connection, tableReference, true); rowSet = dataManager.createReversibleRowSet(); rowSet.initialize(TableLocation.parse(tableReference, isH2).toString(isH2), pkName, progressMonitor); } catch (SQLException | IllegalArgumentException ex) { throw new EditableElementException(ex); } setOpen(true); } }
@Override public void open(ProgressMonitor progressMonitor) throws UnsupportedOperationException, EditableElementException { if(rowSet == null) { try(Connection connection = dataManager.getDataSource().getConnection()) { boolean isH2 = JDBCUtilities.isH2DataBase(connection.getMetaData()); String pkName = MetaData.getPkName(connection, tableReference, true); rowSet = dataManager.createReversibleRowSet(); rowSet.setExcludeGeomFields(excludeGeom); rowSet.initialize(TableLocation.parse(tableReference, isH2).toString(isH2), pkName, progressMonitor); } catch (SQLException | IllegalArgumentException ex) { throw new EditableElementException(ex); } setOpen(true); } }
@Override protected Set<Long> doInBackground() throws Exception { Set<Long> newSelection; // Get all primary value where default geometry intersects a bounding box try (Connection connection = SFSUtilities.wrapConnection(activeLayer.getDataManager().getDataSource().getConnection())) { String geomFieldName = SFSUtilities.getGeometryFields(connection, TableLocation.parse(activeLayer.getTableReference())).get(0); newSelection = ReadTable.getTablePkByEnvelope(mc.getDataManager(), activeLayer.getTableReference(), geomFieldName, selectionRect, !intersects); } catch (SQLException e) { automaton.transition(Code.NO_SELECTION); throw new TransitionException(e); } return newSelection; }
@Override public boolean isEnabled() { TocTreeSelectionIterable<TocTreeNodeStyle> styleIterator = new TocTreeSelectionIterable<>(toc.getTree().getSelectionPaths(),TocTreeNodeStyle.class); int styleSelectionCount = 0; boolean hasNonVectorSource = false; for(TocTreeNodeStyle styleNode : styleIterator) { styleSelectionCount++; ILayer layer = styleNode.getStyle().getLayer(); if(onVectorSourceOnly && !hasNonVectorSource) { try(Connection connection = layer.getDataManager().getDataSource().getConnection()) { TableLocation tableLocation = TableLocation.parse(layer.getTableReference()); List<String> geomFields = SFSUtilities.getGeometryFields(connection, tableLocation); hasNonVectorSource = geomFields.isEmpty(); } catch (SQLException ex) { LOGGER.error(ex.getLocalizedMessage(), ex); } } } return (!onSingleStyleSelection || styleSelectionCount==1) && (!onVectorSourceOnly || !hasNonVectorSource) && styleSelectionCount>=1 && super.isEnabled(); } }