Changeset 1633 for ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/impl/database/ComponentRegistryDbImpl.java
- Timestamp:
- 11/30/11 13:18:31 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/impl/database/ComponentRegistryDbImpl.java
r1631 r1633 3 3 import java.io.ByteArrayInputStream; 4 4 import java.io.ByteArrayOutputStream; 5 import java.io.IOException; 5 6 import java.io.InputStream; 6 7 import java.io.OutputStream; … … 29 30 import clarin.cmdi.componentregistry.impl.ComponentRegistryImplBase; 30 31 import clarin.cmdi.componentregistry.model.AbstractDescription; 31 import clarin.cmdi.componentregistry.model.Comment Mapping.Comment;32 import clarin.cmdi.componentregistry.model.Comment; 32 33 import clarin.cmdi.componentregistry.model.ComponentDescription; 33 34 import clarin.cmdi.componentregistry.model.ProfileDescription; … … 50 51 @Autowired 51 52 private ComponentDescriptionDao componentDescriptionDao; 52 @Autowired 53 @Autowired 53 54 private UserDao userDao; 54 55 @Autowired … … 132 133 return commentsDao.getCommentsFromProfile(profileId); 133 134 } 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) { 134 144 throw new ComponentRegistryException("Database access error while trying to get comment from profile", ex); 135 145 } … … 137 147 138 148 @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); 142 161 } catch (DataAccessException ex) { 143 162 throw new ComponentRegistryException("Database access error while trying to get comment from component", ex); 144 163 } 145 164 } 146 147 @Override148 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 @Override157 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 // @Override166 // public List<Comment> getComments() throws ComponentRegistryException {167 // throw new UnsupportedOperationException("Not supported yet.");168 // }169 165 170 166 @Override … … 212 208 } 213 209 } 214 215 //// @Override216 //// 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 workspace226 //// 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 // }237 210 238 211 @Override … … 383 356 } 384 357 } 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 // }399 358 400 359 @Override … … 450 409 } 451 410 } 452 453 // private void invalidateCommentCache(Comment comment) {454 // commentsCache.remove(comment.getId());455 // }456 411 457 412 private AbstractDescriptionDao<?> getDaoForDescription(AbstractDescription description) { … … 459 414 } 460 415 461 // private CommentsDao getDaoForCommentDescription(Comment comment) {462 // return commentsDao;463 // }464 416 465 417 /** … … 488 440 } 489 441 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 } 505 455 506 456 private String componentSpecToString(CMDComponentSpec spec) throws UnsupportedEncodingException, JAXBException { … … 527 477 } 528 478 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 545 479 private void checkAuthorisation(AbstractDescription desc, Principal principal) throws UserUnauthorizedException { 546 480 if (!isOwnerOfDescription(desc, principal.getName()) && !configuration.isAdminUser(principal)) { … … 550 484 } 551 485 } 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()) + "(" + desc557 //+ ").");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 } 560 494 561 495 private boolean isOwnerOfDescription(AbstractDescription desc, String principalName) { … … 564 498 && principalName.equals(owner); 565 499 } 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 } 566 506 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 owner570 // && principalName.equals(owner);571 // }572 573 507 private void checkAge(AbstractDescription desc, Principal principal) throws DeleteFailedException { 574 508 if (isPublic() && !configuration.isAdminUser(principal)) { … … 588 522 } 589 523 } 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 old598 //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 } 608 542 609 543 private boolean inWorkspace(AbstractDescriptionDao<?> dao, String cmdId) { … … 622 556 // } 623 557 // } 624 625 558 @Override 626 559 public String getName() { … … 641 574 return componentDescriptionDao.getDeletedDescriptions(getUserId()); 642 575 } 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.