Summary Table
Categories |
Total Count |
PII |
0 |
URL |
0 |
DNS |
0 |
EKL |
0 |
IP |
0 |
PORT |
0 |
VsID |
0 |
CF |
0 |
AI |
0 |
VPD |
0 |
PL |
0 |
Other |
0 |
File Content
package gov.va.med.pbm.ampl.utility.operations;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import gov.va.med.pbm.ampl.constant.AmplConstants;
import gov.va.med.pbm.ampl.utility.CollectionUtility;
import gov.va.med.pbm.ampl.utility.ESAPIValidator;
import gov.va.med.pbm.ampl.utility.PropertyReflection;
import gov.va.med.pbm.ampl.utility.operations.comparator.AmplComparator;
import gov.va.med.pbm.ampl.utility.operations.criteria.SortCriteria;
/**
* The SortOperation class allows the API to perform a sort on a given collection of objects.
* <p>
* If the sortParams contains multiple criteria, the sort will be performed on the first in stack.
*
* @author Ian Meinert
*
* @param <T> The Generic type representing the class of the object which the sort will be performed.
*/
public class SortOperation<T> extends PropertyReflection {
/**
* The application logger.
*/
static final Logger LOGGER = LoggerFactory.getLogger(SortOperation.class);
/**
* The collection of search criteria. The setter requires a string input, which is then parsed and processed
*/
private final List<SortCriteria> sortParams;
/**
* Default constructor which initializes the sortParams.
*/
public SortOperation() {
this.sortParams = new ArrayList<SortCriteria>();
}
/**
* This method sorts a given Collection of Generics of Type T.
*
* @param collection Collection of Generics of Type T
* @return Collection of Generics of Type T
*/
public Collection<T> sort(Collection<T> collection) {
LOGGER.info("Sorting the collection");
Collection<T> results = new ArrayList<T>();
// If search parameters haven been provided, apply the filter
results = !getSortParams().isEmpty() ? this.sortCollection(collection) : collection;
return results;
}
/**
* This method will sort a collection where one given field has a null value.
*
* @param collection Collection of Generics of Type T
* @return Collection of Generics of Type T
*/
public Collection<T> sortFieldWithNull(Collection<T> collection) {
List<T> temp = new ArrayList<>(collection);
List<T> foundNulls = new ArrayList<>();
if(sortParams.size() > 1) {
throw new IllegalArgumentException("Exactly 1 parameter is allowed!");
}
SortCriteria sortParam = sortParams.get(0);
// move missing allergyAssessments to the top of the stack
for (T item : collection) {
Optional<?> field = Optional.ofNullable(get(item, sortParam.getKey()));
if (!field.isPresent()) {
foundNulls.add(item);
temp.remove(item);
}
}
// re-add the missing allergies to the top of the stack
temp = new ArrayList<>(this.sort(temp));
temp.addAll(0, foundNulls);
return temp;
}
/**
* The filterCollection method performs a filter against a given collection using a bean of the same type.
*
* @param collection The collection of Type T to filter
* @return a collection of Type T
*/
private Collection<T> sortCollection(Collection<T> collection) {
List<T> list = new ArrayList<T>(collection);
// apply the comparator only if there is more than 1 object in the collection
if (!CollectionUtility.isNullOrEmpty(list) && list.size() > 1) {
Collections.sort(list, new AmplComparator<T>(sortParams));
}
return list;
}
/**
* Collection of SortCriteria.
*
* @return Collection of SortCriteria
*/
public List<SortCriteria> getSortParams() {
return sortParams;
}
/**
* Sets the collection of sort parameters.
* <p>
* The setter requires a string input, which is then parsed and processed using a pattern match
*
* @param sort The sort criteria which must match a predetermined pattern
*/
public void setSortParams(String sort) {
LOGGER.info(ESAPIValidator.validateLogParam("Checking the sort param: " + sort));
Pattern pattern = Pattern.compile("(\\w.+?)\\[([a-zA-Z0-9]+)\\],");
Matcher matcher = pattern.matcher(sort + ",");
while (matcher.find()) {
sortParams.add(new SortCriteria(matcher.group(AmplConstants.NUMBER_ONE), matcher.group(AmplConstants.NUMBER_TWO)));
LOGGER.info("Adding the sort criteria: {}, {}",
ESAPIValidator.validateLogParam(matcher.group(AmplConstants.NUMBER_ONE)),
ESAPIValidator.validateLogParam(matcher.group(AmplConstants.NUMBER_TWO)));
}
}
}