Search in sources :

Example 66 with ResourceNotFoundException

use of com.salesmanager.shop.store.api.exception.ResourceNotFoundException in project shopizer by shopizer-ecommerce.

the class ShippingFacadeImpl method updatePackage.

@Override
public void updatePackage(String code, PackageDetails packaging, MerchantStore store) {
    Validate.notNull(store, "MerchantStore cannot be null");
    Validate.notNull(packaging, "PackageDetails cannot be null");
    Validate.notEmpty(code, "Packaging unique code cannot be empty");
    ShippingConfiguration config = getDbConfig(store);
    com.salesmanager.core.model.shipping.Package p = this.packageDetails(config, code);
    if (p == null) {
        throw new ResourceNotFoundException("Package with unique code [" + packaging.getCode() + "] not found");
    }
    com.salesmanager.core.model.shipping.Package pack = toPackage(packaging);
    pack.setCode(code);
    // need to check if code exists
    List<com.salesmanager.core.model.shipping.Package> packs = config.getPackages().stream().filter(pa -> !pa.getCode().equals(code)).collect(Collectors.toList());
    packs.add(pack);
    config.setPackages(packs);
    this.saveShippingConfiguration(config, store);
}
Also used : ExpeditionConfiguration(com.salesmanager.shop.model.shipping.ExpeditionConfiguration) ShippingOrigin(com.salesmanager.core.model.shipping.ShippingOrigin) ShippingType(com.salesmanager.core.model.shipping.ShippingType) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) Zone(com.salesmanager.core.model.reference.zone.Zone) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) ServiceException(com.salesmanager.core.business.exception.ServiceException) ZoneService(com.salesmanager.core.business.services.reference.zone.ZoneService) Language(com.salesmanager.core.model.reference.language.Language) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) ConversionRuntimeException(com.salesmanager.shop.store.api.exception.ConversionRuntimeException) ShippingOriginService(com.salesmanager.core.business.services.shipping.ShippingOriginService) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) Service(org.springframework.stereotype.Service) ReadableCountryPopulator(com.salesmanager.shop.populator.references.ReadableCountryPopulator) ShippingService(com.salesmanager.core.business.services.shipping.ShippingService) ReadableCountry(com.salesmanager.shop.model.references.ReadableCountry) OperationNotAllowedException(com.salesmanager.shop.store.api.exception.OperationNotAllowedException) CountryService(com.salesmanager.core.business.services.reference.country.CountryService) Validate(org.jsoup.helper.Validate) Logger(org.slf4j.Logger) Country(com.salesmanager.core.model.reference.country.Country) Collectors(java.util.stream.Collectors) List(java.util.List) ShippingPackageType(com.salesmanager.core.model.shipping.ShippingPackageType) PackageDetails(com.salesmanager.core.model.shipping.PackageDetails) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ConversionException(com.salesmanager.core.business.exception.ConversionException) PersistableAddress(com.salesmanager.shop.model.references.PersistableAddress) ReadableAddress(com.salesmanager.shop.model.references.ReadableAddress) ShippingConfiguration(com.salesmanager.core.model.shipping.ShippingConfiguration) Comparator(java.util.Comparator) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) ShippingConfiguration(com.salesmanager.core.model.shipping.ShippingConfiguration)

Example 67 with ResourceNotFoundException

use of com.salesmanager.shop.store.api.exception.ResourceNotFoundException in project shopizer by shopizer-ecommerce.

the class ShippingFacadeImpl method getPackage.

@Override
public PackageDetails getPackage(String code, MerchantStore store) {
    Validate.notNull(store, "MerchantStore cannot be null");
    Validate.notEmpty(code, "Packaging unique code cannot be empty");
    ShippingConfiguration config = getDbConfig(store);
    com.salesmanager.core.model.shipping.Package p = this.packageDetails(config, code);
    if (p == null) {
        throw new ResourceNotFoundException("Package with unique code [" + code + "] not found");
    }
    return toPackageDetails(p);
}
Also used : ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) ShippingConfiguration(com.salesmanager.core.model.shipping.ShippingConfiguration)

Example 68 with ResourceNotFoundException

use of com.salesmanager.shop.store.api.exception.ResourceNotFoundException in project shopizer by shopizer-ecommerce.

the class ProductImageApi method uploadImage.

/**
 * To be used with MultipartFile
 *
 * @param id
 * @param uploadfiles
 * @param request
 * @param response
 * @throws Exception
 */
@ResponseStatus(HttpStatus.CREATED)
@RequestMapping(value = { "/private/products/{id}/images", "/auth/products/{id}/images" }, consumes = { MediaType.MULTIPART_FORM_DATA_VALUE }, method = RequestMethod.POST)
@ApiImplicitParams({ @ApiImplicitParam(name = "store", dataType = "String", defaultValue = "DEFAULT"), @ApiImplicitParam(name = "lang", dataType = "String", defaultValue = "en") })
public void uploadImage(@PathVariable Long id, @RequestParam(value = "file", required = true) MultipartFile[] files, @RequestParam(value = "order", required = false, defaultValue = "0") Integer position, @ApiIgnore MerchantStore merchantStore, @ApiIgnore Language language) throws IOException {
    try {
        // get the product
        Product product = productService.getById(id);
        if (product == null) {
            throw new ResourceNotFoundException("Product not found");
        }
        // product belongs to merchant store
        if (product.getMerchantStore().getId().intValue() != merchantStore.getId().intValue()) {
            throw new UnauthorizedException("Resource not authorized for this merchant");
        }
        boolean hasDefaultImage = false;
        Set<ProductImage> images = product.getImages();
        if (!CollectionUtils.isEmpty(images)) {
            for (ProductImage image : images) {
                if (image.isDefaultImage()) {
                    hasDefaultImage = true;
                    break;
                }
            }
        }
        List<ProductImage> contentImagesList = new ArrayList<ProductImage>();
        int sortOrder = position;
        for (MultipartFile multipartFile : files) {
            if (!multipartFile.isEmpty()) {
                ProductImage productImage = new ProductImage();
                productImage.setImage(multipartFile.getInputStream());
                productImage.setProductImage(multipartFile.getOriginalFilename());
                productImage.setProduct(product);
                if (!hasDefaultImage) {
                    productImage.setDefaultImage(true);
                    hasDefaultImage = true;
                }
                productImage.setSortOrder(sortOrder);
                position++;
                contentImagesList.add(productImage);
            }
        }
        if (CollectionUtils.isNotEmpty(contentImagesList)) {
            productImageService.addProductImages(product, contentImagesList);
        }
    } catch (Exception e) {
        LOGGER.error("Error while creating ProductImage", e);
        throw new ServiceRuntimeException("Error while creating image");
    }
}
Also used : MultipartFile(org.springframework.web.multipart.MultipartFile) ProductImage(com.salesmanager.core.model.catalog.product.image.ProductImage) UnauthorizedException(com.salesmanager.shop.store.api.exception.UnauthorizedException) ArrayList(java.util.ArrayList) Product(com.salesmanager.core.model.catalog.product.Product) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) IOException(java.io.IOException) UnauthorizedException(com.salesmanager.shop.store.api.exception.UnauthorizedException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 69 with ResourceNotFoundException

use of com.salesmanager.shop.store.api.exception.ResourceNotFoundException in project shopizer by shopizer-ecommerce.

the class ProductImageApi method images.

/**
 * Get product images
 * @param id
 * @param imageId
 * @param merchantStore
 * @param language
 * @return
 */
@ResponseStatus(HttpStatus.OK)
@RequestMapping(value = { "/products/{productId}/images" }, method = RequestMethod.GET)
@ApiOperation(httpMethod = "GET", value = "Get images for a given product")
@ApiResponses(value = { @ApiResponse(code = 200, message = "List of ProductImage found", response = List.class) })
@ResponseBody
@ApiImplicitParams({ @ApiImplicitParam(name = "store", dataType = "String", defaultValue = "DEFAULT"), @ApiImplicitParam(name = "lang", dataType = "String", defaultValue = "en") })
public List<ReadableImage> images(@PathVariable Long productId, @ApiIgnore MerchantStore merchantStore, @ApiIgnore Language language) {
    Product p = productService.getById(productId);
    if (p == null) {
        throw new ResourceNotFoundException("Product images not found for product id [" + productId + "] and merchant [" + merchantStore.getCode() + "]");
    }
    if (p.getMerchantStore().getId() != merchantStore.getId()) {
        throw new ResourceNotFoundException("Product images not found for product id [" + productId + "] and merchant [" + merchantStore.getCode() + "]");
    }
    List<ReadableImage> target = new ArrayList<ReadableImage>();
    Set<ProductImage> images = p.getImages();
    if (images != null && images.size() > 0) {
        target = images.stream().map(i -> image(i, merchantStore, language)).sorted(Comparator.comparingInt(ReadableImage::getOrder)).collect(Collectors.toList());
    }
    return target;
}
Also used : ProductService(com.salesmanager.core.business.services.catalog.product.ProductService) PathVariable(org.springframework.web.bind.annotation.PathVariable) RequestParam(org.springframework.web.bind.annotation.RequestParam) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) Controller(org.springframework.stereotype.Controller) ApiResponses(io.swagger.annotations.ApiResponses) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) Valid(javax.validation.Valid) Language(com.salesmanager.core.model.reference.language.Language) ApiOperation(io.swagger.annotations.ApiOperation) HttpServletRequest(javax.servlet.http.HttpServletRequest) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) ReadableImage(com.salesmanager.shop.model.catalog.product.ReadableImage) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) Api(io.swagger.annotations.Api) Tag(io.swagger.annotations.Tag) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) Product(com.salesmanager.core.model.catalog.product.Product) Logger(org.slf4j.Logger) MediaType(org.springframework.http.MediaType) ApiImplicitParam(io.swagger.annotations.ApiImplicitParam) HttpServletResponse(javax.servlet.http.HttpServletResponse) Set(java.util.Set) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) IOException(java.io.IOException) ReadableProductImageMapper(com.salesmanager.shop.mapper.catalog.ReadableProductImageMapper) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) Collectors(java.util.stream.Collectors) ProductImageService(com.salesmanager.core.business.services.catalog.product.image.ProductImageService) UnauthorizedException(com.salesmanager.shop.store.api.exception.UnauthorizedException) ApiIgnore(springfox.documentation.annotations.ApiIgnore) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) NameEntity(com.salesmanager.shop.model.entity.NameEntity) SwaggerDefinition(io.swagger.annotations.SwaggerDefinition) ProductImage(com.salesmanager.core.model.catalog.product.image.ProductImage) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ApiResponse(io.swagger.annotations.ApiResponse) Optional(java.util.Optional) MultipartFile(org.springframework.web.multipart.MultipartFile) Comparator(java.util.Comparator) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) ReadableImage(com.salesmanager.shop.model.catalog.product.ReadableImage) ProductImage(com.salesmanager.core.model.catalog.product.image.ProductImage) ArrayList(java.util.ArrayList) Product(com.salesmanager.core.model.catalog.product.Product) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 70 with ResourceNotFoundException

