Search in sources :

Example 56 with ServiceException

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

the class CmsStaticContentFileManagerImpl method addFiles.

/**
 * <p>
 * Method to add files for given store.Files will be stored in Infinispan
 * and will be retrieved based on the storeID. Following steps will be
 * performed to store static content files
 * </p>
 * <li>Merchant Node will be retrieved from the cacheTree if it exists else
 * new node will be created.</li>
 * <li>Files will be stored in StaticContentCacheAttribute , which
 * eventually will be stored in Infinispan</li>
 *
 * @param merchantStoreCode
 *            Merchant store for which files are getting stored in
 *            Infinispan.
 * @param inputStaticContentDataList
 *            input static content file list which will get
 *            {@link InputContentImage} stored
 * @throws ServiceException
 *             if content file storing process fail.
 * @see InputStaticContentData
 * @see StaticContentCacheAttribute
 */
@Override
public void addFiles(final String merchantStoreCode, Optional<String> path, final List<InputContentFile> inputStaticContentDataList) throws ServiceException {
    if (cacheManager.getTreeCache() == null) {
        LOGGER.error("Unable to find cacheManager.getTreeCache() in Infinispan..");
        throw new ServiceException("CmsStaticContentFileManagerInfinispanImpl has a null cacheManager.getTreeCache()");
    }
    try {
        for (final InputContentFile inputStaticContentData : inputStaticContentDataList) {
            String nodePath = this.getNodePath(merchantStoreCode, inputStaticContentData.getFileContentType());
            final Node<String, Object> merchantNode = this.getNode(nodePath);
            merchantNode.put(inputStaticContentData.getFileName(), IOUtils.toByteArray(inputStaticContentData.getFile()));
        }
        LOGGER.info("Total {} files added successfully.", inputStaticContentDataList.size());
    } catch (final Exception e) {
        LOGGER.error("Error while saving content image", e);
        throw new ServiceException(e);
    }
}
Also used : InputContentFile(com.salesmanager.core.model.content.InputContentFile) ServiceException(com.salesmanager.core.business.exception.ServiceException) ServiceException(com.salesmanager.core.business.exception.ServiceException)

Example 57 with ServiceException

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

the class CmsStaticContentFileManagerImpl method getFileNames.

/**
 * Queries the CMS to retrieve all static content files. Only the name of
 * the file will be returned to the client
 *
 * @param merchantStoreCode
 * @return
 * @throws ServiceException
 */
@Override
public List<String> getFileNames(final String merchantStoreCode, Optional<String> folderPath, final FileContentType staticContentType) throws ServiceException {
    try {
        StringBuilder merchantPath = new StringBuilder();
        merchantPath.append(buildRootPath()).append(merchantStoreCode).append(Constants.SLASH).append(staticContentType);
        Path path = Paths.get(merchantPath.toString());
        List<String> fileNames = null;
        if (Files.exists(path)) {
            fileNames = new ArrayList<String>();
            try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(path)) {
                for (Path dirPath : directoryStream) {
                    String fileName = dirPath.getFileName().toString();
                    if (staticContentType.name().equals(FileContentType.IMAGE.name())) {
                        // File f = new File(fileName);
                        String mimetype = URLConnection.guessContentTypeFromName(fileName);
                        // MimetypesFileTypeMap().getContentType(f);
                        if (!StringUtils.isBlank(mimetype)) {
                            String type = mimetype.split("/")[0];
                            if (type.equals("image")) {
                                fileNames.add(fileName);
                            }
                        }
                    // fileNames.add(fileName);
                    } else {
                        fileNames.add(fileName);
                    }
                }
            }
            return fileNames;
        }
    } catch (final Exception e) {
        LOGGER.error("Error while fetching file for {} merchant ", merchantStoreCode);
        throw new ServiceException(e);
    }
    return new ArrayList<>();
}
Also used : Path(java.nio.file.Path) ServiceException(com.salesmanager.core.business.exception.ServiceException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ServiceException(com.salesmanager.core.business.exception.ServiceException)

Example 58 with ServiceException

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

the class CmsStaticContentFileManagerImpl method removeFiles.

/**
 * Removes the data in a given merchant node
 */
@Override
public void removeFiles(final String merchantStoreCode, Optional<String> folderPath) throws ServiceException {
    LOGGER.debug("Removing all images for {} merchant ", merchantStoreCode);
    try {
        StringBuilder merchantPath = new StringBuilder();
        merchantPath.append(buildRootPath()).append(Constants.SLASH).append(merchantStoreCode);
        Path path = Paths.get(merchantPath.toString());
        Files.deleteIfExists(path);
    } catch (final Exception e) {
        LOGGER.error("Error while deleting content image for {} merchant ", merchantStoreCode);
        throw new ServiceException(e);
    }
}
Also used : Path(java.nio.file.Path) ServiceException(com.salesmanager.core.business.exception.ServiceException) IOException(java.io.IOException) ServiceException(com.salesmanager.core.business.exception.ServiceException)

Example 59 with ServiceException

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

the class CmsStaticContentFileManagerImpl method removeFolder.

@Override
public void removeFolder(String merchantStoreCode, String folderName, Optional<String> folderPath) throws ServiceException {
    // String rootPath = this.buildRootPath();
    try {
        Path merchantPath = this.buildMerchantPath(merchantStoreCode);
        StringBuilder nodePath = new StringBuilder();
        nodePath.append(merchantPath.toString()).append(Constants.SLASH);
        if (folderPath.isPresent()) {
            nodePath.append(folderPath.get()).append(Constants.SLASH);
        }
        nodePath.append(folderName);
        Path longPath = Paths.get(nodePath.toString());
        if (Files.exists(longPath)) {
            Files.delete(longPath);
        }
    } catch (IOException e) {
        LOGGER.error("Error while creating fiolder for {} merchant ", merchantStoreCode);
        throw new ServiceException(e);
    }
}
Also used : Path(java.nio.file.Path) ServiceException(com.salesmanager.core.business.exception.ServiceException) IOException(java.io.IOException)

Example 60 with ServiceException

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

the class CmsStaticContentFileManagerImpl method removeFile.

@Override
public void removeFile(final String merchantStoreCode, final FileContentType staticContentType, final String fileName, Optional<String> folderPath) throws ServiceException {
    try {
        StringBuilder merchantPath = new StringBuilder();
        merchantPath.append(buildRootPath()).append(Constants.SLASH).append(merchantStoreCode).append(Constants.SLASH).append(staticContentType).append(Constants.SLASH).append(fileName);
        Path path = Paths.get(merchantPath.toString());
        Files.deleteIfExists(path);
    } catch (final Exception e) {
        LOGGER.error("Error while deleting files for {} merchant ", merchantStoreCode);
        throw new ServiceException(e);
    }
}
Also used : Path(java.nio.file.Path) ServiceException(com.salesmanager.core.business.exception.ServiceException) IOException(java.io.IOException) 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