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;
}
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);
}
}
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;
}
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");
}
}
}
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("&", "&");
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;
}
Aggregations