view gnv-artifacts/src/main/java/de/intevation/gnv/exports/ODVExport.java @ 1115:f953c9a559d8

Added license file and license headers. gnv-artifacts/trunk@1260 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 02 Nov 2010 17:46:55 +0000
parents 2b4f1c095468
children
line wrap: on
line source
/*
 * Copyright (c) 2010 by Intevation GmbH
 *
 * This program is free software under the LGPL (>=v2.1)
 * Read the file LGPL.txt coming with the software for details
 * or visit http://www.gnu.org/licenses/ if it does not exist.
 */

package de.intevation.gnv.exports;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.log4j.Logger;

import au.com.bytecode.opencsv.CSVWriter;
import de.intevation.gnv.geobackend.base.Result;
import de.intevation.gnv.state.describedata.KeyValueDescibeData;
import de.intevation.gnv.state.exception.StateException;

/**
 * This class is used to create a specific export document which is similar to
 * an CSV document.
 *
 * @author <a href="mailto:tim.englich@intevation.de">Tim Englich</a>
 */
public class ODVExport extends DefaultExport {

    /**
     * Logger used for logging via log4j.
     */
    private static Logger log = Logger.getLogger(ODVExport.class);

    /**
     * Collection of parameters.
     */
    private Collection parameters = null;

    /**
     * The TimeStime which should be integrated in the Export to mark a
     * TimeSeries.
     */
    private String startTime = null;

    /**
     * Constructor
     * @param collector DataCollector used to extract the required data.
     * @param parameters A collection of parameters to be displayed in the
     * export.
     * @param startTime The TimeStamp which should ne integrated in the export
     * to mark a TimeSeries. If it should not be a TimeSeries this Value must
     * be null.
     */
    public ODVExport(DataCollector collector, Collection parameters, String startTime) {
        super(collector);
        this.parameters = parameters;
        this.startTime = startTime;
    }


    @Override
    protected void writeData(
        Profile profile, Collection result, CSVWriter writer
    )throws StateException
    {
        Iterator<Result> it = result.iterator();
        log.debug("Put " + result.size() + " elements into odv export.");

        String[] header = profile.getHeader();
        ArrayList<String> headerList = new ArrayList<String>();
        for (int i= 0; i < header.length; i++){
            headerList.add(header[i]);
        }

        if (this.startTime != null){
            headerList.add("time_ISO8601");
        }
        ArrayList<String> paramids = new ArrayList<String>();

        Map<StringArrayKey, Map<String,String>> aggregatedRows =
            new HashMap<StringArrayKey, Map<String,String>>();

        while (it.hasNext()) {
            Result res = it.next();

            String[] value = collector.getData(res);
            StringArrayKey key = new StringArrayKey(value);
            String parameterValue = res.getString("DATAVALUE");
            String parameterID = res.getString("PARAMETER");

            if (!paramids.contains(parameterID)){
                paramids.add(parameterID);
                headerList.add(findParamTitle(parameters, parameterID));
                headerList.add("QF");
            }

            Map<String,String> aggregatedRow = aggregatedRows.get(key);
            if (aggregatedRow!= null){
                aggregatedRow.put(parameterID, parameterValue);
            }
            else{
                Map<String,String> params = new HashMap<String, String>();
                params.put(parameterID, parameterValue);
                aggregatedRows.put(key, params);
            }
        }

        if (header != null){
            writer.writeNext(headerList.toArray(header));
        }

        Iterator<StringArrayKey> rows = aggregatedRows.keySet().iterator();
        while (rows.hasNext()){
            StringArrayKey row = rows.next();
            Map<String,String> params = aggregatedRows.get(row);
            ArrayList<String> rowList = new ArrayList<String>();
            String[] rowArray = row.getValue();
            for (int i= 0; i < rowArray.length; i++){
                rowList.add(rowArray[i]);
            }
            if (this.startTime != null){
                String measurementtime = rowList.get(3).replace(' ','T');
                rowList.set(3, this.startTime.replace('.','-'));
                rowList.add(measurementtime+":00.0");
            }
            for (int i = 0; i < paramids.size();i++){
                String key = paramids.get(i);
                String value = params.get(key);
                if (value == null){
                    value = "";
                }
                rowList.add(value);
                rowList.add("1");
            }
            writer.writeNext(rowList.toArray(rowArray));
        }
    }

    /**
     * This method is used to search specific value coresponding to its key
     * <code>id</code> and return its description.
     *
     * @param values Collection of parameters.
     * @param id Key used to find the value.
     *
     * @return Description of searched value.
     */
    protected String findParamTitle(Collection values, String id) {
        log.debug("find description of dataset");

        if (values != null){
            Iterator it = values.iterator();
            while (it.hasNext()) {
                KeyValueDescibeData data = (KeyValueDescibeData) it.next();

                if (id.equals(data.getKey()))
                    return data.getValue();
            }
        }
        return "";
    }


}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org