Search in sources :

Example 81 with ServiceException

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

the class SearchServiceImpl method search.

public com.salesmanager.core.model.search.SearchResponse search(MerchantStore store, String languageCode, String term, int entriesCount, int startIndex) throws ServiceException {
    try {
        StringBuilder collectionName = new StringBuilder();
        collectionName.append(PRODUCT_INDEX_NAME).append(UNDERSCORE).append(languageCode).append(UNDERSCORE).append(store.getCode().toLowerCase());
        SearchRequest request = new SearchRequest();
        request.addCollection(collectionName.toString());
        request.setSize(entriesCount);
        request.setStart(startIndex);
        request.setMatch(term);
        SearchResponse response = searchService.search(request);
        com.salesmanager.core.model.search.SearchResponse resp = new com.salesmanager.core.model.search.SearchResponse();
        resp.setTotalCount(0);
        if (response != null) {
            resp.setTotalCount(response.getCount());
            List<SearchEntry> entries = new ArrayList<SearchEntry>();
            Collection<SearchHit> hits = response.getSearchHits();
            if (!CollectionUtils.isEmpty(hits)) {
                for (SearchHit hit : hits) {
                    SearchEntry entry = new SearchEntry();
                    // Map<String,Object> metaEntries = hit.getMetaEntries();
                    Map<String, Object> metaEntries = hit.getItem();
                    IndexProduct indexProduct = new IndexProduct();
                    Object desc = metaEntries.get("description");
                    if (desc instanceof JsonNull == false) {
                        indexProduct.setDescription((String) metaEntries.get("description"));
                    }
                    Object hl = metaEntries.get("highlight");
                    if (hl instanceof JsonNull == false) {
                        indexProduct.setHighlight((String) metaEntries.get("highlight"));
                    }
                    indexProduct.setId((String) metaEntries.get("id"));
                    indexProduct.setLang((String) metaEntries.get("lang"));
                    Object nm = metaEntries.get("name");
                    if (nm instanceof JsonNull == false) {
                        indexProduct.setName(((String) metaEntries.get("name")));
                    }
                    Object mf = metaEntries.get("manufacturer");
                    if (mf instanceof JsonNull == false) {
                        indexProduct.setManufacturer(((String) metaEntries.get("manufacturer")));
                    }
                    indexProduct.setPrice(Double.valueOf(((String) metaEntries.get("price"))));
                    indexProduct.setStore(((String) metaEntries.get("store")));
                    entry.setIndexProduct(indexProduct);
                    entries.add(entry);
                /**
                 * no more support for highlighted
                 */
                }
                resp.setEntries(entries);
                // Map<String,List<FacetEntry>> facets = response.getFacets();
                Map<String, Facet> facets = response.getFacets();
                if (facets != null && facets.size() > 0) {
                    Map<String, List<SearchFacet>> searchFacets = new HashMap<String, List<SearchFacet>>();
                    for (String key : facets.keySet()) {
                        Facet f = facets.get(key);
                        List<com.shopizer.search.services.Entry> ent = f.getEntries();
                        // List<FacetEntry> f = facets.get(key);
                        List<SearchFacet> fs = searchFacets.computeIfAbsent(key, k -> new ArrayList<>());
                        for (com.shopizer.search.services.Entry facetEntry : ent) {
                            SearchFacet searchFacet = new SearchFacet();
                            searchFacet.setKey(facetEntry.getName());
                            searchFacet.setName(facetEntry.getName());
                            searchFacet.setCount(facetEntry.getCount());
                            fs.add(searchFacet);
                        }
                    }
                    resp.setFacets(searchFacets);
                }
            }
        }
        return resp;
    } catch (Exception e) {
        LOGGER.error("Error while searching keywords " + term, e);
        throw new ServiceException(e);
    }
}
Also used : SearchRequest(com.shopizer.search.services.SearchRequest) SearchHit(com.shopizer.search.services.SearchHit) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JsonNull(com.google.gson.JsonNull) SearchEntry(com.salesmanager.core.model.search.SearchEntry) SearchFacet(com.salesmanager.core.model.search.SearchFacet) ArrayList(java.util.ArrayList) List(java.util.List) SearchEntry(com.salesmanager.core.model.search.SearchEntry) SearchFacet(com.salesmanager.core.model.search.SearchFacet) Facet(com.shopizer.search.services.Facet) IndexProduct(com.salesmanager.core.model.search.IndexProduct) ServiceException(com.salesmanager.core.business.exception.ServiceException) SearchResponse(com.shopizer.search.services.SearchResponse) ServiceException(com.salesmanager.core.business.exception.ServiceException)

