use of cz.metacentrum.perun.registrar.exceptions.RegistrarException in project perun by CESNET.
the class Vsup method approveApplication.
/**
* Set "membershipExpiration" attribute value to "expirationManual" so it's consumed by services.
* Set value only if membershipExpiration is after manual, or manual is empty.
* If membershipExpiration is null, set 4000-01-01 as unlimited.
*
* Create userExtSource RC to user for future merging.
*/
@Override
public Application approveApplication(PerunSession session, Application app) throws MemberNotExistsException, AttributeNotExistsException, WrongAttributeAssignmentException, UserNotExistsException, WrongAttributeValueException, WrongReferenceAttributeValueException, PrivilegeException {
PerunBl perun = (PerunBl) session.getPerun();
Vo vo = app.getVo();
User user = app.getUser();
if (user == null) {
log.error("At the end of approval action, we should have user present in application: {}", app);
} else {
Member member = perun.getMembersManagerBl().getMemberByUser(session, vo, user);
Date membershipExpiration = null;
Date manualExpiration = null;
Attribute membershipExpirationAttr = perun.getAttributesManagerBl().getAttribute(session, member, AttributesManager.NS_MEMBER_ATTR_DEF + ":membershipExpiration");
if (membershipExpirationAttr.getValue() != null) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
df.setLenient(false);
String expiration = (String) membershipExpirationAttr.getValue();
try {
membershipExpiration = df.parse(expiration);
} catch (ParseException e) {
log.error("Can't parse manual expiration date.", e);
}
}
Attribute manualExpirationAttr = perun.getAttributesManagerBl().getAttribute(session, user, AttributesManager.NS_USER_ATTR_DEF + ":expirationManual");
if (manualExpirationAttr.getValue() != null) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
df.setLenient(false);
String expiration = (String) manualExpirationAttr.getValue();
try {
manualExpiration = df.parse(expiration);
} catch (ParseException e) {
log.error("Can't parse manual expiration date.", e);
}
}
boolean changed = false;
if (membershipExpiration == null) {
// has no membership expiration - set as unlimited - but it shouldn't happened based on VO rules
manualExpirationAttr.setValue("4000-01-01");
changed = true;
} else if (manualExpiration == null || membershipExpiration.after(manualExpiration)) {
// has no manual expiration - set from membership expiration
// OR
// has membership expiration after manual
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String value = df.format(membershipExpiration);
manualExpirationAttr.setValue(value);
changed = true;
}
if (changed) {
// update manual expiration attribute
perun.getAttributesManagerBl().setAttribute(session, user, manualExpirationAttr);
}
}
// create ues RC for future merging
List<ApplicationFormItemData> data = new ArrayList<>();
try {
data = registrar.getApplicationDataById(session, app.getId());
} catch (RegistrarException e) {
// ignore because application's id is not null
}
for (ApplicationFormItemData item : data) {
if (item.getFormItem() != null && Objects.equals(AttributesManager.NS_USER_ATTR_DEF + ":birthNumber", item.getFormItem().getPerunDestinationAttribute())) {
String rc = item.getValue();
if (rc != null && !rc.isEmpty()) {
ExtSource es = perun.getExtSourcesManager().checkOrCreateExtSource(session, "RC", ExtSourcesManager.EXTSOURCE_NAME_INTERNAL);
UserExtSource ues = new UserExtSource(es, 0, rc);
try {
perun.getUsersManagerBl().addUserExtSource(session, app.getUser(), ues);
} catch (UserExtSourceExistsException e) {
log.info("User external source from RC already created.");
}
}
break;
}
}
return app;
}
Aggregations