Search in sources :

Example 6 with CabinetException

use of cz.metacentrum.perun.cabinet.bl.CabinetException in project perun by CESNET.

the class ThanksManagerBlImpl method createThanks.

// methods -------------------------
public Thanks createThanks(PerunSession sess, Thanks t) throws CabinetException, InternalErrorException {
    if (t.getCreatedDate() == null) {
        t.setCreatedDate(new Date());
    }
    if (thanksExist(t)) {
        throw new CabinetException("Can't create duplicate thanks.", ErrorCodes.THANKS_ALREADY_EXISTS);
    }
    t = getThanksManagerDao().createThanks(sess, t);
    log.debug("{} created.", t);
    // recalculate thanks for all publication's authors
    List<Author> authors = new ArrayList<Author>();
    authors = getAuthorshipManagerBl().getAuthorsByPublicationId(t.getPublicationId());
    // sort to prevent locking
    synchronized (ThanksManagerBlImpl.class) {
        for (Author a : authors) {
            getCabinetManagerBl().setThanksAttribute(a.getId());
        }
    }
    return t;
}
Also used : ArrayList(java.util.ArrayList) Author(cz.metacentrum.perun.cabinet.model.Author) CabinetException(cz.metacentrum.perun.cabinet.bl.CabinetException) Date(java.util.Date)

Example 7 with CabinetException

use of cz.metacentrum.perun.cabinet.bl.CabinetException in project perun by CESNET.

the class PublicationManagerBlImpl method createPublication.

// business methods --------------------------------
@Override
public Publication createPublication(PerunSession sess, Publication p) throws CabinetException, InternalErrorException {
    if (p.getCreatedDate() == null)
        p.setCreatedDate(new Date());
    p.setCreatedByUid(sess.getPerunPrincipal().getUserId());
    // check existence
    if (publicationExists(p)) {
        throw new CabinetException("Cannot create duplicate publication: " + p, ErrorCodes.PUBLICATION_ALREADY_EXISTS);
    }
    Publication createdPublication;
    if (p.getExternalId() == 0 && p.getPublicationSystemId() == 0) {
        // get internal pub. system
        PublicationSystem ps = getPublicationSystemManagerBl().getPublicationSystemByName("INTERNAL");
        // There is only one internal system so, get(0) is safe
        p.setPublicationSystemId(ps.getId());
    }
    stripLongParams(p);
    createdPublication = getPublicationManagerDao().createPublication(sess, p);
    log.debug("{} created.", createdPublication);
    return createdPublication;
}
Also used : Publication(cz.metacentrum.perun.cabinet.model.Publication) CabinetException(cz.metacentrum.perun.cabinet.bl.CabinetException) PublicationSystem(cz.metacentrum.perun.cabinet.model.PublicationSystem) Date(java.util.Date)

Example 8 with CabinetException

use of cz.metacentrum.perun.cabinet.bl.CabinetException in project perun by CESNET.

the class PublicationManagerBlImpl method deletePublication.

@Override
public void deletePublication(PerunSession sess, Publication publication) throws CabinetException, InternalErrorException {
    try {
        // delete authors
        for (Authorship a : getAuthorshipManagerBl().getAuthorshipsByPublicationId(publication.getId())) {
            getAuthorshipManagerBl().deleteAuthorship(sess, a);
        }
        // delete thanks
        for (Thanks t : getThanksManagerBl().getThanksByPublicationId(publication.getId())) {
            getThanksManagerBl().deleteThanks(sess, t);
        }
        // delete publication
        if (AuthzResolver.isAuthorized(sess, Role.PERUNADMIN)) {
            // only perun admin can actually delete publication
            getPublicationManagerDao().deletePublication(publication);
            log.debug("{} deleted.", publication);
        }
    // publications without authors are: "to be deleted by perun admin"
    } 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);
    }
}
Also used : Authorship(cz.metacentrum.perun.cabinet.model.Authorship) Thanks(cz.metacentrum.perun.cabinet.model.Thanks) CabinetException(cz.metacentrum.perun.cabinet.bl.CabinetException) PerunException(cz.metacentrum.perun.core.api.exceptions.PerunException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException)

