Search in sources :

Example 1 with BusinessException

use of com.icthh.xm.commons.exceptions.BusinessException in project xm-ms-entity by xm-online.

the class TenantService method addTenant.

public void addTenant(Tenant tenant) {
    StopWatch stopWatch = StopWatch.createStarted();
    log.info("START - SETUP:CreateTenant: tenantKey: {}", tenant);
    if (!Constants.TENANT_XM.equals(TenantContextUtils.getRequiredTenantKeyValue(tenantContextHolder))) {
        throw new BusinessException("Only 'XM' tenant allow to create new tenant");
    }
    try {
        String tenantName = tenant.getTenantKey().toUpperCase();
        tenantListRepository.addTenant(tenantName);
        addEntitySpecification(tenantName);
        addWebAppSpecification(tenantName);
        tenantDatabaseService.createSchema(tenant);
        tenantDatabaseService.createProfile(tenantName);
        tenantElasticService.create(tenant);
        log.info("STOP  - SETUP:CreateTenant: tenantKey: {}, result: OK, time = {} ms", tenant, stopWatch.getTime());
    } catch (Exception e) {
        log.info("STOP  - SETUP:CreateTenant: tenantKey: {}, result: FAIL, error: {}, time = {} ms", tenant, e.getMessage(), stopWatch.getTime());
        throw e;
    }
}
Also used : BusinessException(com.icthh.xm.commons.exceptions.BusinessException) BusinessException(com.icthh.xm.commons.exceptions.BusinessException) StopWatch(org.apache.commons.lang3.time.StopWatch)

Example 2 with BusinessException

use of com.icthh.xm.commons.exceptions.BusinessException in project xm-ms-entity by xm-online.

the class StorageRepository method store.

private String store(InputStream stream, Integer size, String contentType, String name) {
    try {
        if (size != null && contentType != null && "image".equals(contentType.substring(0, 5))) {
            stream = ImageResizeUtil.resize(stream, size);
        }
        String filename = UUID.randomUUID().toString() + "." + FilenameUtils.getExtension(name);
        amazonS3Template.save(filename, stream);
        IOUtils.closeQuietly(stream);
        String prefix = String.format(applicationProperties.getAmazon().getAws().getTemplate(), applicationProperties.getAmazon().getS3().getBucket());
        return prefix + filename;
    } catch (IOException e) {
        log.error("Error storing file", e);
        throw new BusinessException("Error storing file");
    }
}
Also used : BusinessException(com.icthh.xm.commons.exceptions.BusinessException) IOException(java.io.IOException)

Example 3 with BusinessException

use of com.icthh.xm.commons.exceptions.BusinessException in project xm-ms-entity by xm-online.

the class XmEntityServiceImpl method exportEntities.

@LogicExtensionPoint("Export")
@Override
public byte[] exportEntities(String fileFormat, String typeKey) {
    Set<String> typeKeys = xmEntitySpecService.findNonAbstractTypesByPrefix(typeKey).stream().map(TypeSpec::getKey).collect(Collectors.toSet());
    List<XmEntity> xmEntities = xmEntityRepository.findAllByTypeKeyIn(new PageRequest(0, Integer.MAX_VALUE), typeKeys).getContent();
    ModelMapper modelMapper = new ModelMapper();
    List<SimpleExportXmEntityDto> simpleEntities = xmEntities.stream().map(entity -> modelMapper.map(entity, SimpleExportXmEntityDto.class)).collect(Collectors.toList());
    switch(FileFormatEnum.valueOf(fileFormat.toUpperCase())) {
        case CSV:
            return EntityToCsvConverterUtils.toCsv(simpleEntities, SimpleExportXmEntityDto.class);
        case XLSX:
            return EntityToExcelConverterUtils.toExcel(simpleEntities, typeKey);
        default:
            throw new BusinessException(ErrorConstants.ERR_VALIDATION, String.format("Converter doesn't support '%s' file format", fileFormat));
    }
}
Also used : XmEntityService(com.icthh.xm.ms.entity.service.XmEntityService) StorageService(com.icthh.xm.ms.entity.service.StorageService) RequiredArgsConstructor(lombok.RequiredArgsConstructor) Autowired(org.springframework.beans.factory.annotation.Autowired) StringUtils(org.apache.commons.lang3.StringUtils) XmEntityPermittedRepository(com.icthh.xm.ms.entity.repository.XmEntityPermittedRepository) LogicExtensionPoint(com.icthh.xm.commons.lep.LogicExtensionPoint) Collectors.toMap(java.util.stream.Collectors.toMap) Vote(com.icthh.xm.ms.entity.domain.Vote) Arrays.asList(java.util.Arrays.asList) FileFormatEnum(com.icthh.xm.ms.entity.domain.FileFormatEnum) EntityToCsvConverterUtils(com.icthh.xm.ms.entity.domain.converter.EntityToCsvConverterUtils) Map(java.util.Map) EntityToExcelConverterUtils(com.icthh.xm.ms.entity.domain.converter.EntityToExcelConverterUtils) Pageable(org.springframework.data.domain.Pageable) FindWithPermission(com.icthh.xm.commons.permission.annotation.FindWithPermission) URI(java.net.URI) Resource(org.springframework.core.io.Resource) Link(com.icthh.xm.ms.entity.domain.Link) LifecycleLepStrategy(com.icthh.xm.ms.entity.service.LifecycleLepStrategy) BusinessException(com.icthh.xm.commons.exceptions.BusinessException) Collections.emptyList(java.util.Collections.emptyList) PageRequest(org.springframework.data.domain.PageRequest) Set(java.util.Set) TypeSpec(com.icthh.xm.ms.entity.domain.spec.TypeSpec) LinkService(com.icthh.xm.ms.entity.service.LinkService) XmEntitySearchRepository(com.icthh.xm.ms.entity.repository.search.XmEntitySearchRepository) Page(org.springframework.data.domain.Page) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) Slf4j(lombok.extern.slf4j.Slf4j) HttpEntity(org.springframework.http.HttpEntity) List(java.util.List) ProfileService(com.icthh.xm.ms.entity.service.ProfileService) XmEntitySpecService(com.icthh.xm.ms.entity.service.XmEntitySpecService) XmEntity(com.icthh.xm.ms.entity.domain.XmEntity) Specification(org.springframework.data.jpa.domain.Specification) Comment(com.icthh.xm.ms.entity.domain.Comment) Optional(java.util.Optional) XmAuthenticationContextHolder(com.icthh.xm.commons.security.XmAuthenticationContextHolder) CustomCollectionUtils.nullSafe(com.icthh.xm.ms.entity.util.CustomCollectionUtils.nullSafe) Constants(com.icthh.xm.ms.entity.config.Constants) XmEntityTypeKeyResolver(com.icthh.xm.ms.entity.lep.keyresolver.XmEntityTypeKeyResolver) XmEntityIdKeyTypeKey(com.icthh.xm.ms.entity.projection.XmEntityIdKeyTypeKey) Callable(java.util.concurrent.Callable) LepService(com.icthh.xm.commons.lep.spring.LepService) ModelMapper(org.modelmapper.ModelMapper) XmEntityStateProjection(com.icthh.xm.ms.entity.projection.XmEntityStateProjection) Propagation(org.springframework.transaction.annotation.Propagation) SimpleExportXmEntityDto(com.icthh.xm.ms.entity.domain.SimpleExportXmEntityDto) EntityNotFoundException(com.icthh.xm.commons.exceptions.EntityNotFoundException) IdOrKey(com.icthh.xm.ms.entity.domain.ext.IdOrKey) AttachmentService(com.icthh.xm.ms.entity.service.AttachmentService) SEARCH_BUILDER_TYPE(com.icthh.xm.ms.entity.domain.spec.LinkSpec.SEARCH_BUILDER_TYPE) Rating(com.icthh.xm.ms.entity.domain.Rating) Iterator(java.util.Iterator) Optional.ofNullable(java.util.Optional.ofNullable) ErrorConstants(com.icthh.xm.commons.exceptions.ErrorConstants) LifecycleLepStrategyFactory(com.icthh.xm.ms.entity.service.LifecycleLepStrategyFactory) ObjectUtils(org.springframework.util.ObjectUtils) LinkSpec(com.icthh.xm.ms.entity.domain.spec.LinkSpec) Attachment(com.icthh.xm.ms.entity.domain.Attachment) Tag(com.icthh.xm.ms.entity.domain.Tag) Location(com.icthh.xm.ms.entity.domain.Location) Consumer(java.util.function.Consumer) StateSpec(com.icthh.xm.ms.entity.domain.spec.StateSpec) XmEntityPermittedSearchRepository(com.icthh.xm.ms.entity.repository.search.XmEntityPermittedSearchRepository) XmEntityRepository(com.icthh.xm.ms.entity.repository.XmEntityRepository) Calendar(com.icthh.xm.ms.entity.domain.Calendar) MultipartFile(org.springframework.web.multipart.MultipartFile) NEW_BUILDER_TYPE(com.icthh.xm.ms.entity.domain.spec.LinkSpec.NEW_BUILDER_TYPE) Collections(java.util.Collections) Transactional(org.springframework.transaction.annotation.Transactional) PageRequest(org.springframework.data.domain.PageRequest) BusinessException(com.icthh.xm.commons.exceptions.BusinessException) SimpleExportXmEntityDto(com.icthh.xm.ms.entity.domain.SimpleExportXmEntityDto) XmEntity(com.icthh.xm.ms.entity.domain.XmEntity) ModelMapper(org.modelmapper.ModelMapper) LogicExtensionPoint(com.icthh.xm.commons.lep.LogicExtensionPoint)

Example 4 with BusinessException

use of com.icthh.xm.commons.exceptions.BusinessException in project xm-ms-entity by xm-online.

the class XmEntityServiceImpl method deleteLinkTarget.

@Override
public void deleteLinkTarget(IdOrKey idOrKey, String linkId) {
    XmEntity source = toSourceXmEntity(idOrKey);
    Long longLinkId = Long.parseLong(linkId);
    Link foundLink = linkService.findOne(longLinkId);
    if (foundLink == null) {
        throw new IllegalArgumentException("Link not found by id " + linkId);
    }
    Long foundSourceId = foundLink.getSource().getId();
    if (!foundSourceId.equals(source.getId())) {
        throw new BusinessException("Wrong source id. Expected " + source.getId() + " found " + foundSourceId);
    }
    log.debug("Delete link by id " + linkId);
    linkService.delete(longLinkId);
}
Also used : BusinessException(com.icthh.xm.commons.exceptions.BusinessException) XmEntity(com.icthh.xm.ms.entity.domain.XmEntity) Link(com.icthh.xm.ms.entity.domain.Link)

Example 5 with BusinessException

use of com.icthh.xm.commons.exceptions.BusinessException in project xm-ms-entity by xm-online.

the class LocationResource method createLocation.

/**
 * POST  /locations : Create a new location.
 *
 * @param location the location to create
 * @return the ResponseEntity with status 201 (Created) and with body the new location, or with status 400 (Bad Request) if the location has already an ID
 * @throws URISyntaxException if the Location URI syntax is incorrect
 */
@PostMapping("/locations")
@Timed
@PreAuthorize("hasPermission({'location': #location}, 'LOCATION.CREATE')")
public ResponseEntity<Location> createLocation(@Valid @RequestBody Location location) throws URISyntaxException {
    if (location.getId() != null) {
        throw new BusinessException(ErrorConstants.ERR_BUSINESS_IDEXISTS, "A new location cannot already have an ID");
    }
    Location result = locationRepository.save(location);
    locationSearchRepository.save(result);
    return ResponseEntity.created(new URI("/api/locations/" + result.getId())).headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString())).body(result);
}
Also used : BusinessException(com.icthh.xm.commons.exceptions.BusinessException) URI(java.net.URI) Location(com.icthh.xm.ms.entity.domain.Location) PostMapping(org.springframework.web.bind.annotation.PostMapping) Timed(com.codahale.metrics.annotation.Timed) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Aggregations

BusinessException (com.icthh.xm.commons.exceptions.BusinessException)8 Link (com.icthh.xm.ms.entity.domain.Link)3 XmEntity (com.icthh.xm.ms.entity.domain.XmEntity)3 EntityNotFoundException (com.icthh.xm.commons.exceptions.EntityNotFoundException)2 Location (com.icthh.xm.ms.entity.domain.Location)2 StateSpec (com.icthh.xm.ms.entity.domain.spec.StateSpec)2 XmEntityStateProjection (com.icthh.xm.ms.entity.projection.XmEntityStateProjection)2 LifecycleLepStrategy (com.icthh.xm.ms.entity.service.LifecycleLepStrategy)2 URI (java.net.URI)2 Timed (com.codahale.metrics.annotation.Timed)1 ErrorConstants (com.icthh.xm.commons.exceptions.ErrorConstants)1 LogicExtensionPoint (com.icthh.xm.commons.lep.LogicExtensionPoint)1 LepService (com.icthh.xm.commons.lep.spring.LepService)1 FindWithPermission (com.icthh.xm.commons.permission.annotation.FindWithPermission)1 XmAuthenticationContextHolder (com.icthh.xm.commons.security.XmAuthenticationContextHolder)1 Constants (com.icthh.xm.ms.entity.config.Constants)1 Attachment (com.icthh.xm.ms.entity.domain.Attachment)1 Calendar (com.icthh.xm.ms.entity.domain.Calendar)1 Comment (com.icthh.xm.ms.entity.domain.Comment)1 FileFormatEnum (com.icthh.xm.ms.entity.domain.FileFormatEnum)1