All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.mosip.pms.common.util.SortUtils Maven / Gradle / Ivy

There is a newer version: 1.3.0-dp.1
Show newest version
package io.mosip.pms.common.util;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;

import io.mosip.pms.common.constant.OrderEnum;
import io.mosip.pms.common.constant.SearchErrorCode;
import io.mosip.pms.common.dto.SearchSort;
import io.mosip.pms.common.entity.BaseEntity;
import io.mosip.pms.common.exception.RequestException;



/**
 * {@link SortUtils} use to sort the list based on the sort criteria this class
 * support multiple field sorting
 * 
 */
public class SortUtils {

	/**
	 * Method to sort the list based on the sorting parameter support mutliple sort
	 * criteria
	 * 
	 * @param           list generic type
	 * @param list         input to be sorted
	 * @param sortCriteria sorting criteria
	 * @return sorted list
	 */
	public  List sort(List list, List sortCriteria) {
		List fields = null;
		if (toBeSorted(list, sortCriteria)) {
			T data = list.get(0);
			fields = extractField(data);
			List> comparatorlist = new ArrayList<>();
			for (int i = 0; i < sortCriteria.size(); i++) {
				SearchSort sort = sortCriteria.get(i);
				comparatorlist.add(new FieldComparator(findField(fields, sort.getSortField()), sort));
			}
			return list.parallelStream().sorted(new MultiFieldComparator(comparatorlist))
					.collect(Collectors.toList());
		}
		return list;
	}

	/**
	 * Method to verify sorting criteria and the list to be sorted are present
	 * 
	 * @param           generic list type
	 * @param list         input list
	 * @param sortCriteria sort criteria input
	 * @return true if need sorting, false otherwise
	 */
	private  boolean toBeSorted(List list, List sortCriteria) {
		return (list != null && !list.isEmpty() && sortCriteria != null && !sortCriteria.isEmpty());
	}

	/**
	 * Method to extract the fields
	 * 
	 * @param    generic type
	 * @param clazz input class
	 * @return {@link List} of {@link Field} for the input along with super class
	 *         {@link Field}
	 */
	private  List extractField(T clazz) {
		List fields = new ArrayList<>();
		fields.addAll(Arrays.asList(clazz.getClass().getDeclaredFields()));
		if (clazz.getClass().getSuperclass() != Object.class) {
			fields.addAll(Arrays.asList(clazz.getClass().getSuperclass().getDeclaredFields()));
		}
		return fields;
	}

	private  List extractEntityFields(Class clazz) {
		List fields = new ArrayList<>();
		fields.addAll(Arrays.asList(clazz.getDeclaredFields()));
		fields.addAll(Arrays.asList(clazz.getSuperclass().getDeclaredFields()));
		return fields;

	}

	private Field findField(List fields, String name) {
		Optional field = fields.stream().filter(f -> f.getName().equalsIgnoreCase(name)).findFirst();
		if (StringUtils.isBlank(name)) {
			throw new RequestException(SearchErrorCode.INVALID_SORT_INPUT.getErrorCode(),
					SearchErrorCode.INVALID_SORT_INPUT.getErrorMessage());
		}
		if (field.isPresent()) {
			return field.get();
		} else {
			throw new RequestException(SearchErrorCode.INVALID_SORT_FIELD.getErrorCode(),
					String.format(SearchErrorCode.INVALID_SORT_FIELD.getErrorMessage(), name));

		}
	}

	/**
	 * @param clazz       - generic class
	 * @param searchSorts - {@link SearchSort}
	 */
	public  void validateSortField(Class clazz, List searchSorts) {
		List fields = extractEntityFields(clazz);
		for (SearchSort searchSort : searchSorts) {
			findField(fields, searchSort.getSortField());
			findType(searchSort.getSortType());
		}

	}

	private void findType(String sortType) {
		if (StringUtils.isBlank(sortType)) {
			throw new RequestException(SearchErrorCode.INVALID_SORT_INPUT.getErrorCode(),
					SearchErrorCode.INVALID_SORT_INPUT.getErrorMessage());
		} else if (OrderEnum.asc.name().equalsIgnoreCase(sortType)
				|| OrderEnum.desc.name().equalsIgnoreCase(sortType)) {
			return;
		} else {
			throw new RequestException(SearchErrorCode.INVALID_SORT_TYPE.getErrorCode(),
					String.format(SearchErrorCode.INVALID_SORT_TYPE.getErrorMessage(), sortType));
		}
	}
	

	public  void validateSortField(Class clazz, Class class2,
			List searchSorts) {
		List fields = extractFields(clazz);
		List entityFields = extractEntityFields(class2);
		fields.addAll(entityFields);
		for (SearchSort searchSort : searchSorts) {
			findField(fields, searchSort.getSortField());
			findType(searchSort.getSortType());
		}
	}

	private  List extractFields(Class clazz) {
		List fields = new ArrayList<>();
		fields.addAll(Arrays.asList(clazz.getDeclaredFields()));
		return fields;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy