test_class.file: ontopia-engine/src/test/java/net/ontopia/topicmaps/impl/rdbms/RDBMSPatternSingleTopicMapSourceTest.java
test_class.identifier: RDBMSPatternSingleTopicMapSourceTest
test_class.superclass: extends AbstractTopicMapSourceTest
test_case.body: @Test
    public void testRefresh() throws IOException {
      // constants
      final String title = "test-topic-map";
      final String propfile = System.getProperty("net.ontopia.topicmaps.impl.rdbms.PropertyFile");
  
      RDBMSTopicMapStore store = null;
      RDBMSTopicMapStore store2 = null;
      try {
        // create a new topic map matching pattern
        store = new RDBMSTopicMapStore(propfile);
        ((TopicMap) store.getTopicMap()).setTitle(title);
        store.commit(); // make sure it's in the DB for real
        
        // verify that correct TM is being picked up
        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());
  
        // make another topic map matching pattern
        store2 = new RDBMSTopicMapStore(propfile);
        ((TopicMap) store2.getTopicMap()).setTitle(title);
        store2.commit(); // make sure it's in the DB for real
  
        // verify that new topic map is being picked up
        source.refresh();
        ref = (TopicMapReferenceIF) source.getReferences().iterator().next();
        Assert.assertTrue("reference has wrong ID: " + ref,
                   ((RDBMSTopicMapReference) ref).getTopicMapId() == store2.getLongId());
      } finally {
        // clean up
        if (store != null)
          store.delete(true);
        if (store2 != null)
          store2.delete(true);
      }
    }
