view geo-backend/src/main/java/de/intevation/gnv/geobackend/sde/datasources/Row.java @ 1127:ebeb56428409

Added license headers and license file. geo-backend/trunk@1261 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 02 Nov 2010 17:52:22 +0000
parents 02cd2935b5fa
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.geobackend.sde.datasources;

import com.esri.sde.sdk.client.SDEPoint;
import com.esri.sde.sdk.client.SeException;
import com.esri.sde.sdk.client.SeShape;

import de.intevation.gnv.geobackend.sde.datasources.exception.TechnicalException;

import de.intevation.gnv.geobackend.util.DateUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import org.apache.log4j.Logger;

/**
 * A Row represents a set of values.
 * In a technical manner (e.g. database manner) a row contains all attributes of a single "hit".
 *
 * @author blume
 * @author <a href="mailto:tim.englich@intevation.de">Tim Englich</a>
 */
public class Row {
    /**
     * Default Logging instance
     */
    private static Logger sLogger = Logger.getLogger(Row.class);

    /**
     * Data container.
     */
    private Object[] mObjects;

    /**
     * Constructor.
     * @param pRowSize the number of attributes contained by this row.
     */
    public Row(int pRowSize) {
        mObjects = new Object[pRowSize];
    }

    /**
     * Constructor.
     * @param ArrayStr a line from CSV-File.
     */
    public Row (String[] ArrayStr){
        this (ArrayStr.length);
        int nLength = ArrayStr.length;
        for (int i=0; i < nLength; i++){
            addObject(ArrayStr[i], i);

        }

    }

    /**
     * Adds an attribute value to a specific position of this row.
     *
     * @param pObject the object to be stored.
     * @param pPos    the postion the value to be saved
     */
    public void addObject(Object pObject, int pPos) {
        mObjects[pPos] = pObject;
    }

    /**
     * Returns a Value out of the Row.
     *
     * @param pPos the position of the value to be returned.
     * @return an Object! (not strongly typed)
     * @throws TechnicalException
     */
    public Object getValue(int pPos) throws TechnicalException {
        if (pPos < mObjects.length) {

            Object o =  mObjects[pPos];
            if (o instanceof SeShape){
                return this.getPosValue(pPos);
            }else{
                return o;
            }
        } else {
            throw new TechnicalException("Cannot access this field position. Size is: " + mObjects.length);
        }
    }

    /**
     * This is a covenient method for getting strongly typed objects out of the row.
     * It has to be ensured, that the type of the requested row position has been resolved out of the ColumnDefinition ({@link ResultSet#getColumnDefinitions()}).
     * In fact, this method executes a simple cast to the desired type.
     *
     * @param pPos the position of the object to be resolved.
     * @return a strongly typed Date
     * @throws TechnicalException
     * @see #getValue(int)
     */
    public Date getDateValue(int pPos) throws TechnicalException {
        Date date =  null;
        try {
            Calendar lCalendar = (Calendar) getValue(pPos);
            date = lCalendar.getTime();
        }
        catch (ClassCastException e) {
            try{
                //SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
                date = DateUtils.getDateFromString ((String)getValue(pPos)); //(Date)formatter.parse((String)getValue(pPos));


            }
            catch (Exception ex){
                sLogger.error(getValue(pPos) + " " + ex.getMessage(), ex);
                throw new TechnicalException("Could not cast this value to the Date Type. Object is of value type: " + getValue(pPos).getClass().getName());
            }
        }

        return date;
    }

    /**
     * This is a covenient method for getting strongly typed objects out of the row.
     * It has to be ensured, that the type of the requested row position has been resolved out of the ColumnDefinition ({@link ResultSet#getColumnDefinitions()}).
     * In fact, this method executes a simple cast to the desired type.
     *
     * @param pPos the position of the object to be resolved.
     * @return a strongly typed String
     * @throws TechnicalException
     * @see #getValue(int)
     */
    public String getStringValue(int pPos) throws TechnicalException {
        try {
            Object o = this.getValue(pPos);
            String returnValue = null;
            if (o instanceof SeShape){
                returnValue = this.getPosValue(pPos);
            }else{
                returnValue = (String)o;
            }
            return returnValue;
        } catch (ClassCastException e) {
            throw new TechnicalException("Could not cast this value to the String Type. Object is of value type: " + getValue(pPos).getClass().getName());
        }
    }

    /**
     * This is a covenient method for getting strongly typed objects out of the row.
     * It has to be ensured, that the type of the requested row position has been resolved out of the ColumnDefinition ({@link ResultSet#getColumnDefinitions()}).
     * In fact, this method executes a simple cast to the desired type.
     *
     * @param pPos the position of the object to be resolved.
     * @throws TechnicalException
     * @see #getValue(int)
     *      * @return a strongly typed int
     */
    public int getIntValue(int pPos) throws TechnicalException {
        try {
            return (Integer) getValue(pPos);
        } catch (ClassCastException e) {
            throw new TechnicalException("Could not cast this value to the Integer Type. Object is of value type: " + getValue(pPos).getClass().getName());
        }
    }

