Search in sources :

Example 1 with Template

use of io.gravitee.am.model.Template in project gravitee-access-management by gravitee-io.

the class EmailTemplateServiceImpl method create0.

private Single<Email> create0(ReferenceType referenceType, String referenceId, String client, NewEmail newEmail, User principal) {
    String emailId = RandomString.generate();
    // check if email is unique
    return checkEmailUniqueness(referenceType, referenceId, client, newEmail.getTemplate().template()).flatMap(irrelevant -> {
        Email email = new Email();
        email.setId(emailId);
        email.setReferenceType(referenceType);
        email.setReferenceId(referenceId);
        email.setClient(client);
        email.setEnabled(newEmail.isEnabled());
        email.setTemplate(newEmail.getTemplate().template());
        email.setFrom(newEmail.getFrom());
        email.setFromName(newEmail.getFromName());
        email.setSubject(newEmail.getSubject());
        email.setContent(newEmail.getContent());
        email.setExpiresAfter(newEmail.getExpiresAfter());
        email.setCreatedAt(new Date());
        email.setUpdatedAt(email.getCreatedAt());
        return emailRepository.create(email);
    }).flatMap(email -> {
        // create event for sync process
        Event event = new Event(Type.EMAIL, new Payload(email.getId(), email.getReferenceType(), email.getReferenceId(), Action.CREATE));
        return eventService.create(event).flatMap(__ -> Single.just(email));
    }).onErrorResumeNext(ex -> {
        if (ex instanceof AbstractManagementException) {
            return Single.error(ex);
        }
        LOGGER.error("An error occurs while trying to create a email", ex);
        return Single.error(new TechnicalManagementException("An error occurs while trying to create a email", ex));
    }).doOnSuccess(email -> auditService.report(AuditBuilder.builder(EmailTemplateAuditBuilder.class).principal(principal).type(EventType.EMAIL_TEMPLATE_CREATED).email(email))).doOnError(throwable -> auditService.report(AuditBuilder.builder(EmailTemplateAuditBuilder.class).principal(principal).type(EventType.EMAIL_TEMPLATE_CREATED).throwable(throwable)));
}
Also used : EmailAlreadyExistsException(io.gravitee.am.service.exception.EmailAlreadyExistsException) Date(java.util.Date) Completable(io.reactivex.Completable) Maybe(io.reactivex.Maybe) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) AuditService(io.gravitee.am.service.AuditService) Single(io.reactivex.Single) Event(io.gravitee.am.model.common.event.Event) Type(io.gravitee.am.common.event.Type) TechnicalManagementException(io.gravitee.am.service.exception.TechnicalManagementException) Flowable(io.reactivex.Flowable) User(io.gravitee.am.identityprovider.api.User) AuditBuilder(io.gravitee.am.service.reporter.builder.AuditBuilder) ReferenceType(io.gravitee.am.model.ReferenceType) EventService(io.gravitee.am.service.EventService) Email(io.gravitee.am.model.Email) Action(io.gravitee.am.common.event.Action) Logger(org.slf4j.Logger) EmailTemplateService(io.gravitee.am.service.EmailTemplateService) EventType(io.gravitee.am.common.audit.EventType) AbstractManagementException(io.gravitee.am.service.exception.AbstractManagementException) RandomString(io.gravitee.am.common.utils.RandomString) EmailTemplateAuditBuilder(io.gravitee.am.service.reporter.builder.management.EmailTemplateAuditBuilder) UpdateEmail(io.gravitee.am.service.model.UpdateEmail) NewEmail(io.gravitee.am.service.model.NewEmail) Component(org.springframework.stereotype.Component) Template(io.gravitee.am.model.Template) Payload(io.gravitee.am.model.common.event.Payload) EmailNotFoundException(io.gravitee.am.service.exception.EmailNotFoundException) EmailRepository(io.gravitee.am.repository.management.api.EmailRepository) Lazy(org.springframework.context.annotation.Lazy) AbstractManagementException(io.gravitee.am.service.exception.AbstractManagementException) Email(io.gravitee.am.model.Email) UpdateEmail(io.gravitee.am.service.model.UpdateEmail) NewEmail(io.gravitee.am.service.model.NewEmail) EmailTemplateAuditBuilder(io.gravitee.am.service.reporter.builder.management.EmailTemplateAuditBuilder) Event(io.gravitee.am.model.common.event.Event) Payload(io.gravitee.am.model.common.event.Payload) RandomString(io.gravitee.am.common.utils.RandomString) Date(java.util.Date) TechnicalManagementException(io.gravitee.am.service.exception.TechnicalManagementException)

Example 2 with Template

use of io.gravitee.am.model.Template in project gravitee-access-management by gravitee-io.

the class FormServiceImpl method create0.