test_case.class_method_signature: RDBMSPatternSingleTopicMapSourceTest.testRefresh()
test_case.full_signature: @Test public void testRefresh()
test_case.identifier: testRefresh
test_case.invocations: getProperty setTitle getTopicMap commit makeSource setMatch setPattern next iterator getReferences assertTrue getTopicMapId getLongId setTitle getTopicMap commit refresh next iterator getReferences assertTrue getTopicMapId getLongId delete delete
test_case.modifiers: @Test public
test_case.parameters: ()
test_case.return: void
test_case.signature: void testRefresh()
focal_class.declarator: id
focal_class.modifier: protected
focal_class.original_string: protected String id;
focal_class.type: String
focal_class.var_name: id
focal_class.declarator: referenceId
focal_class.modifier: protected
focal_class.original_string: protected String referenceId;
focal_class.type: String
focal_class.var_name: referenceId
focal_class.declarator: title
focal_class.modifier: protected
focal_class.original_string: protected String title;
focal_class.type: String
focal_class.var_name: title
focal_class.declarator: base_address
focal_class.modifier: protected
focal_class.original_string: protected LocatorIF base_address;
focal_class.type: LocatorIF
focal_class.var_name: base_address
focal_class.declarator: propfile
focal_class.modifier: protected
focal_class.original_string: protected String propfile;
focal_class.type: String
focal_class.var_name: propfile
focal_class.declarator: match = "title"
focal_class.modifier: protected
focal_class.original_string: protected String match = "title";
focal_class.type: String
focal_class.var_name: match
focal_class.declarator: pattern
focal_class.modifier: protected
focal_class.original_string: protected String pattern;
focal_class.type: String
focal_class.var_name: pattern
focal_class.declarator: reference
focal_class.modifier: protected
focal_class.original_string: protected RDBMSTopicMapReference reference;
focal_class.type: RDBMSTopicMapReference
focal_class.var_name: reference
focal_class.declarator: storage
focal_class.modifier: protected
focal_class.original_string: protected RDBMSStorage storage;
focal_class.type: RDBMSStorage
focal_class.var_name: storage
focal_class.declarator: log = LoggerFactory.getLogger(RDBMSPatternSingleTopicMapSource.class.getName())
focal_class.modifier: private static final
focal_class.original_string: private static final Logger log = LoggerFactory.getLogger(RDBMSPatternSingleTopicMapSource.class.getName());
focal_class.type: Logger
focal_class.var_name: log
focal_class.file: ontopia-engine/src/main/java/net/ontopia/topicmaps/impl/rdbms/RDBMSPatternSingleTopicMapSource.java
focal_class.identifier: RDBMSPatternSingleTopicMapSource
focal_class.interfaces: implements TopicMapSourceIF
focal_class.class_method_signature: RDBMSPatternSingleTopicMapSource.createTopicMap(String name, String baseAddress)
focal_class.full_signature: @Override public TopicMapReferenceIF createTopicMap(String name, String baseAddress)
focal_class.identifier: createTopicMap
focal_class.modifiers: @Override public
focal_class.parameters: (String name, String baseAddress)
focal_class.return: TopicMapReferenceIF
focal_class.signature: TopicMapReferenceIF createTopicMap(String name, String baseAddress)
focal_class.class_method_signature: RDBMSPatternSingleTopicMapSource.getReferences()
focal_class.full_signature: @Override public synchronized Collection getReferences()
focal_class.identifier: getReferences
focal_class.modifiers: @Override public synchronized
focal_class.parameters: ()
focal_class.return: Collection
focal_class.signature: Collection getReferences()
focal_class.class_method_signature: RDBMSPatternSingleTopicMapSource.refresh()
focal_class.full_signature: @Override public synchronized void refresh()
focal_class.identifier: refresh
focal_class.modifiers: @Override public synchronized
focal_class.parameters: ()
focal_class.return: void
focal_class.signature: void refresh()
focal_class.class_method_signature: RDBMSPatternSingleTopicMapSource.close()
focal_class.full_signature: @Override public void close()
focal_class.identifier: close
focal_class.modifiers: @Override public
focal_class.parameters: ()
focal_class.return: void
focal_class.signature: void close()
focal_class.class_method_signature: RDBMSPatternSingleTopicMapSource.getId()
focal_class.full_signature: @Override public String getId()
focal_class.identifier: getId
focal_class.modifiers: @Override public
focal_class.parameters: ()
focal_class.return: String
focal_class.signature: String getId()
focal_class.class_method_signature: RDBMSPatternSingleTopicMapSource.setId(String id)
focal_class.full_signature: @Override public void setId(String id)
focal_class.identifier: setId
focal_class.modifiers: @Override public
focal_class.parameters: (String id)
focal_class.return: void
focal_class.signature: void setId(String id)
focal_class.class_method_signature: RDBMSPatternSingleTopicMapSource.getTitle()
focal_class.full_signature: @Override public String getTitle()
focal_class.identifier: getTitle
focal_class.modifiers: @Override public
focal_class.parameters: ()
focal_class.return: String
focal_class.signature: String getTitle()
focal_class.class_method_signature: RDBMSPatternSingleTopicMapSource.setTitle(String title)
focal_class.full_signature: @Override public void setTitle(String title)
focal_class.identifier: setTitle
focal_class.modifiers: @Override public
focal_class.parameters: (String title)
focal_class.return: void
focal_class.signature: void setTitle(String title)
focal_class.class_method_signature: RDBMSPatternSingleTopicMapSource.getMatch()
focal_class.full_signature: public String getMatch()
focal_class.identifier: getMatch
focal_class.modifiers: public
focal_class.parameters: ()
focal_class.return: String
focal_class.signature: String getMatch()
focal_class.class_method_signature: RDBMSPatternSingleTopicMapSource.setMatch(String match)
focal_class.full_signature: public void setMatch(String match)
focal_class.identifier: setMatch
focal_class.modifiers: public
focal_class.parameters: (String match)
focal_class.return: void
focal_class.signature: void setMatch(String match)
focal_class.class_method_signature: RDBMSPatternSingleTopicMapSource.getPattern()
focal_class.full_signature: public String getPattern()
focal_class.identifier: getPattern
focal_class.modifiers: public
focal_class.parameters: ()
focal_class.return: String
focal_class.signature: String getPattern()
focal_class.class_method_signature: RDBMSPatternSingleTopicMapSource.setPattern(String pattern)
focal_class.full_signature: public void setPattern(String pattern)
focal_class.identifier: setPattern
focal_class.modifiers: public
focal_class.parameters: (String pattern)
focal_class.return: void
focal_class.signature: void setPattern(String pattern)
focal_class.class_method_signature: RDBMSPatternSingleTopicMapSource.supportsCreate()
focal_class.full_signature: @Override public boolean supportsCreate()
focal_class.identifier: supportsCreate
focal_class.modifiers: @Override public
focal_class.parameters: ()
focal_class.return: boolean
focal_class.signature: boolean supportsCreate()
focal_class.class_method_signature: RDBMSPatternSingleTopicMapSource.supportsDelete()
focal_class.full_signature: @Override public boolean supportsDelete()
focal_class.identifier: supportsDelete
focal_class.modifiers: @Override public
focal_class.parameters: ()
focal_class.return: boolean
focal_class.signature: boolean supportsDelete()
focal_class.class_method_signature: RDBMSPatternSingleTopicMapSource.createStorage()
focal_class.full_signature: protected RDBMSStorage createStorage()
focal_class.identifier: createStorage
focal_class.modifiers: protected
focal_class.parameters: ()
focal_class.return: RDBMSStorage
focal_class.signature: RDBMSStorage createStorage()
focal_class.class_method_signature: RDBMSPatternSingleTopicMapSource.getPropertyFile()
focal_class.full_signature: public String getPropertyFile()
focal_class.identifier: getPropertyFile
focal_class.modifiers: public
focal_class.parameters: ()
focal_class.return: String
focal_class.signature: String getPropertyFile()
focal_class.class_method_signature: RDBMSPatternSingleTopicMapSource.setPropertyFile(String propfile)
focal_class.full_signature: public void setPropertyFile(String propfile)
focal_class.identifier: setPropertyFile
focal_class.modifiers: public
focal_class.parameters: (String propfile)
focal_class.return: void
focal_class.signature: void setPropertyFile(String propfile)
focal_class.class_method_signature: RDBMSPatternSingleTopicMapSource.getReferenceId()
focal_class.full_signature: public String getReferenceId()
focal_class.identifier: getReferenceId
focal_class.modifiers: public
focal_class.parameters: ()
focal_class.return: String
focal_class.signature: String getReferenceId()
focal_class.class_method_signature: RDBMSPatternSingleTopicMapSource.setReferenceId(String referenceId)
focal_class.full_signature: public void setReferenceId(String referenceId)
focal_class.identifier: setReferenceId
focal_class.modifiers: public
focal_class.parameters: (String referenceId)
focal_class.return: void
focal_class.signature: void setReferenceId(String referenceId)
focal_class.class_method_signature: RDBMSPatternSingleTopicMapSource.getBaseAddress()
focal_class.full_signature: public String getBaseAddress()
focal_class.identifier: getBaseAddress
focal_class.modifiers: public
focal_class.parameters: ()
focal_class.return: String
focal_class.signature: String getBaseAddress()
focal_class.class_method_signature: RDBMSPatternSingleTopicMapSource.setBaseAddress(String base_address)
focal_class.full_signature: public void setBaseAddress(String base_address)
focal_class.identifier: setBaseAddress
focal_class.modifiers: public
focal_class.parameters: (String base_address)
focal_class.return: void
focal_class.signature: void setBaseAddress(String base_address)
focal_method.body: @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();
  
        // retrieve reference id from database
        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) { };
        }
  
        // close existing reference if new topic map id
        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);
          }        
        }
        
        // swap reference only if new or topic map id is different
        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);
      }
    }
focal_method.class_method_signature: RDBMSPatternSingleTopicMapSource.refresh()
focal_method.full_signature: @Override public synchronized void refresh()
focal_method.identifier: refresh
focal_method.invocations: getId getId getId createStorage requestConnection getConnectionFactory equals equals getId prepareStatement setString executeQuery next getLong getString getString warn getId close close close getTopicMapId debug getTopicMapId close error getTopicMapId debug setSource debug
focal_method.modifiers: @Override public synchronized
focal_method.parameters: ()
focal_method.return: void
focal_method.signature: void refresh()