Search in sources :

Example 26 with ServiceException

use of org.broadleafcommerce.common.exception.ServiceException in project BroadleafCommerce by BroadleafCommerce.

the class CustomerCustomPersistenceHandler method remove.

@Override
public void remove(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
    Entity entity = persistencePackage.getEntity();
    try {
        Long customerId = Long.parseLong(entity.findProperty("id").getValue());
        Customer customer = customerService.readCustomerById(customerId);
        if (Status.class.isAssignableFrom(customer.getClass())) {
            ((Status) customer).setArchived('Y');
            // If the customer has a conditional weave on ArchiveStatus, nothing triggers the delete so other
            // normally-cascaded deletes don't happen (like CustomerAddress)
            List<CustomerAddress> addressList = customer.getCustomerAddresses();
            for (CustomerAddress address : addressList) {
                address.setArchived('Y');
            }
            customer = customerService.saveCustomer(customer);
            return;
        }
        // Remove the customer roles for the customer since it's not cascaded
        roleDao.removeCustomerRolesByCustomerId(customerId);
        helper.getCompatibleModule(OperationType.BASIC).remove(persistencePackage);
    } catch (Exception e) {
        LOG.error("Unable to execute persistence activity", e);
        throw new ServiceException("Unable to remove entity for " + entity.getType()[0], e);
    }
}
Also used : Status(org.broadleafcommerce.common.persistence.Status) Entity(org.broadleafcommerce.openadmin.dto.Entity) ServiceException(org.broadleafcommerce.common.exception.ServiceException) Customer(org.broadleafcommerce.profile.core.domain.Customer) ServiceException(org.broadleafcommerce.common.exception.ServiceException) CustomerAddress(org.broadleafcommerce.profile.core.domain.CustomerAddress)

Example 27 with ServiceException

use of org.broadleafcommerce.common.exception.ServiceException in project BroadleafCommerce by BroadleafCommerce.

the class CustomerCustomPersistenceHandler method update.

@Override
public Entity update(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
    Entity entity = persistencePackage.getEntity();
    try {
        PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
        Map<String, FieldMetadata> adminProperties = helper.getSimpleMergedProperties(Customer.class.getName(), persistencePerspective);
        Object primaryKey = helper.getPrimaryKey(entity, adminProperties);
        Customer adminInstance = (Customer) dynamicEntityDao.retrieve(Class.forName(entity.getType()[0]), primaryKey);
        String passwordBefore = adminInstance.getPassword();
        adminInstance.setPassword(null);
        adminInstance = (Customer) helper.createPopulatedInstance(adminInstance, entity, adminProperties, false);
        adminInstance.setPassword(passwordBefore);
        if (useEmailForLogin) {
            adminInstance.setUsername(adminInstance.getEmailAddress());
        }
        adminInstance = customerService.saveCustomer(adminInstance);
        Entity adminEntity = helper.getRecord(adminProperties, adminInstance, null, null);
        return adminEntity;
    } catch (Exception e) {
        throw new ServiceException("Unable to update entity for " + entity.getType()[0], e);
    }
}
Also used : Entity(org.broadleafcommerce.openadmin.dto.Entity) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) ServiceException(org.broadleafcommerce.common.exception.ServiceException) Customer(org.broadleafcommerce.profile.core.domain.Customer) ServiceException(org.broadleafcommerce.common.exception.ServiceException)

Example 28 with ServiceException

use of org.broadleafcommerce.common.exception.ServiceException in project BroadleafCommerce by BroadleafCommerce.

the class IndexFieldCustomPersistenceHandler method update.

@Override
public Entity update(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
    Entity entity = persistencePackage.getEntity();
    try {
        PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
        Map<String, FieldMetadata> adminProperties = helper.getSimpleMergedProperties(IndexField.class.getName(), persistencePerspective);
        Object primaryKey = helper.getPrimaryKey(entity, adminProperties);
        IndexField adminInstance = (IndexField) dynamicEntityDao.retrieve(Class.forName(entity.getType()[0]), primaryKey);
        return getEntity(persistencePackage, dynamicEntityDao, helper, entity, adminProperties, adminInstance);
    } catch (Exception e) {
        throw new ServiceException("Unable to perform update for entity: " + IndexField.class.getName(), e);
    }
}
Also used : Entity(org.broadleafcommerce.openadmin.dto.Entity) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) ServiceException(org.broadleafcommerce.common.exception.ServiceException) CriteriaTransferObject(org.broadleafcommerce.openadmin.dto.CriteriaTransferObject) IndexField(org.broadleafcommerce.core.search.domain.IndexField) ServiceException(org.broadleafcommerce.common.exception.ServiceException)

