Search in sources :

Example 11 with Notification

use of org.orcid.jaxb.model.v3.dev1.notification.Notification in project ORCID-Source by ORCID.

the class NotificationsTest method createPermissionNotification.

@Test
public void createPermissionNotification() throws JSONException {
    NotificationPermission notification = unmarshallFromPath("/notification_3.0_dev1/samples/notification-permission-3.0_dev1.xml");
    notification.setPutCode(null);
    String accessToken = oauthHelper.getClientCredentialsAccessToken(client1ClientId, client1ClientSecret, ScopePathType.PREMIUM_NOTIFICATION);
    ClientResponse response = notificationsClient_V3_0_dev1.addPermissionNotificationXml(testUser1OrcidId, notification, accessToken);
    assertNotNull(response);
    assertEquals(Response.Status.CREATED.getStatusCode(), response.getStatus());
    String locationPath = response.getLocation().getPath();
    assertTrue("Location header path should match pattern, but was " + locationPath, locationPath.matches(".*/v3.0_dev1/" + testUser1OrcidId + "/notification-permission/\\d+"));
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) NotificationPermission(org.orcid.jaxb.model.v3.dev1.notification.permission.NotificationPermission) Test(org.junit.Test)

Example 12 with Notification

use of org.orcid.jaxb.model.v3.dev1.notification.Notification in project ORCID-Source by ORCID.

the class MapperFacadeFactory method getObject.

