class 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();

      // 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);
    }
  }

}