Example 29 with ServiceException

use of org.broadleafcommerce.common.exception.ServiceException in project BroadleafCommerce by BroadleafCommerce.

the class SolrSearchServiceImpl method findSearchResults.

/**
 * Given a qualified solr query string (such as "category:2002"), actually performs a solr search. It will
 * take into considering the search criteria to build out facets / pagination / sorting.
 *
 * @param searchCriteria
 * @param facets
 * @return the ProductSearchResult of the search
 * @throws ServiceException
 */
protected SearchResult findSearchResults(String qualifiedSolrQuery, List<SearchFacetDTO> facets, SearchCriteria searchCriteria, String defaultSort, String... filterQueries) throws ServiceException {
    Map<String, SearchFacetDTO> namedFacetMap = getNamedFacetMap(facets, searchCriteria);
    // Left here for backwards compatibility for this method signature
    if (searchCriteria.getQuery() == null && qualifiedSolrQuery != null) {
        searchCriteria.setQuery(qualifiedSolrQuery);
    }
    // Build the basic query
    // Solr queries with a 'start' parameter cannot be a negative number
    int start = (searchCriteria.getPage() <= 0) ? 0 : (searchCriteria.getPage() - 1);
    SolrQuery solrQuery = new SolrQuery().setQuery(searchCriteria.getQuery()).setRows(searchCriteria.getPageSize()).setStart((start) * searchCriteria.getPageSize()).setRequestHandler(searchCriteria.getRequestHandler());
    // This is for SolrCloud.  We assume that we are always searching against a collection aliased as "PRIMARY"
    if (solrConfiguration.isSiteCollections()) {
        solrQuery.setParam("collection", solrConfiguration.getSiteAliasName(BroadleafRequestContext.getBroadleafRequestContext().getNonPersistentSite()));
    } else {
        // This should be ignored if not using SolrCloud
        solrQuery.setParam("collection", solrConfiguration.getPrimaryName());
    }
    solrQuery.setFields(shs.getIndexableIdFieldName());
    if (filterQueries != null) {
        solrQuery.setFilterQueries(filterQueries);
    }
    // add category filter if applicable
    if (searchCriteria.getCategory() != null) {
        solrQuery.addFilterQuery(getCategoryFilter(searchCriteria));
    }
    solrQuery.addFilterQuery(shs.getNamespaceFieldName() + ":(\"" + solrConfiguration.getNamespace() + "\")");
    solrQuery.set("defType", "edismax");
    solrQuery.set("qf", buildQueryFieldsString(solrQuery, searchCriteria));
    // Attach additional restrictions
    attachActiveFacetFilters(solrQuery, namedFacetMap, searchCriteria);
    attachFacets(solrQuery, namedFacetMap, searchCriteria);
    modifySolrQuery(solrQuery, searchCriteria.getQuery(), facets, searchCriteria, defaultSort);
    // on child documents.
    if (StringUtils.isNotBlank(defaultSort) || StringUtils.isNotBlank(searchCriteria.getSortQuery())) {
        solrQuery.remove("bq");
        solrQuery.remove("bf");
        solrQuery.remove("boost");
    }
    attachSortClause(solrQuery, searchCriteria, defaultSort);
    solrQuery.setShowDebugInfo(shouldShowDebugQuery());
    if (LOG.isTraceEnabled()) {
        try {
            LOG.trace(URLDecoder.decode(solrQuery.toString(), "UTF-8"));
        } catch (Exception e) {
            LOG.trace("Couldn't UTF-8 URL Decode: " + solrQuery.toString());
        }
    }
    // Query solr
    QueryResponse response;
    List<SolrDocument> responseDocuments;
    int numResults = 0;
    try {
        response = solrConfiguration.getServer().query(solrQuery, getSolrQueryMethod());
        responseDocuments = getResponseDocuments(response);
        numResults = (int) response.getResults().getNumFound();
        if (LOG.isTraceEnabled()) {
            LOG.trace(response.toString());
            for (SolrDocument doc : responseDocuments) {
                LOG.trace(doc);
            }
        }
    } catch (SolrServerException e) {
        throw new ServiceException("Could not perform search", e);
    } catch (IOException e) {
        throw new ServiceException("Could not perform search", e);
    }
    // Get the facets
    setFacetResults(namedFacetMap, response);
    sortFacetResults(namedFacetMap);
    filterEmptyFacets(facets);
    SearchResult result = new SearchResult();
    result.setFacets(facets);
    result.setQueryResponse(response);
    setPagingAttributes(result, numResults, searchCriteria);
    if (useSku) {
        List<Sku> skus = getSkus(responseDocuments);
        result.setSkus(skus);
    } else {
        // Get the products
        List<Product> products = getProducts(responseDocuments);
        result.setProducts(products);
    }
    return result;
}
Also used : SolrServerException(org.apache.solr.client.solrj.SolrServerException) SearchFacetDTO(org.broadleafcommerce.core.search.domain.SearchFacetDTO) Product(org.broadleafcommerce.core.catalog.domain.Product) SearchResult(org.broadleafcommerce.core.search.domain.SearchResult) IOException(java.io.IOException) SolrQuery(org.apache.solr.client.solrj.SolrQuery) SolrServerException(org.apache.solr.client.solrj.SolrServerException) ServiceException(org.broadleafcommerce.common.exception.ServiceException) IOException(java.io.IOException) SolrDocument(org.apache.solr.common.SolrDocument) ServiceException(org.broadleafcommerce.common.exception.ServiceException) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) Sku(org.broadleafcommerce.core.catalog.domain.Sku)

