package com.rjconsultores.ventaboletos.web.gui.controladores.relatorios;

import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.List;

import com.rjconsultores.ventaboletos.web.utilerias.MyListbox;

public class EspecializaLista<T> {
	
	private Class<T> classe;
	private MyListbox listBox;
	private String nomeMetodoPk;
	
	public EspecializaLista(Class<T> classe, MyListbox listBox, String nomeMetodoPk) {
		this.classe = classe;
		this.listBox = listBox;
		this.nomeMetodoPk = nomeMetodoPk;
	}
	
	@SuppressWarnings("unchecked")
	public List<T> getLista() {
		return (List<T>)(Object)Arrays.asList(listBox.getData());
	}

	public String obtemIds() {
		StringBuilder ids = new StringBuilder();
		for (T objeto : getLista()) {
			try {
				String valorPk = classe.getMethod(nomeMetodoPk).invoke(objeto).toString();
				ids.append(valorPk);
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			}
			ids.append(", ");
		}
		return removeUltimoCaratere(ids);
	}

	public static String removeUltimoCaratere(StringBuilder ids) {
		return ids.substring(0, ids.length()-2);
	}
}