use of com.salesmanager.shop.store.api.exception.ResourceNotFoundException in project shopizer by shopizer-ecommerce.

the class ProductInstanceGroupFacadeImpl method removeImage.

@Override
public void removeImage(Long imageId, Long productInstanceGroupId, MerchantStore store) {
    Validate.notNull(productInstanceGroupId, "productInstanceGroupId must not be null");
    Validate.notNull(store, "MerchantStore must not be null");
    ProductInstanceImage image = productInstanceImageService.getById(imageId);
    if (image == null) {
        throw new ResourceNotFoundException("ProductInstanceImage [" + imageId + "] was not found");
    }
    ProductInstanceGroup group = this.group(productInstanceGroupId, store);
    try {
        contentService.removeFile(Constants.SLASH + store.getCode() + Constants.SLASH + productInstanceGroupId, FileContentType.INSTANCE, image.getProductImage());
        group.getImages().removeIf(i -> (i.getId() == image.getId()));
        // update productinstanceroup
        productInstanceGroupService.update(group);
    } catch (ServiceException e) {
        throw new ServiceRuntimeException("An exception occured while removing instance image [" + imageId + "]", e);
    }
}
Also used : ServiceException(com.salesmanager.core.business.exception.ServiceException) ProductInstanceImage(com.salesmanager.core.model.catalog.product.instance.ProductInstanceImage) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) ProductInstanceGroup(com.salesmanager.core.model.catalog.product.instance.ProductInstanceGroup) ReadableProductInstanceGroup(com.salesmanager.shop.model.catalog.product.product.instanceGroup.ReadableProductInstanceGroup) PersistableProductInstanceGroup(com.salesmanager.shop.model.catalog.product.product.instanceGroup.PersistableProductInstanceGroup) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException)

Aggregations

ResourceNotFoundException (com.salesmanager.shop.store.api.exception.ResourceNotFoundException)108 ServiceRuntimeException (com.salesmanager.shop.store.api.exception.ServiceRuntimeException)77 ServiceException (com.salesmanager.core.business.exception.ServiceException)62 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)22 UnauthorizedException (com.salesmanager.shop.store.api.exception.UnauthorizedException)22 Product (com.salesmanager.core.model.catalog.product.Product)21 Language (com.salesmanager.core.model.reference.language.Language)19 List (java.util.List)19 Collectors (java.util.stream.Collectors)19 ApiImplicitParams (io.swagger.annotations.ApiImplicitParams)17 ArrayList (java.util.ArrayList)17 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)16 OperationNotAllowedException (com.salesmanager.shop.store.api.exception.OperationNotAllowedException)15 Autowired (org.springframework.beans.factory.annotation.Autowired)15 ConversionException (com.salesmanager.core.business.exception.ConversionException)13 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)13 Inject (javax.inject.Inject)12 Optional (java.util.Optional)11 Service (org.springframework.stereotype.Service)11 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)11