Example 30 with ServiceException

use of org.broadleafcommerce.common.exception.ServiceException in project BroadleafCommerce by BroadleafCommerce.

the class SolrIndexServiceImpl method deleteAllNamespaceDocuments.

@Override
public void deleteAllNamespaceDocuments(SolrClient server) throws ServiceException {
    try {
        String deleteQuery = StringUtil.sanitize(shs.getNamespaceFieldName()) + ":(\"" + StringUtil.sanitize(solrConfiguration.getNamespace()) + "\")";
        LOG.debug("Deleting by query: " + deleteQuery);
        server.deleteByQuery(deleteQuery);
        // Explicitly do a hard commit here since we just deleted the entire index
        server.commit();
    } catch (Exception e) {
        if (ServiceException.class.isAssignableFrom(e.getClass())) {
            throw (ServiceException) e;
        }
        throw new ServiceException("Could not delete documents", e);
    }
}
Also used : ServiceException(org.broadleafcommerce.common.exception.ServiceException) SolrServerException(org.apache.solr.client.solrj.SolrServerException) ServiceException(org.broadleafcommerce.common.exception.ServiceException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException)

Aggregations

ServiceException (org.broadleafcommerce.common.exception.ServiceException)77 Entity (org.broadleafcommerce.openadmin.dto.Entity)46 FieldMetadata (org.broadleafcommerce.openadmin.dto.FieldMetadata)44 PersistencePerspective (org.broadleafcommerce.openadmin.dto.PersistencePerspective)39 BasicFieldMetadata (org.broadleafcommerce.openadmin.dto.BasicFieldMetadata)25 InvocationTargetException (java.lang.reflect.InvocationTargetException)19 SecurityServiceException (org.broadleafcommerce.common.exception.SecurityServiceException)17 ValidationException (org.broadleafcommerce.openadmin.server.service.ValidationException)16 Serializable (java.io.Serializable)15 DynamicResultSet (org.broadleafcommerce.openadmin.dto.DynamicResultSet)14 CriteriaTransferObject (org.broadleafcommerce.openadmin.dto.CriteriaTransferObject)13 Property (org.broadleafcommerce.openadmin.dto.Property)12 ArrayList (java.util.ArrayList)11 HashMap (java.util.HashMap)11 Map (java.util.Map)11 AdminMainEntity (org.broadleafcommerce.common.admin.domain.AdminMainEntity)9 ForeignKey (org.broadleafcommerce.openadmin.dto.ForeignKey)9 StreamCapableTransactionalOperationAdapter (org.broadleafcommerce.common.util.StreamCapableTransactionalOperationAdapter)6 Sku (org.broadleafcommerce.core.catalog.domain.Sku)6 ClassMetadata (org.broadleafcommerce.openadmin.dto.ClassMetadata)6