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());
}
Aggregations