Search in sources :

Example 1 with EMAIL_SUBSCRIPTION

use of com.redhat.cloud.notifications.models.EndpointType.EMAIL_SUBSCRIPTION in project notifications-backend by RedHatInsights.

the class EndpointRepository method getOrCreateDefaultEmailSubscription.

/**
 * The purpose of this method is to find or create an EMAIL_SUBSCRIPTION endpoint with empty properties. This
 * endpoint is used to aggregate and store in the DB the email actions outcome, which will be used later by the
 * event log. The recipients of the current email action have already been resolved before this step, possibly from
 * multiple endpoints and recipients settings. The properties created below have no impact on the resolution of the
 * action recipients.
 */
public Endpoint getOrCreateDefaultEmailSubscription(String accountId) {
    String query = "FROM Endpoint WHERE accountId = :accountId AND compositeType.type = :endpointType";
    List<Endpoint> emailEndpoints = statelessSessionFactory.getCurrentSession().createQuery(query, Endpoint.class).setParameter("accountId", accountId).setParameter("endpointType", EMAIL_SUBSCRIPTION).getResultList();
    loadProperties(emailEndpoints);
    EmailSubscriptionProperties properties = new EmailSubscriptionProperties();
    Optional<Endpoint> endpointOptional = emailEndpoints.stream().filter(endpoint -> properties.hasSameProperties(endpoint.getProperties(EmailSubscriptionProperties.class))).findFirst();
    if (endpointOptional.isPresent()) {
        return endpointOptional.get();
    }
    Endpoint endpoint = new Endpoint();
    endpoint.setProperties(properties);
    endpoint.setAccountId(accountId);
    endpoint.setEnabled(true);
    endpoint.setDescription("System email endpoint");
    endpoint.setName("Email endpoint");
    endpoint.setType(EMAIL_SUBSCRIPTION);
    endpoint.prePersist();
    properties.setEndpoint(endpoint);
    statelessSessionFactory.getCurrentSession().insert(endpoint);
    statelessSessionFactory.getCurrentSession().insert(endpoint.getProperties());
    return endpoint;
}
Also used : CamelProperties(com.redhat.cloud.notifications.models.CamelProperties) EndpointProperties(com.redhat.cloud.notifications.models.EndpointProperties) EmailSubscriptionProperties(com.redhat.cloud.notifications.models.EmailSubscriptionProperties) Endpoint(com.redhat.cloud.notifications.models.Endpoint) Logger(org.jboss.logging.Logger) Set(java.util.Set) EMAIL_SUBSCRIPTION(com.redhat.cloud.notifications.models.EndpointType.EMAIL_SUBSCRIPTION) UUID(java.util.UUID) CAMEL(com.redhat.cloud.notifications.models.EndpointType.CAMEL) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) WEBHOOK(com.redhat.cloud.notifications.models.EndpointType.WEBHOOK) Inject(javax.inject.Inject) HashSet(java.util.HashSet) EventType(com.redhat.cloud.notifications.models.EventType) List(java.util.List) EndpointType(com.redhat.cloud.notifications.models.EndpointType) StatelessSessionFactory(com.redhat.cloud.notifications.db.StatelessSessionFactory) Map(java.util.Map) Optional(java.util.Optional) WebhookProperties(com.redhat.cloud.notifications.models.WebhookProperties) ApplicationScoped(javax.enterprise.context.ApplicationScoped) Endpoint(com.redhat.cloud.notifications.models.Endpoint) EmailSubscriptionProperties(com.redhat.cloud.notifications.models.EmailSubscriptionProperties)

Aggregations

StatelessSessionFactory (com.redhat.cloud.notifications.db.StatelessSessionFactory)1 CamelProperties (com.redhat.cloud.notifications.models.CamelProperties)1 EmailSubscriptionProperties (com.redhat.cloud.notifications.models.EmailSubscriptionProperties)1 Endpoint (com.redhat.cloud.notifications.models.Endpoint)1 EndpointProperties (com.redhat.cloud.notifications.models.EndpointProperties)1 EndpointType (com.redhat.cloud.notifications.models.EndpointType)1 CAMEL (com.redhat.cloud.notifications.models.EndpointType.CAMEL)1 EMAIL_SUBSCRIPTION (com.redhat.cloud.notifications.models.EndpointType.EMAIL_SUBSCRIPTION)1 WEBHOOK (com.redhat.cloud.notifications.models.EndpointType.WEBHOOK)1 EventType (com.redhat.cloud.notifications.models.EventType)1 WebhookProperties (com.redhat.cloud.notifications.models.WebhookProperties)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1 UUID (java.util.UUID)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 ApplicationScoped (javax.enterprise.context.ApplicationScoped)1