Search in sources :

Example 16 with CabinetException

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

the class CabinetManagerBlImpl method setThanksAttribute.

@Override
public void setThanksAttribute(int userId) throws CabinetException, InternalErrorException {
    List<ThanksForGUI> thanks = getThanksManagerBl().getRichThanksByUserId(userId);
    try {
        // get user
        User u = perun.getUsersManager().getUserById(cabinetSession, userId);
        // get attribute
        AttributeDefinition attrDef = perun.getAttributesManager().getAttributeDefinition(cabinetSession, ATTR_PUBS_NAMESPACE + ":" + ATTR_PUBS_FRIENDLY_NAME);
        Attribute attr = new Attribute(attrDef);
        // if there are thanks to set
        if (thanks != null && !thanks.isEmpty()) {
            // create new values map
            LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
            for (ThanksForGUI t : thanks) {
                Integer count = 1;
                if (map.containsKey(t.getOwnerName())) {
                    // if contains value already, do +1
                    String value = map.get(t.getOwnerName());
                    count = Integer.parseInt(value);
                    count = count + 1;
                }
                map.put(t.getOwnerName(), count.toString());
            }
            attr.setValue(map);
            perun.getAttributesManager().setAttribute(cabinetSession, u, attr);
        } else {
            // empty or null thanks - update to: remove
            perun.getAttributesManager().removeAttribute(cabinetSession, u, attrDef);
        }
    } catch (PerunException e) {
        throw new CabinetException("Failed to update " + ATTR_PUBS_NAMESPACE + ":" + ATTR_PUBS_FRIENDLY_NAME + " in Perun.", ErrorCodes.PERUN_EXCEPTION, e);
    }
}
Also used : ThanksForGUI(cz.metacentrum.perun.cabinet.model.ThanksForGUI) CabinetException(cz.metacentrum.perun.cabinet.bl.CabinetException) PerunException(cz.metacentrum.perun.core.api.exceptions.PerunException) LinkedHashMap(java.util.LinkedHashMap)

Example 17 with CabinetException

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

the class CabinetManagerBlImpl method findExternalPublicationsOfUser.

public List<Publication> findExternalPublicationsOfUser(PerunSession sess, int userId, int yearSince, int yearTill, String pubSysNamespace) throws CabinetException, InternalErrorException {
    // get PubSys
    PublicationSystem ps = getPublicationSystemManagerBl().getPublicationSystemByNamespace(pubSysNamespace);
    // get user
    User user;
    try {
        user = perun.getUsersManagerBl().getUserById(sess, userId);
    } catch (UserNotExistsException ex) {
        throw new CabinetException("User with ID: " + userId + " doesn't exists.", ErrorCodes.PERUN_EXCEPTION, ex);
    }
    // result list
    List<Publication> result = new ArrayList<Publication>();
    // PROCESS MU PUB SYS
    if (ps.getLoginNamespace().equalsIgnoreCase("mu")) {
        // get UCO
        List<UserExtSource> ues = perun.getUsersManagerBl().getUserExtSources(sess, user);
        String authorId = "";
        for (UserExtSource es : ues) {
            // get login from LDAP
            if (es.getExtSource().getName().equalsIgnoreCase("LDAPMU")) {
                // get only UCO
                authorId = es.getLogin();
                break;
            // get login from IDP
            } else if (es.getExtSource().getName().equalsIgnoreCase("https://idp2.ics.muni.cz/idp/shibboleth")) {
                // get only UCO from UCO@mail.muni.cz
                authorId = es.getLogin().substring(0, es.getLogin().indexOf("@"));
                break;
            }
        }
        // check
        if (authorId.isEmpty()) {
            throw new CabinetException("You don't have assigned identity in Perun for MU (LDAPMU / Shibboleth IDP).", ErrorCodes.NO_IDENTITY_FOR_PUBLICATION_SYSTEM);
        }
        // get publications
        result.addAll(findPublicationsInPubSys(authorId, yearSince, yearTill, ps));
        return result;
    // PROCESS ZCU 3.0 PUB SYS
    } else if (ps.getLoginNamespace().equalsIgnoreCase("zcu")) {
        // search is based on "lastName,firstName"
        String authorId = user.getLastName() + "," + user.getFirstName();
        result.addAll(findPublicationsInPubSys(authorId, yearSince, yearTill, ps));
        return result;
    } else {
        log.error("Publication System with namespace: [{}] found but not supported for import.", pubSysNamespace);
        throw new CabinetException("PubSys namespace found but not supported for import.");
    }
}
Also used : UserNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserNotExistsException) ArrayList(java.util.ArrayList) Publication(cz.metacentrum.perun.cabinet.model.Publication) PublicationSystem(cz.metacentrum.perun.cabinet.model.PublicationSystem) CabinetException(cz.metacentrum.perun.cabinet.bl.CabinetException)

