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

io.baltoro.client.CustomQuery Maven / Gradle / Ivy

There is a newer version: 4.0.12
Show newest version
package io.baltoro.client;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;

import io.baltoro.client.util.StringUtil;

public class CustomQuery
{
	
	private Class c;
	private String q;
	private Map map = new HashMap<>(50);
	private LocalDB db;
	private RecordList rl;
	private boolean executed;
	
	CustomQuery(Class c, String q, LocalDB db)
	{
		this.c = c;
		this.q = q;
		this.db = db;
		
		if(c == String.class)
		{
			map.put("1", "");
		}
	}
	
	
	public CustomQuery map(String colName, String propertyName)
	{
		map.put(colName.toLowerCase(), propertyName);
		return this;
	}
	
	public CustomQuery map(String colName, Field f)
	{
		map.put(colName.toLowerCase(), f.getName());
		return this;
	}
	
	
	public RecordList execute()
	{
		if(!executed)
		{
			rl = db.executeQuery(c, this);
		}
		executed = true;
		return rl;
	}
	
	String getQuery()
	{
		return q;
	}
	
	Class getClassT()
	{
		return c;
	}
	
	String getPropertyName(String colName)
	{
		return map.get(colName.toLowerCase());
	}
	
	public CustomQuery displayHeaders()
	{
		execute();
		
		for (ColumnMetadata md : rl.getColMD())
		{
			int len =  md.getMaxLen() > md.getColName().length() ? md.getMaxLen() : md.getColName().length();
			System.out.print(StringUtil.pad(md.getColName(), len, '*'));
			System.out.print(" | ");
		}
		
		System.out.println("");
		
		return this;
	}
	
	public void displayResults()
	{
		execute();
		
		for (T t : rl)
		{
			rl.getColMD().forEach(md -> 
			{
			
				Object v = null;
				if(t instanceof Record)
				{
					v = ((Record)t).getValue(md.getColName());
				}
				else
				{
					String pName = getPropertyName(md.getColName());
					if(pName != null)
					{
						try
						{
							v = BeanUtils.getProperty(t, pName);
						} 
						catch (Exception e)
						{
							e.printStackTrace();
						}
					}
				}
				
				
				int len =  md.getMaxLen() > md.getColName().length() ? md.getMaxLen() : md.getColName().length();
				System.out.print(StringUtil.pad(v.toString(),len, ' '));
				System.out.print(" | ");
				
			});
			
			System.out.println("");
		}
	
		
		
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy