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

org.skyway.spring.util.dao.query.JPQLResultsProvider Maven / Gradle / Ivy

The newest version!
/**
* Copyright 2007 - 2011 Skyway Software, Inc.
*/
package org.skyway.spring.util.dao.query;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import org.skyway.common.util.ConversionUtils;

public class JPQLResultsProvider implements Serializable {
	private static final long serialVersionUID = 1L;

	private Object jpqlResults;
	
	public JPQLResultsProvider(Object jpqlResults){
		this.jpqlResults = jpqlResults;
	}
	
	@SuppressWarnings("unchecked")
	public  T getSingleResult(int position, Class targetClass) {
		Object dbResult = null;
		T result = null;
		
		if (jpqlResults instanceof Collection) {
			if (((Collection) jpqlResults).size() > 0){
				dbResult = ((Collection) jpqlResults).iterator().next();
			}
		} else {
			dbResult = jpqlResults;
		}

		if (dbResult != null){
			if (dbResult.getClass().isArray()) {
				dbResult = ((Object[]) dbResult)[position];
			}
			
			result = ConversionUtils.convert(dbResult, targetClass);
		}

		return result;
	}
	
	public  Set getResultsAsSet(int position, Class targetClass) {
		Set results = new LinkedHashSet();
		
		getResults(position, targetClass, results);
		
		return results;
	}
	
	public  List getResultsAsList(int position, Class targetClass) {
		List results = new ArrayList();
		
		getResults(position, targetClass, results);
		
		return results;
	}
	
	@SuppressWarnings("unchecked")
	public  void getResults(int position, Class targetClass, Collection results) {
		Object dbResult = null;
		T positionedResult;

		if (jpqlResults instanceof Collection) {
			results.addAll((Collection)jpqlResults);
			if (results.size() > 0){
				dbResult = ((Collection) jpqlResults).iterator().next();
			}
		} else {
			results.add((T)jpqlResults);
			dbResult = jpqlResults;
		}

		if (dbResult != null && dbResult.getClass().isArray()) {
			for (Iterator i = results.iterator(); i.hasNext();) {
				positionedResult = ConversionUtils.convert(((Object[]) i.next())[position], targetClass);
				results.add(positionedResult);
			}
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy