/** * Sets the file format. The given name should be a short name like "GeoTIFF". * The long name will be inferred from the given short name, if possible. * Storage location is: * * <ul> * <li>{@code metadata/identificationInfo/resourceFormat/formatSpecificationCitation/alternateTitle}</li> * </ul> * * @param abbreviation the format short name or abbreviation, or {@code null} for no-operation. * @throws MetadataStoreException if this method can not connect to the {@code jdbc/SpatialMetadata} database. * Callers should generally handle this exception as a recoverable one (i.e. log a warning and continue). * * @see #addCompression(CharSequence) */ public final void setFormat(final String abbreviation) throws MetadataStoreException { if (abbreviation != null && abbreviation.length() != 0) { if (format == null) { format = MetadataSource.getProvided().lookup(Format.class, abbreviation); /* * Additional step for converting deprecated "name" and "specification" into non-deprecated properties. * This step is not required on SIS branches that depend on development branches of GeoAPI 3.1 or 4.0. */ format = DefaultFormat.castOrCopy(format); } } }
String identifier = proxy(metadata); if (identifier == null) { final Map<String,Object> asMap; try { table = getTableName(standard.getInterface(metadata.getClass())); asMap = asValueMap(metadata); } catch (ClassCastException e) { throw new MetadataStoreException(Errors.format( try (Statement stmt = connection().createStatement()) { identifier = search(table, null, asMap, stmt, helper()); } catch (SQLException e) { throw new MetadataStoreException(e.getLocalizedMessage(), Exceptions.unwrap(e));
/** * Invoked in a background thread for closing all expired {@link CachedStatement} instances. */ @Override public void run() { closeExpired(); } }
final Class<?> type = subType(info.getMetadataType(), toSearch.identifier); final Class<?> returnType = Interim.getReturnType(method); final boolean wantCollection = Collection.class.isAssignableFrom(returnType); final Class<?> elementType = wantCollection ? Classes.boundOfParameterizedProperty(method) : returnType; final boolean isMetadata = standard.isMetadata(elementType); final String tableName = getTableName(type); final String columnName = info.asNameMap(standard).get(method.getName()); final boolean isArray; Object value; synchronized (this) { if (!getExistingColumns(tableName).contains(columnName)) { value = null; isArray = false; CachedStatement result = take(type, JDK8.toUnsignedInt(toSearch.preferredIndex)); if (result == null) { final SQLBuilder helper = helper(); final String query = helper.clear().append("SELECT * FROM ") .appendIdentifier(schema, tableName).append(" WHERE ") .append(ID_COLUMN).append("=?").toString(); result = new CachedStatement(type, connection().prepareStatement(query), listeners); toSearch.preferredIndex = (byte) recycle(result, JDK8.toUnsignedInt(toSearch.preferredIndex)); if (element != null) { if (isMetadata) { element = lookup(elementType, element.toString()); } else try { element = info.convert(elementType, element);
Object value = extractFromCollection(entry.getValue()); final String column = entry.getKey(); if (columns == null) { columns = getExistingColumns(table); value = ((Enum<?>) value).name(); } else { String dependency = proxy(value); if (dependency != null) { value = dependency; final Class<?> type = value.getClass(); if (standard.isMetadata(type)) { dependency = search(getTableName(standard.getInterface(type)), null, asValueMap(value), stmt, new SQLBuilder(helper)); if (dependency == null) { return null; // Dependency not found. identifier = candidate; } else if (!identifier.equals(candidate)) { warning(MetadataSource.class, "search", Errors.getResources((Locale) null).getLogRecord( Level.WARNING, Errors.Keys.DuplicatedElement_1, candidate)); break;
ms = instance; if (ms == null) { ms = new MetadataSource(MetadataStandard.ISO_19115, dataSource, "metadata", null); ms.install(); instance = ms;
Object value; if (CodeList.class.isAssignableFrom(type)) { value = getCodeList(type, identifier); } else { final CacheKey key = new CacheKey(type, identifier); final Class<?> subType = subType(type, identifier); final Dispatcher toSearch = new Dispatcher(identifier, this); try { value = subType.newInstance(); final LookupInfo info = getLookupInfo(subType); final Map<String,Object> map = asValueMap(value); final Map<String,String> methods = standard.asNameMap(subType, NAME_POLICY, KeyNamePolicy.METHOD_NAME); for (final Map.Entry<String,Object> entry : map.entrySet()) { method = subType.getMethod(methods.get(entry.getKey())); info.setMetadataType(subType); final Object p = readColumn(info, method, toSearch); if (p != null) { entry.setValue(p);
public void testSequence() throws Exception { try (TestDatabase db = TestDatabase.create("IdentifierGenerator"); MetadataSource source = new MetadataSource(MetadataStandard.ISO_19115, db.source, null, null)) stmt = source.connection().createStatement(); stmt.executeUpdate("CREATE TABLE \"" + TABLE + "\" (ID VARCHAR(6) NOT NULL PRIMARY KEY)"); generator = new IdentifierGenerator(source, null, TABLE, "ID", new SQLBuilder(source.connection().getMetaData(), false)); stmt.close(); generator.close(); source.close();
/** * Returns a helper class for building SQL statements. */ final SQLBuilder helper() throws SQLException { assert Thread.holdsLock(this); if (helper == null) { helper = new SQLBuilder(connection().getMetaData(), quoteSchema); } return helper; }
/** * Tests {@link MetadataSource#lookup(Class, String)} be fetching some {@link Format} instances from * the given source. The first call to the {@code lookup(…)} method will trig the database installation. * * @param source the instance to test. * @throws MetadataStoreException if an error occurred while querying the database. */ @TestStep public static void verifyFormats(final MetadataSource source) throws MetadataStoreException { verify(source.lookup(Format.class, "PNG"), "PNG", "PNG (Portable Network Graphics) Specification"); verify(source.lookup(Format.class, "NetCDF"), "NetCDF", "NetCDF Classic and 64-bit Offset Format"); verify(source.lookup(Format.class, "GeoTIFF"), "GeoTIFF", "GeoTIFF Coverage Encoding Profile"); verify(source.lookup(Format.class, "CSV"), "CSV", "Common Format and MIME Type for Comma-Separated Values (CSV) Files"); verify(source.lookup(Format.class, "CSV-MF"), "CSV", "OGC Moving Features Encoding Extension: Simple Comma-Separated Values (CSV)"); }
/** * Tests {@link MetadataSource#search(Object)} * * @param source the instance to test. * @throws MetadataStoreException if an error occurred while querying the database. */ @TestStep public static void testSearch(final MetadataSource source) throws MetadataStoreException { final DefaultCitation specification = new DefaultCitation("PNG (Portable Network Graphics) Specification"); specification.setAlternateTitles(Collections.singleton(new SimpleInternationalString("PNG"))); final DefaultFormat format = new DefaultFormat(); format.setFormatSpecificationCitation(specification); assertEquals("PNG", source.search(format)); specification.setTitle(null); assertNull(source.search(format)); }
final Class<?> elementType = wantCollection ? Classes.boundOfParameterizedProperty(method) : returnType; final boolean isMetadata = standard.isMetadata(elementType); final String tableName = getTableName(type); final String columnName = info.asNameMap(standard).get(method.getName()); final boolean isArray; Object value; synchronized (this) { if (!getExistingColumns(tableName).contains(columnName)) { value = null; isArray = false; CachedStatement result = take(type, Byte.toUnsignedInt(toSearch.preferredIndex)); if (result == null) { final SQLBuilder helper = helper(); final String query = helper.clear().append("SELECT * FROM ") .appendIdentifier(schema, tableName).append(" WHERE ") .appendIdentifier(ID_COLUMN).append("=?").toString(); result = new CachedStatement(type, connection().prepareStatement(query), listeners); toSearch.preferredIndex = (byte) recycle(result, Byte.toUnsignedInt(toSearch.preferredIndex)); if (element != null) { if (isMetadata) { element = lookup(elementType, element.toString()); } else try { element = info.convert(elementType, element); value = specialize(UnmodifiableArrayList.wrap(values), returnType, elementType);
Object value = extractFromCollection(entry.getValue()); final String column = entry.getKey(); if (columns == null) { columns = getExistingColumns(table); value = ((Enum<?>) value).name(); } else { String dependency = proxy(value); if (dependency != null) { value = dependency; final Class<?> type = value.getClass(); if (standard.isMetadata(type)) { dependency = search(getTableName(standard.getInterface(type)), null, asValueMap(value), stmt, new SQLBuilder(helper)); if (dependency == null) { return null; // Dependency not found. identifier = candidate; } else if (!identifier.equals(candidate)) { warning(MetadataSource.class, "search", Errors.getResources((Locale) null).getLogRecord( Level.WARNING, Errors.Keys.DuplicatedElement_1, candidate)); break;
final DataSource dataSource = Initializer.getDataSource(); if (dataSource != null) { ms = new MetadataSource(MetadataStandard.ISO_19115, dataSource, "metadata", null); ms.install(); } else { warning = (LogRecord) Initializer.unspecified(null, true);
Object value; if (CodeList.class.isAssignableFrom(type)) { value = getCodeList(type, identifier); } else { final CacheKey key = new CacheKey(type, identifier); try { value = subType.getConstructor().newInstance(); final LookupInfo info = getLookupInfo(subType); final Map<String,Object> map = asValueMap(value); final Map<String,String> methods = standard.asNameMap(subType, NAME_POLICY, KeyNamePolicy.METHOD_NAME); for (final Map.Entry<String,Object> entry : map.entrySet()) { method = subType.getMethod(methods.get(entry.getKey())); info.setMetadataType(subType); final Object p = readColumn(info, method, toSearch); if (p != null) { entry.setValue(p);
/** * Returns a helper class for building SQL statements. */ final SQLBuilder helper() throws SQLException { assert Thread.holdsLock(this); if (helper == null) { helper = new SQLBuilder(connection().getMetaData(), quoteSchema); } return helper; }
/** * Compares {@link MetadataFallback} content with database content using the given source. * This method is invoked by {@link MetadataSourceTest} for opportunistically reusing the * available database. */ static void compare(final MetadataSource source) throws MetadataStoreException { for (final Citation c : Citations.values()) { final String name = ((CitationConstant) c).title; final boolean exclude = EXCLUDES.contains(name); final Citation fromFB = MetadataFallback.createCitation(name); assertEquals(name, exclude, fromFB == null); // Verify that missing fallbacks are known ones. if (!exclude) { compare(name, source.lookup(Citation.class, name), fromFB); } } compare("IOGP", source.lookup(Citation.class, "IOGP"), MetadataFallback.createCitation("IOGP")); }
String identifier = proxy(metadata); if (identifier == null) { final Map<String,Object> asMap; try { table = getTableName(standard.getInterface(metadata.getClass())); asMap = asValueMap(metadata); } catch (ClassCastException e) { throw new MetadataStoreException(Errors.format( try (Statement stmt = connection().createStatement()) { identifier = search(table, null, asMap, stmt, helper()); } catch (SQLException e) { throw new MetadataStoreException(e.getLocalizedMessage(), Exceptions.unwrap(e));
if (abbreviation != null && abbreviation.length() != 0) { if (format == null) { format = MetadataSource.getProvided().lookup(Format.class, abbreviation);
/** * Creates a temporary database for comparing {@link MetadataFallback} content with database content. * This method is provided for allowing to execute this class individually. In a complete Maven build, * of {@code sis-metadata} module, the test will rather be executed by {@link MetadataSourceTest} for * opportunistic reasons. * * @throws Exception if an exception occurred while creating or comparing the database. */ @Test public void compare() throws Exception { try (TestDatabase db = TestDatabase.create("MetadataSource"); MetadataSource source = new MetadataSource(MetadataStandard.ISO_19115, db.source, "metadata", null)) { source.install(); compare(source); } }