Search in sources :

Example 16 with FilterAndSortCriteria

use of org.broadleafcommerce.openadmin.dto.FilterAndSortCriteria in project BroadleafCommerce by BroadleafCommerce.

the class AdminPermissionCustomPersistenceHandler method addDefaultSort.

protected void addDefaultSort(CriteriaTransferObject cto) {
    boolean userSort = false;
    for (FilterAndSortCriteria fasc : cto.getCriteriaMap().values()) {
        if (fasc.getSortDirection() != null) {
            userSort = true;
            break;
        }
    }
    if (!userSort) {
        FilterAndSortCriteria descriptionSort = cto.getCriteriaMap().get("description");
        if (descriptionSort == null) {
            descriptionSort = new FilterAndSortCriteria("description");
            cto.add(descriptionSort);
        }
        descriptionSort.setSortAscending(true);
    }
}
Also used : FilterAndSortCriteria(org.broadleafcommerce.openadmin.dto.FilterAndSortCriteria)

Example 17 with FilterAndSortCriteria

use of org.broadleafcommerce.openadmin.dto.FilterAndSortCriteria in project BroadleafCommerce by BroadleafCommerce.

the class AdminEntityServiceImpl method getRecord.

@Override
public PersistenceResponse getRecord(PersistencePackageRequest request, String id, ClassMetadata cmd, boolean isCollectionRequest) throws ServiceException {
    String idProperty = getIdProperty(cmd);
    FilterAndSortCriteria fasc = new FilterAndSortCriteria(idProperty);
    fasc.setFilterValue(id);
    request.addFilterAndSortCriteria(fasc);
    PersistenceResponse response = fetch(request);
    Entity[] entities = response.getDynamicResultSet().getRecords();
    if (ArrayUtils.isEmpty(entities)) {
        throw new EntityNotFoundException();
    }
    return response;
}
Also used : AdminMainEntity(org.broadleafcommerce.common.admin.domain.AdminMainEntity) Entity(org.broadleafcommerce.openadmin.dto.Entity) PersistenceResponse(org.broadleafcommerce.openadmin.server.service.persistence.PersistenceResponse) FilterAndSortCriteria(org.broadleafcommerce.openadmin.dto.FilterAndSortCriteria) EntityNotFoundException(org.broadleafcommerce.openadmin.exception.EntityNotFoundException)

Example 18 with FilterAndSortCriteria

use of org.broadleafcommerce.openadmin.dto.FilterAndSortCriteria in project BroadleafCommerce by BroadleafCommerce.

the class AdminEntityServiceImpl method getPagedRecordsForCollection.

@Override
public PersistenceResponse getPagedRecordsForCollection(ClassMetadata containingClassMetadata, Entity containingEntity, Property collectionProperty, FilterAndSortCriteria[] fascs, FetchPageRequest fetchPageRequest, String idValueOverride, List<SectionCrumb> sectionCrumbs) throws ServiceException {
    PersistencePackageRequest ppr = PersistencePackageRequest.fromMetadata(collectionProperty.getMetadata(), sectionCrumbs).withFilterAndSortCriteria(fascs).withStartIndex(fetchPageRequest.getStartIndex()).withMaxIndex(fetchPageRequest.getMaxIndex()).withFirstId(fetchPageRequest.getFirstId()).withLastId(fetchPageRequest.getLastId()).withLowerCount(fetchPageRequest.getLowerCount()).withUpperCount(fetchPageRequest.getUpperCount()).withPageSize(fetchPageRequest.getPageSize()).withPresentationFetch(true);
    FilterAndSortCriteria fasc;
    FieldMetadata md = collectionProperty.getMetadata();
    String collectionCeilingClass = null;
    if (md instanceof BasicCollectionMetadata) {
        fasc = new FilterAndSortCriteria(ppr.getForeignKey().getManyToField());
        collectionCeilingClass = ((CollectionMetadata) md).getCollectionCeilingEntity();
    } else if (md instanceof AdornedTargetCollectionMetadata) {
        fasc = new FilterAndSortCriteria(ppr.getAdornedList().getCollectionFieldName());
        collectionCeilingClass = ((CollectionMetadata) md).getCollectionCeilingEntity();
    } else if (md instanceof MapMetadata) {
        fasc = new FilterAndSortCriteria(ppr.getForeignKey().getManyToField());
    } else {
        throw new IllegalArgumentException(String.format("The specified field [%s] for class [%s] was not a " + "collection field.", collectionProperty.getName(), containingClassMetadata.getCeilingType()));
    }
    String id;
    if (idValueOverride == null) {
        id = getContextSpecificRelationshipId(containingClassMetadata, containingEntity, collectionProperty.getName());
    } else {
        id = idValueOverride;
    }
    fasc.setFilterValue(id);
    ppr.addFilterAndSortCriteria(fasc);
    if (collectionCeilingClass != null) {
        ppr.setCeilingEntityClassname(collectionCeilingClass);
    }
    ppr.setSectionEntityField(collectionProperty.getName());
    return fetch(ppr);
}
Also used : BasicCollectionMetadata(org.broadleafcommerce.openadmin.dto.BasicCollectionMetadata) CollectionMetadata(org.broadleafcommerce.openadmin.dto.CollectionMetadata) AdornedTargetCollectionMetadata(org.broadleafcommerce.openadmin.dto.AdornedTargetCollectionMetadata) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) PersistencePackageRequest(org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest) FilterAndSortCriteria(org.broadleafcommerce.openadmin.dto.FilterAndSortCriteria) BasicCollectionMetadata(org.broadleafcommerce.openadmin.dto.BasicCollectionMetadata) MapMetadata(org.broadleafcommerce.openadmin.dto.MapMetadata) AdornedTargetCollectionMetadata(org.broadleafcommerce.openadmin.dto.AdornedTargetCollectionMetadata)

