public int updatePublicationById(PerunSession sess, Publication publication)
      throws CabinetException {

    if (publication.getId() == null
        || publication.getExternalId() == null
        || publication.getPublicationSystemId() == null) {
      // such publication can't exists
      throw new CabinetException(
          "Publication doesn't exists: " + publication, ErrorCodes.PUBLICATION_NOT_EXISTS);
    }

    // strip long params in new publication
    stripLongParams(publication);

    // don't create already existing publication (same id or externalId&&pubSysId)
    Publication filter = new Publication(); // for ext_id & pubSysId

    filter.setPublicationSystemId(publication.getPublicationSystemId());
    filter.setExternalId(publication.getExternalId());
    List<Publication> publications = findPublicationsByFilter(filter);

    if (publications.size() > 1) {
      throw new CabinetException(
          "Consistency error: more than one unique publications found by ExtID and PubSysID.");
    }
    if (publications.size() > 0 && !(publication.getId().equals((publications.get(0).getId())))) {
      throw new CabinetException(
          "Cannot update to duplicate publication: " + publication,
          ErrorCodes.PUBLICATION_ALREADY_EXISTS);
    }

    // save old pub (must be Rich to contain all authors)
    Publication oldPub = findPublicationById(publication.getId());

    // update publication in DB
    int result = publicationDao.updatePublicationById(publication);

    // if updated and rank or category was changed
    if (result > 0
        && ((oldPub.getRank() != publication.getRank())
            || (oldPub.getCategoryId() != publication.getCategoryId()))) {
      // update coeficient for all it's authors
      List<Author> authors = authorService.findAuthorsByPublicationId(oldPub.getId());
      for (Author a : authors) {
        perunService.updatePriorityCoeficient(
            sess, a.getId(), authorshipService.calculateNewRank(a.getAuthorships()));
      }
    }

    return result;
  }
 public boolean publicationExists(Publication p) {
   if (p.getId() != null && p.getId() != 0) {
     return publicationDao.findPublicationById(p.getId()) != null;
   }
   if (p.getExternalId() != null
       && p.getExternalId() != 0
       && p.getPublicationSystemId() != null
       && p.getPublicationSystemId() != 0) {
     Publication filter = new Publication();
     filter.setExternalId(p.getExternalId());
     filter.setPublicationSystemId(p.getPublicationSystemId());
     return publicationDao.findPublicationsByFilter(filter).size() >= 1;
   }
   if (p.getIsbn() != null && p.getIsbn() != "") {
     Publication filter = new Publication();
     filter.setIsbn(p.getIsbn());
     return publicationDao.findPublicationsByFilter(filter).size() >= 1;
   }
   return false;
 }
  public int createPublication(PerunSession sess, Publication p) throws CabinetException {

    if (p.getCreatedDate() == null) p.setCreatedDate(new Date());
    if (p.getLocked() == null) {
      p.setLocked(false);
    }
    if (p.getCreatedByUid() == null && sess != null) {
      p.setCreatedByUid(sess.getPerunPrincipal().getUserId());
    }

    if (p.getExternalId() == 0 && p.getPublicationSystemId() == 0) {
      // check existence
      if (publicationExists(p)) {
        throw new CabinetException(
            "Cannot create duplicate publication: " + p, ErrorCodes.PUBLICATION_ALREADY_EXISTS);
      }
      // get internal pub. system
      PublicationSystem filter = new PublicationSystem();
      filter.setFriendlyName("INTERNAL");
      List<PublicationSystem> list =
          publicationSystemService.findPublicationSystemsByFilter(filter);
      if (list == null || list.isEmpty()) {
        throw new CabinetException(
            "Can't create publication, internal publication system is missing");
      }
      // There is only one internal system so, get(0) is safe
      p.setPublicationSystemId(list.get(0).getId());
      //
      stripLongParams(p);
      // create internal
      return publicationDao.createInternalPublication(sess, p);
    } else {
      if (publicationExists(p))
        throw new CabinetException(
            "Cannot create duplicate publication: " + p, ErrorCodes.PUBLICATION_ALREADY_EXISTS);

      stripLongParams(p);

      return publicationDao.createPublication(sess, p);
    }
  }