Search in sources :

Example 11 with ServiceException

use of com.salesmanager.core.business.exception.ServiceException in project shopizer by shopizer-ecommerce.

the class GCPProductContentFileManager method getProductImage.

@Override
public OutputContentFile getProductImage(String merchantStoreCode, String productCode, String imageName, ProductImageSize size) throws ServiceException {
    InputStream inputStream = null;
    try {
        Storage storage = StorageOptions.getDefaultInstance().getService();
        String bucketName = bucketName();
        if (!this.bucketExists(storage, bucketName)) {
            return null;
        }
        Blob blob = storage.get(BlobId.of(bucketName, filePath(merchantStoreCode, productCode, size.name(), imageName)));
        ReadChannel reader = blob.reader();
        inputStream = Channels.newInputStream(reader);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        IOUtils.copy(inputStream, outputStream);
        OutputContentFile ct = new OutputContentFile();
        ct.setFile(outputStream);
        ct.setFileName(blob.getName());
        return ct;
    } catch (final Exception e) {
        LOGGER.error("Error while getting files", e);
        throw new ServiceException(e);
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (Exception ignore) {
            }
        }
    }
}
Also used : Blob(com.google.cloud.storage.Blob) Storage(com.google.cloud.storage.Storage) ServiceException(com.salesmanager.core.business.exception.ServiceException) InputStream(java.io.InputStream) OutputContentFile(com.salesmanager.core.model.content.OutputContentFile) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServiceException(com.salesmanager.core.business.exception.ServiceException) IOException(java.io.IOException) ReadChannel(com.google.cloud.ReadChannel)

Example 12 with ServiceException

use of com.salesmanager.core.business.exception.ServiceException in project shopizer by shopizer-ecommerce.

the class CmsImageFileManagerImpl method addProductImage.

/**
 * root -productFiles -merchant-id PRODUCT-ID(key) -> CacheAttribute(value) - image 1 - image 2 -
 * image 3
 */