@Override
public MapperFacade getObject() throws Exception {
    MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
    // Register converters
    ConverterFactory converterFactory = mapperFactory.getConverterFactory();
    converterFactory.registerConverter("externalIdentifierIdConverter", new ExternalIdentifierTypeConverter());
    // Register factories
    mapperFactory.registerObjectFactory(new WorkEntityFactory(workDao), TypeFactory.<NotificationWorkEntity>valueOf(NotificationWorkEntity.class), TypeFactory.<Item>valueOf(Item.class));
    // Custom notification
    ClassMapBuilder<NotificationCustom, NotificationCustomEntity> notificationCustomClassMap = mapperFactory.classMap(NotificationCustom.class, NotificationCustomEntity.class);
    registerSourceConverters(mapperFactory, notificationCustomClassMap);
    mapCommonFields(notificationCustomClassMap).register();
    // Permission notification
    ClassMapBuilder<NotificationPermission, NotificationAddItemsEntity> notificationPermissionClassMap = mapperFactory.classMap(NotificationPermission.class, NotificationAddItemsEntity.class);
    registerSourceConverters(mapperFactory, notificationPermissionClassMap);
    mapCommonFields(notificationPermissionClassMap.field("authorizationUrl.uri", "authorizationUrl").field("items.items", "notificationItems").customize(new CustomMapper<NotificationPermission, NotificationAddItemsEntity>() {

        @Override
        public void mapAtoB(NotificationPermission notification, NotificationAddItemsEntity entity, MappingContext context) {
            if (StringUtils.isBlank(entity.getAuthorizationUrl())) {
                String authUrl = orcidUrlManager.getBaseUrl() + notification.getAuthorizationUrl().getPath();
                // validate
                validateAndConvertToURI(authUrl);
                entity.setAuthorizationUrl(authUrl);
            }
        }

        @Override
        public void mapBtoA(NotificationAddItemsEntity entity, NotificationPermission notification, MappingContext context) {
            AuthorizationUrl authUrl = notification.getAuthorizationUrl();
            if (authUrl != null) {
                authUrl.setPath(extractFullPath(authUrl.getUri()));
                authUrl.setHost(orcidUrlManager.getBaseHost());
            }
        }
    })).register();
    // Institutional sign in notification
    ClassMapBuilder<NotificationInstitutionalConnection, NotificationInstitutionalConnectionEntity> institutionalConnectionNotificationClassMap = mapperFactory.classMap(NotificationInstitutionalConnection.class, NotificationInstitutionalConnectionEntity.class);
    registerSourceConverters(mapperFactory, institutionalConnectionNotificationClassMap);
    mapCommonFields(institutionalConnectionNotificationClassMap.field("authorizationUrl.uri", "authorizationUrl").customize(new CustomMapper<NotificationInstitutionalConnection, NotificationInstitutionalConnectionEntity>() {

        @Override
        public void mapAtoB(NotificationInstitutionalConnection notification, NotificationInstitutionalConnectionEntity entity, MappingContext context) {
            if (StringUtils.isBlank(entity.getAuthorizationUrl())) {
                String authUrl = orcidUrlManager.getBaseUrl() + notification.getAuthorizationUrl().getPath();
                // validate
                validateAndConvertToURI(authUrl);
                entity.setAuthorizationUrl(authUrl);
            }
        }

        @Override
        public void mapBtoA(NotificationInstitutionalConnectionEntity entity, NotificationInstitutionalConnection notification, MappingContext context) {
            AuthorizationUrl authUrl = notification.getAuthorizationUrl();
            if (authUrl != null) {
                authUrl.setPath(extractFullPath(authUrl.getUri()));
                authUrl.setHost(orcidUrlManager.getBaseHost());
            }
            String providerId = entity.getAuthenticationProviderId();
            if (StringUtils.isNotBlank(providerId)) {
                String idpName = identityProviderManager.retrieveIdentitifyProviderName(providerId);
                notification.setIdpName(idpName);
            } else {
                notification.setIdpName(LAST_RESORT_IDENTITY_PROVIDER_NAME);
            }
        }
    })).register();
    // Amend notification
    ClassMapBuilder<NotificationAmended, NotificationAmendedEntity> amendNotificationClassMap = mapperFactory.classMap(NotificationAmended.class, NotificationAmendedEntity.class);
    registerSourceConverters(mapperFactory, amendNotificationClassMap);
    mapCommonFields(amendNotificationClassMap).register();
    mapperFactory.classMap(NotificationItemEntity.class, Item.class).fieldMap("externalIdType", "externalIdentifier.type").converter("externalIdentifierIdConverter").add().field("externalIdValue", "externalIdentifier.value").byDefault().register();
    return mapperFactory.getMapperFacade();
}
Also used : NotificationInstitutionalConnectionEntity(org.orcid.persistence.jpa.entities.NotificationInstitutionalConnectionEntity) ConverterFactory(ma.glasnost.orika.converter.ConverterFactory) NotificationCustom(org.orcid.jaxb.model.v3.dev1.notification.custom.NotificationCustom) NotificationAddItemsEntity(org.orcid.persistence.jpa.entities.NotificationAddItemsEntity) MappingContext(ma.glasnost.orika.MappingContext) AuthorizationUrl(org.orcid.jaxb.model.v3.dev1.notification.permission.AuthorizationUrl) Item(org.orcid.jaxb.model.v3.dev1.notification.permission.Item) DefaultMapperFactory(ma.glasnost.orika.impl.DefaultMapperFactory) NotificationPermission(org.orcid.jaxb.model.v3.dev1.notification.permission.NotificationPermission) NotificationAmendedEntity(org.orcid.persistence.jpa.entities.NotificationAmendedEntity) NotificationInstitutionalConnection(org.orcid.model.v3.dev1.notification.institutional_sign_in.NotificationInstitutionalConnection) WorkEntityFactory(org.orcid.core.adapter.impl.WorkEntityFactory) NotificationCustomEntity(org.orcid.persistence.jpa.entities.NotificationCustomEntity) NotificationWorkEntity(org.orcid.persistence.jpa.entities.NotificationWorkEntity) ExternalIdentifierTypeConverter(org.orcid.core.adapter.jsonidentifier.converter.ExternalIdentifierTypeConverter) DefaultMapperFactory(ma.glasnost.orika.impl.DefaultMapperFactory) MapperFactory(ma.glasnost.orika.MapperFactory) NotificationAmended(org.orcid.jaxb.model.v3.dev1.notification.amended.NotificationAmended)

Example 13 with Notification

use of org.orcid.jaxb.model.v3.dev1.notification.Notification in project ORCID-Source by ORCID.

the class NotificationManagerTest method filterActionedNotificationAlertsTest.