Example 18 with CabinetException

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

the class CabinetManagerBlImpl method findPublicationsInPubSys.

// methods --------------------------------------
public List<Publication> findPublicationsInPubSys(String authorId, int yearSince, int yearTill, PublicationSystem ps) throws CabinetException {
    if (StringUtils.isBlank(authorId))
        throw new CabinetException("AuthorId cannot be empty while searching for publications");
    if (ps == null)
        throw new CabinetException("Publication system cannot be null while searching for publications");
    // authorId must be an publication system internal id i.e. UCO! not memberId, userId etc.
    PublicationSystemStrategy prezentator = null;
    try {
        log.debug("Attempting to instantiate class [{}]...", ps.getType());
        prezentator = (PublicationSystemStrategy) Class.forName(ps.getType()).newInstance();
        log.debug("Class [{}] successfully created.", ps.getType());
    } catch (Exception e) {
        throw new CabinetException(e);
    }
    HttpUriRequest request = prezentator.getHttpRequest(authorId, yearSince, yearTill, ps);
    HttpResponse response = prezentator.execute(request);
    if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
        throw new CabinetException("Can't contact publication system. HTTP error code: " + response.getStatusLine().getStatusCode(), ErrorCodes.HTTP_IO_EXCEPTION);
    }
    List<Publication> publications = prezentator.parseHttpResponse(response);
    for (Publication p : publications) {
        // set pub system for founded publications
        p.setPublicationSystemId(ps.getId());
    }
    return publications;
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpResponse(org.apache.http.HttpResponse) Publication(cz.metacentrum.perun.cabinet.model.Publication) CabinetException(cz.metacentrum.perun.cabinet.bl.CabinetException) PublicationSystemStrategy(cz.metacentrum.perun.cabinet.strategy.PublicationSystemStrategy) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) CabinetException(cz.metacentrum.perun.cabinet.bl.CabinetException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) UserNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserNotExistsException) PerunException(cz.metacentrum.perun.core.api.exceptions.PerunException)

Example 19 with CabinetException

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

the class PublicationManagerBlImpl method updatePublication.

@Override
public Publication updatePublication(PerunSession sess, Publication publication) throws CabinetException, InternalErrorException {
    if (publication.getId() == 0 || publication.getExternalId() == 0 || publication.getPublicationSystemId() == 0) {
        // 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 dbPublication = getPublicationByExternalId(publication.getExternalId(), publication.getPublicationSystemId());
    if (publication.getId() != (dbPublication.getId())) {
        throw new CabinetException("Cannot update to duplicate publication: " + publication, ErrorCodes.PUBLICATION_ALREADY_EXISTS);
    }
    // save old pub
    Publication oldPub = getPublicationById(publication.getId());
    // update publication in DB
    getPublicationManagerDao().updatePublication(sess, publication);
    log.debug("{} updated.", publication);
    // if updated and rank or category was changed
    if ((oldPub.getRank() != publication.getRank()) || (oldPub.getCategoryId() != publication.getCategoryId())) {
        synchronized (CabinetManagerBlImpl.class) {
            // update coefficient for all it's authors
            List<Author> authors = getAuthorshipManagerBl().getAuthorsByPublicationId(oldPub.getId());
            for (Author a : authors) {
                getCabinetManagerBl().updatePriorityCoefficient(sess, a.getId(), getAuthorshipManagerBl().calculateNewRank(a.getAuthorships()));
            }
        }
    }
    return publication;
}
Also used : Publication(cz.metacentrum.perun.cabinet.model.Publication) Author(cz.metacentrum.perun.cabinet.model.Author) CabinetException(cz.metacentrum.perun.cabinet.bl.CabinetException)

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