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

de.tsl2.nano.service.util.finder.GroupBy Maven / Gradle / Ivy

Go to download

TSL2 JEE Service Access (Generic Services for Entity Access, JEE File-System-Connector, Generic Featuring, Job-Scheduling, BeanContainer, Batch, Comfortable Bean Query Definitions, JAAS, Authentification, Authorization, )

The newest version!
package de.tsl2.nano.service.util.finder;

import java.util.Collection;

/**
 * additional group by statement creator
 * 
 * @param  result bean type
 * @author Thomas Schneider, Thomas Schneider
 * @version $Revision$
 */
public class GroupBy extends AbstractFinder {
    /** serialVersionUID */
    private static final long serialVersionUID = -6987663981295488159L;

    String[] attributeNames;
    public GroupBy(String[] attributeNames) {
        this(null, attributeNames);
    }
    
    /**
     * constructor
     * 
     * @param attributeNames, group by names
     */
    public GroupBy(Class resultType, String[] attributeNames) {
        super(resultType);
        this.attributeNames = attributeNames;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    StringBuffer prepareQuery(int index,
            StringBuffer currentQuery,
            Collection parameter,
            Collection> lazyRelations) {
        //do nothing
        return currentQuery;
    }

    @Override
    StringBuffer createQuery(StringBuffer currentQuery,
            Collection parameter,
            Collection> lazyRelations) {
        return currentQuery.append(createGroupByPostfix( attributeNames));
    }
    
    static String createGroupByPostfix(String[] attributeNames) {
        StringBuffer buf = new StringBuffer(8 + attributeNames.length * 13);
        buf.append("\n group by");
        for (int i = 0; i < attributeNames.length; i++) {
            buf.append(" " + attributeNames[i] + ",");
        }
        return buf.substring(0, buf.length() - 1);
    }
}