Changeset 4207


Ignore:
Timestamp:
12/19/13 14:36:04 (10 years ago)
Author:
olhsha
Message:

adding trasnactional, refactoring and fixing bugs in updated annotations, removing try-catch from resource methods (The Greek's advice)

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

Legend:

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

    r4188 r4207  
    7171        Document document = element.getOwnerDocument();
    7272        DOMImplementationLS domImplLS = (DOMImplementationLS) document.getImplementation();
    73         String encoding=document.getInputEncoding();
    7473        LSSerializer serializer = domImplLS.createLSSerializer();
    7574        String result = serializer.writeToString(element);
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/AnnotationDao.java

    r4146 r4207  
    114114    public Permission  getPermission(Number annotationID, Number userID);
    115115   
    116     public List<Number> getAnnotationIDsForUserWithPermission(Number userID, String permissionString);
     116    public List<Number> getAnnotationIDsForUserWithPermission(Number userID, String[] permissionStrings);
    117117   
    118118   
     
    164164    /////// UPDATERS //////////////////
    165165   
    166     int updateAnnotationBodyText(Number annotationID, String text);
    167    
    168     /**
    169      *
    170      * @param annotationID
    171      * @param annotationBody
    172      * @return # of updated rows in "annotation" table after updating the annotation's body text with "newBodyText". Should return 1.
    173      */
    174     public int updateAnnotationBody(Number annotationID, AnnotationBody annotationBody);
     166   
     167    public int updateAnnotationBody(Number annotationID, String text, String mimeType, Boolean isXml);
    175168   
    176169   
     
    229222    */
    230223    public int deleteAnnotationPrincipalPermissions(Number annotationID);
     224   
     225    /*
     226     * HELPERS
     227     */
     228   
     229    public String[] retrieveBodyComponents(AnnotationBody annotationBody);
    231230
    232231}
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/DBIntegrityService.java

    r4028 r4207  
    8888    * -- created after time-samp "after and before time-stamp "before".
    8989    */
    90     List<Number> getFilteredAnnotationIDs(String link, String text, Number inloggedUserID, String access, String namespace, UUID
     90    List<Number> getFilteredAnnotationIDs(String link, String text, Number inloggedUserID, String[] accessModes, String namespace, UUID
    9191            owner, Timestamp after, Timestamp before);
    9292   
     
    110110    * -- created after time-samp "after and before time-stamp "before".
    111111     */
    112     AnnotationInfoList getFilteredAnnotationInfos(String word, String text, Number inloggedUserID, String access, String namespace, UUID
     112    AnnotationInfoList getFilteredAnnotationInfos(String word, String text, Number inloggedUserID, String[] accessModes, String namespace, UUID
    113113            ownerID, Timestamp after, Timestamp before);
    114114
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/impl/DBIntegrityServiceImlp.java

    r4181 r4207  
    4040import eu.dasish.annotation.schema.User;
    4141import eu.dasish.annotation.schema.UserWithPermission;
     42import java.io.IOException;
    4243import java.io.InputStream;
    4344import java.lang.Number;
     45import java.sql.SQLException;
    4446import java.sql.Timestamp;
    4547import java.util.ArrayList;
     
    4850import java.util.Map;
    4951import java.util.UUID;
     52import javax.xml.parsers.ParserConfigurationException;
    5053import org.springframework.beans.factory.annotation.Autowired;
    5154import org.slf4j.Logger;
    5255import org.slf4j.LoggerFactory;
     56import org.springframework.transaction.annotation.Transactional;
    5357
    5458/**
     
    9498    }
    9599
    96     ///////////// GETTERS //////////////////////////
     100   
    97101    @Override
    98102    public Number getTargetInternalIdentifier(UUID externalID) {
     
    189193    ////////////////////////////////////////////////////////////////////////
    190194    @Override
    191     public List<Number> getFilteredAnnotationIDs(String link, String text, Number inloggedUserID, String access, String namespace, UUID owner, Timestamp after, Timestamp before) {
    192 
    193         if (access == null) {
    194             return null;
    195         }
    196 
    197         List<Number> annotationIDs = annotationDao.getAnnotationIDsForUserWithPermission(inloggedUserID, access);
     195    public List<Number> getFilteredAnnotationIDs(String link, String text, Number inloggedUserID, String[] accessModes, String namespace, UUID owner, Timestamp after, Timestamp before) {
     196
     197        if (accessModes == null) {
     198            return null;
     199        }
     200
     201        List<Number> annotationIDs = annotationDao.getAnnotationIDsForUserWithPermission(inloggedUserID, accessModes);
    198202
    199203        if (link != null) {
     
    258262
    259263    @Override
    260     public AnnotationInfoList getFilteredAnnotationInfos(String word, String text, Number inloggedUserID, String access, String namespace, UUID owner, Timestamp after, Timestamp before) {
    261         List<Number> annotationIDs = getFilteredAnnotationIDs(word, text, inloggedUserID, access, namespace, owner, after, before);
     264    public AnnotationInfoList getFilteredAnnotationInfos(String word, String text, Number inloggedUserID, String[] accessModes, String namespace, UUID owner, Timestamp after, Timestamp before) {
     265        List<Number> annotationIDs = getFilteredAnnotationIDs(word, text, inloggedUserID, accessModes, namespace, owner, after, before);
    262266        if (annotationIDs != null) {
    263267            AnnotationInfoList result = new AnnotationInfoList();
     
    388392    @Override
    389393    public int updateAnnotationBody(Number internalID, AnnotationBody annotationBody) {
    390         return annotationDao.updateAnnotationBody(internalID, annotationBody);
     394        String[] body = annotationDao.retrieveBodyComponents(annotationBody);
     395        return annotationDao.updateAnnotationBody(internalID, body[0], body[1], annotationBody.getXmlBody()!=null);
    391396    }
    392397   
     
    518523        String bodyText;
    519524        String newBodyText;
     525        String mimeType;
    520526        if (annotationBody.getXmlBody() != null) {
    521527            bodyText = Helpers.elementToString(annotation.getBody().getXmlBody().getAny());
     528            mimeType = annotationBody.getXmlBody().getMimeType();
    522529        } else {
    523530            if (annotation.getBody().getTextBody() != null) {
    524531                bodyText = annotation.getBody().getTextBody().getValue();
    525 
     532                mimeType = annotationBody.getTextBody().getMimeType();
    526533            } else {
    527534                logger.error("The client has sent ill-formed annotation body.");
     
    530537        }
    531538        newBodyText = Helpers.replace(bodyText, targetIdPairs);
    532         return annotationDao.updateAnnotationBodyText(annotationID, newBodyText);
     539        return annotationDao.updateAnnotationBody(annotationID, newBodyText, mimeType, annotationBody.getXmlBody() != null);
    533540    }
    534541
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/impl/JdbcAnnotationDao.java

    r4183 r4207  
    2626import eu.dasish.annotation.schema.AnnotationInfo;
    2727import eu.dasish.annotation.schema.Permission;
     28import java.io.IOException;
    2829import java.lang.String;
    2930import java.sql.ResultSet;
     
    3738import javax.sql.DataSource;
    3839import javax.xml.datatype.DatatypeConfigurationException;
     40import javax.xml.parsers.ParserConfigurationException;
    3941import org.springframework.jdbc.core.RowMapper;
     42import org.xml.sax.SAXException;
    4043
    4144/**
     
    115118
    116119    @Override
    117     public List<Number> getAnnotationIDsForUserWithPermission(Number userID, String permissionString) {
    118         if (userID == null || permissionString == null) {
    119             return null;
    120         }
     120    public List<Number> getAnnotationIDsForUserWithPermission(Number userID, String[] permissionStrings) {
     121        if (userID == null || permissionStrings == null) {
     122            return null;
     123        }
     124
     125        String values = stringsToValuesString(permissionStrings);
     126
    121127        StringBuilder sql = new StringBuilder("SELECT ");
    122128        sql.append(annotation_id).append(" FROM ").append(permissionsTableName).append(" WHERE ").
    123129                append(principal_id).append("  = ").append(userID.toString()).append(" AND ").
    124                 append(permission).append("  = ?");;
    125         return getSimpleJdbcTemplate().query(sql.toString(), internalIDRowMapper, permissionString);
     130                append(permission).append("  IN ").append(values);
     131        return getSimpleJdbcTemplate().query(sql.toString(), internalIDRowMapper);
     132    }
     133
     134    private String stringsToValuesString(String[] strings) {
     135        if (strings == null) {
     136            return null;
     137        }
     138
     139        int length = strings.length;
     140        if (length == 0) {
     141            return null;
     142        }
     143        String result = "(";
     144        for (int i = 0; i < length - 1; i++) {
     145            result = result + "'"+strings[i] + "', ";
     146        }
     147        result = result + "'"+strings[length - 1] + "')";
     148        return result;
    126149    }
    127150
     
    312335
    313336    //////////// UPDATERS /////////////
    314     @Override
    315     public int updateAnnotationBodyText(Number annotationID, String text) {
    316         StringBuilder sql = new StringBuilder("UPDATE ");
    317         sql.append(annotationTableName).append(" SET ").
    318                 append(last_modified).append("=  default,").
    319                 append(body_text).append("= '").append(text).
    320                 append("' WHERE ").append(annotation_id).append("= ?");
    321         int affectedRows = getSimpleJdbcTemplate().update(sql.toString(), annotationID);
    322         return affectedRows;
    323     }
    324 
    325     @Override
    326     public int updateAnnotationBody(Number annotationID, AnnotationBody annotationBody) {
    327         String[] body = retrieveBodyComponents(annotationBody);
     337   
     338
     339    @Override
     340    public int updateAnnotationBody(Number annotationID, String text, String mimeType, Boolean isXml) {
    328341        Map<String, Object> params = new HashMap<String, Object>();
    329         params.put("annotationID", annotation_id);
    330         params.put("bodyText", body[0]);
    331         params.put("bodyMimeType", body[1]);
    332         params.put("isXml", annotationBody.getXmlBody() != null);
     342        params.put("annotationID", annotationID);
     343        params.put("bodyText", text);
     344        params.put("bodyMimeType", mimeType);
     345        params.put("isXml", isXml);
    333346
    334347        StringBuilder sql = new StringBuilder("UPDATE ");
     
    338351                append(body_mimetype).append("= :bodyMimeType, ").
    339352                append(is_xml).append("= :isXml").
    340                 append("' WHERE ").append(annotation_id).append("= :annotationID");
    341         int affectedRows = getSimpleJdbcTemplate().update(sql.toString(), annotationID);
     353                append(" WHERE ").append(annotation_id).append("= :annotationID");
     354        int affectedRows = getSimpleJdbcTemplate().update(sql.toString(), params);
    342355        return affectedRows;
    343356    }
     
    374387        Map<String, Object> params = new HashMap<String, Object>();
    375388        params.put("permission", permission.value());
    376         params.put("annotationID", annotation_id);
    377         params.put("principalID", principal_id);
     389        params.put("annotationID", annotationID);
     390        params.put("principalID", userID);
    378391
    379392        StringBuilder sql = new StringBuilder("UPDATE ");
    380393        sql.append(permissionsTableName).append(" SET ").
    381394                append(this.permission).append("= :permission").
    382                 append(" WHERE ").append(annotation_id).append("= : annotationID").
     395                append(" WHERE ").append(annotation_id).append("= :annotationID").
    383396                append(" AND ").append(principal_id).append("= :principalID");
    384397        return getSimpleJdbcTemplate().update(sql.toString(), params);
     
    474487
    475488    /////////////// helpers //////////////////
    476     private String[] retrieveBodyComponents(AnnotationBody annotationBody) {
     489    public String[] retrieveBodyComponents(AnnotationBody annotationBody) {
    477490        boolean body_is_xml = annotationBody.getXmlBody() != null;
    478491        String[] result = new String[2];
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/AnnotationResource.java

    r4173 r4207  
    3434import java.io.IOException;
    3535import java.net.URI;
    36 import java.security.Principal;
    3736import java.sql.Timestamp;
    3837import java.util.ArrayList;
     
    5251import javax.ws.rs.core.Context;
    5352import javax.ws.rs.core.MediaType;
    54 import javax.ws.rs.core.SecurityContext;
    5553import javax.ws.rs.core.UriInfo;
    5654import javax.ws.rs.ext.Providers;
     
    6159import org.slf4j.Logger;
    6260import org.slf4j.LoggerFactory;
     61import org.springframework.transaction.annotation.Transactional;
    6362
    6463/**
     
    6867@Component
    6968@Path("/annotations")
     69@Transactional(rollbackFor = {Exception.class})
    7070public class AnnotationResource {
    7171
     
    110110    @Path("{annotationid: " + BackendConstants.regExpIdentifier + "}")
    111111    @Secured("ROLE_USER")
    112     public JAXBElement<Annotation> getAnnotation(@PathParam("annotationid") String ExternalIdentifier) {
     112    @Transactional(readOnly = true)
     113    public JAXBElement<Annotation> getAnnotation(@PathParam("annotationid") String ExternalIdentifier) throws IOException {
    113114        URI baseURI = uriInfo.getBaseUri();
    114115        String baseURIstr = baseURI.toString();
     
    123124            return rootElement;
    124125        } else {
    125             try {
    126                 logger.error("FORBIDDEN-access attempt");
    127                 httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN);
    128             } catch (IOException ioe) {
    129                 logger.error("IOException: Cannot send server respond about unaithorized access.");
    130             }
     126            httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN);
    131127            return null;
    132128        }
     
    139135    @Path("{annotationid: " + BackendConstants.regExpIdentifier + "}/targets")
    140136    @Secured("ROLE_USER")
    141     public JAXBElement<ReferenceList> getAnnotationTargets(@PathParam("annotationid") String ExternalIdentifier) {
     137    @Transactional(readOnly = true)
     138    public JAXBElement<ReferenceList> getAnnotationTargets(@PathParam("annotationid") String ExternalIdentifier) throws IOException {
    142139        dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
    143140        final Number annotationID = dbIntegrityService.getAnnotationInternalIdentifier(UUID.fromString(ExternalIdentifier));
     
    148145            return new ObjectFactory().createTargetList(TargetList);
    149146        } else {
    150             try {
    151                 logger.error("FORBIDDEN-access attempt");
    152                 httpServletResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED);
    153             } catch (IOException ioe) {
    154                 logger.error("IOException: Cannot send server respond about unaithorized access.");
    155             }
     147            httpServletResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED);
    156148            return null;
    157149        }
     
    163155    @Path("")
    164156    @Secured("ROLE_USER")
     157    @Transactional(readOnly = true)
    165158    public JAXBElement<AnnotationInfoList> getFilteredAnnotations(@QueryParam("link") String link,
    166159            @QueryParam("text") String text,
     
    175168        UUID ownerExternalUUID = (ownerExternalId != null) ? UUID.fromString(ownerExternalId) : null;
    176169        String access = (permission != null) ? permission : default_permission;
    177         final AnnotationInfoList annotationInfoList = dbIntegrityService.getFilteredAnnotationInfos(link, text, userID, access, namespace, ownerExternalUUID, after, before);
     170        final AnnotationInfoList annotationInfoList = dbIntegrityService.getFilteredAnnotationInfos(link, text, userID, makeAccessModeChain(access), namespace, ownerExternalUUID, after, before);
    178171        logger.info("getFilteredAnnotations method: OK");
    179172        return new ObjectFactory().createAnnotationInfoList(annotationInfoList);
     
    185178    @Path("{annotationid: " + BackendConstants.regExpIdentifier + "}/permissions")
    186179    @Secured("ROLE_USER")
    187     public JAXBElement<UserWithPermissionList> getAnnotationPermissions(@PathParam("annotationid") String ExternalIdentifier) {
     180    @Transactional(readOnly = true)
     181    public JAXBElement<UserWithPermissionList> getAnnotationPermissions(@PathParam("annotationid") String ExternalIdentifier) throws IOException {
    188182        dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
    189183        final Number annotationID = dbIntegrityService.getAnnotationInternalIdentifier(UUID.fromString(ExternalIdentifier));
     
    194188            return new ObjectFactory().createPermissionList(permissionList);
    195189        } else {
    196             try {
    197                 logger.error("FORBIDDEN-access attempt");
    198                 httpServletResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED);
    199             } catch (IOException ioe) {
    200                 logger.error("IOException: Cannot send server respond about unaithorized access.");
    201             }
     190            httpServletResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED);
    202191            return null;
    203192        }
     
    210199    @Path("{annotationid: " + BackendConstants.regExpIdentifier + "}")
    211200    @Secured("ROLE_USER")
    212     public String deleteAnnotation(@PathParam("annotationid") String externalIdentifier) {
     201    public String deleteAnnotation(@PathParam("annotationid") String externalIdentifier) throws IOException {
    213202        dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
    214203        final Number annotationID = dbIntegrityService.getAnnotationInternalIdentifier(UUID.fromString(externalIdentifier));
     
    220209            return result + " annotation(s) deleted.";
    221210        } else {
    222             logger.error("FORBIDDEN-access attempt. Only the owner can delete an annotation.");
    223             try {
    224                 httpServletResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED);
    225             } catch (IOException ioe) {
    226                 logger.error("IOException: Cannot send server respond about unaithorized access.");
    227             }
     211            httpServletResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED);
    228212            return null;
    229213        }
     
    237221    @Path("")
    238222    @Secured("ROLE_USER")
    239     public JAXBElement<ResponseBody> createAnnotation(Annotation annotation) {
     223    public JAXBElement<ResponseBody> createAnnotation(Annotation annotation) throws IOException {
    240224        dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
    241225        final Number userID = dbIntegrityService.getUserInternalIDFromRemoteID(httpServletRequest.getRemoteUser());
     
    252236    @Path("{annotationid: " + BackendConstants.regExpIdentifier + "}")
    253237    @Secured("ROLE_USER")
    254     public JAXBElement<ResponseBody> updateAnnotation(@PathParam("annotationid") String externalIdentifier, Annotation annotation) {
     238    public JAXBElement<ResponseBody> updateAnnotation(@PathParam("annotationid") String externalIdentifier, Annotation annotation) throws IOException {
    255239        String path = uriInfo.getBaseUri().toString();
    256240        dbIntegrityService.setServiceURI(path);
     
    269253
    270254        } else {
    271             logger.error("FORBIDDEN-access attempt.");
    272             logger.error("The logged-in user is not authorised to alter this annotation. ");
    273             try {
    274                 httpServletResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED);
    275             } catch (IOException ioe) {
    276                 logger.error("IOException: Cannot send server respond about unaithorized access.");
    277             }
    278             return null;
    279         }
    280 
    281     }
    282    
     255            httpServletResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED);
     256            return null;
     257        }
     258
     259    }
     260
    283261    @PUT
    284262    @Consumes(MediaType.APPLICATION_XML)
     
    286264    @Path("{annotationid: " + BackendConstants.regExpIdentifier + "}/body")
    287265    @Secured("ROLE_USER")
    288     public JAXBElement<ResponseBody> updateAnnotationBody(@PathParam("annotationid") String externalIdentifier, AnnotationBody annotationBody) {
     266    public JAXBElement<ResponseBody> updateAnnotationBody(@PathParam("annotationid") String externalIdentifier, AnnotationBody annotationBody) throws IOException {
    289267        String path = uriInfo.getBaseUri().toString();
    290268        dbIntegrityService.setServiceURI(path);
    291        
     269
    292270        final Number annotationID = dbIntegrityService.getAnnotationInternalIdentifier(UUID.fromString(externalIdentifier));
    293271        final Number userID = dbIntegrityService.getUserInternalIDFromRemoteID(httpServletRequest.getRemoteUser());
     
    296274            logger.info("updateAnnotationBody method: OK");
    297275            return new ObjectFactory().createResponseBody(makeAnnotationResponseEnvelope(annotationID));
    298 
    299         } else {
    300             logger.error("FORBIDDEN-access attempt.");
    301             logger.error("The logged-in user is not authorised to alter this annotation. ");
    302             try {
    303                 httpServletResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED);
    304             } catch (IOException ioe) {
    305                 logger.error("IOException: Cannot send server respond about unaithorized access.");
    306             }
     276        } else {
     277            httpServletResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED);
    307278            return null;
    308279        }
     
    315286    @Path("{annotationid: " + BackendConstants.regExpIdentifier + "}/permissions/{userid: " + BackendConstants.regExpIdentifier + "}")
    316287    @Secured("ROLE_USER")
    317     public String updatePermission(@PathParam("annotationid") String annotationExternalId, @PathParam("userid") String userExternalId, Permission permission) {
     288    public String updatePermission(@PathParam("annotationid") String annotationExternalId, @PathParam("userid") String userExternalId, Permission permission) throws IOException {
    318289        dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
    319290        final Number annotationID = dbIntegrityService.getAnnotationInternalIdentifier(UUID.fromString(annotationExternalId));
     
    328299
    329300        } else {
    330             logger.error("FORBIDDEN-access attempt");
    331             logger.error("The logged-in user is not authorised to alter this annotation. ");
    332             try {
    333                 httpServletResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED);
    334             } catch (IOException ioe) {
    335                 logger.error("IOException: Cannot send server respond about unaithorized access.");
    336             }
     301            httpServletResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED);
    337302            return null;
    338303        }
     
    344309    @Path("{annotationid: " + BackendConstants.regExpIdentifier + "}/permissions/")
    345310    @Secured("ROLE_USER")
    346     public JAXBElement<ResponseBody> updatePermissions(@PathParam("annotationid") String annotationExternalId, UserWithPermissionList permissions) {
     311    public JAXBElement<ResponseBody> updatePermissions(@PathParam("annotationid") String annotationExternalId, UserWithPermissionList permissions) throws IOException {
    347312        dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
    348313        final Number annotationID = dbIntegrityService.getAnnotationInternalIdentifier(UUID.fromString(annotationExternalId));
     
    353318            return new ObjectFactory().createResponseBody(makePermissionResponseEnvelope(annotationID));
    354319        } else {
    355             logger.error("FORBIDDEN-access attempt");
    356             logger.error("The logged-in user is not authorised to alter this annotation. ");
    357             try {
    358                 httpServletResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED);
    359             } catch (IOException ioe) {
    360                 logger.error("IOException: Cannot send server respond about unaithorized access.");
    361             }
     320            httpServletResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED);
    362321            return null;
    363322        }
     
    390349    }
    391350
     351    // REFACTOR : move to the integrity service all te methods below 
    392352    private List<Action> makeActionList(List<String> resourceURIs, String message) {
    393353        if (resourceURIs != null) {
     
    435395        }
    436396    }
     397
     398    private String[] makeAccessModeChain(String accessMode) {
     399        if (accessMode != null) {
     400            if (accessMode == Permission.OWNER.value()) {
     401                String[] result = new String[1];
     402                result[0] = accessMode;
     403                return result;
     404            } else {
     405                if (accessMode == Permission.WRITER.value()) {
     406                    String[] result = new String[2];
     407                    result[0] = Permission.WRITER.value();
     408                    result[1] = Permission.OWNER.value();
     409                    return result;
     410                } else {
     411                    if (accessMode == Permission.READER.value()) {
     412                        String[] result = new String[3];
     413                        result[0] = Permission.READER.value();
     414                        result[1] = Permission.WRITER.value();
     415                        result[2] = Permission.OWNER.value();
     416                        return result;
     417                    } else {
     418                        logger.error("Invalide access " + accessMode);
     419                        return null;
     420                    }
     421
     422                }
     423            }
     424
     425        } else {
     426            return null;
     427        }
     428    }
    437429}
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/CachedRepresentationResource.java

    r3949 r4207  
    3737import javax.ws.rs.core.UriInfo;
    3838import javax.xml.bind.JAXBElement;
     39import javax.xml.parsers.ParserConfigurationException;
    3940import org.springframework.beans.factory.annotation.Autowired;
    4041import org.springframework.security.access.annotation.Secured;
    4142import org.springframework.stereotype.Component;
     43import org.springframework.transaction.annotation.Transactional;
    4244
    4345/**
     
    4749@Component
    4850@Path("/cached")
     51@Transactional(rollbackFor={Exception.class, SQLException.class, IOException.class, ParserConfigurationException.class})
    4952public class CachedRepresentationResource {
    5053
     
    6568    @Produces(MediaType.TEXT_XML)
    6669    @Path("{cachedid: " + BackendConstants.regExpIdentifier + "}/metadata")
    67     @Secured("ROLE_USER")
     70    @Secured("ROLE_USER")   
     71    @Transactional(readOnly=true)
    6872    public JAXBElement<CachedRepresentationInfo> getCachedRepresentationInfo(@PathParam("cachedid") String externalId) throws SQLException {
    6973        dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
     
    7680    @Produces({"image/jpeg", "image/png"})
    7781    @Path("{cachedid: " + BackendConstants.regExpIdentifier + "}/content")
    78     @Secured("ROLE_USER")
     82    @Secured("ROLE_USER")   
     83    @Transactional(readOnly=true)
    7984    public BufferedImage getCachedRepresentationContent(@PathParam("cachedid") String externalId) throws SQLException, IOException {
    8085        dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/NotebookResource.java

    r3722 r4207  
    1919
    2020import eu.dasish.annotation.backend.dao.DBIntegrityService;
     21import java.io.IOException;
     22import java.sql.SQLException;
    2123import javax.servlet.http.HttpServletRequest;
    2224import javax.ws.rs.Path;
     
    2426import javax.ws.rs.core.UriInfo;
    2527import javax.ws.rs.ext.Providers;
     28import javax.xml.parsers.ParserConfigurationException;
    2629import org.springframework.beans.factory.annotation.Autowired;
    2730import org.springframework.stereotype.Component;
     31import org.springframework.transaction.annotation.Transactional;
    2832
    2933/**
     
    3438@Component
    3539@Path("/notebooks")
     40@Transactional(rollbackFor={Exception.class, SQLException.class, IOException.class, ParserConfigurationException.class})
    3641public class NotebookResource {
    3742
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/TargetResource.java

    r4028 r4207  
    2626import eu.dasish.annotation.schema.ReferenceList;
    2727import eu.dasish.annotation.schema.Target;
     28import java.io.IOException;
    2829import java.io.InputStream;
    2930import java.sql.SQLException;
     
    4142import javax.ws.rs.core.UriInfo;
    4243import javax.xml.bind.JAXBElement;
     44import javax.xml.parsers.ParserConfigurationException;
    4345import org.springframework.beans.factory.annotation.Autowired;
    4446import org.springframework.security.access.annotation.Secured;
    4547import org.springframework.stereotype.Component;
     48import org.springframework.transaction.annotation.Transactional;
    4649
    4750/**
     
    5558@Component
    5659@Path("/targets")
     60@Transactional(rollbackFor={Exception.class, SQLException.class, IOException.class, ParserConfigurationException.class})
    5761public class TargetResource {
    5862
     
    7579    @Produces(MediaType.TEXT_XML)
    7680    @Path("{targetid: " + BackendConstants.regExpIdentifier + "}")
    77     @Secured("ROLE_USER")
     81    @Secured("ROLE_USER")   
     82    @Transactional(readOnly=true)
    7883    public JAXBElement<Target> getTarget(@PathParam("targetid") String ExternalIdentifier) throws SQLException {
    7984        dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
     
    8792    @Produces(MediaType.TEXT_XML)
    8893    @Path("{targetid: " + BackendConstants.regExpIdentifier + "}/versions")
    89     @Secured("ROLE_USER")
     94    @Secured("ROLE_USER")   
     95    @Transactional(readOnly=true)
    9096    public JAXBElement<ReferenceList> getSiblingTargets(@PathParam("targetid") String ExternalIdentifier) throws SQLException {
    9197        dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/UserResource.java

    r4010 r4207  
    2323import eu.dasish.annotation.schema.ObjectFactory;
    2424import eu.dasish.annotation.schema.User;
     25import java.io.IOException;
    2526import java.sql.SQLException;
    2627import java.util.UUID;
     
    3839import javax.ws.rs.core.UriInfo;
    3940import javax.xml.bind.JAXBElement;
     41import javax.xml.parsers.ParserConfigurationException;
    4042import org.springframework.beans.factory.annotation.Autowired;
    4143import org.springframework.security.access.annotation.Secured;
    4244import org.springframework.stereotype.Component;
     45import org.springframework.transaction.annotation.Transactional;
    4346
    4447/**
     
    4851@Component
    4952@Path("/users")
     53@Transactional(rollbackFor={Exception.class, SQLException.class, IOException.class, ParserConfigurationException.class})
    5054public class UserResource {
    5155    @Autowired
     
    6670    @Produces(MediaType.TEXT_XML)
    6771    @Path("{userid: " + BackendConstants.regExpIdentifier + "}")
    68     @Secured("ROLE_USER")
     72    @Secured("ROLE_USER")   
     73    @Transactional(readOnly=true)
    6974    public JAXBElement<User> getUser(@PathParam("userid") String ExternalIdentifier) throws SQLException {
    7075         dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
     
    7782    @Produces(MediaType.TEXT_XML)
    7883    @Path("/info")
    79     @Secured("ROLE_USER")
     84    @Secured("ROLE_USER")   
     85    @Transactional(readOnly=true)
    8086    public JAXBElement<User> getUserByInfo(@QueryParam("email") String email) throws SQLException {
    8187        dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
     
    8793    @Produces(MediaType.TEXT_XML)
    8894    @Path("{userid: " + BackendConstants.regExpIdentifier + "}/current")
    89     @Secured("ROLE_USER")
     95    @Secured("ROLE_USER")   
     96    @Transactional(readOnly=true)
    9097    public JAXBElement<CurrentUserInfo> getCurrentUserInfo(@PathParam("userid") String ExternalIdentifier){
    9198        dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/test/java/eu/dasish/annotation/backend/dao/impl/DBIntegrityServiceTest.java

    r4173 r4207  
    295295        mockAnnotationIDs1.add(3);
    296296        mockAnnotationIDs1.add(4);
     297        mockAnnotationIDs1.add(5);
    297298       
    298299        final List<Number> mockAnnotationIDs2 = new ArrayList<Number>();
     
    300301        mockAnnotationIDs2.add(3);
    301302
    302         final String text = "some html";
     303       
    303304        final UUID owner = UUID.fromString(TestBackendConstants._TEST_USER_4_EXT_ID);
    304305        final Timestamp after = new Timestamp(0);
     
    306307
    307308        final List<Number> mockRetval = new ArrayList<Number>();
    308         mockRetval.add(2);
    309        
    310        
     309        mockRetval.add(3);
     310       
     311        final String[] accessModes = new String[2];
     312        accessModes[0] = "reader";
     313        accessModes[1] = "writer";
    311314
    312315        mockeryDao.checking(new Expectations() {
     
    315318                will(returnValue(mockTargetIDs));
    316319
    317                 oneOf(annotationDao).getAnnotationIDsForUserWithPermission(3, "reader");
     320                oneOf(annotationDao).getAnnotationIDsForUserWithPermission(3, accessModes);
    318321                will(returnValue(mockAnnotationIDs1));
    319322
     
    326329               
    327330               
    328                 oneOf(annotationDao).getFilteredAnnotationIDs(mockAnnotationIDs1, text, null, 4, after, before);
     331                oneOf(annotationDao).getFilteredAnnotationIDs(mockAnnotationIDs1, "some html", null, 4, after, before);
    329332                will(returnValue(mockRetval));
    330333
     
    333336
    334337
    335         List result = dbIntegrityService.getFilteredAnnotationIDs(word, text, 3, "reader", null, owner, after, before);
     338        List result = dbIntegrityService.getFilteredAnnotationIDs(word, "some html", 3, accessModes, null, owner, after, before);
    336339        assertEquals(1, result.size());
    337         assertEquals(2, result.get(0));
     340        assertEquals(3, result.get(0));
    338341    }
    339342
     
    388391        mockTargetIDs.add(2);
    389392
    390          final List<Number> mockAnnotationIDs1 = new ArrayList<Number>();
     393        final List<Number> mockAnnotationIDs1 = new ArrayList<Number>();
    391394        mockAnnotationIDs1.add(3);
    392395        mockAnnotationIDs1.add(4);
     396        mockAnnotationIDs1.add(5);
    393397       
    394398        final List<Number> mockAnnotationIDs2 = new ArrayList<Number>();
     
    403407
    404408        final List<Number> mockAnnotIDs = new ArrayList<Number>();
    405         mockAnnotIDs.add(2);
     409        mockAnnotIDs.add(3);
    406410       
    407411        final AnnotationInfo mockAnnotInfo = new AnnotationInfo();
    408412       
    409         mockAnnotInfo.setHeadline(TestBackendConstants._TEST_ANNOT_2_HEADLINE);       
    410         mockAnnotInfo.setRef(TestBackendConstants._TEST_SERVLET_URI_annotations + TestBackendConstants._TEST_ANNOT_2_EXT);
     413        mockAnnotInfo.setHeadline(TestBackendConstants._TEST_ANNOT_3_HEADLINE);       
     414        mockAnnotInfo.setRef(TestBackendConstants._TEST_SERVLET_URI_annotations + TestBackendConstants._TEST_ANNOT_3_EXT);
    411415       
    412416        final List<Number> TargetIDs = new ArrayList<Number>();
    413         TargetIDs.add(1);
    414417        TargetIDs.add(2);
    415418       
    416         final Map<AnnotationInfo,Number> mockPair = new HashMap<AnnotationInfo, Number>();
    417         mockPair.put(mockAnnotInfo, 3);
    418        
    419         mockeryDao.checking(new Expectations() {
    420             {
    421                 oneOf(annotationDao).getAnnotationIDsForUserWithPermission(3, "reader");
     419        final Map<AnnotationInfo,Number> mockPair = new HashMap<AnnotationInfo, Number>(); // annotationInfo-ownerID
     420        mockPair.put(mockAnnotInfo, 4);
     421       
     422        final String[] accessModes = new String[2];
     423        accessModes[0] = "reader";
     424        accessModes[1] = "writer";
     425       
     426        mockeryDao.checking(new Expectations() {
     427            {
     428                oneOf(annotationDao).getAnnotationIDsForUserWithPermission(3, accessModes);
    422429                will(returnValue(mockAnnotationIDs1));
    423430
     
    439446//                ///////////////////////////////////
    440447//               
    441                 oneOf(annotationDao).getAnnotationInfoWithoutTargets(2);
     448                oneOf(annotationDao).getAnnotationInfoWithoutTargets(3);
    442449                will(returnValue(mockPair));
    443450               
    444451                ////
    445                 oneOf(annotationDao).retrieveTargetIDs(2);
     452                oneOf(annotationDao).retrieveTargetIDs(3);
    446453                will(returnValue(TargetIDs));
    447454               
    448                 oneOf(targetDao).getURIFromInternalID(1);
    449                 will(returnValue(TestBackendConstants._TEST_SERVLET_URI_Targets +TestBackendConstants._TEST_Target_1_EXT_ID));
    450455               
    451456                oneOf(targetDao).getURIFromInternalID(2);
     
    453458                ////
    454459               
    455                 oneOf(userDao).getURIFromInternalID(3);
    456                 will(returnValue(TestBackendConstants._TEST_SERVLET_URI_users +TestBackendConstants._TEST_USER_3_EXT_ID));
     460                oneOf(userDao).getURIFromInternalID(4);
     461                will(returnValue(TestBackendConstants._TEST_SERVLET_URI_users +TestBackendConstants._TEST_USER_4_EXT_ID));
    457462             
    458463               
     
    461466       
    462467     
    463         AnnotationInfoList result = dbIntegrityService.getFilteredAnnotationInfos(word, text, 3, "reader", null, ownerUUID, after, before);
     468        AnnotationInfoList result = dbIntegrityService.getFilteredAnnotationInfos(word, text, 3, accessModes, null, ownerUUID, after, before);
    464469        assertEquals(1, result.getAnnotationInfo().size());
    465470        AnnotationInfo resultAnnotInfo = result.getAnnotationInfo().get(0);
    466471        assertEquals(mockAnnotInfo.getHeadline(), resultAnnotInfo.getHeadline());
    467         assertEquals(TestBackendConstants._TEST_SERVLET_URI_users +TestBackendConstants._TEST_USER_3_EXT_ID, resultAnnotInfo.getOwnerRef());
     472        assertEquals(TestBackendConstants._TEST_SERVLET_URI_users +TestBackendConstants._TEST_USER_4_EXT_ID, resultAnnotInfo.getOwnerRef());
    468473        assertEquals(mockAnnotInfo.getRef(),result.getAnnotationInfo().get(0).getRef() );
    469         assertEquals(TestBackendConstants._TEST_SERVLET_URI_Targets +TestBackendConstants._TEST_Target_1_EXT_ID, resultAnnotInfo.getTargets().getRef().get(0));
    470         assertEquals(TestBackendConstants._TEST_SERVLET_URI_Targets +TestBackendConstants._TEST_Target_2_EXT_ID, resultAnnotInfo.getTargets().getRef().get(1));
     474        assertEquals(TestBackendConstants._TEST_SERVLET_URI_Targets +TestBackendConstants._TEST_Target_2_EXT_ID, resultAnnotInfo.getTargets().getRef().get(0));
    471475         
    472476    }
     
    690694                ///////////
    691695
    692                 oneOf(annotationDao).updateAnnotationBodyText(6, testAnnotation.getBody().getTextBody().getValue());
     696                oneOf(annotationDao).updateAnnotationBody(6, testAnnotation.getBody().getTextBody().getValue(), testAnnotation.getBody().getTextBody().getMimeType(), false);
    693697                will(returnValue(1)); // the DB update will be called at perform anyway, even if the body is not changed (can be optimized)
    694698
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/test/java/eu/dasish/annotation/backend/dao/impl/JdbcAnnotationDaoTest.java

    r4173 r4207  
    376376   
    377377    //////////////////////////////////
    378     @Test
    379     public void testUpdateBodyText() throws SQLException{
    380         System.out.println("test updateBodyText");
    381         String newBodyText = "new body";
    382         int result = jdbcAnnotationDao.updateAnnotationBodyText(2, newBodyText);
    383         assertEquals(1, result);
    384         Map<Annotation,Number> getResult= jdbcAnnotationDao.getAnnotationWithoutTargetsAndPermissions(2);
    385         Annotation[] annotations = new Annotation[1];
    386         getResult.keySet().toArray(annotations);
    387         assertEquals(newBodyText, annotations[0].getBody().getTextBody().getValue());
    388         System.out.println("update time "+annotations[0].getLastModified());
    389     }
    390 
     378   
    391379 
    392380   
Note: See TracChangeset for help on using the changeset viewer.