    /**
     * This is a covenient method for getting strongly typed objects out of the row.
     * It has to be ensured, that the type of the requested row position has been resolved out of the ColumnDefinition ({@link ResultSet#getColumnDefinitions()}).
     * In fact, this method executes a simple cast to the desired type.
     *
     * @param pPos the position of the object to be resolved.
     * @throws TechnicalException
     * @see #getValue(int)
     *      * @return a strongly typed Double
     */
    public Double getDoubleValue(int pPos) throws TechnicalException {
        try {
            return (Double) getValue(pPos);
        } catch (ClassCastException e) {
            try{
                return new Double ((String)getValue(pPos));
            }
            catch(Exception ex){
                sLogger.error(getValue(pPos) + " " + ex.getMessage(), ex);
                throw new TechnicalException("Could not cast this value to the Double Type. Object is of value type: " + getValue(pPos).getClass().getName());
            }
        }
    }

    /**
     * This is a covenient method for getting strongly typed objects out of the row.
     * It has to be ensured, that the type of the requested row position has been resolved out of the ColumnDefinition ({@link ResultSet#getColumnDefinitions()}).
     * In fact, this method executes a simple cast to the desired type.
     *
     * @param pPos the position of the object to be resolved.
     * @return a strongly typed Float
     * @throws TechnicalException
     * @see #getValue(int)
     */
    public Float getFloatValue(int pPos) throws TechnicalException {
        try {
            return (Float) getValue(pPos);
        } catch (ClassCastException e) {
            throw new TechnicalException("Could not cast this value to the Float Type. Object is of value type: " + getValue(pPos).getClass().getName());
        }
    }
    /**
     * This is a covenient method for getting strongly typed objects out of the row.
     * It has to be ensured, that the type of the requested row position has been resolved out of the ColumnDefinition ({@link ResultSet#getColumnDefinitions()}).
     * In fact, this method executes a simple cast to the desired type.
     *
     * @param pPos the position of the object to be resolved.
     * @return a strongly typed Float
     * @throws TechnicalException
     * @see #getValue(int)
     */
    public String getPosValue(int pPos)throws TechnicalException{
        StringBuffer returnValue = new StringBuffer();
        synchronized (returnValue) {
            try {
                SeShape val = (SeShape) this.mObjects[pPos];
                if (val.isPoint()){
                    // Cannot use val.asText() because the
                    // generated WKT is invalid.
                    ArrayList aList = val.getAllPoints(0,false);
                    SDEPoint[] mPoint = (SDEPoint[])aList.get(0);
                    returnValue.append("POINT(")
                               .append(mPoint[0].getX())
                               .append(" ")
                               .append(mPoint[0].getY());
                    if (mPoint[0].is3D()){
                       returnValue.append(" ").append(mPoint[0].getZ());
                    }
                    returnValue.append(")");
                }else if (val.isLine() || val.isSimpleLine()){
                    // Cannot use val.asText() because the
                    // generated WKT is invalid.
                    ArrayList aList = val.getAllPoints(0,false);
                    SDEPoint[] mPoint = (SDEPoint[])aList.get(0);
                    boolean isMultiLineString = val.isMultiPart();
                    int length = mPoint.length;
                    int nextOffset = length;
                    int[] offsets = (int[])aList.get(1);
                    int offsetPos = 1;

                    if(isMultiLineString){
                        returnValue.append("MULTILINESTRING((");
                        nextOffset = offsets.length-1 >= offsetPos ? offsets[offsetPos++] : length;
                    }else{
                        returnValue.append("LINESTRING(");
                    }

                    for (int i = 0; i< length;i++){

                        if (i == nextOffset){
                            returnValue.append("),(");
                            nextOffset = offsets.length-1 >= offsetPos ? offsets[offsetPos++] : length;
                        }

                        returnValue.append(mPoint[i].getX())
                                   .append(" ")
                                   .append(mPoint[i].getY());
                        if (mPoint[i].is3D()){
                            returnValue.append(" ").append(mPoint[i].getZ());
                        }
                        if (i < length-1 && i < nextOffset -1){
                            returnValue.append(" , ");
                        }
                    }

                    if(isMultiLineString){
                        returnValue.append("))");
                    }else{
                        returnValue.append(")");
                    }

                } else{
                    returnValue.append(val.asText(val.getTextSize()));
                }
            } catch (SeException e) {
                throw new TechnicalException("Could not cast this value to the " +
                                             "Float Type. Object is of value " +
                                             "type: " +
                                             getValue(pPos).getClass().getName());
            }
        }
        return returnValue.toString();
    }
}

http://dive4elements.wald.intevation.org