Search in sources :

Example 61 with TechnicalManagementException

use of io.gravitee.rest.api.service.exceptions.TechnicalManagementException in project gravitee-management-rest-api by gravitee-io.

the class TenantServiceImpl method findByIdAndReference.

@Override
public TenantEntity findByIdAndReference(String tenantId, String referenceId, TenantReferenceType tenantReferenceType) {
    try {
        LOGGER.debug("Find tenant by ID: {}", tenantId);
        Optional<Tenant> optTenant = tenantRepository.findByIdAndReference(tenantId, referenceId, repoTenantReferenceType(tenantReferenceType));
        if (!optTenant.isPresent()) {
            throw new TenantNotFoundException(tenantId);
        }
        return convert(optTenant.get());
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to find tenant by ID", ex);
        throw new TechnicalManagementException("An error occurs while trying to find tenant by ID", ex);
    }
}
Also used : Tenant(io.gravitee.repository.management.model.Tenant) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) TenantNotFoundException(io.gravitee.rest.api.service.exceptions.TenantNotFoundException) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 62 with TechnicalManagementException

use of io.gravitee.rest.api.service.exceptions.TechnicalManagementException 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);
    }
}
Also used : Ticket(io.gravitee.repository.management.model.Ticket) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) HashMap(java.util.HashMap) EmailRequiredException(io.gravitee.rest.api.service.exceptions.EmailRequiredException) UuidString(io.gravitee.rest.api.service.common.UuidString) EmailNotificationBuilder(io.gravitee.rest.api.service.builder.EmailNotificationBuilder) Date(java.util.Date) SupportUnavailableException(io.gravitee.rest.api.service.exceptions.SupportUnavailableException) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 63 with TechnicalManagementException

use of io.gravitee.rest.api.service.exceptions.TechnicalManagementException in project gravitee-management-rest-api by gravitee-io.

the class TicketServiceImpl method search.

@Override
public Page<TicketEntity> search(TicketQuery query, Sortable sortable, Pageable pageable) {
    try {
        LOGGER.debug("search tickets");
        TicketCriteria criteria = queryToCriteriaBuilder(query).build();
        Page<Ticket> tickets = ticketRepository.search(criteria, buildSortable(sortable), new PageableBuilder().pageNumber(pageable.getPageNumber() - 1).pageSize(pageable.getPageSize()).build());
        List<TicketEntity> entities = tickets.getContent().stream().map(this::getApiNameAndApplicationName).map(this::convert).collect(Collectors.toList());
        LOGGER.debug("search tickets - Done with {} elements", entities.size());
        return new Page<>(entities, tickets.getPageNumber() + 1, (int) tickets.getPageElements(), tickets.getTotalElements());
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to search tickets", ex);
        throw new TechnicalManagementException("An error occurs while trying to search tickets", ex);
    }
}
Also used : Ticket(io.gravitee.repository.management.model.Ticket) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) TicketCriteria(io.gravitee.repository.management.api.search.TicketCriteria) Page(io.gravitee.common.data.domain.Page) PageableBuilder(io.gravitee.repository.management.api.search.builder.PageableBuilder) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 64 with TechnicalManagementException

use of io.gravitee.rest.api.service.exceptions.TechnicalManagementException in project gravitee-management-rest-api by gravitee-io.

the class UserMetadataServiceImpl method deleteAllByCustomFieldId.

@Override
public void deleteAllByCustomFieldId(String key, String refId, CustomUserFieldReferenceType refType) {
    try {
        // CustomField is linked to an Org
        // we have to retrieve users based on org and then
        // delete the user metadata identifier by the field key and the userId
        String orgId = null;
        if (refType.equals(CustomUserFieldReferenceType.ENVIRONMENT)) {
            final EnvironmentEntity cufEnvironment = this.environmentService.findById(refId);
            orgId = cufEnvironment.getOrganizationId();
        } else {
            orgId = refId;
        }
        final UserCriteria criteria = new UserCriteria.Builder().organizationId(orgId).build();
        int pageNumber = 0;
        Page<User> pageOfUser = null;
        do {
            pageOfUser = this.userRepository.search(criteria, new PageableBuilder().pageNumber(pageNumber).pageSize(100).build());
            for (User user : pageOfUser.getContent()) {
                try {
                    this.delete(CustomFieldSanitizer.formatKeyValue(key), USER, user.getId());
                } catch (MetadataNotFoundException e) {
                    LOGGER.debug("Metadata key={}, refType={}, refId={} not found," + " ignore error because we want to delete it and user may not have this metadata", key, USER, user.getId());
                }
            }
            pageNumber++;
        } while (pageOfUser.getPageElements() > 0);
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurred while trying to all metadata with key {}", key, ex);
        throw new TechnicalManagementException("An error occurred while trying to all metadata with key " + key, ex);
    }
}
Also used : MetadataNotFoundException(io.gravitee.rest.api.service.exceptions.MetadataNotFoundException) User(io.gravitee.repository.management.model.User) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) UserCriteria(io.gravitee.repository.management.api.search.UserCriteria) PageableBuilder(io.gravitee.repository.management.api.search.builder.PageableBuilder) PageableBuilder(io.gravitee.repository.management.api.search.builder.PageableBuilder) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 65 with TechnicalManagementException

use of io.gravitee.rest.api.service.exceptions.TechnicalManagementException in project gravitee-management-rest-api by gravitee-io.

the class OrganizationServiceImpl method initialize.

@Override
public void initialize() {
    Organization defaultOrganization = new Organization();
    defaultOrganization.setId(GraviteeContext.getDefaultOrganization());
    defaultOrganization.setName("Default organization");
    defaultOrganization.setDescription("Default organization");
    try {
        organizationRepository.create(defaultOrganization);
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to create default organization", ex);
        throw new TechnicalManagementException("An error occurs while trying to create default organization", ex);
    }
}
Also used : Organization(io.gravitee.repository.management.model.Organization) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Aggregations

TechnicalManagementException (io.gravitee.rest.api.service.exceptions.TechnicalManagementException)149 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)120 UuidString (io.gravitee.rest.api.service.common.UuidString)26 Date (java.util.Date)23 Component (org.springframework.stereotype.Component)18 Logger (org.slf4j.Logger)17 LoggerFactory (org.slf4j.LoggerFactory)17 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)16 Collectors (java.util.stream.Collectors)13 Autowired (org.springframework.beans.factory.annotation.Autowired)13 IOException (java.io.IOException)12 JsonNode (com.fasterxml.jackson.databind.JsonNode)11 Rating (io.gravitee.repository.management.model.Rating)9 ApiRatingUnavailableException (io.gravitee.rest.api.service.exceptions.ApiRatingUnavailableException)9 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)8 Dictionary (io.gravitee.repository.management.model.Dictionary)8 AuditService (io.gravitee.rest.api.service.AuditService)8 java.util (java.util)8 Theme (io.gravitee.repository.management.model.Theme)6 List (java.util.List)6