private Single<Form> create0(ReferenceType referenceType, String referenceId, String client, NewForm newForm, User principal) {
    String formId = RandomString.generate();
    // check if form is unique
    return checkFormUniqueness(referenceType, referenceId, client, newForm.getTemplate().template()).flatMap(irrelevant -> {
        Form form = new Form();
        form.setId(formId);
        form.setReferenceType(referenceType);
        form.setReferenceId(referenceId);
        form.setClient(client);
        form.setEnabled(newForm.isEnabled());
        form.setTemplate(newForm.getTemplate().template());
        form.setContent(newForm.getContent());
        form.setAssets(newForm.getAssets());
        form.setCreatedAt(new Date());
        form.setUpdatedAt(form.getCreatedAt());
        return formRepository.create(form);
    }).flatMap(page -> {
        // create event for sync process
        Event event = new Event(Type.FORM, new Payload(page.getId(), page.getReferenceType(), page.getReferenceId(), Action.CREATE));
        return eventService.create(event).flatMap(__ -> Single.just(page));
    }).onErrorResumeNext(ex -> {
        if (ex instanceof AbstractManagementException) {
            return Single.error(ex);
        }
        LOGGER.error("An error occurs while trying to create a form", ex);
        return Single.error(new TechnicalManagementException("An error occurs while trying to create a form", ex));
    }).doOnSuccess(form -> auditService.report(AuditBuilder.builder(FormTemplateAuditBuilder.class).principal(principal).type(EventType.FORM_TEMPLATE_CREATED).form(form))).doOnError(throwable -> auditService.report(AuditBuilder.builder(FormTemplateAuditBuilder.class).principal(principal).type(EventType.FORM_TEMPLATE_CREATED).throwable(throwable)));
}
Also used : Date(java.util.Date) FormRepository(io.gravitee.am.repository.management.api.FormRepository) Completable(io.reactivex.Completable) Maybe(io.reactivex.Maybe) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) AuditService(io.gravitee.am.service.AuditService) Single(io.reactivex.Single) Event(io.gravitee.am.model.common.event.Event) Type(io.gravitee.am.common.event.Type) FormAlreadyExistsException(io.gravitee.am.service.exception.FormAlreadyExistsException) TechnicalManagementException(io.gravitee.am.service.exception.TechnicalManagementException) Flowable(io.reactivex.Flowable) User(io.gravitee.am.identityprovider.api.User) Form(io.gravitee.am.model.Form) AuditBuilder(io.gravitee.am.service.reporter.builder.AuditBuilder) ReferenceType(io.gravitee.am.model.ReferenceType) EventService(io.gravitee.am.service.EventService) NewForm(io.gravitee.am.service.model.NewForm) Action(io.gravitee.am.common.event.Action) Logger(org.slf4j.Logger) FormTemplateAuditBuilder(io.gravitee.am.service.reporter.builder.management.FormTemplateAuditBuilder) EventType(io.gravitee.am.common.audit.EventType) AbstractManagementException(io.gravitee.am.service.exception.AbstractManagementException) RandomString(io.gravitee.am.common.utils.RandomString) Component(org.springframework.stereotype.Component) List(java.util.List) FormNotFoundException(io.gravitee.am.service.exception.FormNotFoundException) Template(io.gravitee.am.model.Template) Payload(io.gravitee.am.model.common.event.Payload) UpdateForm(io.gravitee.am.service.model.UpdateForm) FormService(io.gravitee.am.service.FormService) Lazy(org.springframework.context.annotation.Lazy) AbstractManagementException(io.gravitee.am.service.exception.AbstractManagementException) Form(io.gravitee.am.model.Form) NewForm(io.gravitee.am.service.model.NewForm) UpdateForm(io.gravitee.am.service.model.UpdateForm) Event(io.gravitee.am.model.common.event.Event) Payload(io.gravitee.am.model.common.event.Payload) RandomString(io.gravitee.am.common.utils.RandomString) FormTemplateAuditBuilder(io.gravitee.am.service.reporter.builder.management.FormTemplateAuditBuilder) Date(java.util.Date) TechnicalManagementException(io.gravitee.am.service.exception.TechnicalManagementException)

Aggregations

EventType (io.gravitee.am.common.audit.EventType)2 Action (io.gravitee.am.common.event.Action)2 Type (io.gravitee.am.common.event.Type)2 RandomString (io.gravitee.am.common.utils.RandomString)2 User (io.gravitee.am.identityprovider.api.User)2 ReferenceType (io.gravitee.am.model.ReferenceType)2 Template (io.gravitee.am.model.Template)2 Event (io.gravitee.am.model.common.event.Event)2 Payload (io.gravitee.am.model.common.event.Payload)2 AuditService (io.gravitee.am.service.AuditService)2 EventService (io.gravitee.am.service.EventService)2 AbstractManagementException (io.gravitee.am.service.exception.AbstractManagementException)2 TechnicalManagementException (io.gravitee.am.service.exception.TechnicalManagementException)2 AuditBuilder (io.gravitee.am.service.reporter.builder.AuditBuilder)2 Completable (io.reactivex.Completable)2 Flowable (io.reactivex.Flowable)2 Maybe (io.reactivex.Maybe)2 Single (io.reactivex.Single)2 Date (java.util.Date)2 Logger (org.slf4j.Logger)2