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);
}
}
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.");
}
}
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;
}
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;
}
Aggregations