public int deletePublicationById(PerunSession sess, Integer id) throws CabinetException {

    Publication pub = findPublicationById(id);
    if (pub == null) throw new CabinetException(ErrorCodes.PUBLICATION_NOT_EXISTS);

    // To delete publication user must be either PERUNADMIN
    // or user who created record (publication.createdBy==actor property)
    try {
      if (!AuthzResolver.isAuthorized(sess, Role.PERUNADMIN)
          && !pub.getCreatedBy().equalsIgnoreCase(sess.getPerunPrincipal().getActor())
          && !pub.getCreatedByUid().equals(sess.getPerunPrincipal().getUserId())) {
        // not perun admin or author of record
        throw new CabinetException(
            "You are not allowed to delete publications you didn't created.",
            ErrorCodes.NOT_AUTHORIZED);
      }
    } catch (PerunException pe) {
      throw new CabinetException(ErrorCodes.PERUN_EXCEPTION, pe);
    }

    // delete action
    try {

      // delete authors
      for (Authorship a : authorshipService.findAuthorshipsByPublicationId(id)) {
        authorshipService.deleteAuthorshipById(sess, a.getId());
      }
      // delete thanks
      for (Thanks t : thanksService.findThanksByPublicationId(id)) {
        thanksService.deleteThanksById(sess, t.getId());
      }

      // delete publication
      if (AuthzResolver.isAuthorized(sess, Role.PERUNADMIN)) {

        // only perun admin can actually delete publication
        return publicationDao.deletePublicationById(id);

      } else {

        return 1; // for others return as OK - perunadmin then deletes pubs manually
      }

    } catch (DataIntegrityViolationException ex) {
      throw new CabinetException(
          "Can't delete publication with authors or thanks. Please remove them first in order to delete publication.",
          ErrorCodes.PUBLICATION_HAS_AUTHORS_OR_THANKS);
    } catch (PerunException ex) {
      throw new CabinetException(ErrorCodes.PERUN_EXCEPTION, ex);
    }
  }
Example #2
0
 private static Authorship createAuthorship(Map<String, String> beanAttr)
     throws InternalErrorException {
   if (beanAttr == null) return null;
   Authorship authorship = new Authorship();
   authorship.setId(Integer.valueOf(beanAttr.get("id")).intValue());
   authorship.setPublicationId(Integer.valueOf(beanAttr.get("publicationId")).intValue());
   authorship.setUserId(Integer.valueOf(beanAttr.get("userId")).intValue());
   authorship.setCreatedBy(BeansUtils.eraseEscaping(beanAttr.get("createdBy")));
   authorship.setCreatedByUid(
       (beanAttr.get("createdByUid").equals("\\0"))
           ? null
           : Integer.valueOf(beanAttr.get("createdByUid")).intValue());
   if (BeansUtils.eraseEscaping(beanAttr.get("createdDate")) == null)
     authorship.setCreatedDate(null);
   else {
     Date date;
     try {
       date =
           BeansUtils.DATE_FORMATTER.parse(BeansUtils.eraseEscaping(beanAttr.get("createdDate")));
     } catch (ParseException ex) {
       throw new InternalErrorException("Error when date was parsing from String to Date.", ex);
     }
     authorship.setCreatedDate(date);
   }
   return authorship;
 }