@Test
public void filterActionedNotificationAlertsTest() {
    TargetProxyHelper.injectIntoProxy(notificationManager, "notificationDao", mockNotificationDao);
    when(mockNotificationDao.findByOricdAndId(Matchers.anyString(), Matchers.anyLong())).thenReturn(null);
    List<Notification> notifications = IntStream.range(0, 10).mapToObj(new IntFunction<Notification>() {

        @Override
        public Notification apply(int value) {
            if (value % 3 == 0) {
                NotificationInstitutionalConnection n = new NotificationInstitutionalConnection();
                n.setSource(new Source("0000-0000-0000-0000"));
                n.setPutCode(Long.valueOf(value));
                return n;
            } else {
                NotificationPermission n = new NotificationPermission();
                n.setPutCode(Long.valueOf(value));
                return n;
            }
        }
    }).collect(Collectors.toList());
    assertEquals(10, notifications.size());
    notifications = notificationManager.filterActionedNotificationAlerts(notifications, "some-orcid");
    assertEquals(6, notifications.size());
    for (Notification n : notifications) {
        assertEquals(NotificationType.PERMISSION, n.getNotificationType());
        assertNotNull(n.getPutCode());
        assertThat(n.getPutCode(), not(anyOf(is(Long.valueOf(0)), is(Long.valueOf(3)), is(Long.valueOf(6)), is(Long.valueOf(9)))));
    }
}
Also used : NotificationInstitutionalConnection(org.orcid.model.v3.dev1.notification.institutional_sign_in.NotificationInstitutionalConnection) IntFunction(java.util.function.IntFunction) NotificationPermission(org.orcid.jaxb.model.v3.dev1.notification.permission.NotificationPermission) Notification(org.orcid.jaxb.model.v3.dev1.notification.Notification) Source(org.orcid.jaxb.model.v3.dev1.common.Source) DBUnitTest(org.orcid.test.DBUnitTest) Test(org.junit.Test)

Example 14 with Notification

use of org.orcid.jaxb.model.v3.dev1.notification.Notification in project ORCID-Source by ORCID.

the class NotificationManagerImpl method sendAmendEmail.

@Override
public void sendAmendEmail(String userOrcid, AmendedSection amendedSection, Collection<Item> items) {
    String amenderOrcid = sourceManager.retrieveSourceOrcid();
    ProfileEntity record = profileEntityCacheManager.retrieve(userOrcid);
    Locale locale = getUserLocaleFromProfileEntity(record);
    if (amenderOrcid == null) {
        LOGGER.info("Not sending amend email to {} because amender is null", userOrcid);
        return;
    }
    if (amenderOrcid.equals(userOrcid)) {
        LOGGER.debug("Not sending amend email, because self edited: {}", userOrcid);
        return;
    }
    Boolean sendChangeNotifications = record.getSendChangeNotifications();
    if (sendChangeNotifications == null || !sendChangeNotifications) {
        LOGGER.debug("Not sending amend email, because option to send change notifications is disabled: {}", userOrcid);
        return;
    }
    org.orcid.jaxb.model.common_v2.OrcidType amenderType = profileDao.retrieveOrcidType(amenderOrcid);
    if (amenderType != null && OrcidType.ADMIN.equals(OrcidType.fromValue(amenderType.value()))) {
        LOGGER.debug("Not sending amend email, because modified by admin ({}): {}", amenderOrcid, userOrcid);
        return;
    }
    String subject = getSubject("email.subject.amend", locale);
    // Create map of template params
    Map<String, Object> templateParams = new HashMap<String, Object>();
    templateParams.put("emailName", deriveEmailFriendlyName(record));
    templateParams.put("orcid", userOrcid);
    templateParams.put("amenderName", extractAmenderName(userOrcid, amenderOrcid));
    templateParams.put("baseUri", orcidUrlManager.getBaseUrl());
    templateParams.put("baseUriHttp", orcidUrlManager.getBaseUriHttp());
    templateParams.put("subject", subject);
    addMessageParams(templateParams, locale);
    // Generate body from template
    String body = templateManager.processTemplate("amend_email.ftl", templateParams);
    // Generate html from template
    String html = templateManager.processTemplate("amend_email_html.ftl", templateParams);
    boolean notificationsEnabled = record.getEnableNotifications();
    if (notificationsEnabled) {
        NotificationAmended notification = new NotificationAmended();
        notification.setNotificationType(NotificationType.AMENDED);
        notification.setAmendedSection(amendedSection);
        if (items != null) {
            notification.setItems(new Items(new ArrayList<>(items)));
        }
        createNotification(userOrcid, notification);
    } else {
        String primaryEmail = emailManager.findPrimaryEmail(userOrcid).getEmail();
        mailGunManager.sendEmail(AMEND_NOTIFY_ORCID_ORG, primaryEmail, subject, body, html);
    }
}
Also used : Locale(java.util.Locale) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ProfileEntity(org.orcid.persistence.jpa.entities.ProfileEntity) Items(org.orcid.jaxb.model.v3.dev1.notification.permission.Items) NotificationAmended(org.orcid.jaxb.model.v3.dev1.notification.amended.NotificationAmended)

Example 15 with Notification

use of org.orcid.jaxb.model.v3.dev1.notification.Notification in project ORCID-Source by ORCID.

the class NotificationManagerImpl method sendDelegationRequestEmail.

