Changeset 4028


Ignore:
Timestamp:
11/15/13 16:36:16 (11 years ago)
Author:
olhsha
Message:

updating body of the annotation. Testing Get methods and verifying their produced xml-s. Ok.

Location:
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/AnnotationDao.java

    r4010 r4028  
    1919
    2020import eu.dasish.annotation.schema.Annotation;
     21import eu.dasish.annotation.schema.AnnotationBody;
    2122import eu.dasish.annotation.schema.AnnotationInfo;
    2223import eu.dasish.annotation.schema.Permission;
     
    162163     
    163164    /////// UPDATERS //////////////////
    164     /**
    165      *
    166      * @param annotationID
    167      * @param newBodyText
     165   
     166    int updateAnnotationBodyText(Number annotationID, String text);
     167   
     168    /**
     169     *
     170     * @param annotationID
     171     * @param annotationBody
    168172     * @return # of updated rows in "annotation" table after updating the annotation's body text with "newBodyText". Should return 1.
    169173     */
    170     public int updateBodyText(Number annotationID, String newBodyText);
    171    
    172     /////// UPDATERS //////////////////
    173     /**
    174      *
    175      * @param annotationID
    176      * @param newMimeType
    177      * @return # of updated rows in "annotation" table after updating the annotation's body with "newMimeType". Should return 1.
    178      */
    179     public int updateBodyMimeType(Number annotationID, String newMimeType);
     174    public int updateAnnotationBody(Number annotationID, AnnotationBody annotationBody);
     175   
     176   
    180177   
    181178    /**
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/DBIntegrityService.java

    r4010 r4028  
    2020
    2121import eu.dasish.annotation.schema.Annotation;
     22import eu.dasish.annotation.schema.AnnotationBody;
    2223import eu.dasish.annotation.schema.AnnotationInfoList;
    2324import eu.dasish.annotation.schema.CachedRepresentationInfo;
     
    247248     * @param annotation
    248249     * @return 1 of the annotation if it is updated
    249      * @throws SQLException
    250250     */
    251251    int updateUsersAnnotation(Number userID, Annotation annotation);
     252   
     253     
     254   
     255    /**
     256     *
     257     * @param userID
     258     * @param annotationBody
     259     * @return 1 of the annotation if it is updated
     260     */
     261    int updateAnnotationBody(Number internalID, AnnotationBody annotationBody);
     262   
    252263   
    253264   
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/impl/DBIntegrityServiceImlp.java

    r4010 r4028  
    2626import eu.dasish.annotation.backend.rest.AnnotationResource;
    2727import eu.dasish.annotation.schema.Annotation;
     28import eu.dasish.annotation.schema.AnnotationBody;
    2829import eu.dasish.annotation.schema.AnnotationInfo;
    2930import eu.dasish.annotation.schema.AnnotationInfoList;
     
    4142import java.io.InputStream;
    4243import java.lang.Number;
    43 import java.sql.Blob;
    44 import java.sql.SQLException;
    4544import java.sql.Timestamp;
    4645import java.util.ArrayList;
     
    389388        return updatedAnnotations;
    390389    }
     390   
     391     
     392    // TODO: unit test
     393    @Override
     394    public int updateAnnotationBody(Number internalID, AnnotationBody annotationBody) {
     395        return annotationDao.updateAnnotationBody(internalID, annotationBody);
     396    }
     397   
     398   
     399   
    391400    /////////////// ADDERS  /////////////////////////////////
    392401    @Override
     
    511520        List<TargetInfo> targets = annotation.getTargets().getTargetInfo();
    512521        Map<String, String> targetIdPairs = addTargetsForAnnotation(annotationID, targets);
     522        AnnotationBody annotationBody = annotation.getBody();
    513523        String bodyText;
    514         String newBody;
    515         if (annotation.getBody().getXmlBody() != null) {
     524        String newBodyText;
     525        if (annotationBody.getXmlBody() != null) {
    516526            bodyText = Helpers.elementToString(annotation.getBody().getXmlBody().getAny());
    517527        } else {
     
    524534            }
    525535        }
    526         newBody = Helpers.replace(bodyText, targetIdPairs);
    527         return annotationDao.updateBodyText(annotationID, newBody);
     536        newBodyText = Helpers.replace(bodyText, targetIdPairs);
     537        return annotationDao.updateAnnotationBodyText(annotationID, newBodyText);
    528538    }
    529539
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/impl/JdbcAnnotationDao.java

    r4010 r4028  
    326326    //////////// UPDATERS /////////////
    327327    @Override
    328     public int updateBodyText(Number annotationID, String newBodyText) {
     328    public int updateAnnotationBodyText(Number annotationID, String text) {
    329329        StringBuilder sql = new StringBuilder("UPDATE ");
    330         sql.append(annotationTableName).append(" SET ").append(body_text).append("= '").append(newBodyText).append("' WHERE ").append(annotation_id).append("= ?");
    331         return getSimpleJdbcTemplate().update(sql.toString(), annotationID);
    332     }
    333 
    334     @Override
    335     public int updateBodyMimeType(Number annotationID, String newMimeType) {
     330        sql.append(annotationTableName).append(" SET ").
     331                append(body_text).append("= '").append(text).
     332                append("' WHERE ").append(annotation_id).append("= ?");
     333        int affectedRows = getSimpleJdbcTemplate().update(sql.toString(), annotationID);
     334        return affectedRows;
     335    }
     336   
     337   
     338   
     339    @Override
     340    public int updateAnnotationBody(Number annotationID, AnnotationBody annotationBody) {
     341        String[] body = retrieveBodyComponents(annotationBody);       
    336342        StringBuilder sql = new StringBuilder("UPDATE ");
    337         sql.append(annotationTableName).append(" SET ").append(body_mimetype).append("= '").append(newMimeType).append("' WHERE ").append(annotation_id).append("= ?");
    338         return getSimpleJdbcTemplate().update(sql.toString(), annotationID);
    339     }
     343        sql.append(annotationTableName).append(" SET ").
     344                append(body_text).append("= '").append(body[0]).append("',").
     345                append(body_mimetype).append("= '").append(body[1]).append("',").
     346                append(is_xml).append("= '").append(annotationBody.getXmlBody() != null).
     347                append("' WHERE ").append(annotation_id).append("= ?");
     348        int affectedRows = getSimpleJdbcTemplate().update(sql.toString(), annotationID);
     349        return affectedRows;
     350    }
     351   
     352   
    340353
    341354    // TODO Unit test
     
    343356    public int updateAnnotation(Annotation annotation, Number ownerID){
    344357
    345         String[] body = retrieveBodyComponents(annotation);
     358        String[] body = retrieveBodyComponents(annotation.getBody());
    346359
    347360        String externalID = stringURItoExternalID(annotation.getURI());
     
    373386    public Number addAnnotation(Annotation annotation, Number ownerID){
    374387
    375         String[] body = retrieveBodyComponents(annotation);
     388        String[] body = retrieveBodyComponents(annotation.getBody());
    376389
    377390        // generate a new annotation ID
     
    457470
    458471    /////////////// helpers //////////////////
    459     private String[] retrieveBodyComponents(Annotation annotation){
    460         boolean body_is_xml = annotation.getBody().getXmlBody() != null;
     472    private String[] retrieveBodyComponents(AnnotationBody annotationBody){
     473        boolean body_is_xml = annotationBody.getXmlBody() != null;
    461474        String[] result = new String[2];
    462475        if (body_is_xml) {
    463             result[0] = Helpers.elementToString(annotation.getBody().getXmlBody().getAny());
    464             result[1] = annotation.getBody().getXmlBody().getMimeType();
     476            result[0] = Helpers.elementToString(annotationBody.getXmlBody().getAny());
     477            result[1] = annotationBody.getXmlBody().getMimeType();
    465478        } else {
    466             TextBody textBody = annotation.getBody().getTextBody();
     479            TextBody textBody = annotationBody.getTextBody();
    467480            if (textBody != null) {
    468481                result[0] = textBody.getValue();
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/AnnotationResource.java

    r4013 r4028  
    2525import eu.dasish.annotation.schema.Action;
    2626import eu.dasish.annotation.schema.ActionList;
     27import eu.dasish.annotation.schema.AnnotationBody;
    2728import eu.dasish.annotation.schema.ObjectFactory;
    2829import eu.dasish.annotation.schema.Permission;
     
    269270
    270271    }
     272   
     273    @PUT
     274    @Consumes(MediaType.APPLICATION_XML)
     275    @Produces(MediaType.APPLICATION_XML)
     276    @Path("{annotationid: " + BackendConstants.regExpIdentifier + "}/body")
     277    @Secured("ROLE_USER")
     278    public JAXBElement<ResponseBody> updateAnnotationBody(@PathParam("annotationid") String externalIdentifier, AnnotationBody annotationBody) {
     279        String path = uriInfo.getBaseUri().toString();
     280        dbIntegrityService.setServiceURI(path);
     281       
     282        final Number annotationID = dbIntegrityService.getAnnotationInternalIdentifier(UUID.fromString(externalIdentifier));
     283        final Number userID = dbIntegrityService.getUserInternalIDFromRemoteID(httpServletRequest.getRemoteUser());
     284        if (canWrite(userID, annotationID)) {
     285            int updatedRows = dbIntegrityService.updateAnnotationBody(annotationID, annotationBody);
     286            logger.info("updateAnnotationBody method: OK");
     287            return new ObjectFactory().createResponseBody(makeAnnotationResponseEnvelope(annotationID));
     288
     289        } else {
     290            logger.error("FORBIDDEN-access attempt.");
     291            logger.error("The logged-in user is not authorised to alter this annotation. ");
     292            try {
     293                httpServletResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED);
     294            } catch (IOException ioe) {
     295                logger.error("IOException: Cannot send server respond about unaithorized access.");
     296            }
     297            return null;
     298        }
     299
     300    }
    271301
    272302    @PUT
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/TargetResource.java

    r3972 r4028  
    9999    //how to overwork the input stream to make it downloadable
    100100    // using mime type as well
    101     @DELETE
    102     @Produces(MediaType.TEXT_XML)
    103     @Path("{targetid: " + BackendConstants.regExpIdentifier + "}/cached/{cachedid: " + BackendConstants.regExpIdentifier + "}")
    104     @Secured("ROLE_ADMIN")
    105     public int deleteCached(@PathParam("targetid") String targetIdentifier, @PathParam("cachedid") String cachedIdentifier) throws SQLException {
    106         dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
    107         final Number targetID = dbIntegrityService.getCachedRepresentationInternalIdentifier(UUID.fromString(targetIdentifier));
    108         final Number cachedID = dbIntegrityService.getCachedRepresentationInternalIdentifier(UUID.fromString(cachedIdentifier));
    109         int[] result = dbIntegrityService.deleteCachedRepresentationOfTarget(targetID, cachedID);
    110         return result[1];
    111     }
     101//    @DELETE
     102//    @Produces(MediaType.TEXT_XML)
     103//    @Path("{targetid: " + BackendConstants.regExpIdentifier + "}/cached/{cachedid: " + BackendConstants.regExpIdentifier + "}")
     104//    @Secured("ROLE_ADMIN")
     105//    public int deleteCached(@PathParam("targetid") String targetIdentifier, @PathParam("cachedid") String cachedIdentifier) throws SQLException {
     106//        dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
     107//        final Number targetID = dbIntegrityService.getCachedRepresentationInternalIdentifier(UUID.fromString(targetIdentifier));
     108//        final Number cachedID = dbIntegrityService.getCachedRepresentationInternalIdentifier(UUID.fromString(cachedIdentifier));
     109//        int[] result = dbIntegrityService.deleteCachedRepresentationOfTarget(targetID, cachedID);
     110//        return result[1];
     111//    }
    112112
    113113    @POST
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/test/java/eu/dasish/annotation/backend/dao/impl/DBIntegrityServiceTest.java

    r3972 r4028  
    690690                ///////////
    691691
    692                 oneOf(annotationDao).updateBodyText(6, testAnnotation.getBody().getTextBody().getValue());
     692                oneOf(annotationDao).updateAnnotationBodyText(6, testAnnotation.getBody().getTextBody().getValue());
    693693                will(returnValue(1)); // the DB update will be called at perform anyway, even if the body is not changed (can be optimized)
    694694
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/test/java/eu/dasish/annotation/backend/dao/impl/JdbcAnnotationDaoTest.java

    r3972 r4028  
    376376        System.out.println("test updateBodyText");
    377377        String newBodyText = "new body";
    378         int result = jdbcAnnotationDao.updateBodyText(2, newBodyText);
     378        int result = jdbcAnnotationDao.updateAnnotationBodyText(2, newBodyText);
    379379        assertEquals(1, result);
    380380        Map<Annotation,Number> getResult= jdbcAnnotationDao.getAnnotationWithoutTargetsAndPermissions(2);
     
    384384    }
    385385
    386     //////////////////////////////////
    387     @Test
    388     public void testUpdateMimeType() throws SQLException{
    389         System.out.println("test updateBodyMimeType");
    390         String newBodyMimeType = "text/xml";
    391         int result = jdbcAnnotationDao.updateBodyMimeType(2, newBodyMimeType);
    392         assertEquals(1, result);
    393         Map<Annotation,Number> getResult= jdbcAnnotationDao.getAnnotationWithoutTargetsAndPermissions(2);
    394         Annotation[] annotations = new Annotation[1];
    395         getResult.keySet().toArray(annotations);
    396         assertEquals(newBodyMimeType, annotations[0].getBody().getTextBody().getMimeType());
    397     }
     386 
    398387   
    399388    // public List<Map<Number, String>> retrievePermissions(Number annotationId)
Note: See TracChangeset for help on using the changeset viewer.