Search in sources :

Example 6 with RegistrarException

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;
}
Also used : User(cz.metacentrum.perun.core.api.User) Attribute(cz.metacentrum.perun.core.api.Attribute) ArrayList(java.util.ArrayList) PerunBl(cz.metacentrum.perun.core.bl.PerunBl) RegistrarException(cz.metacentrum.perun.registrar.exceptions.RegistrarException) ApplicationFormItemData(cz.metacentrum.perun.registrar.model.ApplicationFormItemData) Date(java.util.Date) UserExtSourceExistsException(cz.metacentrum.perun.core.api.exceptions.UserExtSourceExistsException) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) Vo(cz.metacentrum.perun.core.api.Vo) ParseException(java.text.ParseException) ExtSource(cz.metacentrum.perun.core.api.ExtSource) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) Member(cz.metacentrum.perun.core.api.Member) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

RegistrarException (cz.metacentrum.perun.registrar.exceptions.RegistrarException)6 MailSentForApplication (cz.metacentrum.perun.audit.events.MailManagerEvents.MailSentForApplication)3 MessagingException (javax.mail.MessagingException)3 ExtSource (cz.metacentrum.perun.core.api.ExtSource)2 Member (cz.metacentrum.perun.core.api.Member)2 User (cz.metacentrum.perun.core.api.User)2 UserExtSource (cz.metacentrum.perun.core.api.UserExtSource)2 UserExtSourceExistsException (cz.metacentrum.perun.core.api.exceptions.UserExtSourceExistsException)2 PerunBl (cz.metacentrum.perun.core.bl.PerunBl)2 Application (cz.metacentrum.perun.registrar.model.Application)2 ApplicationFormItemData (cz.metacentrum.perun.registrar.model.ApplicationFormItemData)2 ParseException (java.text.ParseException)2 MimeMessage (javax.mail.internet.MimeMessage)2 MailException (org.springframework.mail.MailException)2 AuditEvent (cz.metacentrum.perun.audit.events.AuditEvent)1 InvitationSentEvent (cz.metacentrum.perun.audit.events.MailManagerEvents.InvitationSentEvent)1 Attribute (cz.metacentrum.perun.core.api.Attribute)1 Vo (cz.metacentrum.perun.core.api.Vo)1 AttributeNotExistsException (cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException)1 ExtSourceNotExistsException (cz.metacentrum.perun.core.api.exceptions.ExtSourceNotExistsException)1