RDBMSPatternSingleTopicMapSource implements TopicMapSourceIF { @Override public synchronized void refresh() { if (match == null) throw new OntopiaRuntimeException("match property must be specified on source with id '" + getId() + "'."); if (pattern == null) throw new OntopiaRuntimeException("pattern property must be specified on source with id '" + getId() + "'."); if (referenceId == null) throw new OntopiaRuntimeException("referenceId property must be specified on source with id '" + getId() + "'."); boolean foundReference = false; long topicmap_id = -2; String _title = title; LocatorIF _base_address = base_address; try { createStorage(); Connection conn = storage.getConnectionFactory(true).requestConnection(); try { String sqlquery; if ("title".equals(match)) sqlquery = "select max(M.id), M.title, M.base_address from TM_TOPIC_MAP M where M.title = ? group by M.title, M.base_address order by max(M.id) desc"; else if ("comments".equals(match)) sqlquery = "select max(M.id), M.title, M.base_address from TM_TOPIC_MAP M where M.comments = ? group by M.title, M.base_address order by max(M.id) desc"; else throw new OntopiaRuntimeException("match property contains illegal value '" + match + "' on source with id '" + getId() + "'."); PreparedStatement stm = conn.prepareStatement(sqlquery); try { stm.setString(1, pattern); ResultSet rs = stm.executeQuery(); if (rs.next()) { foundReference = true; topicmap_id = rs.getLong(1); if (_title == null) _title = rs.getString(2); if (_base_address == null) { String loc = rs.getString(3); if (loc != null) _base_address = new URILocator(loc); } } else { log.warn("Source with id '" + getId() + "' could not find any matching topic maps with pattern '" + pattern + "'."); } rs.close(); } finally { stm.close(); } } catch (Exception e) { throw new OntopiaRuntimeException(e); } finally { try { conn.close(); } catch (Exception e) { }; } if (reference != null && topicmap_id != reference.getTopicMapId()) { try { log.debug("Closing reference '" + referenceId + "' to topic map " + reference.getTopicMapId() + "."); reference.close(); } catch (Exception e) { log.error("Error occurred when closing reference.", e); } } if (foundReference) { if (reference == null || topicmap_id != reference.getTopicMapId()) { log.debug("Created new reference '" + referenceId + "' to topic map " + topicmap_id); RDBMSTopicMapReference ref = new RDBMSTopicMapReference(referenceId, _title, storage, topicmap_id, _base_address); ref.setSource(this); this.reference = ref; } else { log.debug("Reference with id '" + referenceId + "' already refers to topic map " + topicmap_id); } } else { this.reference = null; } } catch (Exception e) { throw new OntopiaRuntimeException(e); } }  @Override TopicMapReferenceIF createTopicMap(String name, String baseAddress); @Override synchronized Collection getReferences(); @Override synchronized void refresh(); @Override void close(); @Override String getId(); @Override void setId(String id); @Override String getTitle(); @Override void setTitle(String title); String getMatch(); void setMatch(String match); String getPattern(); void setPattern(String pattern); @Override boolean supportsCreate(); @Override boolean supportsDelete(); String getPropertyFile(); void setPropertyFile(String propfile); String getReferenceId(); void setReferenceId(String referenceId); String getBaseAddress(); void setBaseAddress(String base_address);  }
@Test public void testRefresh() throws IOException { final String title = "test-topic-map"; final String propfile = System.getProperty("net.ontopia.topicmaps.impl.rdbms.PropertyFile"); RDBMSTopicMapStore store = null; RDBMSTopicMapStore store2 = null; try { store = new RDBMSTopicMapStore(propfile); ((TopicMap) store.getTopicMap()).setTitle(title); store.commit(); RDBMSPatternSingleTopicMapSource source = makeSource(); source.setMatch("title"); source.setPattern(title); TopicMapReferenceIF ref = (TopicMapReferenceIF) source.getReferences().iterator().next(); Assert.assertTrue("reference has wrong ID: " + ref, ((RDBMSTopicMapReference) ref).getTopicMapId() == store.getLongId()); store2 = new RDBMSTopicMapStore(propfile); ((TopicMap) store2.getTopicMap()).setTitle(title); store2.commit(); source.refresh(); ref = (TopicMapReferenceIF) source.getReferences().iterator().next(); Assert.assertTrue("reference has wrong ID: " + ref, ((RDBMSTopicMapReference) ref).getTopicMapId() == store2.getLongId()); } finally { if (store != null) store.delete(true); if (store2 != null) store2.delete(true); } }