Example 19 with FilterAndSortCriteria

use of org.broadleafcommerce.openadmin.dto.FilterAndSortCriteria in project BroadleafCommerce by BroadleafCommerce.

the class AdminAbstractController method getSectionPersistencePackageRequest.

/**
 * Returns the result of a call to getSectionPersistencePackageRequest(..) with the additional filter
 * and sort criteria attached.
 *
 * @param sectionClassName
 * @param requestParams
 * @param sectionCrumbs
 * @param pathVars
 * @return the PersistencePacakageRequest
 */
protected PersistencePackageRequest getSectionPersistencePackageRequest(String sectionClassName, MultiValueMap<String, String> requestParams, List<SectionCrumb> sectionCrumbs, Map<String, String> pathVars) {
    FilterAndSortCriteria[] fascs = getCriteria(requestParams);
    String[] sectionCriteria = customCriteriaService.mergeSectionCustomCriteria(sectionClassName, getSectionCustomCriteria());
    PersistencePackageRequest ppr = PersistencePackageRequest.standard().withCeilingEntityClassname(sectionClassName).withCustomCriteria(sectionCriteria).withFilterAndSortCriteria(fascs).withStartIndex(getStartIndex(requestParams)).withMaxIndex(getMaxIndex(requestParams)).withSectionCrumbs(sectionCrumbs).withLastId(getLastId(requestParams)).withFirstId(getFirstId(requestParams)).withUpperCount(getUpperCount(requestParams)).withLowerCount(getLowerCount(requestParams)).withPageSize(getPageSize(requestParams)).withPresentationFetch(true);
    attachSectionSpecificInfo(ppr, pathVars);
    return ppr;
}
Also used : PersistencePackageRequest(org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest) FilterAndSortCriteria(org.broadleafcommerce.openadmin.dto.FilterAndSortCriteria)

Example 20 with FilterAndSortCriteria

use of org.broadleafcommerce.openadmin.dto.FilterAndSortCriteria in project BroadleafCommerce by BroadleafCommerce.

the class AdminAbstractController method getCriteria.

/**
 * <p>Helper method to return an array of {@link org.broadleafcommerce.openadmin.dto.FilterAndSortCriteria} based on a map of propertyName -> list of criteria
 * value. This will also grab the sorts off of the request parameters, if any.</p>
 *
 * <p>The multi-valued map allows users to specify multiple criteria values per property, as well as multiple sort
 * properties and sort directions. For multiple sort properties and sort directions, these would usually come in as
 * request parameters like:
 * <br />
 * <br />
 * ....?sortProperty=defaultSku.name&sortProperty=manufacturer&sortDirection=ASCENDING&sortDirection=DESCENDING
 * <br />
 * <br />
 * This would attach criteria such that defaultSku.name was sorted ascending, and manufacturer was sorted descending</p>
 *
 * @param requestParams usually a {@link MultiValueMap} that has been bound by a controller to receive all of the
 * request parameters that are not explicitly named
 * @return the final array of {@link org.broadleafcommerce.openadmin.dto.FilterAndSortCriteria} to pass to the fetch
 *
 * @see {@link #getSortPropertyNames(Map)}
 * @see {@link #getSortDirections(Map)}
 */
