Search in sources :

Example 1 with MetadataEntity

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

the class MetadataServiceImpl method update.

@Override
public MetadataEntity update(final UpdateMetadataEntity metadataEntity) {
    try {
        // First we prevent the duplicate metadata name
        final Optional<Metadata> optionalMetadata = metadataRepository.findByReferenceType(MetadataReferenceType.DEFAULT).stream().filter(metadata -> !metadataEntity.getKey().equals(metadata.getKey()) && metadataEntity.getName().equalsIgnoreCase(metadata.getName())).findAny();
        if (optionalMetadata.isPresent()) {
            throw new DuplicateMetadataNameException(optionalMetadata.get().getName());
        }
        checkMetadataFormat(metadataEntity.getFormat(), metadataEntity.getValue());
        final Metadata metadata = convert(metadataEntity);
        final Date now = new Date();
        metadata.setUpdatedAt(now);
        metadataRepository.update(metadata);
        // Audit
        auditService.createPortalAuditLog(Collections.singletonMap(METADATA, metadata.getKey()), METADATA_UPDATED, metadata.getCreatedAt(), null, metadata);
        return convert(metadata);
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurred while trying to update metadata {}", metadataEntity.getName(), ex);
        throw new TechnicalManagementException("An error occurred while trying to update metadata " + metadataEntity.getName(), ex);
    }
}
Also used : URL(java.net.URL) Date(java.util.Date) DuplicateMetadataNameException(io.gravitee.management.service.exceptions.DuplicateMetadataNameException) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) SimpleDateFormat(java.text.SimpleDateFormat) MetadataEntity(io.gravitee.management.model.MetadataEntity) AuditService(io.gravitee.management.service.AuditService) MetadataRepository(io.gravitee.repository.management.api.MetadataRepository) InternetAddress(javax.mail.internet.InternetAddress) IdGenerator(io.gravitee.common.utils.IdGenerator) MetadataFormat(io.gravitee.management.model.MetadataFormat) METADATA_CREATED(io.gravitee.repository.management.model.Metadata.AuditEvent.METADATA_CREATED) MetadataReferenceType(io.gravitee.repository.management.model.MetadataReferenceType) METADATA(io.gravitee.repository.management.model.Audit.AuditProperties.METADATA) Logger(org.slf4j.Logger) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException) UpdateMetadataEntity(io.gravitee.management.model.UpdateMetadataEntity) Collectors(java.util.stream.Collectors) METADATA_UPDATED(io.gravitee.repository.management.model.Metadata.AuditEvent.METADATA_UPDATED) MetadataService(io.gravitee.management.service.MetadataService) NewMetadataEntity(io.gravitee.management.model.NewMetadataEntity) Component(org.springframework.stereotype.Component) List(java.util.List) METADATA_DELETED(io.gravitee.repository.management.model.Metadata.AuditEvent.METADATA_DELETED) Metadata(io.gravitee.repository.management.model.Metadata) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) Optional(java.util.Optional) Collections(java.util.Collections) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) DuplicateMetadataNameException(io.gravitee.management.service.exceptions.DuplicateMetadataNameException) Metadata(io.gravitee.repository.management.model.Metadata) Date(java.util.Date) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 2 with MetadataEntity

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

the class MetadataServiceImpl method convert.

private MetadataEntity convert(final Metadata metadata) {
    final MetadataEntity metadataEntity = new MetadataEntity();
    metadataEntity.setKey(metadata.getKey());
    metadataEntity.setName(metadata.getName());
    metadataEntity.setValue(metadata.getValue());
    metadataEntity.setFormat(MetadataFormat.valueOf(metadata.getFormat().name()));
    return metadataEntity;
}
Also used : MetadataEntity(io.gravitee.management.model.MetadataEntity) UpdateMetadataEntity(io.gravitee.management.model.UpdateMetadataEntity) NewMetadataEntity(io.gravitee.management.model.NewMetadataEntity)

Example 3 with MetadataEntity

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

the class MetadataServiceImpl method create.

