001: /*
002: * Created on 10/07/2004
003: *
004: * Swing Components - visit http://sf.net/projects/gfd
005: *
006: * Copyright (C) 2004 Igor Regis da Silva Simões
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or (at your option) any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022:
023: package br.com.gfpshare.db.formatter;
024:
025: import java.lang.reflect.InvocationTargetException;
026: import java.sql.SQLException;
027: import java.util.HashMap;
028: import java.util.Map;
029:
030: import br.com.gfpshare.db.DAOCreationException;
031: import br.com.gfpshare.db.DAOProvider;
032: import br.com.gfpshare.db.PersistentObject;
033: import br.com.gfpshare.plugin.core.DynamicClassLoader;
034:
035: /**
036: * Classe capaz de formatar qualquer tipo de objeto
037: * @author Igor Regis da Silva Simoes
038: */
039: public class GeneralFormatter implements Formatter {
040:
041: /**
042: * Lista de formatters ja carregados
043: */
044: private Map<String, Formatter> formatters = new HashMap<String, Formatter>();
045:
046: /**
047: * @see br.com.gfpshare.db.formatter.Formatter#format(java.lang.Object, int, java.lang.String)
048: */
049: @SuppressWarnings("unchecked")
050: public String format(Object objeto, int tipo, String dataType) {
051: if (hasFormatter(dataType))
052: return formatters.get(dataType).format(objeto, tipo,
053: dataType);
054:
055: PersistentObject persistentObject = null;
056: try {
057: if (objeto instanceof PersistentObject) {
058: persistentObject = (PersistentObject) objeto;
059: } else if (Integer.parseInt(objeto.toString()) >= 0) {
060: try {
061: persistentObject = (PersistentObject) DynamicClassLoader
062: .getClassLoader().loadClass(
063: "br.com.gfp.data." + dataType)
064: .getConstructor(
065: new Class[] { Integer.class })
066: .newInstance(
067: new Object[] { new Integer(objeto
068: .toString()) });
069: persistentObject = DAOProvider
070: .getControllerProvider().getController(
071: dataType).getBy(persistentObject);
072: } catch (DAOCreationException cce) {
073: //TODO Excecao
074: cce.printStackTrace();
075: return "";
076: } catch (SQLException sqle) {
077: //TODO Excecao
078: sqle.printStackTrace();
079: return objeto.toString();
080: } catch (InstantiationException ie) {
081: //TODO Excecao
082: ie.printStackTrace();
083: return "";
084: } catch (ClassNotFoundException cnfe) {
085: //TODO Excecao
086: cnfe.printStackTrace();
087: return "";
088: } catch (NoSuchMethodException nsme) {
089: //TODO Excecao
090: nsme.printStackTrace();
091: return "";
092: } catch (IllegalAccessException iae) {
093: //TODO Excecao
094: iae.printStackTrace();
095: return "";
096: } catch (InvocationTargetException ite) {
097: //TODO Excecao
098: ite.printStackTrace();
099: return "";
100: }
101: } else {
102: return "";
103: }
104: } catch (NumberFormatException nfe) {
105: return null;
106: }
107:
108: switch (tipo) {
109: case Formatter.FORMATO_CURTO:
110: return persistentObject.getAsString(PersistentObject.CURTO);
111: case Formatter.FORMATO_LONGO:
112: return "" + persistentObject;
113: default:
114: return "";
115: }
116: }
117:
118: /**
119: * @see br.com.gfpshare.db.formatter.Formatter#parse(java.lang.String, java.lang.String)
120: */
121: public Object parse(String dado, String dataType) {
122: if (hasFormatter(dataType))
123: return formatters.get(dataType).parse(dado, dataType);
124: //TODO falta implementar a passagem da String para Map e daí para o PersistentObject
125: return null;
126: }
127:
128: /**
129: * Indica se existe um formatter para determinado tipo de dados
130: * @param dataType Tipo de dados a ser formatado
131: * @return boolean Com valor true se existe um formatter para este tipo de dado
132: */
133: public boolean hasFormatter(String dataType) {
134: if (formatters.containsKey(dataType))
135: return true;
136: try {
137: formatters.put(dataType,
138: (Formatter) DynamicClassLoader.getClassLoader()
139: .loadClass(
140: "br.com.gfp.data." + dataType
141: + "Formatter")
142: .newInstance());
143: return true;
144: } catch (IllegalAccessException iae) {
145: return false;
146: } catch (InstantiationException ie) {
147: return false;
148: } catch (ClassNotFoundException cnfe) {
149: return false;
150: }
151: }
152: }
|