Search in sources :

Example 1 with ApiModelEntity

use of io.gravitee.management.model.ApiModelEntity in project gravitee-management-rest-api by gravitee-io.

the class TicketServiceImpl method create.

@Override
public void create(final String userId, final NewTicketEntity ticketEntity) {
    if (!enabled) {
        throw new SupportUnavailableException();
    }
    LOGGER.info("Creating a support ticket: {}", ticketEntity);
    final Map<String, Object> parameters = new HashMap<>();
    final UserEntity user = userService.findById(userId);
    if (user.getEmail() == null) {
        throw new EmailRequiredException(userId);
    }
    parameters.put("user", user);
    final String emailTo;
    if (ticketEntity.getApi() == null) {
        final MetadataEntity emailMetadata = metadataService.findDefaultByKey(METADATA_EMAIL_SUPPORT_KEY);
        if (emailMetadata == null) {
            throw new IllegalStateException("The support email metadata has not been found");
        }
        emailTo = emailMetadata.getValue();
    } else {
        final ApiModelEntity api = apiService.findByIdForTemplates(ticketEntity.getApi());
        final String apiMetadataEmailSupport = api.getMetadata().get(METADATA_EMAIL_SUPPORT_KEY);
        if (apiMetadataEmailSupport == null) {
            throw new IllegalStateException("The support email API metadata has not been found");
        }
        emailTo = apiMetadataEmailSupport;
        parameters.put("api", api);
    }
    if (DEFAULT_METADATA_EMAIL_SUPPORT.equals(emailTo)) {
        throw new IllegalStateException("The support email API metadata has not been changed");
    }
    if (ticketEntity.getApplication() != null) {
        parameters.put("application", applicationService.findById(ticketEntity.getApplication()));
    }
    parameters.put("content", ticketEntity.getContent().replaceAll("(\r\n|\n)", "<br />"));
    emailService.sendEmailNotification(new EmailNotificationBuilder().from(user.getEmail()).fromName(user.getFirstname() + ' ' + user.getLastname()).to(emailTo).subject(ticketEntity.getSubject()).copyToSender(ticketEntity.isCopyToSender()).template(SUPPORT_TICKET).params(parameters).build());
}
Also used : MetadataEntity(io.gravitee.management.model.MetadataEntity) HashMap(java.util.HashMap) ApiModelEntity(io.gravitee.management.model.ApiModelEntity) EmailRequiredException(io.gravitee.management.service.exceptions.EmailRequiredException) EmailNotificationBuilder(io.gravitee.management.service.builder.EmailNotificationBuilder) UserEntity(io.gravitee.management.model.UserEntity) SupportUnavailableException(io.gravitee.management.service.exceptions.SupportUnavailableException)

Aggregations

ApiModelEntity (io.gravitee.management.model.ApiModelEntity)1 MetadataEntity (io.gravitee.management.model.MetadataEntity)1 UserEntity (io.gravitee.management.model.UserEntity)1 EmailNotificationBuilder (io.gravitee.management.service.builder.EmailNotificationBuilder)1 EmailRequiredException (io.gravitee.management.service.exceptions.EmailRequiredException)1 SupportUnavailableException (io.gravitee.management.service.exceptions.SupportUnavailableException)1 HashMap (java.util.HashMap)1