Ignore:
Timestamp:
11/30/11 13:18:31 (13 years ago)
Author:
jeafer
Message:

Jean-Charles branch commit2,
Changes regarding comment on the ComponentRegistry service.
Delete_comment from RestService? to Database access (CommentsDao?) implemented.
Post_comment from RestService? to Database access (CommentsDao?) implemented. (Not yet tested)
Comment class updated
TestComment? class updated
DataBase? : Table comment updated, Corrections of fkey references
Validate classes implemented to validate comment

File:
1 edited

Legend:

Unmodified
Added
Removed
  • ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/impl/database/ComponentRegistryDbImpl.java

    r1631 r1633  
    33import java.io.ByteArrayInputStream;
    44import java.io.ByteArrayOutputStream;
     5import java.io.IOException;
    56import java.io.InputStream;
    67import java.io.OutputStream;
     
    2930import clarin.cmdi.componentregistry.impl.ComponentRegistryImplBase;
    3031import clarin.cmdi.componentregistry.model.AbstractDescription;
    31 import clarin.cmdi.componentregistry.model.CommentMapping.Comment;
     32import clarin.cmdi.componentregistry.model.Comment;
    3233import clarin.cmdi.componentregistry.model.ComponentDescription;
    3334import clarin.cmdi.componentregistry.model.ProfileDescription;
     
    5051    @Autowired
    5152    private ComponentDescriptionDao componentDescriptionDao;
    52     @Autowired   
     53    @Autowired
    5354    private UserDao userDao;
    5455    @Autowired
     
    132133            return commentsDao.getCommentsFromProfile(profileId);
    133134        } catch (DataAccessException ex) {
     135            throw new ComponentRegistryException("Database access error while trying to get list of comments from profile", ex);
     136        }
     137    }
     138
     139    @Override
     140    public Comment getSpecifiedCommentInProfile(String commentId) throws ComponentRegistryException {
     141        try {
     142            return commentsDao.getSpecifiedCommentFromProfile(commentId);
     143        } catch (DataAccessException ex) {
    134144            throw new ComponentRegistryException("Database access error while trying to get comment from profile", ex);
    135145        }
     
    137147
    138148    @Override
    139     public Comment getSpecifiedCommentInProfile(String commentId) throws ComponentRegistryException {
    140         try {
    141             return commentsDao.getSpecifiedCommentFromProfile(commentId);
     149    public List<Comment> getCommentsInComponent(String componentId) throws ComponentRegistryException {
     150        try {
     151            return commentsDao.getCommentsFromComponent(componentId);
     152        } catch (DataAccessException ex) {
     153            throw new ComponentRegistryException("Database access error while trying to get list of comments from component", ex);
     154        }
     155    }
     156
     157    @Override
     158    public Comment getSpecifiedCommentInComponent(String commentId) throws ComponentRegistryException {
     159        try {
     160            return commentsDao.getSpecifiedCommentFromComponent(commentId);
    142161        } catch (DataAccessException ex) {
    143162            throw new ComponentRegistryException("Database access error while trying to get comment from component", ex);
    144163        }
    145164    }
    146 
    147     @Override
    148     public List<Comment> getCommentsInComponent(String componentId) throws ComponentRegistryException {
    149         try {
    150             return commentsDao.getCommentsFromComponent(componentId);
    151         } catch (DataAccessException ex) {
    152             throw new ComponentRegistryException("Database access error while trying to get comment from component", ex);
    153         }
    154     }
    155 
    156     @Override
    157     public Comment getSpecifiedCommentInComponent(String commentId) throws ComponentRegistryException {
    158         try {
    159             return commentsDao.getSpecifiedCommentFromComponent(commentId);
    160         } catch (DataAccessException ex) {
    161             throw new ComponentRegistryException("Database access error while trying to get comment from component", ex);
    162         }
    163     }
    164 
    165 //    @Override
    166 //    public List<Comment> getComments() throws ComponentRegistryException {
    167 //        throw new UnsupportedOperationException("Not supported yet.");
    168 //    }
    169165
    170166    @Override
     
    212208        }
    213209    }
    214 
    215 ////    @Override
    216 ////    public CMDComponentSpec getMDComment(String id) throws ComponentRegistryException {
    217 //////        if (inWorkspace(commentsDao, id)) {
    218 //////            CMDComponentSpec result = commentsCache.get(id);
    219 //////            if (result == null && !commentsCache.containsKey(id)) {
    220 //////                result = getUncachedMDComment(id);
    221 //////                commentsCache.put(id, result);
    222 //////            }
    223 //////            return result;
    224 //////        } else {
    225 //////            // May exist, but not in this workspace
    226 ////            return null;
    227 //////       }
    228 ////    }
    229 //
    230 ////    public CMDComponentSpec getUncachedMDComment(String id) throws ComponentRegistryException {
    231 ////        try {
    232 ////            return getUncachedMDComment(id, commentsDao);
    233 ////        } catch (DataAccessException ex) {
    234 ////            throw new ComponentRegistryException("Database access error while trying to get component", ex);
    235 //       }
    236 //  }
    237210
    238211    @Override
     
    383356        }
    384357    }
    385    
    386 //    public void deleteMDComment(String commentId, Principal principal) throws UserUnauthorizedException, DeleteFailedException, ComponentRegistryException {
    387 //        Comment desc = getCommentsInComponent(commentId);
    388 //        if(desc != null) {
    389 //            try {
    390 //                checkAuthorisationComment(desc, principal);
    391 //                checkCommentAge(desc, principal);
    392 //                commentsDao.setDeleted(desc, true);
    393 //                invalidateCommentCache(desc);
    394 //            } catch (DataAccessException ex) {
    395 //                throw new DeleteFailedException("Database access error while trying to delete profile", ex);
    396 //            }
    397 //        }
    398 //    }
    399358
    400359    @Override
     
    450409        }
    451410    }
    452    
    453 //        private void invalidateCommentCache(Comment comment) {
    454 //            commentsCache.remove(comment.getId());
    455 //    }
    456411
    457412    private AbstractDescriptionDao<?> getDaoForDescription(AbstractDescription description) {
     
    459414    }
    460415   
    461 //    private CommentsDao getDaoForCommentDescription(Comment comment) {
    462 //        return commentsDao;
    463 //    }
    464416
    465417    /**
     
    488440    }
    489441
    490    
    491 //        private Number getIdForCommentDescription(Comment comment) throws IllegalArgumentException {
    492 //        Number dbId = null;
    493 //        CommentsDao dao = getDaoForCommentDescription(comment);
    494 //        try {
    495 //            dbId = dao.getDbId(comment.getId());
    496 //        } catch (DataAccessException ex) {
    497 //            LOG.error("Error getting dbId for comment with id " + comment.getId(), ex);
    498 //        }
    499 //        if (dbId == null) {
    500 //            throw new IllegalArgumentException("Could not get database Id for description");
    501 //        } else {
    502 //            return dbId;
    503 //        }
    504 //    }
     442        private Number getIdForCommentDescription(Comment comment) throws IllegalArgumentException {
     443        Number dbId = null;
     444        try {
     445            dbId = commentsDao.getDbId(Integer.parseInt(comment.getId()));
     446        } catch (DataAccessException ex) {
     447            LOG.error("Error getting dbId for comment with id " + comment.getId(), ex);
     448        }
     449        if (dbId == null) {
     450            throw new IllegalArgumentException("Could not get database Id for description");
     451        } else {
     452            return dbId;
     453        }
     454    }
    505455   
    506456    private String componentSpecToString(CMDComponentSpec spec) throws UnsupportedEncodingException, JAXBException {
     
    527477    }
    528478
    529 //    private CMDComponentSpec getUncachedMDComment(String id, CommentsDao dao) {
    530 //        String xml = dao.getContent(false, id);
    531 //        if (xml != null) {
    532 //            try {
    533 //                InputStream is = new ByteArrayInputStream(xml.getBytes("UTF-8"));
    534 //                return MDMarshaller.unmarshal(CMDComponentSpec.class, is, null);
    535 //
    536 //            } catch (JAXBException ex) {
    537 //                LOG.error(null, ex);
    538 //            } catch (UnsupportedEncodingException ex) {
    539 //                LOG.error(null, ex);
    540 //            }
    541 //        }
    542 //        return null;
    543 //    }
    544 
    545479    private void checkAuthorisation(AbstractDescription desc, Principal principal) throws UserUnauthorizedException {
    546480        if (!isOwnerOfDescription(desc, principal.getName()) && !configuration.isAdminUser(principal)) {
     
    550484        }
    551485    }
    552    
    553 //    private void checkAuthorisationComment(Comment desc, Principal principal)throws UserUnauthorizedException {
    554 //        if (!isOwnerOfComment(desc, principal.getName()) && !configuration.isAdminUser(principal)) {
    555 //            throw new UserUnauthorizedException("Unauthorized operation user '" + principal.getName()
    556 //                    + "' is not the creator (nor an administrator) of the " + (desc.getId()) + "(" + desc
    557 //                    + ").");
    558 //        }
    559 //    }
     486
     487    private void checkAuthorisationComment(Comment desc, Principal principal) throws UserUnauthorizedException {
     488        if (!isOwnerOfComment(desc, principal.getName()) && !configuration.isAdminUser(principal)) {
     489            throw new UserUnauthorizedException("Unauthorized operation user '" + principal.getName()
     490                    + "' is not the creator (nor an administrator) of the " + (desc.getId()) + "(" + desc
     491                    + ").");
     492        }
     493    }
    560494
    561495    private boolean isOwnerOfDescription(AbstractDescription desc, String principalName) {
     
    564498                && principalName.equals(owner);
    565499    }
     500
     501        private boolean isOwnerOfComment(Comment desc, String principalName) {
     502        String owner = commentsDao.getOwnerPrincipalName(getIdForCommentDescription(desc));
     503        return owner != null // If owner is null, no one can be owner
     504                && principalName.equals(owner);
     505    }
    566506   
    567 //        private boolean isOwnerOfComment(Comment desc, String principalName) {
    568 //        String owner = getDaoForCommentDescription(desc).getOwnerPrincipalName(getIdForCommentDescription(desc));
    569 //        return owner != null // If owner is null, no one can be owner
    570 //                && principalName.equals(owner);
    571 //    }
    572 
    573507    private void checkAge(AbstractDescription desc, Principal principal) throws DeleteFailedException {
    574508        if (isPublic() && !configuration.isAdminUser(principal)) {
     
    588522        }
    589523    }
    590    
    591 //        private void checkCommentAge(Comment desc, Principal principal) throws DeleteFailedException {
    592 //        if (isPublic() && !configuration.isAdminUser(principal)) {
    593 //            try {
    594 //                Date regDate = Comment.getDate(desc.getCommentDate());
    595 //                Calendar calendar = Calendar.getInstance();
    596 //                calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1);
    597 //                if (regDate.before(calendar.getTime())) { // More then month old
    598 //                    throw new DeleteFailedException(
    599 //                            "The "
    600 //                            + (desc.getId())
    601 //                            + " is more then a month old and cannot be deleted anymore.");
    602 //                }
    603 //            } catch (ParseException e) {
    604 //                LOG.error("Cannot parse date of " + desc + " Error:" + e);
    605 //            }
    606 //        }
    607 //    }
     524
     525    private void checkCommentAge(Comment desc, Principal principal) throws DeleteFailedException {
     526        if (isPublic() && !configuration.isAdminUser(principal)) {
     527            try {
     528                Date regDate = Comment.getDate(desc.getCommentDate());
     529                Calendar calendar = Calendar.getInstance();
     530                calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1);
     531                if (regDate.before(calendar.getTime())) { // More then month old
     532                    throw new DeleteFailedException(
     533                            "The "
     534                            + (desc.getId())
     535                            + " is more then a month old and cannot be deleted anymore.");
     536                }
     537            } catch (ParseException e) {
     538                LOG.error("Cannot parse date of " + desc + " Error:" + e);
     539            }
     540        }
     541    }
    608542
    609543    private boolean inWorkspace(AbstractDescriptionDao<?> dao, String cmdId) {
     
    622556//        }
    623557//    }
    624 
    625558    @Override
    626559    public String getName() {
     
    641574        return componentDescriptionDao.getDeletedDescriptions(getUserId());
    642575    }
    643 }
     576
     577    @Override
     578    public void deleteComment(String commentId, Principal principal) throws IOException,
     579    ComponentRegistryException, UserUnauthorizedException, DeleteFailedException {
     580
     581            Comment com = commentsDao.getById(commentId);       
     582            if (com!= null) {
     583            try {
     584                    checkAuthorisationComment(com, principal);
     585                    checkCommentAge(com, principal);
     586                    commentsDao.deleteComment(com, true);
     587                } catch (DataAccessException ex) {
     588                    throw new DeleteFailedException("Database access error while trying to delete component", ex);
     589                }
     590            }
     591        }
     592    }
Note: See TracChangeset for help on using the changeset viewer.