@Override
public void addProductImage(ProductImage productImage, ImageContentFile contentImage) throws ServiceException {
    if (cacheManager.getTreeCache() == null) {
        throw new ServiceException("CmsImageFileManagerInfinispan has a null cacheManager.getTreeCache()");
    }
    try {
        // node
        StringBuilder nodePath = new StringBuilder();
        nodePath.append(productImage.getProduct().getMerchantStore().getCode()).append(Constants.SLASH).append(productImage.getProduct().getSku()).append(Constants.SLASH);
        if (contentImage.getFileContentType().name().equals(FileContentType.PRODUCT.name())) {
            nodePath.append(SMALL);
        } else if (contentImage.getFileContentType().name().equals(FileContentType.PRODUCTLG.name())) {
            nodePath.append(LARGE);
        }
        Node<String, Object> productNode = this.getNode(nodePath.toString());
        InputStream isFile = contentImage.getFile();
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        IOUtils.copy(isFile, output);
        // object for a given product containing all images
        productNode.put(contentImage.getFileName(), output.toByteArray());
    } catch (Exception e) {
        throw new ServiceException(e);
    }
}
Also used : ServiceException(com.salesmanager.core.business.exception.ServiceException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServiceException(com.salesmanager.core.business.exception.ServiceException)

Example 13 with ServiceException

use of com.salesmanager.core.business.exception.ServiceException in project shopizer by shopizer-ecommerce.

the class CmsImageFileManagerImpl method removeImages.

@SuppressWarnings("unchecked")
@Override
public void removeImages(final String merchantStoreCode) throws ServiceException {
    if (cacheManager.getTreeCache() == null) {
        throw new ServiceException("CmsImageFileManagerInfinispan has a null cacheManager.getTreeCache()");
    }
    try {
        final StringBuilder merchantPath = new StringBuilder();
        merchantPath.append(getRootName()).append(merchantStoreCode);
        cacheManager.getTreeCache().getRoot().remove(merchantPath.toString());
    } catch (Exception e) {
        throw new ServiceException(e);
    } finally {
    }
}
Also used : ServiceException(com.salesmanager.core.business.exception.ServiceException) ServiceException(com.salesmanager.core.business.exception.ServiceException)

Example 14 with ServiceException

use of com.salesmanager.core.business.exception.ServiceException in project shopizer by shopizer-ecommerce.

the class CmsImageFileManagerImpl method getImages.

@Override
public List<OutputContentFile> getImages(final String merchantStoreCode, FileContentType imageContentType) throws ServiceException {
    if (cacheManager.getTreeCache() == null) {
        throw new ServiceException("CmsImageFileManagerInfinispan has a null cacheManager.getTreeCache()");
    }
    List<OutputContentFile> images = new ArrayList<OutputContentFile>();
    FileNameMap fileNameMap = URLConnection.getFileNameMap();
    try {
        StringBuilder nodePath = new StringBuilder();
        nodePath.append(merchantStoreCode);
        Node<String, Object> merchantNode = this.getNode(nodePath.toString());
        Set<Node<String, Object>> childs = merchantNode.getChildren();
        // TODO image sizes
        for (Node<String, Object> node : childs) {
            for (String key : node.getKeys()) {
                byte[] imageBytes = (byte[]) merchantNode.get(key);
                OutputContentFile contentImage = new OutputContentFile();
                InputStream input = new ByteArrayInputStream(imageBytes);
                ByteArrayOutputStream output = new ByteArrayOutputStream();
                IOUtils.copy(input, output);
                String contentType = fileNameMap.getContentTypeFor(key);
                contentImage.setFile(output);
                contentImage.setMimeType(contentType);
                contentImage.setFileName(key);
                images.add(contentImage);
            }
        }
    } catch (Exception e) {
        throw new ServiceException(e);
    } finally {
    }
    return images;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Node(org.infinispan.tree.Node) OutputContentFile(com.salesmanager.core.model.content.OutputContentFile) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileNameMap(java.net.FileNameMap) ServiceException(com.salesmanager.core.business.exception.ServiceException) ServiceException(com.salesmanager.core.business.exception.ServiceException) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 15 with ServiceException

use of com.salesmanager.core.business.exception.ServiceException in project shopizer by shopizer-ecommerce.

the class CmsImageFileManagerImpl method removeProductImages.

@Override
public void removeProductImages(Product product) throws ServiceException {
    if (cacheManager.getTreeCache() == null) {
        throw new ServiceException("CmsImageFileManagerInfinispan has a null cacheManager.getTreeCache()");
    }
    try {
        StringBuilder nodePath = new StringBuilder();
        nodePath.append(product.getMerchantStore().getCode());
        Node<String, Object> merchantNode = this.getNode(nodePath.toString());
        merchantNode.remove(product.getSku());
    } catch (Exception e) {
        throw new ServiceException(e);
    } finally {
    }
}
Also used : ServiceException(com.salesmanager.core.business.exception.ServiceException) ServiceException(com.salesmanager.core.business.exception.ServiceException)

Aggregations

ServiceException (com.salesmanager.core.business.exception.ServiceException)230 ServiceRuntimeException (com.salesmanager.shop.store.api.exception.ServiceRuntimeException)88 ResourceNotFoundException (com.salesmanager.shop.store.api.exception.ResourceNotFoundException)54 ArrayList (java.util.ArrayList)45 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)44 Language (com.salesmanager.core.model.reference.language.Language)38 List (java.util.List)36 Collectors (java.util.stream.Collectors)30 IOException (java.io.IOException)28 InputStream (java.io.InputStream)27 IntegrationConfiguration (com.salesmanager.core.model.system.IntegrationConfiguration)22 Autowired (org.springframework.beans.factory.annotation.Autowired)21 OutputContentFile (com.salesmanager.core.model.content.OutputContentFile)20 Product (com.salesmanager.core.model.catalog.product.Product)19 HashMap (java.util.HashMap)19 Map (java.util.Map)18 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)17 InputContentFile (com.salesmanager.core.model.content.InputContentFile)17 Optional (java.util.Optional)17 IntegrationModule (com.salesmanager.core.model.system.IntegrationModule)16