ingo@1115: /* ingo@1115: * Copyright (c) 2010 by Intevation GmbH ingo@1115: * ingo@1115: * This program is free software under the LGPL (>=v2.1) ingo@1115: * Read the file LGPL.txt coming with the software for details ingo@1115: * or visit http://www.gnu.org/licenses/ if it does not exist. ingo@1115: */ ingo@1115: ingo@238: package de.intevation.gnv.exports; ingo@238: sascha@779: import com.vividsolutions.jts.geom.Point; sascha@779: sascha@779: import com.vividsolutions.jts.io.ParseException; sascha@779: sascha@779: import de.intevation.gnv.geobackend.base.Result; sascha@779: sascha@779: import de.intevation.gnv.state.exception.StateException; sascha@779: ingo@238: import java.text.DateFormat; ingo@238: import java.text.SimpleDateFormat; ingo@238: sascha@779: import java.util.Date; ingo@238: sascha@779: import org.apache.log4j.Logger; ingo@238: ingo@238: /** ingo@771: * This class is a specialization of {@link ShapeDataCollector} and turns ingo@771: * furthermore a given datetime string into a specific format. ingo@771: * ingo@771: * @author Ingo Weinzierl ingo@238: */ ingo@238: public class SimpleOdvDataCollector ingo@238: extends ShapeDataCollector ingo@238: { ingo@771: /** ingo@771: * Logger used for logging with log4j. ingo@771: */ ingo@238: private static Logger log = Logger.getLogger(SimpleOdvDataCollector.class); ingo@238: ingo@771: /** ingo@771: * Constant field which defines the source format of a given datetime. ingo@771: */ ingo@238: public static final String SRC_FORMAT = "yyyy.MM.dd HH:mm:ss"; ingo@771: ingo@771: /** ingo@771: * Constant field which defines the target format of a given datetime. ingo@771: */ ingo@238: public static final String DEST_FORMAT = "yyyy-MM-dd HH:mm"; ingo@238: ingo@771: /** ingo@771: * Source format. ingo@771: */ ingo@238: public static DateFormat srcFormat = new SimpleDateFormat(SRC_FORMAT); ingo@771: ingo@771: /** ingo@771: * Target format. ingo@771: */ ingo@238: public static DateFormat destFormat = new SimpleDateFormat(DEST_FORMAT); ingo@238: ingo@771: /** ingo@771: * Constructor ingo@771: * ingo@771: * @param names See {@link #names} ingo@771: */ ingo@238: public SimpleOdvDataCollector(String[] names) { ingo@238: super(names); ingo@238: } ingo@238: ingo@771: /** ingo@771: * Given datetime values which corresponds to the attribute key 'TIMEVALUE' ingo@771: * are transformed from {@link #SRC_FORMAT} into {@link #DEST_FORMAT}. ingo@771: * ingo@771: * @see de.intevation.gnv.exports.Export.DataCollector#getData(Result) ingo@771: */ ingo@238: public String[] getData(Result result) tim@335: throws StateException ingo@238: { ingo@238: if (rd == null) ingo@238: init(result); ingo@238: ingo@238: try { ingo@238: String [] entries = new String[names.length+1]; ingo@238: int j = 0; ingo@238: for (int i = 0; i < names.length; i++) { sascha@778: ingo@238: if (names[i].equals("SHAPE")) { ingo@238: Point p = (Point)wktReader.read(result.getString("SHAPE")); ingo@238: ingo@238: entries[j++] = ""+p.getX(); ingo@238: entries[j++] = ""+p.getY(); ingo@238: } sascha@778: // Change the datetime format from yyyy.MM.dd HH:mm:ss to ingo@238: // yyyy-MM-dd HH:mm ingo@238: else if (names[i].equals("TIMEVALUE")) { ingo@238: Date source = srcFormat.parse(result.getString(names[i])); ingo@238: entries[j++] = destFormat.format(source); ingo@238: } ingo@238: else { ingo@238: entries[j++] = result.getString(names[i]); ingo@238: } ingo@238: } ingo@238: ingo@238: return entries; ingo@238: } ingo@238: catch (ParseException pe) { ingo@238: log.error(pe, pe); tim@335: throw new StateException( ingo@238: "Error occured while parsing source data."); ingo@238: } ingo@238: catch (java.text.ParseException pe) { ingo@238: log.error(pe, pe); tim@335: throw new StateException( ingo@238: "Error occured while parsing source data."); ingo@238: } ingo@238: } ingo@238: } ingo@771: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :