TextAnnotationDBHandler implements TextAnnotationCache { @Override public void addTextAnnotation(String dataset, TextAnnotation ta) { try { Connection connection = DBHelper.getConnection(dbFile); PreparedStatement stmt = connection.prepareStatement("select id from datasets where name = ?"); stmt.setString(1, dataset); ResultSet rs = stmt.executeQuery(); rs.next(); int datasetId = rs.getInt("id"); stmt.close(); int id = ta.getTokenizedText().hashCode(); stmt = connection.prepareStatement("select * from sentences where id = ?"); stmt.setInt(1, id); rs = stmt.executeQuery(); if (!rs.next()) { stmt = connection.prepareStatement("insert into sentences (id, ta) values (?,?)"); byte[] bytes = serialize(ta); stmt.setInt(1, id); stmt.setBytes(2, bytes); stmt.executeUpdate(); } stmt = connection .prepareStatement("select * from sentencesToDataset where sentenceId = ? and datasetId = ?"); stmt.setInt(1, id); stmt.setInt(2, datasetId); rs = stmt.executeQuery(); if (!rs.next()) { stmt = connection .prepareStatement("insert into sentencesToDataset (sentenceId, datasetId) values(?,?)"); stmt.setInt(1, id); stmt.setInt(2, datasetId); stmt.executeUpdate(); } else { log.debug("Repeated in {} : {}", dataset, ta); } } catch (SQLException e) { throw new RuntimeException(e); } } TextAnnotationDBHandler(String dbFile, String[] datasetNames); void initializeDatasets(String dbFile); @Override void addTextAnnotation(String dataset, TextAnnotation ta); @Override void updateTextAnnotation(TextAnnotation ta); @Override IResetableIterator<TextAnnotation> getDataset(String dataset); @Override boolean contains(TextAnnotation ta); @Override void removeTextAnnotation(TextAnnotation ta); @Override TextAnnotation getTextAnnotation(TextAnnotation ta);  }
@Test public void testAddTextAnnotation() throws Exception { TextAnnotation ta = BasicTextAnnotationBuilder.createTextAnnotationFromTokens(tokenizedSentences); try { dbHandler.addTextAnnotation(datasetName, ta); } catch (RuntimeException e) { fail(e.getMessage()); } IResetableIterator<TextAnnotation> dataset = null; try { dataset = dbHandler.getDataset(datasetName); } catch (RuntimeException e) { fail(e.getMessage()); } assertEquals(true, dataset.hasNext()); TextAnnotation next = dataset.next(); assertEquals(ta.toString(), next.toString()); }