@Override
public void sendDelegationRequestEmail(String managedOrcid, String trustedOrcid, String link) {
    // Create map of template params
    Map<String, Object> templateParams = new HashMap<String, Object>();
    templateParams.put("baseUri", orcidUrlManager.getBaseUrl());
    templateParams.put("baseUriHttp", orcidUrlManager.getBaseUriHttp());
    templateParams.put("link", link);
    ProfileEntity managedEntity = profileEntityCacheManager.retrieve(managedOrcid);
    ProfileEntity trustedEntity = profileEntityCacheManager.retrieve(trustedOrcid);
    String emailNameForDelegate = deriveEmailFriendlyName(managedEntity);
    String trustedOrcidName = deriveEmailFriendlyName(trustedEntity);
    templateParams.put("emailNameForDelegate", emailNameForDelegate);
    templateParams.put("trustedOrcidName", trustedOrcidName);
    templateParams.put("trustedOrcidValue", trustedOrcid);
    templateParams.put("managedOrcidValue", managedOrcid);
    String primaryEmail = emailManager.findPrimaryEmail(managedOrcid).getEmail();
    if (primaryEmail == null) {
        LOGGER.info("Cant send admin delegate email if primary email is null: {}", managedOrcid);
        return;
    }
    org.orcid.jaxb.model.common_v2.Locale locale = managedEntity.getLocale();
    Locale userLocale = LocaleUtils.toLocale("en");
    if (locale != null) {
        userLocale = LocaleUtils.toLocale(locale.value());
    }
    addMessageParams(templateParams, userLocale);
    String htmlBody = templateManager.processTemplate("admin_delegate_request_html.ftl", templateParams);
    // Send message
    if (apiRecordCreationEmailEnabled) {
        String subject = messages.getMessage("email.subject.admin_as_delegate", new Object[] { trustedOrcidName }, userLocale);
        boolean notificationsEnabled = trustedEntity != null ? trustedEntity.getEnableNotifications() : false;
        if (notificationsEnabled) {
            NotificationCustom notification = new NotificationCustom();
            notification.setNotificationType(NotificationType.CUSTOM);
            notification.setSubject(subject);
            notification.setBodyHtml(htmlBody);
            createNotification(managedOrcid, notification);
        } else {
            mailGunManager.sendEmail(DELEGATE_NOTIFY_ORCID_ORG, primaryEmail, subject, null, htmlBody);
        }
        profileEventDao.persist(new ProfileEventEntity(managedOrcid, ProfileEventType.ADMIN_PROFILE_DELEGATION_REQUEST));
    } else {
        LOGGER.debug("Not sending admin delegate email, because API record creation email option is disabled. Message would have been: {}", htmlBody);
    }
}
Also used : Locale(java.util.Locale) ProfileEventEntity(org.orcid.persistence.jpa.entities.ProfileEventEntity) HashMap(java.util.HashMap) ProfileEntity(org.orcid.persistence.jpa.entities.ProfileEntity) NotificationCustom(org.orcid.jaxb.model.v3.dev1.notification.custom.NotificationCustom)

Aggregations

Test (org.junit.Test)15 Notification (org.orcid.jaxb.model.v3.dev1.notification.Notification)13 NotificationPermission (org.orcid.jaxb.model.v3.dev1.notification.permission.NotificationPermission)13 NotificationCustom (org.orcid.jaxb.model.v3.dev1.notification.custom.NotificationCustom)9 HashMap (java.util.HashMap)8 AuthorizationUrl (org.orcid.jaxb.model.v3.dev1.notification.permission.AuthorizationUrl)8 ProfileEntity (org.orcid.persistence.jpa.entities.ProfileEntity)8 ClientResponse (com.sun.jersey.api.client.ClientResponse)7 ClientDetailsEntity (org.orcid.persistence.jpa.entities.ClientDetailsEntity)6 Locale (java.util.Locale)5 NotificationInstitutionalConnection (org.orcid.model.v3.dev1.notification.institutional_sign_in.NotificationInstitutionalConnection)5 Source (org.orcid.jaxb.model.v3.dev1.common.Source)4 NotificationAmended (org.orcid.jaxb.model.v3.dev1.notification.amended.NotificationAmended)4 NotificationEntity (org.orcid.persistence.jpa.entities.NotificationEntity)4 SourceEntity (org.orcid.persistence.jpa.entities.SourceEntity)4 IOException (java.io.IOException)3 URISyntaxException (java.net.URISyntaxException)3 ArrayList (java.util.ArrayList)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3 Email (org.orcid.jaxb.model.v3.dev1.record.Email)3