Example 82 with ServiceException

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

the class ShoppingCartServiceImpl method saveOrUpdate.

/**
 * Save or update a {@link ShoppingCart} for a given customer
 */
@Override
public void saveOrUpdate(ShoppingCart shoppingCart) throws ServiceException {
    Validate.notNull(shoppingCart, "ShoppingCart must not be null");
    Validate.notNull(shoppingCart.getMerchantStore(), "ShoppingCart.merchantStore must not be null");
    try {
        UserContext userContext = UserContext.getCurrentInstance();
        if (userContext != null) {
            shoppingCart.setIpAddress(userContext.getIpAddress());
        }
    } catch (Exception s) {
        LOGGER.error("Cannot add ip address to shopping cart ", s);
    }
    if (shoppingCart.getId() == null || shoppingCart.getId() == 0) {
        super.create(shoppingCart);
    } else {
        super.update(shoppingCart);
    }
}
Also used : UserContext(com.salesmanager.core.model.common.UserContext) ServiceException(com.salesmanager.core.business.exception.ServiceException)

Example 83 with ServiceException

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

the class ShoppingCartServiceImpl method getByCode.

/**
 * Get a {@link ShoppingCart} for a given code. Will update the shopping
 * cart prices and items based on the actual inventory. This method will
 * remove the shopping cart if no items are attached.
 */
@Override
@Transactional
public ShoppingCart getByCode(final String code, final MerchantStore store) throws ServiceException {
    try {
        ShoppingCart shoppingCart = shoppingCartRepository.findByCode(store.getId(), code);
        if (shoppingCart == null) {
            return null;
        }
        getPopulatedShoppingCart(shoppingCart);
        if (shoppingCart.isObsolete()) {
            delete(shoppingCart);
            return null;
        } else {
            return shoppingCart;
        }
    } catch (javax.persistence.NoResultException nre) {
        return null;
    } catch (Throwable e) {
        throw new ServiceException(e);
    }
}
Also used : ShoppingCart(com.salesmanager.core.model.shoppingcart.ShoppingCart) ServiceException(com.salesmanager.core.business.exception.ServiceException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 84 with ServiceException

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

the class EmailServiceImpl method getEmailConfiguration.

@Override
public EmailConfig getEmailConfiguration(MerchantStore store) throws ServiceException {
    MerchantConfiguration configuration = merchantConfigurationService.getMerchantConfiguration(Constants.EMAIL_CONFIG, store);
    EmailConfig emailConfig = null;
    if (configuration != null) {
        String value = configuration.getValue();
        ObjectMapper mapper = new ObjectMapper();
        try {
            emailConfig = mapper.readValue(value, EmailConfig.class);
        } catch (Exception e) {
            throw new ServiceException("Cannot parse json string " + value);
        }
    }
    return emailConfig;
}
Also used : ServiceException(com.salesmanager.core.business.exception.ServiceException) EmailConfig(com.salesmanager.core.business.modules.email.EmailConfig) MerchantConfiguration(com.salesmanager.core.model.system.MerchantConfiguration) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ServiceException(com.salesmanager.core.business.exception.ServiceException)

Example 85 with ServiceException

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

the class MerchantConfigurationServiceImpl method getMerchantConfig.

@Override
public MerchantConfig getMerchantConfig(MerchantStore store) throws ServiceException {
    MerchantConfiguration configuration = merchantConfigurationRepository.findByMerchantStoreAndKey(store.getId(), MerchantConfigurationType.CONFIG.name());
    MerchantConfig config = null;
    if (configuration != null) {
        String value = configuration.getValue();
        ObjectMapper mapper = new ObjectMapper();
        try {
            config = mapper.readValue(value, MerchantConfig.class);
        } catch (Exception e) {
            throw new ServiceException("Cannot parse json string " + value);
        }
    }
    return config;
}
Also used : ServiceException(com.salesmanager.core.business.exception.ServiceException) MerchantConfiguration(com.salesmanager.core.model.system.MerchantConfiguration) MerchantConfig(com.salesmanager.core.model.system.MerchantConfig) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) 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