use of io.gravitee.rest.api.service.exceptions.SupportUnavailableException in project gravitee-management-rest-api by gravitee-io.
the class TicketServiceImpl method create.
@Override
public TicketEntity create(final String userId, final NewTicketEntity ticketEntity, final String referenceId, final ParameterReferenceType referenceType) {
try {
if (!isEnabled(referenceId, referenceType)) {
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;
final ApiModelEntity api;
final ApplicationEntity applicationEntity;
if (ticketEntity.getApi() == null || ticketEntity.getApi().isEmpty()) {
api = null;
final MetadataEntity emailMetadata = metadataService.findDefaultByKey(DefaultMetadataUpgrader.METADATA_EMAIL_SUPPORT_KEY);
if (emailMetadata == null) {
throw new IllegalStateException("The support email metadata has not been found");
}
emailTo = emailMetadata.getValue();
} else {
api = apiService.findByIdForTemplates(ticketEntity.getApi(), true);
final String apiMetadataEmailSupport = api.getMetadata().get(DefaultMetadataUpgrader.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 (DefaultMetadataUpgrader.DEFAULT_METADATA_EMAIL_SUPPORT.equals(emailTo)) {
throw new IllegalStateException("The support email API metadata has not been changed");
}
if (ticketEntity.getApplication() != null && !ticketEntity.getApplication().isEmpty()) {
applicationEntity = applicationService.findById(ticketEntity.getApplication());
parameters.put("application", applicationEntity);
} else {
applicationEntity = null;
}
parameters.put("content", ticketEntity.getContent().replaceAll("(\r\n|\n)", "<br />"));
parameters.put("ticketSubject", ticketEntity.getSubject());
final String fromName = user.getFirstname() == null ? user.getEmail() : user.getFirstname() + ' ' + user.getLastname();
emailService.sendEmailNotification(new EmailNotificationBuilder().replyTo(user.getEmail()).fromName(fromName).to(emailTo).copyToSender(ticketEntity.isCopyToSender()).template(TEMPLATES_FOR_ACTION_SUPPORT_TICKET).params(parameters).build());
sendUserNotification(user, api, applicationEntity);
Ticket ticket = convert(ticketEntity);
ticket.setId(UuidString.generateRandom());
ticket.setCreatedAt(new Date());
ticket.setFromUser(userId);
Ticket createdTicket = ticketRepository.create(ticket);
return convert(createdTicket);
} catch (TechnicalException ex) {
LOGGER.error("An error occurs while trying to create a ticket {}", ticketEntity, ex);
throw new TechnicalManagementException("An error occurs while trying to create a ticket " + ticketEntity, ex);
}
}
Aggregations