Skip to content

Commit

Permalink
Fixed IndexField's view all list grid filter and sort. (#1768)
Browse files Browse the repository at this point in the history
- Sorting on index field name now works
- Adding custom FilterSort criteria now works since previously most of the cto was ignored if the "field" filter sort was added
  • Loading branch information
Jay Aisenbrey authored and phillipuniverse committed Jan 15, 2018
1 parent 4b09139 commit 2b5175c
Showing 1 changed file with 28 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@
*/
package org.broadleafcommerce.admin.server.service.handler;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.From;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand All @@ -25,7 +35,6 @@
import org.broadleafcommerce.common.persistence.Status;
import org.broadleafcommerce.common.presentation.client.OperationType;
import org.broadleafcommerce.core.search.domain.IndexField;
import org.broadleafcommerce.core.search.domain.IndexFieldImpl;
import org.broadleafcommerce.core.search.domain.IndexFieldType;
import org.broadleafcommerce.core.search.domain.IndexFieldTypeImpl;
import org.broadleafcommerce.core.search.domain.solr.FieldType;
Expand All @@ -39,23 +48,13 @@
import org.broadleafcommerce.openadmin.server.dao.DynamicEntityDao;
import org.broadleafcommerce.openadmin.server.service.handler.CustomPersistenceHandlerAdapter;
import org.broadleafcommerce.openadmin.server.service.persistence.module.RecordHelper;
import org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FieldPath;
import org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FieldPathBuilder;
import org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FilterMapping;
import org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.Restriction;
import org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.PredicateProvider;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Order;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

/**
* @author Chad Harchar (charchar)
*/
Expand Down Expand Up @@ -169,72 +168,26 @@ public Entity add(PersistencePackage persistencePackage, DynamicEntityDao dynami
}

@Override
public DynamicResultSet fetch(PersistencePackage persistencePackage, CriteriaTransferObject cto, DynamicEntityDao
dynamicEntityDao, RecordHelper helper) throws ServiceException {
public DynamicResultSet fetch(PersistencePackage persistencePackage, CriteriaTransferObject cto,
DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {

FilterAndSortCriteria fieldFsc = cto.getCriteriaMap().get("field");
if (fieldFsc != null) {
List<String> filterValues = fieldFsc.getFilterValues();
boolean didFilter = false;
cto.getCriteriaMap().remove("field");

CriteriaBuilder builder = dynamicEntityDao.getStandardEntityManager().getCriteriaBuilder();
CriteriaQuery<IndexField> criteria = builder.createQuery(IndexField.class);
Root<IndexFieldImpl> root = criteria.from(IndexFieldImpl.class);
criteria.select(root);

// Check if we are searching for specific field names
List<Predicate> restrictions = new ArrayList<>();
if (CollectionUtils.isNotEmpty(filterValues)) {
restrictions.add(builder.like(root.get("field").<String>get("friendlyName"), "%" + filterValues.get(0) + "%"));
didFilter = true;
}

// Check if this filter value has a sort direction associated with it
Order order = null;
for (FilterAndSortCriteria sortCriteria : cto.getCriteriaMap().values()) {
if (sortCriteria.getSortDirection() != null) {
Path path = root;
try {
// Try to find the path to the property in IndexFieldImpl
String[] pathParts = sortCriteria.getPropertyId().split("\\.");
for (String part : pathParts) {
path = path.get(part);
}

// If we made it here, we have the path, set the sorting (asc/desc)
if (sortCriteria.getSortAscending()) {
order = builder.asc(path);
} else {
order = builder.desc(path);
cto.getAdditionalFilterMappings().add(new FilterMapping()
.withFieldPath(new FieldPath().withTargetProperty("field.friendlyName"))
.withFilterValues(filterValues)
.withSortDirection(fieldFsc.getSortDirection())
.withRestriction(new Restriction()
.withPredicateProvider(new PredicateProvider() {
@Override
public Predicate buildPredicate(CriteriaBuilder builder, FieldPathBuilder fieldPathBuilder, From root,
String ceilingEntity, String fullPropertyName, Path explicitPath, List directValues) {
return builder.like(explicitPath.as(String.class), "%" + directValues.get(0) + "%");
}
criteria.orderBy(order);
break;
} catch (IllegalArgumentException e) {
// This isn't an actual entity property
continue;
}
}
}

criteria.where(restrictions.toArray(new Predicate[restrictions.size()]));

TypedQuery<IndexField> query = dynamicEntityDao.getStandardEntityManager().createQuery(criteria);
List<IndexField> indexFields = query.getResultList();

// Convert the result list into a list of entities
PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
Map<String, FieldMetadata> indexFieldMetadata = helper.getSimpleMergedProperties(IndexField.class.getName(), persistencePerspective);
Entity[] entities = helper.getRecords(indexFieldMetadata, indexFields);

// We need to get the total number of records available because the entityList returned may not be the full set.
Map<String, FieldMetadata> originalProps = helper.getSimpleMergedProperties(IndexField.class.getName(), persistencePerspective);
List<FilterMapping> filterMappings = helper.getFilterMappings(persistencePerspective, cto, IndexField.class.getName(), originalProps);
int totalRecords = helper.getTotalRecords(persistencePackage.getCeilingEntityFullyQualifiedClassname(), filterMappings);

// Create a Dynamic Result Set from the entity list created above.
DynamicResultSet resultSet = new DynamicResultSet(entities, (didFilter ? entities.length : totalRecords));
return resultSet;
})
));
}

DynamicResultSet resultSet = helper.getCompatibleModule(OperationType.BASIC).fetch(persistencePackage, cto);
Expand Down

0 comments on commit 2b5175c

Please sign in to comment.