protected FilterAndSortCriteria[] getCriteria(Map<String, List<String>> requestParams) {
    if (requestParams == null || requestParams.isEmpty()) {
        return null;
    }
    Map<String, FilterAndSortCriteria> fasMap = new HashMap<>();
    List<FilterAndSortCriteria> result = new ArrayList<>();
    for (Entry<String, List<String>> entry : requestParams.entrySet()) {
        if (!entry.getKey().equals(FilterAndSortCriteria.SORT_PROPERTY_PARAMETER) && !entry.getKey().equals(FilterAndSortCriteria.SORT_DIRECTION_PARAMETER) && !entry.getKey().equals(FilterAndSortCriteria.MAX_INDEX_PARAMETER) && !entry.getKey().equals(FilterAndSortCriteria.START_INDEX_PARAMETER)) {
            List<String> values = entry.getValue();
            List<String> collapsedValues = new ArrayList<>();
            for (String value : values) {
                if (value.contains(FILTER_VALUE_SEPARATOR)) {
                    String[] vs = value.split(FILTER_VALUE_SEPARATOR_REGEX);
                    for (String v : vs) {
                        collapsedValues.add(v);
                    }
                } else {
                    collapsedValues.add(value);
                }
            }
            FilterAndSortCriteria fasCriteria = new FilterAndSortCriteria(entry.getKey(), collapsedValues, Integer.MIN_VALUE);
            fasMap.put(entry.getKey(), fasCriteria);
        }
    }
    List<String> sortProperties = getSortPropertyNames(requestParams);
    List<String> sortDirections = getSortDirections(requestParams);
    if (CollectionUtils.isNotEmpty(sortProperties)) {
        // set up a map to determine if there is already some criteria set for the sort property
        for (int i = 0; i < sortProperties.size(); i++) {
            boolean sortAscending = SortDirection.ASCENDING.toString().equals(sortDirections.get(i));
            FilterAndSortCriteria propertyCriteria = fasMap.get(sortProperties.get(i));
            // FilterAndSortCriteria for the sort
            if (propertyCriteria != null) {
                propertyCriteria.setSortAscending(sortAscending);
            } else {
                propertyCriteria = new FilterAndSortCriteria(sortProperties.get(i));
                propertyCriteria.setOrder(Integer.MIN_VALUE);
                propertyCriteria.setSortAscending(sortAscending);
                fasMap.put(sortProperties.get(i), propertyCriteria);
            }
        }
    }
    result.addAll(fasMap.values());
    return result.toArray(new FilterAndSortCriteria[result.size()]);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) FilterAndSortCriteria(org.broadleafcommerce.openadmin.dto.FilterAndSortCriteria)

Aggregations

FilterAndSortCriteria (org.broadleafcommerce.openadmin.dto.FilterAndSortCriteria)20 FilterMapping (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FilterMapping)10 BasicFieldMetadata (org.broadleafcommerce.openadmin.dto.BasicFieldMetadata)8 Entity (org.broadleafcommerce.openadmin.dto.Entity)8 FieldMetadata (org.broadleafcommerce.openadmin.dto.FieldMetadata)8 DynamicResultSet (org.broadleafcommerce.openadmin.dto.DynamicResultSet)7 ArrayList (java.util.ArrayList)6 PersistencePackageRequest (org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest)6 List (java.util.List)5 PersistencePerspective (org.broadleafcommerce.openadmin.dto.PersistencePerspective)5 FieldPath (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FieldPath)5 Restriction (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.Restriction)5 Serializable (java.io.Serializable)4 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)4 From (javax.persistence.criteria.From)4 Path (javax.persistence.criteria.Path)4 ServiceException (org.broadleafcommerce.common.exception.ServiceException)4 CriteriaTransferObject (org.broadleafcommerce.openadmin.dto.CriteriaTransferObject)4 ForeignKey (org.broadleafcommerce.openadmin.dto.ForeignKey)4 Property (org.broadleafcommerce.openadmin.dto.Property)4