Changeset 4262


Ignore:
Timestamp:
01/14/14 17:52:21 (10 years ago)
Author:
olhsha
Message:

Refactoring:duplication of info in the Db is removed (owner_id in the annotations, and annotation_principal_permission). GET works. PUT needs debugging, POSt needs testing.

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

Legend:

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

    r4207 r4262  
    4949     *
    5050     */
    51     public Map<Annotation, Number> getAnnotationWithoutTargetsAndPermissions(Number annotationID);
     51    public Annotation getAnnotationWithoutTargetsAndPermissions(Number annotationID);
    5252   
    5353   
     
    6666     *
    6767     */
    68     public List<Number> getFilteredAnnotationIDs(List<Number> annotationIDs, String text, String namespace, Number ownerID, Timestamp after, Timestamp before);
     68    public List<Number> getFilteredAnnotationIDs(List<Number> annotationIDs, String text, String namespace, Timestamp after, Timestamp before);
    6969     
    7070       /**
    7171     * unit test is missing
    7272     * @param annotationIDs
    73      * @return pair <annotationInfo, owner_id>  for the annotation with the internal annotationID.
    74      *
    75      */
    76     public Map<AnnotationInfo, Number> getAnnotationInfoWithoutTargets(Number annotationID);   
     73     * @return annotationInfo  for the annotation with the internal annotationID.
     74     *
     75     */
     76    public AnnotationInfo getAnnotationInfoWithoutTargets(Number annotationID);   
    7777   
    7878    /**
     
    105105     */
    106106    public List<Map<Number, String>>  getPermissions(Number annotationID);
     107   
     108    public Number  getOwner(Number annotationID);
    107109   
    108110    /**
     
    159161     **/
    160162   
    161     public Number addAnnotation(Annotation annotation, Number ownerID);
     163    public Number addAnnotation(Annotation annotation);
    162164 
    163165     
     
    172174     *
    173175     * @param annotation
    174      * @param ownerID
    175176     * @return # of updated rows in "annotation" table after updating the annotation. Should return 1 if update  happens
    176177     * @throws SQLException
    177178     */
    178     public int updateAnnotation(Annotation annotation, Number ownerID);
     179    public int updateAnnotation(Annotation annotation);
    179180   
    180181   
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/DBIntegrityService.java

    r4252 r4262  
    7777    * @param access
    7878    * @param namespace
    79     * @param owner
    8079    * @param after
    8180    * @param before
     
    8887    * -- created after time-samp "after and before time-stamp "before".
    8988    */
    90     List<Number> getFilteredAnnotationIDs(String link, String text, Number inloggedUserID, String[] accessModes, String namespace, UUID
    91             owner, Timestamp after, Timestamp before);
     89    List<Number> getFilteredAnnotationIDs(String link, String text, Number inloggedUserID, String[] accessModes, String namespace, Timestamp after, Timestamp before);
    9290   
    9391   
     
    145143    String getTargetURI(Number targetID);
    146144   
    147      UUID getTargetExternalIdentifier(Number targetID);
     145    UUID getTargetExternalIdentifier(Number targetID);
    148146     
    149147    String getUserURI(Number userID);
     
    340338    public int deleteUser(Number userID);
    341339   
     340     /**
     341     *
     342     * @param userID
     343     * @return # of affected rows in the table "principal".
     344     * It is 1 if the userId is found and deleted;
     345     * it is 0 if it is not found or not deleted, e.g. because it is in use in the table "annotationsPreincipalsPermissions"
     346     */
     347    public int deleteUserSafe(Number userID);
     348   
    342349    /**
    343350     *
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/impl/DBIntegrityServiceImlp.java

    r4260 r4262  
    141141    public Annotation getAnnotation(Number annotationID) {
    142142        if (annotationID != null) {
    143             Map<Annotation, Number> annotationOwner = annotationDao.getAnnotationWithoutTargetsAndPermissions(annotationID);
    144 
    145             Annotation[] annotations = new Annotation[1];
    146             annotationOwner.keySet().toArray(annotations);
    147             Annotation result = annotations[0];
    148             result.setOwnerRef(userDao.getURIFromInternalID(annotationOwner.get(result)));
    149 
     143            Annotation result = annotationDao.getAnnotationWithoutTargetsAndPermissions(annotationID);
     144            result.setOwnerRef(userDao.getURIFromInternalID(annotationDao.getOwner(annotationID)));
    150145            List<Number> targetIDs = annotationDao.retrieveTargetIDs(annotationID);
    151146            TargetInfoList sis = new TargetInfoList();
     
    191186    ////////////////////////////////////////////////////////////////////////
    192187    @Override
    193     public List<Number> getFilteredAnnotationIDs(String link, String text, Number inloggedUserID, String[] accessModes, String namespace, UUID owner, Timestamp after, Timestamp before) {
     188    public List<Number> getFilteredAnnotationIDs(String link, String text, Number inloggedUserID, String[] accessModes, String namespace, Timestamp after, Timestamp before) {
    194189
    195190        if (accessModes == null) {
     
    205200        }
    206201
    207         return annotationDao.getFilteredAnnotationIDs(annotationIDs, text, namespace, userDao.getInternalID(owner), after, before);
     202        return annotationDao.getFilteredAnnotationIDs(annotationIDs, text, namespace, after, before);
    208203    }
    209204
     
    261256    @Override
    262257    public AnnotationInfoList getFilteredAnnotationInfos(String word, String text, Number inloggedUserID, String[] accessModes, String namespace, UUID owner, Timestamp after, Timestamp before) {
    263         List<Number> annotationIDs = getFilteredAnnotationIDs(word, text, inloggedUserID, accessModes, namespace, owner, after, before);
     258        List<Number> annotationIDs = getFilteredAnnotationIDs(word, text, inloggedUserID, accessModes, namespace, after, before);
     259        Number givenOwnerID = userDao.getInternalID(owner);
    264260        if (annotationIDs != null) {
    265261            AnnotationInfoList result = new AnnotationInfoList();
    266262            for (Number annotationID : annotationIDs) {
    267                 Map<AnnotationInfo, Number> annotationInfoOwnerId = annotationDao.getAnnotationInfoWithoutTargets(annotationID);
    268                 ReferenceList targets = getAnnotationTargets(annotationID);
    269                 AnnotationInfo[] annotationInfos = new AnnotationInfo[1];
    270                 annotationInfoOwnerId.keySet().toArray(annotationInfos);
    271                 AnnotationInfo annotationInfo = annotationInfos[0];
    272                 annotationInfo.setTargets(targets);
    273                 annotationInfo.setOwnerRef(userDao.getURIFromInternalID(annotationInfoOwnerId.get(annotationInfo)));
    274                 result.getAnnotationInfo().add(annotationInfo);
     263                Number ownerID = annotationDao.getOwner(annotationID);
     264                if ((owner == null) || ownerID.equals(givenOwnerID)) {
     265                    ReferenceList targets = getAnnotationTargets(annotationID);
     266                    AnnotationInfo annotationInfo = annotationDao.getAnnotationInfoWithoutTargets(annotationID);
     267                    annotationInfo.setTargets(targets);
     268                    annotationInfo.setOwnerRef(userDao.getURIFromInternalID(ownerID));
     269                    result.getAnnotationInfo().add(annotationInfo);
     270                }
    275271            }
    276272
     
    384380    @Override
    385381    public int updateUsersAnnotation(Number userID, Annotation annotation) {
    386         int updatedAnnotations = annotationDao.updateAnnotation(annotation, userID);
     382        int updatedAnnotations = annotationDao.updateAnnotation(annotation);
    387383        Number annotationID = annotationDao.getInternalIDFromURI(annotation.getURI());
    388384        int deletedTargets = annotationDao.deleteAllAnnotationTarget(annotationID);
    389385        int deletedPrinsipalsPermissions = annotationDao.deleteAnnotationPrincipalPermissions(annotationID);
    390386        int addedTargets = addTargets(annotation, annotationID);
    391         int addedPrincipalsPermissions = addPrincipalsPermissions(annotation, annotationID);
     387        int addedPrincipalsPermissions = addPrincipalsPermissions(annotation.getPermissions().getUserWithPermission(), annotationID);
     388        int addedOwner = annotationDao.addAnnotationPrincipalPermission(annotationID, userID, Permission.OWNER);
    392389        return updatedAnnotations;
    393390    }
     
    435432    @Override
    436433    public Number addUsersAnnotation(Number userID, Annotation annotation) {
    437         Number annotationID = annotationDao.addAnnotation(annotation, userID);
     434        Number annotationID = annotationDao.addAnnotation(annotation);
    438435        int affectedAnnotRows = addTargets(annotation, annotationID);
    439436        int affectedPermissions = annotationDao.addAnnotationPrincipalPermission(annotationID, userID, Permission.OWNER);
     
    464461    public int deleteUser(Number userID) {
    465462        return userDao.deleteUser(userID);
     463    }
     464
     465    ////////////// DELETERS //////////////////
     466    @Override
     467    public int deleteUserSafe(Number userID) {
     468        return userDao.deleteUserSafe(userID);
    466469    }
    467470
     
    542545    }
    543546
    544     private int addPrincipalsPermissions(Annotation annotation, Number annotationID) {
    545         List<UserWithPermission> permissions = annotation.getPermissions().getUserWithPermission();
     547    private int addPrincipalsPermissions(List<UserWithPermission> permissions, Number annotationID) {
    546548        int addedPermissions = 0;
    547549        for (UserWithPermission permission : permissions) {
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/impl/JdbcAnnotationDao.java

    r4207 r4262  
    2626import eu.dasish.annotation.schema.AnnotationInfo;
    2727import eu.dasish.annotation.schema.Permission;
    28 import java.io.IOException;
    2928import java.lang.String;
    3029import java.sql.ResultSet;
     
    3736import java.util.UUID;
    3837import javax.sql.DataSource;
    39 import javax.xml.datatype.DatatypeConfigurationException;
    40 import javax.xml.parsers.ParserConfigurationException;
    4138import org.springframework.jdbc.core.RowMapper;
    42 import org.xml.sax.SAXException;
    4339
    4440/**
     
    143139        String result = "(";
    144140        for (int i = 0; i < length - 1; i++) {
    145             result = result + "'"+strings[i] + "', ";
    146         }
    147         result = result + "'"+strings[length - 1] + "')";
     141            result = result + "'" + strings[i] + "', ";
     142        }
     143        result = result + "'" + strings[length - 1] + "')";
    148144        return result;
    149145    }
     
    151147    ////////////////////////////////////////////////////////////////////////
    152148    @Override
    153     public List<Number> getFilteredAnnotationIDs(List<Number> annotationIDs, String text, String namespace, Number ownerID, Timestamp after, Timestamp before) {
     149    public List<Number> getFilteredAnnotationIDs(List<Number> annotationIDs, String text, String namespace, Timestamp after, Timestamp before) {
    154150
    155151        StringBuilder sql = new StringBuilder("SELECT DISTINCT ");
     
    168164        sql.append(" AND ").append(annotation_id).append(" IN ").append(values);
    169165
    170 
    171         if (ownerID != null) {
    172             sql.append(" AND ").append(owner_id).append(" = :owner ");
    173             params.put("owner", ownerID);
    174         }
    175 
     166 
    176167        if (after != null) {
    177168            sql.append(" AND ").append(last_modified).append("  > :after");
     
    208199
    209200    @Override
    210     public Map<AnnotationInfo, Number> getAnnotationInfoWithoutTargets(Number annotationID) {
     201    public AnnotationInfo getAnnotationInfoWithoutTargets(Number annotationID) {
    211202        if (annotationID == null) {
    212203            return null;
     
    214205        StringBuilder sql = new StringBuilder("SELECT  ");
    215206        sql.append(annotationStar).append(" FROM ").append(annotationTableName).append(" WHERE ").append(annotation_id).append("  = ? ");
    216         List<Map<AnnotationInfo, Number>> result = getSimpleJdbcTemplate().query(sql.toString(), annotationInfoRowMapper, annotationID);
     207        List<AnnotationInfo> result = getSimpleJdbcTemplate().query(sql.toString(), annotationInfoRowMapper, annotationID);
    217208        if (result == null) {
    218209            return null;
     
    224215        return result.get(0);
    225216    }
    226     private final RowMapper<Map<AnnotationInfo, Number>> annotationInfoRowMapper = new RowMapper<Map<AnnotationInfo, Number>>() {
     217    private final RowMapper<AnnotationInfo> annotationInfoRowMapper = new RowMapper<AnnotationInfo>() {
    227218        @Override
    228         public Map<AnnotationInfo, Number> mapRow(ResultSet rs, int rowNumber) throws SQLException {
    229             Map<AnnotationInfo, Number> result = new HashMap<AnnotationInfo, Number>();
     219        public AnnotationInfo mapRow(ResultSet rs, int rowNumber) throws SQLException {
    230220            AnnotationInfo annotationInfo = new AnnotationInfo();
    231221            annotationInfo.setRef(externalIDtoURI(rs.getString(external_id)));
    232222            annotationInfo.setHeadline(rs.getString(headline));
    233223            annotationInfo.setLastModified(timeStampToXMLGregorianCalendar(rs));
    234             result.put(annotationInfo, rs.getInt(owner_id));
    235             return result;
     224            return annotationInfo;
    236225        }
    237226    };
     
    269258    //////////////////////////////////////////////////////////////////////////
    270259    @Override
    271     public Map<Annotation, Number> getAnnotationWithoutTargetsAndPermissions(Number annotationID) {
     260    public Annotation getAnnotationWithoutTargetsAndPermissions(Number annotationID) {
    272261        if (annotationID == null) {
    273262            return null;
     
    275264        StringBuilder sql = new StringBuilder("SELECT ");
    276265        sql.append(annotationStar).append(" FROM ").append(annotationTableName).append(" WHERE ").append(annotationAnnotation_id).append("= ? LIMIT  1");
    277         List<Map<Annotation, Number>> respond = getSimpleJdbcTemplate().query(sql.toString(), annotationRowMapper, annotationID);
     266        List<Annotation> respond = getSimpleJdbcTemplate().query(sql.toString(), annotationRowMapper, annotationID);
    278267        return (respond.isEmpty() ? null : respond.get(0));
    279268    }
    280     private final RowMapper<Map<Annotation, Number>> annotationRowMapper = new RowMapper<Map<Annotation, Number>>() {
     269    private final RowMapper<Annotation> annotationRowMapper = new RowMapper<Annotation>() {
    281270        @Override
    282         public Map<Annotation, Number> mapRow(ResultSet rs, int rowNumber) throws SQLException {
    283             Map<Annotation, Number> result = new HashMap<Annotation, Number>();
    284 
     271        public Annotation mapRow(ResultSet rs, int rowNumber) throws SQLException {
    285272            Annotation annotation = new Annotation();
    286             result.put(annotation, rs.getInt(owner_id));
    287 
    288273            annotation.setHeadline(rs.getString(headline));
    289 
    290274            AnnotationBody body = new AnnotationBody();
    291275            if (rs.getBoolean(is_xml)) {
     
    307291            annotation.setURI(externalIDtoURI(rs.getString(external_id)));
    308292            annotation.setLastModified(timeStampToXMLGregorianCalendar(rs));
    309             return result;
     293            return annotation;
    310294        }
    311295    };
    312 
     296   
     297    @Override
     298    public Number  getOwner(Number annotationID){
     299        if (annotationID == null) {
     300            return null;
     301        }
     302        StringBuilder sql = new StringBuilder("SELECT ");
     303        sql.append(principal_id).append(" FROM ").append(permissionsTableName).append(" WHERE ").
     304                append(permission).append("= 'owner' AND ").
     305                append(annotation_id).append("= ? LIMIT  1");
     306        List<Number> respond = getSimpleJdbcTemplate().query(sql.toString(), principalIDRowMapper, annotationID);
     307        return (respond.isEmpty() ? null : respond.get(0));
     308    }
     309       
    313310    /////////////////////////////
    314311    @Override
     
    335332
    336333    //////////// UPDATERS /////////////
    337    
    338 
    339334    @Override
    340335    public int updateAnnotationBody(Number annotationID, String text, String mimeType, Boolean isXml) {
     
    358353    // TODO Unit test
    359354    @Override
    360     public int updateAnnotation(Annotation annotation, Number ownerID) {
     355    public int updateAnnotation(Annotation annotation) {
    361356
    362357        String[] body = retrieveBodyComponents(annotation.getBody());
     
    386381
    387382        Map<String, Object> params = new HashMap<String, Object>();
    388         params.put("permission", permission.value());
     383
    389384        params.put("annotationID", annotationID);
    390385        params.put("principalID", userID);
    391386
    392         StringBuilder sql = new StringBuilder("UPDATE ");
    393         sql.append(permissionsTableName).append(" SET ").
    394                 append(this.permission).append("= :permission").
    395                 append(" WHERE ").append(annotation_id).append("= :annotationID").
    396                 append(" AND ").append(principal_id).append("= :principalID");
    397         return getSimpleJdbcTemplate().update(sql.toString(), params);
     387        if (permission != null) {
     388            params.put("permission", permission.value());
     389            StringBuilder sql = new StringBuilder("UPDATE ");
     390            sql.append(permissionsTableName).append(" SET ").
     391                    append(this.permission).append("= :permission").
     392                    append(" WHERE ").append(annotation_id).append("= :annotationID").
     393                    append(" AND ").append(principal_id).append("= :principalID");
     394            return getSimpleJdbcTemplate().update(sql.toString(), params);
     395        } else {
     396           StringBuilder sql = new StringBuilder("DELETE FROM ");
     397           sql.append(permissionsTableName).append(" WHERE ").append(annotation_id).append("= :annotationID").
     398                    append(" AND ").append(principal_id).append("= :principalID");
     399           return (getSimpleJdbcTemplate().update(sql.toString(), params));
     400        }
     401
    398402    }
    399403
    400404    //////////// ADDERS ////////////////////////
    401405    @Override
    402     public Number addAnnotation(Annotation annotation, Number ownerID) {
     406    public Number addAnnotation(Annotation annotation) {
    403407
    404408        String[] body = retrieveBodyComponents(annotation.getBody());
     
    408412        Map<String, Object> params = new HashMap<String, Object>();
    409413        params.put("externalId", externalID.toString());
    410         params.put("ownerId", ownerID);
    411414        params.put("headline", annotation.getHeadline());
    412415        params.put("bodyText", body[0]);
     
    415418
    416419        StringBuilder sql = new StringBuilder("INSERT INTO ");
    417         sql.append(annotationTableName).append("(").append(external_id).append(",").append(owner_id);
     420        sql.append(annotationTableName).append("(").append(external_id);
    418421        sql.append(",").append(headline).append(",").append(body_text).append(",").append(body_mimetype).append(",").append(is_xml).
    419                 append(" ) VALUES (:externalId, :ownerId, :headline, :bodyText, :bodyMimeType, :isXml)");
     422                append(" ) VALUES (:externalId, :headline, :bodyText, :bodyMimeType, :isXml)");
    420423        int affectedRows = getSimpleJdbcTemplate().update(sql.toString(), params);
    421424        return ((affectedRows > 0) ? getInternalID(externalID) : null);
     
    487490
    488491    /////////////// helpers //////////////////
     492    @Override
    489493    public String[] retrieveBodyComponents(AnnotationBody annotationBody) {
    490494        boolean body_is_xml = annotationBody.getXmlBody() != null;
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/impl/JdbcNotebookDao.java

    r4146 r4262  
    7878    @Override
    7979    public List<Notebook> getUsersNotebooks(UUID userID) {
    80         String sql = "SELECT " + notebookStar + " FROM " + notebookTableName + ", " + principalTableName + " where " + principal_id + " = " + owner_id + " and " + principalExternal_id + " = ?";
     80        String sql = "SELECT " + notebookStar + " FROM " + notebookTableName + ", " + principalTableName + " where " + principal_id + " = " + notebookOwner_id + " and " + principalExternal_id + " = ?";
    8181        return getSimpleJdbcTemplate().query(sql, notebookRowMapper, userID.toString());
    8282    }
     
    106106        try {
    107107            final UUID externalIdentifier = UUID.randomUUID();
    108             String sql = "INSERT INTO " + notebookTableName + " (" + external_id + ", " + this.title + "," + owner_id + ") VALUES (:notebookId, :title, (SELECT " + principal_id + " FROM " + principalTableName + " WHERE " + principalExternal_id + " = :userID))";
     108            String sql = "INSERT INTO " + notebookTableName + " (" + external_id + ", " + this.title + "," + notebookOwner_id + ") VALUES (:notebookId, :title, (SELECT " + principal_id + " FROM " + principalTableName + " WHERE " + principalExternal_id + " = :userID))";
    109109            Map<String, Object> params = new HashMap<String, Object>();
    110110            params.put("notebookId", externalIdentifier.toString());
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/impl/JdbcResourceDao.java

    r4252 r4262  
    5555    final static protected String target_id = "target_id";
    5656    final static protected String external_id = "external_id";
    57     final static protected String owner_id = "owner_id";
    5857    final static protected String headline = "headline";
    5958    final static protected String body_text = "body_text";
     
    6160    final static protected String title = "title";
    6261    final static protected String principal_id = "principal_id";
     62    final static protected String notebookOwner_id = "owner_id";
    6363    final static protected String last_modified = "last_modified";
    6464    final static protected String version = "version";
     
    8585    final static protected String notebookTitle = notebookTableName + "." + title;
    8686    final static protected String notebookExternal_id = notebookTableName + "." + external_id;
    87     final static protected String notebookOwner_id = notebookTableName + "." + owner_id;
    8887    final static protected String notebooksAnnotationsTableNameAnnotation_id = notebooksAnnotationsTableName + "." + annotation_id;
    8988    final static protected String principalPrincipal_id = principalTableName + "." + principal_id;
     
    200199        }
    201200    };
    202     protected final RowMapper<Number> ownerIDRowMapper = new RowMapper<Number>() {
    203         @Override
    204         public Number mapRow(ResultSet rs, int rowNumber) throws SQLException {
    205             return rs.getInt(owner_id);
     201   
     202    protected final RowMapper<Number> notebookOwnerIDRowMapper = new RowMapper<Number>() {
     203        @Override
     204        public Number mapRow(ResultSet rs, int rowNumber) throws SQLException {
     205            return rs.getInt(notebookOwner_id);
    206206        }
    207207    };
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/impl/JdbcUserDao.java

    r4260 r4262  
    8686       
    8787        StringBuilder sqlNotebooks  = new StringBuilder("SELECT ");
    88         sqlNotebooks.append(owner_id).append(" FROM ").append(notebookTableName).append(" WHERE ").append(owner_id).append("= ? LIMIT 1");
    89         List<Number> resultNotebooks = getSimpleJdbcTemplate().query(sqlNotebooks.toString(), ownerIDRowMapper, userID);
     88        sqlNotebooks.append(notebookOwner_id).append(" FROM ").append(notebookTableName).append(" WHERE ").append(notebookOwner_id).append("= ? LIMIT 1");
     89        List<Number> resultNotebooks = getSimpleJdbcTemplate().query(sqlNotebooks.toString(), notebookOwnerIDRowMapper, userID);
    9090        if (resultNotebooks.size() > 0) {
    9191            return true;
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/AnnotationResource.java

    r4260 r4262  
    3232import eu.dasish.annotation.schema.ReferenceList;
    3333import eu.dasish.annotation.schema.ResponseBody;
     34import eu.dasish.annotation.schema.UserWithPermission;
    3435import java.io.IOException;
    3536import java.net.URI;
     
    100101    public AnnotationResource() {
    101102    }
     103   
     104   
    102105
    103106    @GET
     
    164167// TODO Unit test
    165168
     169   
     170   
     171   
    166172    @GET
    167173    @Produces(MediaType.TEXT_XML)
     
    249255        }
    250256    }
     257   
     258    // TODO: how to return the status code?
     259
     260   
    251261///////////////////////////////////////////////////////
    252262
     
    278288        dbIntegrityService.setServiceURI(path);
    279289        String annotationURI = annotation.getURI();
     290       
    280291        if (!(path + "annotations/" + externalIdentifier).equals(annotationURI)) {
    281             logger.error("Wrong request: the external annotation ID and the annotation ID from the request body do not match.");
    282             logger.error("Will do nothing.");
    283             return null;
    284         }
     292            httpServletResponse.sendError(HttpServletResponse.SC_CONFLICT, "Wrong request: the external annotation ID and the annotation ID from the request body do not match. Correct the request and resend.");
     293            return null;
     294        }
     295       
     296        List<UserWithPermission> permissions = annotation.getPermissions().getUserWithPermission();
     297       
     298       
    285299        final Number annotationID = dbIntegrityService.getAnnotationInternalIdentifier(UUID.fromString(externalIdentifier));
    286300        if (annotationID != null) {
    287301            final Number userID = dbIntegrityService.getUserInternalIDFromRemoteID(httpServletRequest.getRemoteUser());
    288302            if (userID != null) {
    289                 if (canWrite(userID, annotationID)) {
     303                if (isOwner(userID, annotationID)) {
    290304                    int updatedRows = dbIntegrityService.updateUsersAnnotation(userID, annotation);
    291305                    logger.info("updateAnnotation method: OK");
     
    293307
    294308                } else {
    295                     httpServletResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED);
     309                    httpServletResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Only owner can update the annotation fully (incl. permissions). Only writer can update the annotation's body.");
    296310                    return null;
    297311                }
     
    449463        final Permission permission = dbIntegrityService.getPermission(annotationID, userID);
    450464        if (permission != null) {
    451             return (permission.value() == Permission.OWNER.value() || permission.value() == Permission.WRITER.value() || permission.value() == Permission.READER.value());
     465            return (permission.value().equals(Permission.OWNER.value()) || permission.value().equals(Permission.WRITER.value()) || permission.value().equals(Permission.READER.value()));
    452466        } else {
    453467            return false;
     
    458472        final Permission permission = dbIntegrityService.getPermission(annotationID, userID);
    459473        if (permission != null) {
    460             return (permission.value() == Permission.OWNER.value() || permission.value() == Permission.WRITER.value());
     474            return (permission.value().equals(Permission.OWNER.value()) || permission.value().equals(Permission.WRITER.value()));
    461475        } else {
    462476            return false;
     
    467481        final Permission permission = dbIntegrityService.getPermission(annotationID, userID);
    468482        if (permission != null) {
    469             return (permission.value() == Permission.OWNER.value());
     483            return (permission.value().equals(Permission.OWNER.value()));
    470484        } else {
    471485            return false;
     
    475489    private String[] makeAccessModeChain(String accessMode) {
    476490        if (accessMode != null) {
    477             if (accessMode == Permission.OWNER.value()) {
     491            if (accessMode.contains(Permission.OWNER.value())) {
    478492                String[] result = new String[1];
    479493                result[0] = accessMode;
    480494                return result;
    481495            } else {
    482                 if (accessMode == Permission.WRITER.value()) {
     496                if (accessMode.equals(Permission.WRITER.value())) {
    483497                    String[] result = new String[2];
    484498                    result[0] = Permission.WRITER.value();
     
    486500                    return result;
    487501                } else {
    488                     if (accessMode == Permission.READER.value()) {
     502                    if (accessMode.equals(Permission.READER.value())) {
    489503                        String[] result = new String[3];
    490504                        result[0] = Permission.READER.value();
     
    504518        }
    505519    }
     520   
     521   
    506522}
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/UserResource.java

    r4254 r4262  
    216216        }
    217217    }
     218   
     219   
     220    @DELETE
     221    @Path("{userId}/safe")
     222    public String deleteUserSafe(@PathParam("userId") String externalIdentifier) throws IOException {
     223        final Number remoteUserID = dbIntegrityService.getUserInternalIDFromRemoteID(httpServletRequest.getRemoteUser());
     224        if (remoteUserID != null) {
     225            if (dbIntegrityService.userHasAdminRights(remoteUserID)) {
     226                dbIntegrityService.setServiceURI(uriInfo.getBaseUri().toString());
     227                final Number userID = dbIntegrityService.getUserInternalIdentifier(UUID.fromString(externalIdentifier));
     228                if (userID != null) {
     229                    final Integer result = dbIntegrityService.deleteUserSafe(userID);
     230                    return "There is " + result.toString() + " row deleted";
     231                } else {
     232                    httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The user with the given id is not found in the database");
     233                    return null;
     234                }
     235            } else {
     236                httpServletResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED, "The logged in user does not have admin rights to update a user info in the database");
     237                return null;
     238            }
     239        } else {
     240            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The logged in user is not found in the database");
     241            return null;
     242        }
     243    }
    218244
    219245    private boolean ifLoggedIn(Number userID) {
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/sql/DashishAnnotatorCreate.sql

    r4183 r4262  
    6060    external_id text UNIQUE NOT NULL,
    6161    last_modified timestamp default current_timestamp AT TIME ZONE 'UTC',
    62     owner_id integer REFERENCES principal(principal_id),
    6362    headline text,
    6463    body_text text,
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/test/java/eu/dasish/annotation/backend/dao/impl/DBIntegrityServiceTest.java

    r4207 r4262  
    210210        listMap.add(map5);
    211211
    212         final UUID externalID3 = UUID.fromString(TestBackendConstants._TEST_USER_3_EXT_ID);
    213         final UUID externalID4 = UUID.fromString(TestBackendConstants._TEST_USER_4_EXT_ID);
    214         final UUID externalID5 = UUID.fromString(TestBackendConstants._TEST_USER_5_EXT_ID);
    215 
    216212        final String uri3 = TestBackendConstants._TEST_SERVLET_URI_users +TestBackendConstants._TEST_USER_3_EXT_ID;
    217213        final String uri4 = TestBackendConstants._TEST_SERVLET_URI_users +TestBackendConstants._TEST_USER_4_EXT_ID;
    218214        final String uri5 = TestBackendConstants._TEST_SERVLET_URI_users +TestBackendConstants._TEST_USER_5_EXT_ID;
    219215
    220         final Map<Annotation, Number> mockAnnotationOwnerID = new HashMap<Annotation, Number>();
    221         mockAnnotationOwnerID.put(mockAnnotation, 3);
    222216       
    223217        mockeryDao.checking(new Expectations() {
    224218            {
    225219                oneOf(annotationDao).getAnnotationWithoutTargetsAndPermissions(2);
    226                 will(returnValue(mockAnnotationOwnerID));
     220                will(returnValue(mockAnnotation));
     221               
     222                 oneOf(annotationDao).getOwner(2);
     223                will(returnValue(3));
    227224
    228225                oneOf(userDao).getURIFromInternalID(3);
     
    300297        mockAnnotationIDs2.add(2);
    301298        mockAnnotationIDs2.add(3);
    302 
    303        
    304         final UUID owner = UUID.fromString(TestBackendConstants._TEST_USER_4_EXT_ID);
     299       
    305300        final Timestamp after = new Timestamp(0);
    306301        final Timestamp before = new Timestamp(System.currentTimeMillis());
     
    325320                will(returnValue(mockAnnotationIDs2));
    326321
    327                 oneOf(userDao).getInternalID(owner);
    328                 will(returnValue(4));
    329                
    330322               
    331                 oneOf(annotationDao).getFilteredAnnotationIDs(mockAnnotationIDs1, "some html", null, 4, after, before);
     323                oneOf(annotationDao).getFilteredAnnotationIDs(mockAnnotationIDs1, "some html", null, after, before);
    332324                will(returnValue(mockRetval));
    333325
     
    336328
    337329
    338         List result = dbIntegrityService.getFilteredAnnotationIDs(word, "some html", 3, accessModes, null, owner, after, before);
     330        List result = dbIntegrityService.getFilteredAnnotationIDs(word, "some html", 3, accessModes, null, after, before);
    339331        assertEquals(1, result.size());
    340332        assertEquals(3, result.get(0));
     
    417409        TargetIDs.add(2);
    418410       
    419         final Map<AnnotationInfo,Number> mockPair = new HashMap<AnnotationInfo, Number>(); // annotationInfo-ownerID
    420         mockPair.put(mockAnnotInfo, 4);
    421411       
    422412        final String[] accessModes = new String[2];
     
    426416        mockeryDao.checking(new Expectations() {
    427417            {
     418                oneOf(userDao).getInternalID(ownerUUID);
     419                will(returnValue(4));
     420               
    428421                oneOf(annotationDao).getAnnotationIDsForUserWithPermission(3, accessModes);
    429422                will(returnValue(mockAnnotationIDs1));
    430423
    431424               
     425             
    432426                // getFilteredAnnotationIds
    433427                oneOf(targetDao).getTargetsReferringTo(word);
     
    437431                will(returnValue(mockAnnotationIDs2));
    438432               
    439                 oneOf(userDao).getInternalID(ownerUUID);
    440                 will(returnValue(4));
    441              
    442                 oneOf(annotationDao).getFilteredAnnotationIDs(mockAnnotationIDs1, text, null, 4, after, before);
     433               
     434                oneOf(annotationDao).getFilteredAnnotationIDs(mockAnnotationIDs1, text, null, after, before);
    443435                will(returnValue(mockAnnotIDs));
    444436               
     
    447439//               
    448440                oneOf(annotationDao).getAnnotationInfoWithoutTargets(3);
    449                 will(returnValue(mockPair));
     441                will(returnValue(mockAnnotInfo));
     442               
     443                oneOf(annotationDao).getOwner(3);
     444                will(returnValue(4));
     445               
     446                oneOf(userDao).getURIFromInternalID(4);
     447                will(returnValue(TestBackendConstants._TEST_SERVLET_URI_users+"00000000-0000-0000-0000-000000000112"));
    450448               
    451449                ////
     
    456454                oneOf(targetDao).getURIFromInternalID(2);
    457455                will(returnValue(TestBackendConstants._TEST_SERVLET_URI_Targets +TestBackendConstants._TEST_Target_2_EXT_ID));
    458                 ////
    459                
    460                 oneOf(userDao).getURIFromInternalID(4);
    461                 will(returnValue(TestBackendConstants._TEST_SERVLET_URI_users +TestBackendConstants._TEST_USER_4_EXT_ID));
    462              
     456             
    463457               
    464458            }
     
    682676        mockeryDao.checking(new Expectations() {
    683677            {
    684                 oneOf(annotationDao).addAnnotation(testAnnotation, 5);
     678                oneOf(annotationDao).addAnnotation(testAnnotation);
    685679                will(returnValue(6)); // the next free number is 6
    686680
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/test/java/eu/dasish/annotation/backend/dao/impl/JdbcAnnotationDaoTest.java

    r4207 r4262  
    232232        System.out.println("test getAnnotationWithoutTargets");
    233233        jdbcAnnotationDao.setServiceURI(TestBackendConstants._TEST_SERVLET_URI_annotations);
    234         final Map<Annotation, Number> result= jdbcAnnotationDao.getAnnotationWithoutTargetsAndPermissions(2);
    235         Annotation[] annotations = new Annotation[1];
    236         result.keySet().toArray(annotations);
    237        
    238         assertEquals(TestBackendConstants._TEST_ANNOT_2_HEADLINE, annotations[0].getHeadline());
    239         assertEquals(3, result.get(annotations[0]));
    240         assertEquals(TestBackendConstants._TEST_ANNOT_2_BODY, annotations[0].getBody().getTextBody().getValue());
    241         assertEquals(TestBackendConstants._TEST_BODY_MIMETYPE_HTML, annotations[0].getBody().getTextBody().getMimeType());
     234        final Annotation result= jdbcAnnotationDao.getAnnotationWithoutTargetsAndPermissions(2);
     235       
     236        assertEquals(TestBackendConstants._TEST_ANNOT_2_HEADLINE, result.getHeadline());
     237        assertEquals(TestBackendConstants._TEST_ANNOT_2_BODY, result.getBody().getTextBody().getValue());
     238        assertEquals(TestBackendConstants._TEST_BODY_MIMETYPE_HTML, result.getBody().getTextBody().getMimeType());
    242239        assertEquals(TestBackendConstants._TEST_SERVLET_URI_annotations+
    243                 TestBackendConstants._TEST_ANNOT_2_EXT, annotations[0].getURI());
    244         assertEquals(DatatypeFactory.newInstance().newXMLGregorianCalendar(TestBackendConstants._TEST_ANNOT_2_TIME_STAMP), annotations[0].getLastModified());
     240                TestBackendConstants._TEST_ANNOT_2_EXT, result.getURI());
     241        assertEquals(DatatypeFactory.newInstance().newXMLGregorianCalendar(TestBackendConstants._TEST_ANNOT_2_TIME_STAMP), result.getLastModified());
    245242    }
    246243
     
    276273        assertEquals(null, annotationToAdd.getLastModified());
    277274       
    278         Number newAnnotationID = jdbcAnnotationDao.addAnnotation(annotationToAdd, 5);
     275        Number newAnnotationID = jdbcAnnotationDao.addAnnotation(annotationToAdd);
    279276        assertEquals(6, newAnnotationID);
    280277       
    281278        // checking
    282         Map<Annotation, Number> getResult= jdbcAnnotationDao.getAnnotationWithoutTargetsAndPermissions(6);
    283         Annotation[] annotations = new Annotation[1];
    284         getResult.keySet().toArray(annotations);
    285         Annotation addedAnnotation = annotations[0];
     279        Annotation addedAnnotation= jdbcAnnotationDao.getAnnotationWithoutTargetsAndPermissions(6);
    286280        assertFalse(null == addedAnnotation.getURI());
    287281        assertFalse(null == addedAnnotation.getLastModified());
    288         assertEquals(5, getResult.get(addedAnnotation));
    289282        assertEquals(annotationToAdd.getBody().getTextBody().getMimeType(), addedAnnotation.getBody().getTextBody().getMimeType());
    290283        assertEquals(annotationToAdd.getBody().getTextBody().getValue(), addedAnnotation.getBody().getTextBody().getValue());
     
    343336        annotationIDs.add(3);
    344337       
    345         List<Number> result_1 = jdbcAnnotationDao.getFilteredAnnotationIDs(annotationIDs, null, null, null, null, null);       
     338        List<Number> result_1 = jdbcAnnotationDao.getFilteredAnnotationIDs(annotationIDs, null, null, null, null);       
    346339        assertEquals(2, result_1.size());
    347340        assertEquals(2, result_1.get(0));
     
    349342       
    350343       
    351         List<Number> result_2 = jdbcAnnotationDao.getFilteredAnnotationIDs(annotationIDs, "some html", null, null, null, null);       
     344        List<Number> result_2 = jdbcAnnotationDao.getFilteredAnnotationIDs(annotationIDs, "some html", null, null, null);       
    352345        assertEquals(2, result_2.size());
    353346        assertEquals(2, result_2.get(0));
     
    355348       
    356349       
    357        
    358         List<Number> result_3 = jdbcAnnotationDao.getFilteredAnnotationIDs(annotationIDs, "some html", null, 3, null, null);       
    359         assertEquals(1, result_3.size());
    360         assertEquals(2, result_3.get(0));
    361        
    362        
     350             
    363351        Timestamp after = new Timestamp(0);
    364352        Timestamp before = new Timestamp(System.currentTimeMillis()); 
    365         List<Number> result_4 = jdbcAnnotationDao.getFilteredAnnotationIDs(annotationIDs, "some html", null, 3, after, before);       
    366         assertEquals(1, result_4.size());
     353        List<Number> result_4 = jdbcAnnotationDao.getFilteredAnnotationIDs(annotationIDs, "some html", null, after, before);       
     354        assertEquals(2, result_4.size());
    367355        assertEquals(2, result_4.get(0));
    368        
     356        assertEquals(3, result_2.get(1));
    369357       
    370358        Timestamp after_1 = new Timestamp(System.currentTimeMillis()); // no annotations added after "now"       
    371         List<Number> result_5 = jdbcAnnotationDao.getFilteredAnnotationIDs(annotationIDs, "some html", null, 3, after_1, null);       
     359        List<Number> result_5 = jdbcAnnotationDao.getFilteredAnnotationIDs(annotationIDs, "some html", null, after_1, null);       
    372360        assertEquals(0, result_5.size());
    373361       
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/test/resources/test-data/InsertTestData.sql

    r4181 r4262  
    2727-- INSERT INTO notebook (title, owner_id, external_id) VALUES ('a notebook', 1, 1);
    2828
    29 INSERT INTO annotation (owner_id,headline, body_text, body_mimetype, external_id, is_xml) VALUES (1, 'a headline', '<html><body>some html</body></html>', 'text/html' , '00000000-0000-0000-0000-000000000005', false); --1
     29INSERT INTO annotation (headline, body_text, body_mimetype, external_id, is_xml) VALUES ('a headline', '<html><body>some html</body></html>', 'text/html' , '00000000-0000-0000-0000-000000000005', false); --1
    3030
    3131INSERT INTO notebook (title, owner_id, external_id) VALUES ('a second notebook', 2, '00000000-0000-0000-0000-000000000002'); --2
     
    4848INSERT INTO notebook (title, owner_id, external_id) VALUES ('Notebook 6', 5, '00000000-0000-0000-0000-000000000014'); --6
    4949
    50 INSERT INTO annotation (owner_id,headline,body_text, body_mimetype, external_id, last_modified, is_xml) VALUES (3, 'Sagrada Famiglia','<html><body>some html 1</body></html>', 'text/html' , '00000000-0000-0000-0000-000000000021', '2013-08-12 09:25:00.383', false); --2
    51 INSERT INTO annotation (owner_id,headline,body_text, body_mimetype, external_id, is_xml) VALUES (4, 'Gaudi','<html><body>some html 2</body></html>', 'text/html' , '00000000-0000-0000-0000-000000000022',false); --3
    52 INSERT INTO annotation (owner_id,headline,body_text, body_mimetype, external_id, is_xml) VALUES (5, 'Art Nuveau','some plain text', 'text/plain' , '00000000-0000-0000-0000-000000000023', false); --4
    53 INSERT INTO annotation (owner_id,headline,body_text, body_mimetype, external_id, is_xml) VALUES (3, 'Annotation to delete','<html><body>some html 4</body></html>', 'text/html' , '00000000-0000-0000-0000-000000000024',false); --5
     50INSERT INTO annotation (headline,body_text, body_mimetype, external_id, last_modified, is_xml) VALUES ('Sagrada Famiglia','<html><body>some html 1</body></html>', 'text/html' , '00000000-0000-0000-0000-000000000021', '2013-08-12 09:25:00.383', false); --2
     51INSERT INTO annotation (headline,body_text, body_mimetype, external_id, is_xml) VALUES ('Gaudi','<html><body>some html 2</body></html>', 'text/html' , '00000000-0000-0000-0000-000000000022',false); --3
     52INSERT INTO annotation (headline,body_text, body_mimetype, external_id, is_xml) VALUES ('Art Nuveau','some plain text', 'text/plain' , '00000000-0000-0000-0000-000000000023', false); --4
     53INSERT INTO annotation (headline,body_text, body_mimetype, external_id, is_xml) VALUES ('Annotation to delete','<html><body>some html 4</body></html>', 'text/html' , '00000000-0000-0000-0000-000000000024',false); --5
    5454
    5555
Note: See TracChangeset for help on using the changeset viewer.