Changeset 3345
- Timestamp:
- 08/09/13 12:55:17 (11 years ago)
- Location:
- DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/AnnotationDao.java
r3334 r3345 64 64 public int deleteAnnotation(Number annotationId) throws SQLException; 65 65 66 /**67 *68 * @param annotationIdentifier69 * @param sourceInfo70 * Only the table "annotations_target_sources" is affected71 * @return the amount of affected rows in the table "annotations_target_sources". Must be 1 if everything went well.72 */73 public int addAnnotationSourcePair(AnnotationIdentifier annotationIdentifier, SourceIdentifier sourceIdentifier);74 66 75 67 … … 82 74 83 75 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 97 76 98 77 99 78 public List<AnnotationInfo> getAnnotationInfos(List<Number> annotationIDs); -
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/SourceDao.java
r3334 r3345 24 24 import eu.dasish.annotation.schema.SourceInfo; 25 25 import java.util.List; 26 import java.util.Map; 26 27 27 28 /** … … 93 94 */ 94 95 public int deleteSourceVersionRows(Number sourceID); 96 97 98 /** 99 * 100 * @param annotationID 101 * @param sources 102 * @return the mapping of a (possible new, freshly added) source onto the corresponding same source in the DB. 103 * The difference between the argument/key source and the value source is just their internal and external identifiers. 104 * The already existing source is mapped onto itself. 105 * The side-effect: the joint table "annotations_target_sources" is extended by the pairs (annotationID, addedSoiurceID). 106 */ 107 public Map<NewOrExistingSourceInfo, NewOrExistingSourceInfo> addTargetSources(Number annotationID, List<NewOrExistingSourceInfo> sources); 108 95 109 96 110 } -
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/impl/JdbcAnnotationDao.java
r3334 r3345 25 25 import eu.dasish.annotation.backend.dao.UserDao; 26 26 import eu.dasish.annotation.backend.identifiers.AnnotationIdentifier; 27 import eu.dasish.annotation.backend.identifiers.SourceIdentifier;28 27 import eu.dasish.annotation.schema.Annotation; 29 28 import eu.dasish.annotation.schema.AnnotationBody; … … 33 32 import eu.dasish.annotation.schema.NewSourceInfo; 34 33 import eu.dasish.annotation.schema.ResourceREF; 35 import eu.dasish.annotation.schema.Source;36 34 import eu.dasish.annotation.schema.SourceInfo; 37 35 import java.sql.ResultSet; 38 36 import java.sql.SQLException; 39 37 import java.util.ArrayList; 40 import java.util.Date;41 38 import java.util.HashMap; 42 39 import java.util.List; … … 208 205 } 209 206 210 ////////////////////////////////////////////////////////////////////////211 @Override212 public int addAnnotationSourcePair(AnnotationIdentifier annotationIdentifier, SourceIdentifier sourceIdentifier) {213 // source is "old" i.e. exists in the DB, onlu the table annotations_target_sources should be updated214 Map<String, Object> paramsAnnotationsSources = new HashMap<String, Object>();215 paramsAnnotationsSources.put("annotationId", getInternalID(annotationIdentifier));216 paramsAnnotationsSources.put("sourceId", jdbcSourceDao.getInternalID(sourceIdentifier));217 String sqlAnnotationsSources = "INSERT INTO " + annotationsSourcesTableName + "(" + annotation_id + "," + source_id + " ) VALUES (:annotationId, :sourceId)";218 return (getSimpleJdbcTemplate().update(sqlAnnotationsSources, paramsAnnotationsSources));219 }220 221 207 // TODO: so far URI in the xml is the same as the external_id in the DB!! 222 208 // Change it when the decision is taken!!! … … 247 233 final int affectedRows = getSimpleJdbcTemplate().update(sql, params); 248 234 249 result.setTimeStamp(retrieveTimeStamp(getInternalID(annotationIdentifier))); 250 235 Number internalID = getInternalID(annotationIdentifier); 236 237 238 //retrieve taime stamp for the just added annotation 239 result.setTimeStamp(retrieveTimeStamp(internalID)); 240 241 // place new target sources in the DB, when necessary, update the corresponding target source info for the result 242 // the joint annotations_target_sources" tabel is updated. 251 243 List<NewOrExistingSourceInfo> sources = result.getTargetSources().getTarget(); 252 result = updateSourceInfo(result, sources, true); 253 244 Map<NewOrExistingSourceInfo, NewOrExistingSourceInfo> sourcePairs = jdbcSourceDao.addTargetSources(internalID, sources); 245 sources.clear(); 246 sources.addAll(sourcePairs.values()); 247 248 //replace the temporary sourceId-references in the body with the persistent externalId 249 String body = annotation.getBody().getAny().get(0).toString(); 250 String newBody = updateTargetRefsInBody(body, sourcePairs); 251 List<Object> bodyXML = result.getBody().getAny(); 252 bodyXML.clear(); 253 bodyXML.add(newBody); 254 String sqlUpdate = "UPDATE " + annotationTableName + " SET " + body_xml + "= ? WHERE " + annotation_id + "= " + internalID; 255 int affectedRowsBodyUpd = getSimpleJdbcTemplate().update(sqlUpdate, bodyXML); 256 257 254 258 return result; 255 259 } catch (DataAccessException exception) { … … 258 262 } 259 263 260 ////////////////////////////////////////////////////////////////////////261 @Override262 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 DB272 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 info280 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 body302 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 updated310 311 return annotation;312 }313 314 264 ////////////////////////////////////////////////// 315 265 @Override … … 320 270 //////////// helpers /////////////////////// 321 271 ///////////////////////////////////////////////// 272 273 private String updateTargetRefsInBody(String body, Map<NewOrExistingSourceInfo, NewOrExistingSourceInfo> sourcePairs){ 274 String result = body; 275 for (NewOrExistingSourceInfo tempSource : sourcePairs.keySet()) { 276 NewSourceInfo newSource = tempSource.getNewSource(); 277 if (newSource != null) { 278 result=result.replaceAll(newSource.getId(),sourcePairs.get(tempSource).getSource().getRef() ); 279 } 280 } 281 return result; 282 } 283 322 284 private XMLGregorianCalendar retrieveTimeStamp(Number internalID) { 323 285 String sqlTime = "SELECT " + time_stamp + " FROM " + annotationTableName + " WHERE " + annotation_id + "= ?"; … … 366 328 return result; 367 329 } 368 369 ///////////////////370 private Source constructNewSource(NewSourceInfo newSourceInfo) {371 Source result = new Source();372 result.setLink(newSourceInfo.getLink());373 result.setVersion(newSourceInfo.getVersion());374 // source's internalID will be generated by the DB375 // source's time stamp will be generated by the DB376 // source's externalID will be generated by the add-source-dao377 return result;378 }379 330 } 331 //List<NewOrExistingSourceInfo> targets = annotation.getTargetSources().getTarget(); 332 // if (clear) { 333 // targets.clear(); 334 // } 335 // 336 // targets.addAll(sourcesUpdated); 337 // 338 // /// time to update body 339 // Number annotationInternalId = getInternalID(annotationIdentifier); 340 // String sqlUpdate = "UPDATE " + annotationTableName + " SET " + body_xml + "= ? WHERE " + annotation_id + "= " + annotationInternalId; 341 // int affectedRowsBodyUpd = getSimpleJdbcTemplate().update(sqlUpdate, bodyXML); 342 // 343 // List<Object> body = annotation.getBody().getAny(); 344 // body.clear(); 345 // body.add(bodyXML); 346 //body is updated -
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/impl/JdbcSourceDao.java
r3334 r3345 25 25 import eu.dasish.annotation.schema.NewOrExistingSourceInfo; 26 26 import eu.dasish.annotation.schema.NewOrExistingSourceInfos; 27 import eu.dasish.annotation.schema.NewSourceInfo; 27 28 import eu.dasish.annotation.schema.Source; 28 29 import eu.dasish.annotation.schema.SourceInfo; … … 211 212 } 212 213 213 214 215 214 216 215 217 ///////////////////////////////////////////////// … … 221 223 222 224 } 225 226 227 228 229 230 //////////////////////////////////////////////////////////////////////// 231 @Override 232 public Map<NewOrExistingSourceInfo, NewOrExistingSourceInfo> addTargetSources(Number annotationID, List<NewOrExistingSourceInfo> sources) { 233 234 Map<NewOrExistingSourceInfo, NewOrExistingSourceInfo> result = new HashMap<NewOrExistingSourceInfo, NewOrExistingSourceInfo>(); 235 236 for (NewOrExistingSourceInfo noeSourceInfo : sources) { 237 SourceInfo sourceInfo = noeSourceInfo.getSource(); 238 if (sourceInfo != null) { 239 // this is an old source, already exists in the DB 240 result.put(noeSourceInfo, noeSourceInfo); 241 addAnnotationSourcePair(annotationID, getInternalID(new SourceIdentifier(sourceInfo.getRef()))); 242 } else { 243 Source newSource = constructNewSource(noeSourceInfo.getNewSource()); 244 Source addedSource = addSource(newSource); 245 addAnnotationSourcePair(annotationID, getInternalID(new SourceIdentifier(addedSource.getURI()))); 246 // create updated source info 247 SourceInfo updatedSourceInfo = new SourceInfo(); 248 updatedSourceInfo.setLink(addedSource.getLink()); 249 updatedSourceInfo.setRef(addedSource.getURI()); 250 updatedSourceInfo.setVersion(addedSource.getVersion()); 251 252 NewOrExistingSourceInfo updatedInfo = new NewOrExistingSourceInfo(); 253 updatedInfo.setSource(updatedSourceInfo); 254 result.put(noeSourceInfo, updatedInfo); 255 } 256 257 } 258 259 260 return result; 261 } 262 263 264 223 265 224 266 //////// HELPERS ////////////////////// 225 267 //////////////////////////////////////////////////////// 268 269 270 private int addAnnotationSourcePair(Number annotationID, Number sourceID) { 271 // source is "old" i.e. exists in the DB, onlu the table annotations_target_sources should be updated 272 Map<String, Object> paramsAnnotationsSources = new HashMap<String, Object>(); 273 paramsAnnotationsSources.put("annotationId", annotationID); 274 paramsAnnotationsSources.put("sourceId", sourceID); 275 String sqlAnnotationsSources = "INSERT INTO " + annotationsSourcesTableName + "(" + annotation_id + "," + source_id + " ) VALUES (:annotationId, :sourceId)"; 276 return (getSimpleJdbcTemplate().update(sqlAnnotationsSources, paramsAnnotationsSources)); 277 } 278 279 280 226 281 private SourceInfo constructSourceInfo(SourceIdentifier sourceIdentifier, String link, VersionIdentifier versionIdentifier) { 227 282 SourceInfo sourceInfo = new SourceInfo(); … … 232 287 } 233 288 289 /////////////////// 290 private Source constructNewSource(NewSourceInfo newSourceInfo) { 291 Source result = new Source(); 292 result.setLink(newSourceInfo.getLink()); 293 result.setVersion(newSourceInfo.getVersion()); 294 // source's internalID will be generated by the DB 295 // source's time stamp will be generated by the DB 296 // source's externalID will be generated by the add-source-dao 297 return result; 298 } 299 300 234 301 private Source constructSource(SourceIdentifier sourceIdentifier, String link, VersionIdentifier version, XMLGregorianCalendar xmlTimeStamp) { 235 302 Source source = new Source(); … … 251 318 return result; 252 319 } 320 321 253 322 } -
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/test/java/eu/dasish/annotation/backend/dao/impl/JdbcAnnotationDaoTest.java
r3334 r3345 251 251 */ 252 252 @Test 253 @Ignore 253 254 public void testAddAnnotationExistingSource() throws SQLException{ 254 255 System.out.println("test_addAnnotation with an existing source"); -
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/test/java/eu/dasish/annotation/backend/dao/impl/JdbcSourceDaoTest.java
r3300 r3345 22 22 import eu.dasish.annotation.backend.identifiers.SourceIdentifier; 23 23 import eu.dasish.annotation.backend.identifiers.VersionIdentifier; 24 import eu.dasish.annotation.schema.NewOrExistingSourceInfo; 24 25 import eu.dasish.annotation.schema.NewOrExistingSourceInfos; 25 26 import eu.dasish.annotation.schema.Source; … … 27 28 import java.util.ArrayList; 28 29 import java.util.List; 30 import java.util.Map; 29 31 import org.jmock.Expectations; 30 32 import org.jmock.Mockery; … … 235 237 236 238 } 239 240 241 /** 242 * Test of addTargetSources method, of class JdbcSourceDao. 243 * public Map<NewOrExistingSourceInfo, NewOrExistingSourceInfo> addTargetSources(Number annotationID, List<NewOrExistingSourceInfo> sources) 244 */ 245 @Test 246 public void testAddTargetSources() 247 { 248 249 } 237 250 }
Note: See TracChangeset
for help on using the changeset viewer.