@Override
public MetadataEntity create(final NewMetadataEntity metadataEntity) {
    // if no format defined, we just set String format
    if (metadataEntity.getFormat() == null) {
        metadataEntity.setFormat(MetadataFormat.STRING);
    }
    try {
        // First we prevent the duplicate metadata name
        final Optional<MetadataEntity> optionalMetadata = findAllDefault().stream().filter(metadata -> metadataEntity.getName().equalsIgnoreCase(metadata.getName())).findAny();
        if (optionalMetadata.isPresent()) {
            throw new DuplicateMetadataNameException(optionalMetadata.get().getName());
        }
        checkMetadataFormat(metadataEntity.getFormat(), metadataEntity.getValue());
        final Metadata metadata = convert(metadataEntity);
        final Date now = new Date();
        metadata.setCreatedAt(now);
        metadata.setUpdatedAt(now);
        metadataRepository.create(metadata);
        // Audit
        auditService.createPortalAuditLog(Collections.singletonMap(METADATA, metadata.getKey()), METADATA_CREATED, metadata.getCreatedAt(), null, metadata);
        return convert(metadata);
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurred while trying to create metadata {}", metadataEntity.getName(), ex);
        throw new TechnicalManagementException("An error occurred while trying to create metadata " + metadataEntity.getName(), ex);
    }
}
Also used : URL(java.net.URL) Date(java.util.Date) DuplicateMetadataNameException(io.gravitee.management.service.exceptions.DuplicateMetadataNameException) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) SimpleDateFormat(java.text.SimpleDateFormat) MetadataEntity(io.gravitee.management.model.MetadataEntity) AuditService(io.gravitee.management.service.AuditService) MetadataRepository(io.gravitee.repository.management.api.MetadataRepository) InternetAddress(javax.mail.internet.InternetAddress) IdGenerator(io.gravitee.common.utils.IdGenerator) MetadataFormat(io.gravitee.management.model.MetadataFormat) METADATA_CREATED(io.gravitee.repository.management.model.Metadata.AuditEvent.METADATA_CREATED) MetadataReferenceType(io.gravitee.repository.management.model.MetadataReferenceType) METADATA(io.gravitee.repository.management.model.Audit.AuditProperties.METADATA) Logger(org.slf4j.Logger) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException) UpdateMetadataEntity(io.gravitee.management.model.UpdateMetadataEntity) Collectors(java.util.stream.Collectors) METADATA_UPDATED(io.gravitee.repository.management.model.Metadata.AuditEvent.METADATA_UPDATED) MetadataService(io.gravitee.management.service.MetadataService) NewMetadataEntity(io.gravitee.management.model.NewMetadataEntity) Component(org.springframework.stereotype.Component) List(java.util.List) METADATA_DELETED(io.gravitee.repository.management.model.Metadata.AuditEvent.METADATA_DELETED) Metadata(io.gravitee.repository.management.model.Metadata) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) Optional(java.util.Optional) Collections(java.util.Collections) MetadataEntity(io.gravitee.management.model.MetadataEntity) UpdateMetadataEntity(io.gravitee.management.model.UpdateMetadataEntity) NewMetadataEntity(io.gravitee.management.model.NewMetadataEntity) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) DuplicateMetadataNameException(io.gravitee.management.service.exceptions.DuplicateMetadataNameException) Metadata(io.gravitee.repository.management.model.Metadata) Date(java.util.Date) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 4 with MetadataEntity

use of io.gravitee.management.model.MetadataEntity 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

MetadataEntity (io.gravitee.management.model.MetadataEntity)4 NewMetadataEntity (io.gravitee.management.model.NewMetadataEntity)3 UpdateMetadataEntity (io.gravitee.management.model.UpdateMetadataEntity)3 IdGenerator (io.gravitee.common.utils.IdGenerator)2 MetadataFormat (io.gravitee.management.model.MetadataFormat)2 AuditService (io.gravitee.management.service.AuditService)2 MetadataService (io.gravitee.management.service.MetadataService)2 DuplicateMetadataNameException (io.gravitee.management.service.exceptions.DuplicateMetadataNameException)2 TechnicalManagementException (io.gravitee.management.service.exceptions.TechnicalManagementException)2 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)2 MetadataRepository (io.gravitee.repository.management.api.MetadataRepository)2 METADATA (io.gravitee.repository.management.model.Audit.AuditProperties.METADATA)2 Metadata (io.gravitee.repository.management.model.Metadata)2 METADATA_CREATED (io.gravitee.repository.management.model.Metadata.AuditEvent.METADATA_CREATED)2 METADATA_DELETED (io.gravitee.repository.management.model.Metadata.AuditEvent.METADATA_DELETED)2 METADATA_UPDATED (io.gravitee.repository.management.model.Metadata.AuditEvent.METADATA_UPDATED)2 MetadataReferenceType (io.gravitee.repository.management.model.MetadataReferenceType)2 URL (java.net.URL)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Collections (java.util.Collections)2