teichmann@5863: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5863: * Software engineering by Intevation GmbH teichmann@5863: * teichmann@5994: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5863: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5994: * documentation coming with Dive4Elements River for details. teichmann@5863: */ teichmann@5863: teichmann@5831: package org.dive4elements.river.artifacts.datacage.templating; sascha@998: aheinecke@7592: import java.sql.Date; aheinecke@7592: import java.sql.Timestamp; teichmann@7403: teichmann@7403: import org.apache.log4j.Logger; teichmann@7403: sascha@998: public class TypeConverter sascha@998: { teichmann@7403: private static Logger log = Logger.getLogger(TypeConverter.class); teichmann@7403: sascha@998: private TypeConverter() { sascha@998: } sascha@998: sascha@998: public static Object convert(Object object, String type) { sascha@998: sascha@998: if (type == null) { sascha@998: return object; sascha@998: } sascha@998: teichmann@7403: type = type.toLowerCase(); teichmann@7403: teichmann@7403: if ("integer".equals(type)) { sascha@998: return Integer.valueOf(object.toString()); sascha@998: } sascha@998: teichmann@7403: if ("double".equals(type)) { sascha@998: return Double.valueOf(object.toString()); sascha@998: } sascha@998: teichmann@7403: if ("string".equals(type)) { sascha@998: return object.toString(); sascha@998: } sascha@998: teichmann@7403: if ("date".equals(type)) { teichmann@7403: if (object instanceof Date) { teichmann@7403: return object; teichmann@7403: } teichmann@7403: try { teichmann@7403: return new Date((long)Double.parseDouble(object.toString())); teichmann@7403: } teichmann@7403: catch (NumberFormatException nfe) { teichmann@7403: log.warn(nfe); teichmann@7403: return null; teichmann@7403: } teichmann@7403: } teichmann@7403: aheinecke@7592: if ("timestamp".equals(type)) { aheinecke@7592: if (object instanceof Timestamp) { aheinecke@7592: return object; aheinecke@7592: } aheinecke@7592: try { tom@8856: return new Timestamp( tom@8856: (long)Double.parseDouble(object.toString())); aheinecke@7592: } aheinecke@7592: catch (NumberFormatException nfe) { aheinecke@7592: log.warn(nfe); aheinecke@7592: return null; aheinecke@7592: } aheinecke@7592: } aheinecke@7592: sascha@998: // TODO: Add more types sascha@998: sascha@998: return object; sascha@998: } sascha@998: } sascha@998: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :