Changeset 3334
- Timestamp:
- 08/08/13 16:36:14 (11 years ago)
- Location:
- DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src
- Files:
-
- 1 added
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/AnnotationDao.java
r3330 r3334 22 22 import eu.dasish.annotation.schema.Annotation; 23 23 import eu.dasish.annotation.schema.AnnotationInfo; 24 import eu.dasish.annotation.schema.NewOrExistingSourceInfo; 24 25 import eu.dasish.annotation.schema.ResourceREF; 25 import eu.dasish.annotation.schema.SourceInfo;26 26 import java.sql.SQLException; 27 27 import java.util.List; … … 82 82 83 83 84 /** 85 * 86 * @param annotation 87 * @param sources 88 * @param clear 89 * @return annotation which target sources replaced (if clear == true) or extended (if clear -- false) by "sources" 90 * side-effects: updates the annotation_target_source table in the DB 91 * adds a source if it is not in the DB 92 * in the body (both, annotation-class, and in the table "annotation" replaced temporary sourceIDs of new sources 93 * with persistent once once they are added to the DB 94 */ 95 public Annotation updateSourceInfo(Annotation annotation, List<NewOrExistingSourceInfo> sources, boolean clear); 96 84 97 85 98 86 List<AnnotationInfo> getAnnotationInfos(List<Number> annotationIDs);99 public List<AnnotationInfo> getAnnotationInfos(List<Number> annotationIDs); 87 100 88 101 89 102 90 List<ResourceREF> getAnnotationREFs(List<Number> annotationIDs);103 public List<ResourceREF> getAnnotationREFs(List<Number> annotationIDs); 91 104 92 105 } -
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/SourceDao.java
r3309 r3334 19 19 20 20 import eu.dasish.annotation.backend.identifiers.SourceIdentifier; 21 import eu.dasish.annotation.schema.NewOrExistingSourceInfo; 21 22 import eu.dasish.annotation.schema.NewOrExistingSourceInfos; 22 23 import eu.dasish.annotation.schema.Source; … … 84 85 public NewOrExistingSourceInfos contructNewOrExistingSourceInfo(List<SourceInfo> sourceInfoList); 85 86 87 88 86 89 /** 87 90 * -
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/impl/JdbcAnnotationDao.java
r3330 r3334 18 18 package eu.dasish.annotation.backend.dao.impl; 19 19 20 import eu.dasish.annotation.backend.Helpers; 20 21 import eu.dasish.annotation.backend.dao.AnnotationDao; 21 22 import eu.dasish.annotation.backend.dao.NotebookDao; … … 37 38 import java.sql.SQLException; 38 39 import java.util.ArrayList; 40 import java.util.Date; 39 41 import java.util.HashMap; 40 42 import java.util.List; 41 43 import java.util.Map; 42 44 import javax.sql.DataSource; 45 import javax.xml.datatype.DatatypeConfigurationException; 46 import javax.xml.datatype.XMLGregorianCalendar; 43 47 import org.springframework.beans.factory.annotation.Autowired; 44 48 import org.springframework.dao.DataAccessException; … … 203 207 //TODO implement deleting sources (see the specification document and the interfaces' javadoc 204 208 } 205 206 ////////////////////////////////////////////////////////////////////////209 210 //////////////////////////////////////////////////////////////////////// 207 211 @Override 208 212 public int addAnnotationSourcePair(AnnotationIdentifier annotationIdentifier, SourceIdentifier sourceIdentifier) { … … 214 218 return (getSimpleJdbcTemplate().update(sqlAnnotationsSources, paramsAnnotationsSources)); 215 219 } 216 217 220 218 221 // TODO: so far URI in the xml is the same as the external_id in the DB!! … … 240 243 params.put("headline", annotation.getHeadline()); 241 244 params.put("bodyXml", annotation.getBody().getAny().get(0).toString()); 242 String sql = "INSERT INTO " + annotationTableName + "(" + external_id + "," + owner_id + "," + headline + "," + body_xml + " ) VALUES (:externalId, :ownerId, :headline, :bodyXml)"; 243 // Add the first version of the annotaion to the DB, with (possible) temporary source refs in the body 244 // This version is necessary for the integrity since we are to add "annotation-source" pairs in the table "annotations_target_sources", 245 // while checking which sources are new and which are old onces, already existing in the DB 246 // Later in this code, after the persistent id-s for the sources will be generated when necessary, 247 // the just added annotation will be updated with the updated body (can be optimized, though) 245 246 String sql = "INSERT INTO " + annotationTableName + "(" + external_id + "," + owner_id + "," + headline + "," + body_xml + " ) VALUES (:externalId, :ownerId, :headline, :bodyXml)"; 248 247 final int affectedRows = getSimpleJdbcTemplate().update(sql, params); 249 248 250 // adding sources 251 if (affectedRows == 1) { 252 // add sources when necessary and update annotations_target_sources" table 253 List<NewOrExistingSourceInfo> sources = annotation.getTargetSources().getTarget(); 254 String bodyXML = result.getBody().getAny().get(0).toString(); 255 for (NewOrExistingSourceInfo noeSourceInfo : sources) { 256 SourceInfo sourceInfo = noeSourceInfo.getSource(); 257 if (sourceInfo != null) { 258 // this is an old source, already exists in the DB 259 int affectedRowsAnnotationsSources = addAnnotationSourcePair(annotationIdentifier, new SourceIdentifier(sourceInfo.getRef())); 260 if (affectedRowsAnnotationsSources != 1) { 261 //something went wrong 262 return null; 263 } 264 } else { 265 Source newSource = constructNewSource(noeSourceInfo.getNewSource()); 266 Source addedSource = jdbcSourceDao.addSource(newSource); 267 int affectedRowsAnnotationsSources = addAnnotationSourcePair(annotationIdentifier, new SourceIdentifier(addedSource.getURI())); 268 if (affectedRowsAnnotationsSources != 1) { 269 //something went wrong 270 return null; 271 } 272 // replace temporary source id with the persistent id generated by add-source-dao 273 bodyXML=bodyXML.replaceAll(newSource.getURI(), addedSource.getURI()); 274 } 275 } 276 // TODO: optimize updating 277 // update body because it could be changed by replacing temporary source id-s by persistent 278 Number annotationInternalId = getInternalID(annotationIdentifier); 279 String sqlUpdate = "UPDATE "+annotationTableName+" SET "+body_xml+ "= ? WHERE "+annotation_id +"= "+annotationInternalId; 280 int affectedRowsBodyUpd = getSimpleJdbcTemplate().update(sqlUpdate, bodyXML); 249 result.setTimeStamp(retrieveTimeStamp(getInternalID(annotationIdentifier))); 250 251 List<NewOrExistingSourceInfo> sources = result.getTargetSources().getTarget(); 252 result = updateSourceInfo(result, sources, true); 253 254 return result; 255 } catch (DataAccessException exception) { 256 throw exception; 257 } 258 } 259 260 //////////////////////////////////////////////////////////////////////// 261 @Override 262 public Annotation updateSourceInfo(Annotation annotation, List<NewOrExistingSourceInfo> sources, boolean clear) { 263 264 AnnotationIdentifier annotationIdentifier = new AnnotationIdentifier(annotation.getURI()); 265 List<NewOrExistingSourceInfo> sourcesUpdated = new ArrayList<NewOrExistingSourceInfo>(); 266 String bodyXML = annotation.getBody().getAny().get(0).toString(); 267 268 for (NewOrExistingSourceInfo noeSourceInfo : sources) { 269 SourceInfo sourceInfo = noeSourceInfo.getSource(); 270 if (sourceInfo != null) { 271 // this is an old source, already exists in the DB 272 int affectedRowsAnnotationsSources = addAnnotationSourcePair(annotationIdentifier, new SourceIdentifier(sourceInfo.getRef())); 273 sourcesUpdated.add(noeSourceInfo); 274 } else { 275 Source newSource = constructNewSource(noeSourceInfo.getNewSource()); 276 Source addedSource = jdbcSourceDao.addSource(newSource); 277 int affectedRowsAnnotationsSources = addAnnotationSourcePair(annotationIdentifier, new SourceIdentifier(addedSource.getURI())); 278 279 // create updated source info 280 SourceInfo updatedSourceInfo = new SourceInfo(); 281 updatedSourceInfo.setLink(addedSource.getLink()); 282 updatedSourceInfo.setRef(addedSource.getURI()); 283 updatedSourceInfo.setVersion(addedSource.getVersion()); 284 285 NewOrExistingSourceInfo updatedInfo = new NewOrExistingSourceInfo(); 286 updatedInfo.setSource(updatedSourceInfo); 287 sourcesUpdated.add(updatedInfo); 288 289 bodyXML = bodyXML.replaceAll(noeSourceInfo.getNewSource().getId(), addedSource.getURI()); 290 291 } 292 } 293 294 List<NewOrExistingSourceInfo> targets = annotation.getTargetSources().getTarget(); 295 if (clear) { 296 targets.clear(); 297 } 298 299 targets.addAll(sourcesUpdated); 300 301 /// time to update body 302 Number annotationInternalId = getInternalID(annotationIdentifier); 303 String sqlUpdate = "UPDATE " + annotationTableName + " SET " + body_xml + "= ? WHERE " + annotation_id + "= " + annotationInternalId; 304 int affectedRowsBodyUpd = getSimpleJdbcTemplate().update(sqlUpdate, bodyXML); 305 306 List<Object> body = annotation.getBody().getAny(); 307 body.clear(); 308 body.add(bodyXML); 309 //bodu=y is updated 310 311 return annotation; 312 } 313 314 ////////////////////////////////////////////////// 315 @Override 316 public AnnotationIdentifier getExternalID(Number internalID) { 317 return new AnnotationIdentifier(super.getExternalIdentifier(internalID)); 318 } 319 320 //////////// helpers /////////////////////// 321 ///////////////////////////////////////////////// 322 private XMLGregorianCalendar retrieveTimeStamp(Number internalID) { 323 String sqlTime = "SELECT " + time_stamp + " FROM " + annotationTableName + " WHERE " + annotation_id + "= ?"; 324 List<XMLGregorianCalendar> timeStamp = getSimpleJdbcTemplate().query(sqlTime, timeStampRowMapper, internalID); 325 if (timeStamp.isEmpty()) { 326 return null; 327 } 328 return timeStamp.get(0); 329 } 330 private final RowMapper<XMLGregorianCalendar> timeStampRowMapper = new RowMapper<XMLGregorianCalendar>() { 331 @Override 332 public XMLGregorianCalendar mapRow(ResultSet rs, int rowNumber) throws SQLException { 333 try { 334 XMLGregorianCalendar result = Helpers.setXMLGregorianCalendar(rs.getDate(time_stamp)); 281 335 return result; 282 } else{283 // something went wrong336 } catch (DatatypeConfigurationException e) { 337 System.out.println(e); 284 338 return null; 285 339 } 286 } catch (DataAccessException exception) { 287 throw exception; 288 } 289 } 290 291 ////////////////////////////////////////////////// 292 @Override 293 public AnnotationIdentifier getExternalID(Number internalID) { 294 return new AnnotationIdentifier(super.getExternalIdentifier(internalID)); 295 } 296 297 //////////// helpers /////////////////////// 298 ///////////////////////////////////////////////// 340 } 341 }; 342 343 /////////////////////////////////////////////////////////// 299 344 private ResourceREF getResourceREF(String resourceID) { 300 345 ResourceREF result = new ResourceREF(); … … 323 368 324 369 /////////////////// 325 326 private Source constructNewSource(NewSourceInfo newSourceInfo){ 370 private Source constructNewSource(NewSourceInfo newSourceInfo) { 327 371 Source result = new Source(); 328 372 result.setLink(newSourceInfo.getLink()); … … 333 377 return result; 334 378 } 335 336 337 379 } -
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/impl/JdbcSourceDao.java
r3330 r3334 18 18 package eu.dasish.annotation.backend.dao.impl; 19 19 20 import eu.dasish.annotation.backend.Helpers; 20 21 import eu.dasish.annotation.backend.dao.SourceDao; 21 22 import eu.dasish.annotation.backend.dao.VersionDao; … … 29 30 import java.sql.SQLException; 30 31 import java.util.ArrayList; 31 import java.util.Date;32 import java.util.GregorianCalendar;33 32 import java.util.HashMap; 34 33 import java.util.List; … … 36 35 import javax.sql.DataSource; 37 36 import javax.xml.datatype.DatatypeConfigurationException; 38 import javax.xml.datatype.DatatypeFactory;39 37 import javax.xml.datatype.XMLGregorianCalendar; 40 38 import org.springframework.beans.factory.annotation.Autowired; … … 94 92 public Source mapRow(ResultSet rs, int rowNumber) throws SQLException { 95 93 try { 96 97 Date date = rs.getDate(time_stamp); 98 GregorianCalendar c = new GregorianCalendar(); 99 c.setTime(date); 100 XMLGregorianCalendar xmlDate = DatatypeFactory.newInstance().newXMLGregorianCalendar(c); 101 94 XMLGregorianCalendar xmlDate = Helpers.setXMLGregorianCalendar(rs.getDate(time_stamp)); 102 95 Source result = constructSource(new SourceIdentifier(rs.getString(external_id)), rs.getString(link_uri), 103 96 versionDao.getExternalID(rs.getInt(version_id)), xmlDate); … … 217 210 return result; 218 211 } 212 213 219 214 220 215 ///////////////////////////////////////////////// -
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/test/java/eu/dasish/annotation/backend/TestBackendConstants.java
r3300 r3334 92 92 public static final String _TEST_VERSION_3_EXT_ID = "00000000-0000-0000-0000-000000000043"; 93 93 94 94 95 public static final String _TEST_TEMP_SOURCE_ID = "Barcelona-1"; 96 public static final String _TEST_ANNOT_TO_ADD_NEW_SOURCE_BODY = "refers to "+_TEST_TEMP_SOURCE_ID; 97 public static final String _TEST_ANNOT_TO_ADD_NEW_SOURCE_HEADLINE = "SF in Catalan"; 98 public static final String _TEST_NEW_SOURCE_LINK = "http://www.sagradafamilia.cat/docs_instit/historia.php "; 95 99 } 96 100 -
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/test/java/eu/dasish/annotation/backend/TestInstances.java
r3236 r3334 22 22 import eu.dasish.annotation.schema.NewOrExistingSourceInfo; 23 23 import eu.dasish.annotation.schema.NewOrExistingSourceInfos; 24 import eu.dasish.annotation.schema.NewSourceInfo; 24 25 import eu.dasish.annotation.schema.ResourceREF; 25 26 import eu.dasish.annotation.schema.SourceInfo; … … 34 35 final private Annotation _annotationOne; 35 36 final private Annotation _annotationToAdd; 37 final private Annotation _annotationToAddNewSource; 36 38 37 39 public TestInstances(){ 38 40 _annotationOne = makeAnnotationOne(); 39 41 _annotationToAdd = makeAnnotationToAdd(); 40 42 _annotationToAddNewSource = makeAnnotationToAddNewSource(); 41 43 } 42 44 … … 64 66 } 65 67 68 private Annotation makeAnnotationToAddNewSource(){ 69 Annotation result = makeAnnotation(TestBackendConstants._TEST_ANNOT_TO_ADD_NEW_SOURCE_BODY, TestBackendConstants._TEST_ANNOT_TO_ADD_NEW_SOURCE_HEADLINE, 5); 70 71 NewSourceInfo newSourceInfo = new NewSourceInfo(); 72 newSourceInfo.setLink(TestBackendConstants._TEST_NEW_SOURCE_LINK); 73 newSourceInfo.setId(TestBackendConstants._TEST_TEMP_SOURCE_ID); 74 // TODO: so far, the version is the external version id generated when a version is added 75 // because for now the version is used to keep external id of the version, not is human-friendly headline 76 // fix it by adding external Id to the version 77 newSourceInfo.setVersion(null); 78 79 NewOrExistingSourceInfo noeSourceInfo = new NewOrExistingSourceInfo(); 80 noeSourceInfo.setNewSource(newSourceInfo); 81 NewOrExistingSourceInfos noeSourceInfos = new NewOrExistingSourceInfos(); 82 noeSourceInfos.getTarget().add(noeSourceInfo); 83 result.setTargetSources(noeSourceInfos); 84 85 return result; 86 } 87 66 88 67 89 // so far tests only adding annot with existing sources!!! … … 76 98 ResourceREF owner = new ResourceREF(); 77 99 owner.setRef(String.valueOf(ownerId)); 78 result.setOwner(owner); 79 100 result.setOwner(owner); 101 102 result.setTimeStamp(null); 103 result.setURI(null); 104 result.setTargetSources(null); 105 result.setURI(null); 106 80 107 return result; 81 108 } … … 92 119 } 93 120 94 121 public Annotation getAnnotationToAddNewSource(){ 122 return _annotationToAddNewSource; 123 } 124 95 125 } -
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/test/java/eu/dasish/annotation/backend/dao/impl/JdbcAnnotationDaoTest.java
r3330 r3334 30 30 import eu.dasish.annotation.schema.NewOrExistingSourceInfos; 31 31 import eu.dasish.annotation.schema.ResourceREF; 32 import eu.dasish.annotation.schema.Source; 32 33 import eu.dasish.annotation.schema.SourceInfo; 33 34 import java.sql.SQLException; … … 37 38 import org.jmock.Mockery; 38 39 import static org.junit.Assert.*; 40 import org.junit.Ignore; 39 41 import org.junit.Test; 40 42 import org.junit.runner.RunWith; … … 249 251 */ 250 252 @Test 251 public void testAddAnnotation() throws SQLException{ 252 System.out.println("test_addAnnotation"); 253 final Annotation annotationToAdd = testInstances.getAnnotationToAdd();// existing sources 254 255 final Number testAnnotationID = 6; 256 257 SourceInfo sourceInfo = new SourceInfo(); 258 sourceInfo.setLink(TestBackendConstants._TEST_SOURCE_1_LINK); 259 sourceInfo.setRef(TestBackendConstants._TEST_SOURCE_1_EXT_ID); 260 sourceInfo.setVersion(Integer.toString(TestBackendConstants._TEST_SOURCE_1_VERSION_ID)); 261 final List<SourceInfo> sourceInfoList = new ArrayList<SourceInfo>(); 262 sourceInfoList.add(sourceInfo); 253 public void testAddAnnotationExistingSource() throws SQLException{ 254 System.out.println("test_addAnnotation with an existing source"); 255 256 Annotation annotationToAdd = testInstances.getAnnotationToAdd();// existing sources 257 assertEquals(null, annotationToAdd.getURI()); 258 assertEquals(null, annotationToAdd.getTimeStamp()); 259 260 mockery.checking(new Expectations() { 261 { 262 oneOf(sourceDao).getInternalID(new SourceIdentifier(TestBackendConstants._TEST_SOURCE_1_EXT_ID)); 263 will(returnValue(1)); 264 } 265 }); 266 267 Annotation result = jdbcAnnotationDao.addAnnotation(annotationToAdd, 5); 268 assertFalse(null==result.getURI()); 269 assertFalse(null==result.getTimeStamp()); 270 assertEquals(annotationToAdd.getBody().getAny().get(0), result.getBody().getAny().get(0)); 271 assertEquals(annotationToAdd.getHeadline(), result.getHeadline()); 272 assertEquals(String.valueOf(5), result.getOwner().getRef()); 273 assertEquals(annotationToAdd.getPermissions(), result.getPermissions()); 274 assertEquals(annotationToAdd.getTargetSources(), result.getTargetSources()); 275 } 276 277 278 /** 279 * Test of addAnnotation method, of class JdbcAnnotationDao. 280 */ 281 @Test 282 @Ignore 283 public void testAddAnnotationNewSource() throws SQLException{ 284 System.out.println("test_addAnnotation with a new source"); 285 286 final Annotation annotationToAddNewSource = testInstances.getAnnotationToAddNewSource();// existing sources 287 288 289 final Source addedSource = new Source(); 290 final SourceIdentifier sourceIdentifier = new SourceIdentifier(); 291 addedSource.setLink(TestBackendConstants._TEST_NEW_SOURCE_LINK); 292 addedSource.setURI(sourceIdentifier.toString()); 263 293 264 294 mockery.checking(new Expectations() { 265 295 { 266 oneOf(sourceDao).getSourceInfos(testAnnotationID); 267 will(returnValue(sourceInfoList)); 268 269 oneOf(sourceDao).contructNewOrExistingSourceInfo(sourceInfoList); 270 will(returnValue(annotationToAdd.getTargetSources())); 271 272 oneOf(sourceDao).getInternalID(new SourceIdentifier(TestBackendConstants._TEST_SOURCE_1_EXT_ID)); 273 will(returnValue(1)); 296 oneOf(sourceDao).addSource(with(aNonNull(Source.class))); 297 will(returnValue(addedSource)); 298 299 oneOf(sourceDao).getInternalID(sourceIdentifier); 300 will(returnValue(5)); 274 301 275 302 } 276 303 }); 277 304 278 Annotation result = jdbcAnnotationDao.addAnnotation(annotationToAdd, 5); 279 280 AnnotationIdentifier generatedAnnotationExternalID = new AnnotationIdentifier(result.getURI()); 281 Annotation addedAnnotation = jdbcAnnotationDao.getAnnotation(jdbcAnnotationDao.getInternalID(generatedAnnotationExternalID)); 282 assertEquals(annotationToAdd.getBody().getAny().get(0), addedAnnotation.getBody().getAny().get(0)); 283 assertEquals(annotationToAdd.getHeadline(), addedAnnotation.getHeadline()); 284 assertEquals(String.valueOf(5), addedAnnotation.getOwner().getRef()); 285 assertEquals(annotationToAdd.getPermissions(), addedAnnotation.getPermissions()); 286 assertEquals(annotationToAdd.getTargetSources(), addedAnnotation.getTargetSources()); 287 assertEquals(annotationToAdd.getTimeStamp(), addedAnnotation.getTimeStamp()); 288 289 290 291 } 305 Annotation result = jdbcAnnotationDao.addAnnotation(annotationToAddNewSource, 5); 306 assertFalse(null==result.getURI()); 307 assertFalse(null==result.getTimeStamp()); 308 //assertEquals(annotationToAddNewSource.getBody().getAny().get(0).toString(), result.getBody().getAny().get(0)); 309 assertEquals(annotationToAddNewSource.getHeadline(), result.getHeadline()); 310 assertEquals(String.valueOf(5), result.getOwner().getRef()); 311 assertEquals(annotationToAddNewSource.getPermissions(), result.getPermissions()); 312 //assertEquals(annotationToAddNewSource.getTargetSources(), result.getTargetSources()); 313 314 } 315 292 316 293 317 @Test
Note: See TracChangeset
for help on using the changeset viewer.