Example 9 with CabinetException

use of cz.metacentrum.perun.cabinet.bl.CabinetException in project perun by CESNET.

the class ThanksManagerIntegrationTest method createThanksWhenExistsByOwnerAndPublicationTest.

@Test
public void createThanksWhenExistsByOwnerAndPublicationTest() throws Exception {
    System.out.println("ThanksManagerIntegrationTest.createThanksWhenExistsByOwnerAndPublicationTest");
    Thanks t = new Thanks();
    t.setCreatedBy(sess.getPerunPrincipal().getActor());
    t.setCreatedDate(new Date());
    t.setOwnerId(owner.getId());
    t.setPublicationId(publicationOne.getId());
    getCabinetManager().createThanks(sess, t);
    try {
        getCabinetManager().createThanks(sess, t);
    } catch (CabinetException ex) {
        if (!ex.getType().equals(ErrorCodes.THANKS_ALREADY_EXISTS)) {
            fail("Different exception was thrown when creating \"same\" Thanks: " + ex + ", but expected: THANKS_ALREADY_EXISTS");
        }
    }
}
Also used : Thanks(cz.metacentrum.perun.cabinet.model.Thanks) CabinetException(cz.metacentrum.perun.cabinet.bl.CabinetException) Date(java.util.Date) Test(org.junit.Test)

Example 10 with CabinetException

use of cz.metacentrum.perun.cabinet.bl.CabinetException in project perun by CESNET.

the class ThanksManagerIntegrationTest method createThanksWhenExistsByIDTest.

@Test
public void createThanksWhenExistsByIDTest() throws Exception {
    System.out.println("ThanksManagerIntegrationTest.createThanksWhenExistsByIDTest");
    Thanks t = new Thanks();
    t.setCreatedBy(sess.getPerunPrincipal().getActor());
    t.setCreatedDate(new Date());
    t.setOwnerId(owner.getId());
    t.setPublicationId(publicationOne.getId());
    getCabinetManager().createThanks(sess, t);
    try {
        getCabinetManager().createThanks(sess, t);
    } catch (CabinetException ex) {
        if (!ex.getType().equals(ErrorCodes.THANKS_ALREADY_EXISTS)) {
            fail("Different exception was thrown when creating \"same\" Thanks: " + ex + ", but expected: THANKS_ALREADY_EXISTS");
        }
    }
}
Also used : Thanks(cz.metacentrum.perun.cabinet.model.Thanks) CabinetException(cz.metacentrum.perun.cabinet.bl.CabinetException) Date(java.util.Date) Test(org.junit.Test)

Aggregations

CabinetException (cz.metacentrum.perun.cabinet.bl.CabinetException)19 Publication (cz.metacentrum.perun.cabinet.model.Publication)8 Date (java.util.Date)7 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)5 PerunException (cz.metacentrum.perun.core.api.exceptions.PerunException)4 Author (cz.metacentrum.perun.cabinet.model.Author)3 Authorship (cz.metacentrum.perun.cabinet.model.Authorship)3 PublicationSystem (cz.metacentrum.perun.cabinet.model.PublicationSystem)3 Thanks (cz.metacentrum.perun.cabinet.model.Thanks)3 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)3 IOException (java.io.IOException)3 HttpResponse (org.apache.http.HttpResponse)3 UserNotExistsException (cz.metacentrum.perun.core.api.exceptions.UserNotExistsException)2 StringReader (java.io.StringReader)2 DocumentBuilder (javax.xml.parsers.DocumentBuilder)2 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 XPath (javax.xml.xpath.XPath)2 XPathExpression (javax.xml.xpath.XPathExpression)2