Ignore:
Timestamp:
12/01/11 15:04:15 (13 years ago)
Author:
jeafer
Message:

Jean-Charles branch commit3,
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
CommentsDao? updated
CommentTest? class implemented (New Class)
Test classes ComponentRegistryRestServiceTest? and RegistryHelperTest? starting to implement tests for Comment validation (insertion, deletion)

File:
1 edited

Legend:

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

    r1633 r1635  
    230230        }
    231231    }
     232   
     233    @Override
     234    public int registerComment(Comment comment, Comment spec){
     235        try {
     236            String xml = commmentSpecToString(spec);
     237            // Convert principal name to user record id
     238            Number uid = convertUserIdInComment(comment);
     239            commentsDao.insertComment(comment, xml, uid);
     240            return 0;
     241        } catch (DataAccessException ex) {
     242            LOG.error("Database error while registering component", ex);
     243            return -1;
     244        } catch (JAXBException ex) {
     245            LOG.error("Error while registering component", ex);
     246            return -2;
     247        } catch (UnsupportedEncodingException ex) {
     248            LOG.error("Error while registering component", ex);
     249            return -3;
     250        }
     251    }
    232252
    233253    /**
     
    253273        if (uid != null) {
    254274            description.setUserId(uid.toString());
     275        }
     276        return uid;
     277    }
     278   
     279        private Number convertUserIdInComment(Comment comment) throws DataAccessException {
     280        Number uid = null;
     281        if (comment.getUserId() != null) {
     282            User user = userDao.getByPrincipalName(comment.getUserId());
     283            if (user != null) {
     284                uid = user.getId();
     285            }
     286        } else {
     287            uid = userId;
     288        }
     289        if (uid != null) {
     290            comment.setUserId(uid.toString());
    255291        }
    256292        return uid;
     
    460496        return xml;
    461497    }
     498   
     499        private String commmentSpecToString(Comment spec) throws UnsupportedEncodingException, JAXBException {
     500        ByteArrayOutputStream os = new ByteArrayOutputStream();
     501        MDMarshaller.marshal(spec, os);
     502        String xml = os.toString("UTF-8");
     503        return xml;
     504    }
    462505
    463506    private CMDComponentSpec getUncachedMDComponent(String id, AbstractDescriptionDao dao) {
     
    523566    }
    524567
    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     }
    542 
    543568    private boolean inWorkspace(AbstractDescriptionDao<?> dao, String cmdId) {
    544569        if (isPublic()) {
     
    549574    }
    550575
    551 //    private boolean inWorkspace(CommentsDao dao, String cmdId) {
    552 //        if (isPublic()) {
    553 //            return dao.isPublic(cmdId);
    554 //        } else {
    555 //            return dao.isInUserSpace(cmdId, getUserId());
    556 //        }
    557 //    }
    558576    @Override
    559577    public String getName() {
     
    583601            try {
    584602                    checkAuthorisationComment(com, principal);
    585                     checkCommentAge(com, principal);
    586603                    commentsDao.deleteComment(com, true);
    587604                } catch (DataAccessException ex) {
Note: See TracChangeset for help on using the changeset viewer.