Changeset 3330
- Timestamp:
- 08/08/13 12:35:33 (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
r3309 r3330 19 19 20 20 import eu.dasish.annotation.backend.identifiers.AnnotationIdentifier; 21 import eu.dasish.annotation.backend.identifiers.SourceIdentifier; 21 22 import eu.dasish.annotation.schema.Annotation; 22 23 import eu.dasish.annotation.schema.AnnotationInfo; 23 24 import eu.dasish.annotation.schema.ResourceREF; 25 import eu.dasish.annotation.schema.SourceInfo; 24 26 import java.sql.SQLException; 25 27 import java.util.List; … … 62 64 public int deleteAnnotation(Number annotationId) throws SQLException; 63 65 66 /** 67 * 68 * @param annotationIdentifier 69 * @param sourceInfo 70 * Only the table "annotations_target_sources" is affected 71 * @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 64 75 65 76 /** -
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/impl/JdbcAnnotationDao.java
r3303 r3330 24 24 import eu.dasish.annotation.backend.dao.UserDao; 25 25 import eu.dasish.annotation.backend.identifiers.AnnotationIdentifier; 26 import eu.dasish.annotation.backend.identifiers.SourceIdentifier; 26 27 import eu.dasish.annotation.schema.Annotation; 27 28 import eu.dasish.annotation.schema.AnnotationBody; … … 29 30 import eu.dasish.annotation.schema.NewOrExistingSourceInfo; 30 31 import eu.dasish.annotation.schema.NewOrExistingSourceInfos; 32 import eu.dasish.annotation.schema.NewSourceInfo; 31 33 import eu.dasish.annotation.schema.ResourceREF; 34 import eu.dasish.annotation.schema.Source; 32 35 import eu.dasish.annotation.schema.SourceInfo; 33 36 import java.sql.ResultSet; … … 47 50 * @author Peter Withers <peter.withers@mpi.nl> 48 51 */ 49 50 51 52 53 52 public class JdbcAnnotationDao extends JdbcResourceDao implements AnnotationDao { 54 55 @Autowired 56 PermissionsDao jdbcPermissionsDao; 57 58 @Autowired 59 UserDao jdbcUserDao; 60 61 @Autowired 62 NotebookDao jdbcNotebookDao; 63 64 @Autowired 65 SourceDao jdbcSourceDao; 66 53 54 @Autowired 55 PermissionsDao jdbcPermissionsDao; 56 @Autowired 57 UserDao jdbcUserDao; 58 @Autowired 59 NotebookDao jdbcNotebookDao; 60 @Autowired 61 SourceDao jdbcSourceDao; 62 67 63 public JdbcAnnotationDao(DataSource dataSource) { 68 setDataSource(dataSource); 64 setDataSource(dataSource); 69 65 internalIdName = annotation_id; 70 66 resourceTableName = annotationTableName; 71 67 } 72 73 74 68 75 69 //////////////////////////////////////////////////////////////////////////// 76 77 /** 78 * 79 * @param annotationIDs is a list of internal annotation identifiers 80 * @return the list of the corresponding annotation-infos for the annotation identifiers from the list; 81 * if the input list is null return null 82 * if the input list is empty (zero elements) returns an empty list 83 */ 84 70 /** 71 * 72 * @param annotationIDs is a list of internal annotation identifiers 73 * @return the list of the corresponding annotation-infos for the annotation 74 * identifiers from the list; if the input list is null return null if the 75 * input list is empty (zero elements) returns an empty list 76 */ 85 77 @Override 86 78 public List<AnnotationInfo> getAnnotationInfos(List<Number> annotationIDs) { 87 79 88 80 if (annotationIDs == null) { 89 81 return null; 90 82 } 91 83 92 84 if (annotationIDs.isEmpty()) { 93 85 return (new ArrayList<AnnotationInfo>()); 94 86 } 95 87 96 88 String values = makeListOfValues(annotationIDs); 97 String sql = "SELECT "+annotationStar+" FROM "+annotationTableName +" WHERE "+annotationAnnotation_id+" IN "+values; 98 return getSimpleJdbcTemplate().query(sql, annotationInfoRowMapper); 99 } 100 101 private final RowMapper<AnnotationInfo> annotationInfoRowMapper = new RowMapper<AnnotationInfo>() { 89 String sql = "SELECT " + annotationStar + " FROM " + annotationTableName + " WHERE " + annotationAnnotation_id + " IN " + values; 90 return getSimpleJdbcTemplate().query(sql, annotationInfoRowMapper); 91 } 92 private final RowMapper<AnnotationInfo> annotationInfoRowMapper = new RowMapper<AnnotationInfo>() { 102 93 @Override 103 94 public AnnotationInfo mapRow(ResultSet rs, int rowNumber) throws SQLException { 104 AnnotationInfo annotationInfo = new AnnotationInfo();105 annotationInfo.setOwner(getResourceREF(Integer.toString(rs.getInt(owner_id))));106 annotationInfo.setHeadline(rs.getString(headline));107 return annotationInfo;95 AnnotationInfo annotationInfo = new AnnotationInfo(); 96 annotationInfo.setOwner(getResourceREF(Integer.toString(rs.getInt(owner_id)))); 97 annotationInfo.setHeadline(rs.getString(headline)); 98 return annotationInfo; 108 99 } 109 100 }; 110 111 112 101 113 102 ///////////////////////////////////////////////// 114 115 103 /** 116 * 104 * 117 105 * @param annotationIDs 118 * @return list of annotation references corresponding to the annotation-ids from the input list 119 * if the input list is null or empty (zero elements) returns an empty list 106 * @return list of annotation references corresponding to the annotation-ids 107 * from the input list if the input list is null or empty (zero elements) 108 * returns an empty list 120 109 */ 121 122 110 @Override 123 111 public List<ResourceREF> getAnnotationREFs(List<Number> annotationIDs) { 124 112 125 113 if (annotationIDs == null) { 126 114 return null; 127 115 } 128 116 129 117 if (annotationIDs.isEmpty()) { 130 118 return (new ArrayList<ResourceREF>()); 131 119 } 132 120 133 121 String values = makeListOfValues(annotationIDs); 134 String sql = "SELECT "+annotationAnnotation_id+" FROM "+annotationTableName+" WHERE "+annotationAnnotation_id+" IN "+values; 135 return getSimpleJdbcTemplate().query(sql, annotationREFRowMapper); 136 } 137 138 private final RowMapper<ResourceREF> annotationREFRowMapper = new RowMapper<ResourceREF>() { 122 String sql = "SELECT " + annotationAnnotation_id + " FROM " + annotationTableName + " WHERE " + annotationAnnotation_id + " IN " + values; 123 return getSimpleJdbcTemplate().query(sql, annotationREFRowMapper); 124 } 125 private final RowMapper<ResourceREF> annotationREFRowMapper = new RowMapper<ResourceREF>() { 139 126 @Override 140 127 public ResourceREF mapRow(ResultSet rs, int rowNumber) throws SQLException { 141 ResourceREF annotationREF = new ResourceREF();142 annotationREF.setRef(Integer.toString(rs.getInt(annotation_id)));143 return annotationREF;128 ResourceREF annotationREF = new ResourceREF(); 129 annotationREF.setRef(Integer.toString(rs.getInt(annotation_id))); 130 return annotationREF; 144 131 } 145 132 }; 146 133 147 134 ////////////////////////////////////////////////////////////////////////// 148 @Override149 public Annotation getAnnotation(Number annotationID) throws SQLException {135 @Override 136 public Annotation getAnnotation(Number annotationID) throws SQLException { 150 137 if (annotationID == null) { 151 138 return null; 152 139 } 153 String sql = "SELECT "+annotationStar+" FROM "+annotationTableName+" WHERE "+annotationAnnotation_id +"= ?"; 154 List<Annotation> result= getSimpleJdbcTemplate().query(sql, annotationRowMapper, annotationID); 155 156 if (result == null) { 157 return null; 158 } 159 if (result.isEmpty()) { 160 return null; 161 } 162 return result.get(0); 163 } 164 165 private final RowMapper<Annotation> annotationRowMapper = new RowMapper<Annotation>() { 140 String sql = "SELECT " + annotationStar + " FROM " + annotationTableName + " WHERE " + annotationAnnotation_id + "= ?"; 141 List<Annotation> result = getSimpleJdbcTemplate().query(sql, annotationRowMapper, annotationID); 142 143 if (result == null) { 144 return null; 145 } 146 if (result.isEmpty()) { 147 return null; 148 } 149 return result.get(0); 150 } 151 private final RowMapper<Annotation> annotationRowMapper = new RowMapper<Annotation>() { 166 152 @Override 167 153 public Annotation mapRow(ResultSet rs, int rowNumber) throws SQLException { 168 Annotation result = new Annotation();169 result.setHeadline(rs.getString(headline));170 171 ResourceREF ownerREF = new ResourceREF();172 ownerREF.setRef(String.valueOf(rs.getInt(owner_id)));173 result.setOwner(ownerREF);174 175 List<SourceInfo> sourceInfoList = jdbcSourceDao.getSourceInfos(rs.getInt(annotation_id));176 NewOrExistingSourceInfos noeSourceInfos = jdbcSourceDao.contructNewOrExistingSourceInfo(sourceInfoList);177 result.setTargetSources(noeSourceInfos);178 179 result.setBody(convertToAnnotationBody(rs.getString(body_xml)));180 return result;154 Annotation result = new Annotation(); 155 result.setHeadline(rs.getString(headline)); 156 157 ResourceREF ownerREF = new ResourceREF(); 158 ownerREF.setRef(String.valueOf(rs.getInt(owner_id))); 159 result.setOwner(ownerREF); 160 161 List<SourceInfo> sourceInfoList = jdbcSourceDao.getSourceInfos(rs.getInt(annotation_id)); 162 NewOrExistingSourceInfos noeSourceInfos = jdbcSourceDao.contructNewOrExistingSourceInfo(sourceInfoList); 163 result.setTargetSources(noeSourceInfos); 164 165 result.setBody(convertToAnnotationBody(rs.getString(body_xml))); 166 return result; 181 167 } 182 168 }; 183 184 // TODO: fill in the stub, when the annotation body is elaborated 185 private AnnotationBody convertToAnnotationBody(String input){ 186 if (input == null) { 187 return null; 188 } 189 190 AnnotationBody result = new AnnotationBody(); 191 List<Object> element =result.getAny(); 192 element.add(input); 193 return result; 194 } 195 196 197 198 199 @Override 200 public int deleteAnnotation(Number annotationId) throws SQLException{ 201 202 //TODO: why does it not work via calling notebook and permissions "remove" methods?? 203 204 String sqlNotebooks = "DELETE FROM " + notebooksAnnotationsTableName + " where "+annotation_id + " = ?"; 169 170 // TODO: fill in the stub, when the annotation body is elaborated 171 private AnnotationBody convertToAnnotationBody(String input) { 172 if (input == null) { 173 return null; 174 } 175 176 AnnotationBody result = new AnnotationBody(); 177 List<Object> element = result.getAny(); 178 element.add(input); 179 return result; 180 } 181 182 @Override 183 public int deleteAnnotation(Number annotationId) throws SQLException { 184 185 //TODO: why does it not work via calling notebook and permissions "remove" methods?? 186 187 String sqlNotebooks = "DELETE FROM " + notebooksAnnotationsTableName + " where " + annotation_id + " = ?"; 205 188 int affectedNotebooks = getSimpleJdbcTemplate().update(sqlNotebooks, annotationId); 206 207 String sqlPermissions = "DELETE FROM " + permissionsTableName + " where " +annotation_id + " = ?";189 190 String sqlPermissions = "DELETE FROM " + permissionsTableName + " where " + annotation_id + " = ?"; 208 191 int affectedPermissions = getSimpleJdbcTemplate().update(sqlPermissions, annotationId); 209 192 210 193 // TODO make a separate methods in sources DAO so that it handles removal of sources which are not referered by annotations 211 String sqlTargetSources = "DELETE FROM " + annotationsSourcesTableName + " where " +annotation_id + " = ?";194 String sqlTargetSources = "DELETE FROM " + annotationsSourcesTableName + " where " + annotation_id + " = ?"; 212 195 int affectedSources = getSimpleJdbcTemplate().update(sqlTargetSources, annotationId); 213 214 String sqlAnnotation = "DELETE FROM " + annotationTableName + " where " +annotation_id + " = ?";196 197 String sqlAnnotation = "DELETE FROM " + annotationTableName + " where " + annotation_id + " = ?"; 215 198 int affectedAnnotations = getSimpleJdbcTemplate().update(sqlAnnotation, annotationId); 216 if (affectedAnnotations >1) {217 throw new SQLException("There was more than one annotation (" +affectedAnnotations+") with the same ID "+annotationId);199 if (affectedAnnotations > 1) { 200 throw new SQLException("There was more than one annotation (" + affectedAnnotations + ") with the same ID " + annotationId); 218 201 } 219 202 return affectedAnnotations; 220 203 //TODO implement deleting sources (see the specification document and the interfaces' javadoc 221 204 } 222 223 224 225 // TODO: so far URI in the xml is the same as the external_id in the DB!! 226 // Chnage it when the decision is taken!!! 227 @Override 228 public Annotation addAnnotation(Annotation annotation, Number ownerID) throws SQLException{ 229 205 206 //////////////////////////////////////////////////////////////////////// 207 @Override 208 public int addAnnotationSourcePair(AnnotationIdentifier annotationIdentifier, SourceIdentifier sourceIdentifier) { 209 // source is "old" i.e. exists in the DB, onlu the table annotations_target_sources should be updated 210 Map<String, Object> paramsAnnotationsSources = new HashMap<String, Object>(); 211 paramsAnnotationsSources.put("annotationId", getInternalID(annotationIdentifier)); 212 paramsAnnotationsSources.put("sourceId", jdbcSourceDao.getInternalID(sourceIdentifier)); 213 String sqlAnnotationsSources = "INSERT INTO " + annotationsSourcesTableName + "(" + annotation_id + "," + source_id + " ) VALUES (:annotationId, :sourceId)"; 214 return (getSimpleJdbcTemplate().update(sqlAnnotationsSources, paramsAnnotationsSources)); 215 } 216 217 218 // TODO: so far URI in the xml is the same as the external_id in the DB!! 219 // Change it when the decision is taken!!! 220 @Override 221 public Annotation addAnnotation(Annotation annotation, Number ownerID) throws SQLException { 230 222 if (annotation == null) { 231 223 return null; 232 } 233 224 } 234 225 try { 235 226 Annotation result = makeFreshCopy(annotation); 236 227 237 228 ResourceREF ownerRef = new ResourceREF(); 238 229 ownerRef.setRef(String.valueOf(ownerID)); 239 230 result.setOwner(ownerRef); 240 231 241 232 // generate a new annotation ID 242 233 AnnotationIdentifier annotationIdentifier = new AnnotationIdentifier(); 243 234 result.setURI(annotationIdentifier.toString()); 244 235 245 236 Map<String, Object> params = new HashMap<String, Object>(); 246 237 params.put("externalId", annotationIdentifier.toString()); 247 params.put("timeStamp", annotation.getTimeStamp());238 //params.put("timeStamp", annotation.getTimeStamp()); is generated while adding the annotation in the DB as "now" 248 239 params.put("ownerId", ownerID); 249 240 params.put("headline", annotation.getHeadline()); 250 // may be changed once we elaborate the body251 241 params.put("bodyXml", annotation.getBody().getAny().get(0).toString()); 252 String sql = "INSERT INTO "+annotationTableName + "("+external_id+","+ time_stamp+"," + owner_id+","+headline+","+body_xml+" ) VALUES (:externalId, :timeStamp, :ownerId, :headline, :bodyXml)"; 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) 253 248 final int affectedRows = getSimpleJdbcTemplate().update(sql, params); 254 249 250 // adding sources 255 251 if (affectedRows == 1) { 256 //jdbcSourceDao method "addsources" 257 // Consider two cases: the one which exists already in the Db and the one which does not exists 258 return result; 259 } 260 else { 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); 281 return result; 282 } else { 261 283 // something went wrong 262 284 return null; … … 266 288 } 267 289 } 268 269 270 ////////////////////////////////////////////////// 290 291 ////////////////////////////////////////////////// 271 292 @Override 272 293 public AnnotationIdentifier getExternalID(Number internalID) { 273 294 return new AnnotationIdentifier(super.getExternalIdentifier(internalID)); 274 } 275 276 277 295 } 296 278 297 //////////// helpers /////////////////////// 279 280 281 298 ///////////////////////////////////////////////// 282 private ResourceREF getResourceREF(String resourceID){ 283 ResourceREF result = new ResourceREF(); 284 result.setRef(resourceID); 285 return result; 286 } 287 288 289 290 ////////////////////////////////////////// 291 292 private Annotation makeFreshCopy(Annotation annotation){ 293 299 private ResourceREF getResourceREF(String resourceID) { 300 ResourceREF result = new ResourceREF(); 301 result.setRef(resourceID); 302 return result; 303 } 304 305 ////////////////////////////////////////// 306 private Annotation makeFreshCopy(Annotation annotation) { 307 294 308 if (annotation == null) { 295 309 return null; 296 310 } 297 311 298 312 Annotation result = new Annotation(); 299 313 result.setBody(annotation.getBody()); … … 304 318 result.setTimeStamp(annotation.getTimeStamp()); 305 319 result.setURI(annotation.getURI()); 306 320 307 321 return result; 308 322 } 323 324 /////////////////// 325 326 private Source constructNewSource(NewSourceInfo newSourceInfo){ 327 Source result = new Source(); 328 result.setLink(newSourceInfo.getLink()); 329 result.setVersion(newSourceInfo.getVersion()); 330 // source's internalID will be generated by the DB 331 // source's time stamp will be generated by the DB 332 // source's externalID will be generated by the add-source-dao 333 return result; 334 } 309 335 336 310 337 } 311 -
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/impl/JdbcCachedRepresentationDao.java
r3299 r3330 148 148 return result; 149 149 } 150 150 151 } -
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/impl/JdbcSourceDao.java
r3299 r3330 177 177 return null; 178 178 } 179 179 180 180 } 181 181 -
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/impl/JdbcVersionDao.java
r3299 r3330 171 171 if (affectedRows == 1) { 172 172 Version versionAdded = makeFreshCopy(freshVersion); 173 // TODO change for external identifier when the s hcemis fixed173 // TODO change for external identifier when the schema is fixed 174 174 versionAdded.setVersion(newExternalIdentifier); 175 175 return versionAdded; … … 177 177 return null; 178 178 } 179 180 // adding the corresponding cached representation is initiated from the separate service POST api/sources/<sid>/cached 181 // so it is not implemented here 179 182 } 180 183 -
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/test/java/eu/dasish/annotation/backend/dao/impl/JdbcAnnotationDaoTest.java
r3303 r3330 24 24 import eu.dasish.annotation.backend.dao.SourceDao; 25 25 import eu.dasish.annotation.backend.identifiers.AnnotationIdentifier; 26 import eu.dasish.annotation.backend.identifiers.SourceIdentifier; 26 27 import eu.dasish.annotation.schema.Annotation; 27 28 import eu.dasish.annotation.schema.AnnotationInfo; … … 250 251 public void testAddAnnotation() throws SQLException{ 251 252 System.out.println("test_addAnnotation"); 252 final Annotation annotationToAdd = testInstances.getAnnotationToAdd(); 253 final Annotation annotationToAdd = testInstances.getAnnotationToAdd();// existing sources 253 254 254 255 final Number testAnnotationID = 6; … … 268 269 oneOf(sourceDao).contructNewOrExistingSourceInfo(sourceInfoList); 269 270 will(returnValue(annotationToAdd.getTargetSources())); 271 272 oneOf(sourceDao).getInternalID(new SourceIdentifier(TestBackendConstants._TEST_SOURCE_1_EXT_ID)); 273 will(returnValue(1)); 274 270 275 } 271 276 }); … … 282 287 assertEquals(annotationToAdd.getTimeStamp(), addedAnnotation.getTimeStamp()); 283 288 284 // try to add an already existing annptation, should produce null 285 //Annotation annotationOne = testInstances.getAnnotationOne(); 286 //AnnotationIdentifier resultOne = jdbcAnnotationDao.addAnnotation(annotationOne); 287 // TODO: why it doesp produce an annotation, it already exists!! 288 //assertTrue(resultOne == null); 289 289 290 290 291 }
Note: See TracChangeset
for help on using the changeset viewer.