public long insert(Address address) throws SQLException { if (!address.isSetXalAddress() || !address.getXalAddress().isSetAddressDetails()) return 0; XalAddressProperty xalAddressProperty = address.getXalAddress(); AddressDetails addressDetails = xalAddressProperty.getAddressDetails(); // ok, let's start long addressId = dbImporterManager.getDBId(DBSequencerEnum.ADDRESS_ID_SEQ); if (addressId == 0) return 0; boolean success = false; String streetAttr, houseNoAttr, poBoxAttr, zipCodeAttr, cityAttr, countryAttr, xalSource; streetAttr = houseNoAttr = poBoxAttr = zipCodeAttr = cityAttr = countryAttr = xalSource = null; PGgeometry multiPoint = null; // try and interpret <country> child element if (addressDetails.isSetCountry()) { Country country = addressDetails.getCountry(); // country name if (country.isSetCountryName()) { List<String> countryName = new ArrayList<String>(); for (CountryName name : country.getCountryName()) countryName.add(name.getContent()); countryAttr = Util.collection2string(countryName, ","); } // locality if (country.isSetLocality()) { Locality locality = country.getLocality(); // check whether we deal with a city or a town if (locality.isSetType() && (locality.getType().toUpperCase().equals("CITY") || locality.getType().toUpperCase().equals("TOWN"))) { // city name if (locality.isSetLocalityName()) { List<String> localityName = new ArrayList<String>(); for (LocalityName name : locality.getLocalityName()) localityName.add(name.getContent()); cityAttr = Util.collection2string(localityName, ","); } // thoroughfare - just streets are supported if (locality.isSetThoroughfare()) { Thoroughfare thoroughfare = locality.getThoroughfare(); // check whether we deal with a street if (thoroughfare.isSetType() && (thoroughfare.getType().toUpperCase().equals("STREET") || thoroughfare.getType().toUpperCase().equals("ROAD"))) { // street name if (thoroughfare.isSetThoroughfareName()) { List<String> fareName = new ArrayList<String>(); for (ThoroughfareName name : thoroughfare.getThoroughfareName()) fareName.add(name.getContent()); streetAttr = Util.collection2string(fareName, ","); } // house number - we do not support number ranges so far... if (thoroughfare.isSetThoroughfareNumberOrThoroughfareNumberRange()) { List<String> houseNumber = new ArrayList<String>(); for (ThoroughfareNumberOrRange number : thoroughfare.getThoroughfareNumberOrThoroughfareNumberRange()) { if (number.isSetThoroughfareNumber()) houseNumber.add(number.getThoroughfareNumber().getContent()); } houseNoAttr = Util.collection2string(houseNumber, ","); } } } // dependent locality if (streetAttr == null && houseNoAttr == null && locality.isSetDependentLocality()) { DependentLocality dependentLocality = locality.getDependentLocality(); if (dependentLocality.isSetType() && dependentLocality.getType().toUpperCase().equals("DISTRICT")) { if (dependentLocality.isSetThoroughfare()) { Thoroughfare thoroughfare = dependentLocality.getThoroughfare(); // street name if (streetAttr == null && thoroughfare.isSetThoroughfareName()) { List<String> fareName = new ArrayList<String>(); for (ThoroughfareName name : thoroughfare.getThoroughfareName()) fareName.add(name.getContent()); streetAttr = Util.collection2string(fareName, ","); } // house number - we do not support number ranges so far... if (houseNoAttr == null && thoroughfare.isSetThoroughfareNumberOrThoroughfareNumberRange()) { List<String> houseNumber = new ArrayList<String>(); for (ThoroughfareNumberOrRange number : thoroughfare.getThoroughfareNumberOrThoroughfareNumberRange()) { if (number.isSetThoroughfareNumber()) houseNumber.add(number.getThoroughfareNumber().getContent()); } houseNoAttr = Util.collection2string(houseNumber, ","); } } } } // postal code if (locality.isSetPostalCode()) { PostalCode postalCode = locality.getPostalCode(); // get postal code number if (postalCode.isSetPostalCodeNumber()) { List<String> zipCode = new ArrayList<String>(); for (PostalCodeNumber number : postalCode.getPostalCodeNumber()) zipCode.add(number.getContent()); zipCodeAttr = Util.collection2string(zipCode, ","); } } // post box if (locality.isSetPostBox()) { PostBox postBox = locality.getPostBox(); // get post box nummber if (postBox.isSetPostBoxNumber()) poBoxAttr = postBox.getPostBoxNumber().getContent(); } } } // multiPoint geometry if (address.isSetMultiPoint()) multiPoint = stGeometry.getMultiPoint(address.getMultiPoint()); success = true; } else { StringBuilder msg = new StringBuilder(Util.getFeatureSignature(address.getCityGMLClass(), address.getId())); msg.append(": Failed to interpret xAL address element."); LOG.error(msg.toString()); } // get XML representation of <xal:AddressDetails> if (importXalSource) { xalSource = dbImporterManager.marshal(addressDetails, XALModuleType.CORE); if (xalSource != null && xalSource.length() > 0) success = true; } if (success) { psAddress.setLong(1, addressId); psAddress.setString(2, streetAttr); psAddress.setString(3, houseNoAttr); psAddress.setString(4, poBoxAttr); psAddress.setString(5, zipCodeAttr); psAddress.setString(6, cityAttr); psAddress.setString(7, countryAttr); if (multiPoint != null) { psAddress.setObject(8, multiPoint); } else psAddress.setNull(8, Types.OTHER, "ST_GEOMETRY"); if (xalSource != null) psAddress.setString(9, xalSource); else psAddress.setNull(9, Types.CLOB); psAddress.addBatch(); if (++batchCounter == Internal.POSTGRESQL_MAX_BATCH_SIZE) dbImporterManager.executeBatch(DBImporterEnum.ADDRESS); // enable xlinks if (address.isSetId()) dbImporterManager.putGmlId(address.getId(), addressId, address.getCityGMLClass()); } else addressId = 0; return addressId; }
public boolean insert(DBXlinkTextureParam xlink) throws SQLException { String gmlId = xlink.getGmlId(); ResultSet rs = null; // check whether we deal with a local gml:id // remote gml:ids are not supported so far... if (Util.isRemoteXlink(gmlId)) return false; // replace leading # gmlId = gmlId.replaceAll("^#", ""); try { // step 1: get the exterior linear ring element psSelectLinearRing.setString(1, gmlId); rs = psSelectLinearRing.executeQuery(); if (!rs.next()) return false; // if an interior ring is returned we silently discard it int exteriorRing = rs.getInt("RING_NO"); if (exteriorRing != 0) return true; String parentGmlId = rs.getString("PARENT_GMLID"); rs.close(); // step 2: check whether parent geometry exists... we need to do this // since we require the database key for referencing GmlIdEntry surfaceGeometryEntry = resolverManager.getDBId(parentGmlId, CityGMLClass.ABSTRACT_GML_GEOMETRY); if (surfaceGeometryEntry == null || surfaceGeometryEntry.getId() == -1) { StringBuilder msg = new StringBuilder(Util.getGeometrySignature(GMLClass.LINEAR_RING, gmlId)); msg.append(": The element could not be assigned to an existing geometry object."); LOG.error(msg.toString()); return false; } // step 3: find all corresponding interior rings psSelectInteriorLinearRing.setString(1, parentGmlId); rs = psSelectInteriorLinearRing.executeQuery(); HashMap<String, Integer> innerRingMap = new HashMap<String, Integer>(); int maxRingNo = 0; while (rs.next()) { String innerGmlId = rs.getString("GMLID"); if (innerGmlId == null) innerGmlId = parentGmlId; int ringNo = rs.getInt("RING_NO"); innerRingMap.put(innerGmlId, ringNo); if (ringNo > maxRingNo) maxRingNo = ringNo; } rs.close(); // step 4: find corresponding texture coordinates List<String> texCoordList = new ArrayList<String>(); String textureCoordinates = xlink.getTextureCoord(); // reverse order of texture coordinates if necessary if (surfaceGeometryEntry.isReverse()) textureCoordinates = reverseTextureCoordinates(textureCoordinates); texCoordList.add(0, textureCoordinates); for (int i = 0; i < maxRingNo; i++) texCoordList.add(""); psSelectTexCoord.setString(1, xlink.getTexCoordListId()); psSelectTexCoord.setString(2, xlink.getGmlId()); rs = psSelectTexCoord.executeQuery(); while (rs.next()) { String innerGmlId = rs.getString("GMLID"); textureCoordinates = rs.getString("TEXTURE_COORDINATES"); if (Util.isRemoteXlink(innerGmlId)) continue; // reverse order of texture coordinates if necessary if (surfaceGeometryEntry.isReverse()) textureCoordinates = reverseTextureCoordinates(textureCoordinates); // replace leading # innerGmlId = innerGmlId.replaceAll("^#", ""); if (innerRingMap.containsKey(innerGmlId)) texCoordList.set(innerRingMap.get(innerGmlId), textureCoordinates); } // step 5: sanity check String texCoord = Util.collection2string(texCoordList, ";"); if (texCoord.length() > 4000) { LOG.error( "Texture coordinates exceed 4000 characters for target geometry object '" + parentGmlId + "'."); return false; } if (texCoord.contains(";;") || texCoord.endsWith(";")) LOG.warn("Missing texture coordinates for target geometry object '" + parentGmlId + "'."); psTexCoordList.setLong(1, surfaceGeometryEntry.getId()); psTexCoordList.setString(2, texCoord); psTexCoordList.setLong(3, xlink.getId()); psTexCoordList.addBatch(); if (++batchCounter == Internal.POSTGRESQL_MAX_BATCH_SIZE) executeBatch(); if (xlink.getTexParamGmlId() != null) { // propagate xlink... resolverManager.propagateXlink( new DBXlinkTextureAssociation( xlink.getId(), surfaceGeometryEntry.getId(), xlink.getTexParamGmlId())); } return true; } finally { if (rs != null) { try { rs.close(); } catch (SQLException sqlEx) { // } rs = null; } } }