Search in sources :

Example 1 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 {
    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 2 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 {
    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
        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 3 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 {
    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 4 with CabinetException

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

the class PublicationSystemStrategyIntegrationTest method contactPublicationSystemOBDTest.

@Test
public void contactPublicationSystemOBDTest() throws Exception {
    System.out.println("PublicationSystemStrategyIntegrationTest.contactPublicationSystemOBDTest");
    PublicationSystem publicationSystem = getCabinetManager().getPublicationSystemByNamespace("zcu");
    assertNotNull(publicationSystem);
    PublicationSystemStrategy prezentator = (PublicationSystemStrategy) Class.forName(publicationSystem.getType()).newInstance();
    assertNotNull(prezentator);
    PublicationSystemStrategy obd = (PublicationSystemStrategy) Class.forName(publicationSystem.getType()).newInstance();
    assertNotNull(obd);
    String authorId = "Sitera,Jiří";
    int yearSince = 2006;
    int yearTill = 2009;
    HttpUriRequest request = obd.getHttpRequest(authorId, yearSince, yearTill, publicationSystem);
    try {
        HttpResponse response = obd.execute(request);
        assertNotNull(response);
    } catch (CabinetException ex) {
        if (!ex.getType().equals(ErrorCodes.HTTP_IO_EXCEPTION)) {
            fail("Different exception code, was: " + ex.getType() + ", but expected: HTTP_IO_EXCEPTION.");
        // fail if different error
        } else {
            System.out.println("-- Test silently skipped because of HTTP_IO_EXCEPTION");
        }
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpResponse(org.apache.http.HttpResponse) PublicationSystem(cz.metacentrum.perun.cabinet.model.PublicationSystem) CabinetException(cz.metacentrum.perun.cabinet.bl.CabinetException) CabinetBaseIntegrationTest(cz.metacentrum.perun.cabinet.CabinetBaseIntegrationTest) Test(org.junit.Test)

Example 5 with CabinetException

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

the class EuropePMCStrategy method parseResponse.

/**
 * Parse String response as XML document and retrieve Publications from it.
 * @param xml XML response from EuropePMC
 * @return List of Publications
 * @throws CabinetException If anything fails
 */
protected List<Publication> parseResponse(String xml) throws CabinetException {
    assert xml != null;
    List<Publication> result = new ArrayList<Publication>();
    // hook for titles with &
    xml = xml.replace("&", "&amp;");
    log.trace("RESPONSE: " + xml);
    // Create new document factory builder
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;
    try {
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException ex) {
        throw new CabinetException("Error when creating newDocumentBuilder.", ex);
    }
    Document doc;
    try {
        doc = builder.parse(new InputSource(new StringReader(xml)));
    } catch (SAXParseException ex) {
        throw new CabinetException("Error when parsing uri by document builder.", ErrorCodes.MALFORMED_HTTP_RESPONSE, ex);
    } catch (SAXException ex) {
        throw new CabinetException("Problem with parsing is more complex, not only invalid characters.", ErrorCodes.MALFORMED_HTTP_RESPONSE, ex);
    } catch (IOException ex) {
        throw new CabinetException("Error when parsing uri by document builder. Problem with input or output.", ErrorCodes.MALFORMED_HTTP_RESPONSE, ex);
    }
    // Prepare xpath expression
    XPathFactory xPathFactory = XPathFactory.newInstance();
    XPath xpath = xPathFactory.newXPath();
    XPathExpression publicationsQuery;
    try {
        publicationsQuery = xpath.compile("/responseWrapper/resultList/result");
    } catch (XPathExpressionException ex) {
        throw new CabinetException("Error when compiling xpath query.", ex);
    }
    NodeList nodeList;
    try {
        nodeList = (NodeList) publicationsQuery.evaluate(doc, XPathConstants.NODESET);
    } catch (XPathExpressionException ex) {
        throw new CabinetException("Error when evaluate xpath query on document.", ex);
    }
    // Test if there is any nodeset in result
    if (nodeList.getLength() == 0) {
        // There is no results, return empty subjects
        return result;
    }
    // Iterate through nodes and convert them to Map<String,String>
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node singleNode = nodeList.item(i);
        // remove node from original structure in order to keep access time constant (otherwise is exp.)
        singleNode.getParentNode().removeChild(singleNode);
        try {
            Publication publication = convertNodeToPublication(singleNode, xPathFactory);
            result.add(publication);
        } catch (InternalErrorException ex) {
            log.error("Unable to parse Publication:", ex);
        }
    }
    return result;
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) Publication(cz.metacentrum.perun.cabinet.model.Publication) IOException(java.io.IOException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) XPathFactory(javax.xml.xpath.XPathFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXParseException(org.xml.sax.SAXParseException) StringReader(java.io.StringReader) CabinetException(cz.metacentrum.perun.cabinet.bl.CabinetException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Aggregations

CabinetException (cz.metacentrum.perun.cabinet.bl.CabinetException)21 Publication (cz.metacentrum.perun.cabinet.model.Publication)10 ArrayList (java.util.ArrayList)7 Date (java.util.Date)7 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)6 Test (org.junit.Test)6 Author (cz.metacentrum.perun.cabinet.model.Author)4 PerunException (cz.metacentrum.perun.core.api.exceptions.PerunException)4 IOException (java.io.IOException)4 XPath (javax.xml.xpath.XPath)4 XPathExpression (javax.xml.xpath.XPathExpression)4 XPathExpressionException (javax.xml.xpath.XPathExpressionException)4 Node (org.w3c.dom.Node)4 NodeList (org.w3c.dom.NodeList)4 Authorship (cz.metacentrum.perun.cabinet.model.Authorship)3 PublicationSystem (cz.metacentrum.perun.cabinet.model.PublicationSystem)3 Thanks (cz.metacentrum.perun.cabinet.model.Thanks)3 StringReader (java.io.StringReader)3 DocumentBuilder (javax.xml.parsers.DocumentBuilder)3 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)3