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

dk.apaq.framework.criteria.CriteriaUtil Maven / Gradle / Ivy

package dk.apaq.framework.criteria;

import java.lang.reflect.Method;
import dk.apaq.framework.criteria.rules.SimpleFiltrationItem;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author michael
 */
public class CriteriaUtil {
    private static final Pattern BEANPATTERN_GET = Pattern.compile("get[A-Z][a-zA-Z0-9]*");
    private static final Pattern BEANPATTERN_IS = Pattern.compile("is[A-Z][a-zA-Z0-9]*");

    private static final Logger LOG = LoggerFactory.getLogger(CriteriaUtil.class);
    
    public static Rule.FiltrationItem buildItemFromBean(Object object) {
        SimpleFiltrationItem item = new SimpleFiltrationItem();

        Method[] methods = object.getClass().getMethods();
        for(Method method : methods) {
            
            boolean prefixedWithGet = BEANPATTERN_GET.matcher(method.getName()).matches();
            boolean prefixedWithIs = BEANPATTERN_IS.matcher(method.getName()).matches();
            if(method.getParameterTypes().length ==0 &&
                    method.getReturnType() != Void.class &&
                    (prefixedWithGet || prefixedWithIs)
            ) {
                try {
                    Object returnVal = method.invoke(object);
                    int cutIndex = prefixedWithIs ? 2: 3; 
                    String propertyName = method.getName().substring(cutIndex, cutIndex + 1 ).toLowerCase() + method.getName().substring(cutIndex + 1);
                    item.setProperty(propertyName, returnVal);
                } catch (Exception ex) {
                    LOG.error("Error while building filtrationitem from bean.", ex);
                }
            }
        }
        return item;
    }
    
    
    public static  List filterCollection(Collection c, Rule rule) {
        return filterCollection(c, new Criteria(rule));
    }
    
    public static  List filterCollection(Collection c, Criteria criteria) {
        criteria = criteria == null ? new Criteria() : criteria;
        Map map = new HashMap();
        
        List intermediates = new ArrayList();
        for(T element : c) {
            Rule.FiltrationItem fi = buildItemFromBean(element);
            if(criteria.getRule() == null || criteria.getRule().passesRule(fi)) {
                intermediates.add(fi);
                map.put(fi, element);
            }
        }
        
        if(criteria.getSorter() != null) {
            Collections.sort(intermediates, new Sorter.Comparator(criteria.getSorter()));
        }
        
        List result = new ArrayList();
        int count = 0;
        
        for(Rule.FiltrationItem item : intermediates) {
            if(criteria.getLimit() != null) {
                if(count < criteria.getLimit().getOffset()) {
                    count++;
                    continue;
                }
                
                if(count >= criteria.getLimit().getOffset() + criteria.getLimit().getCount()) {
                    break;
                }
            }
            result.add(map.get(item));
            count++;
        }
        
        return result;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy