tim@129: /** tim@129: * Title: Row, $Header: /share/gdi/SDI-Suite/Repository/projekte/BSH-GDI/genericViewer/src/main/java/de/conterra/bsh/gdi/gnviewer/datasources/Row.java,v 1.4 2008/01/30 12:38:34 blume Exp $ tim@129: * Source: $Source: /share/gdi/SDI-Suite/Repository/projekte/BSH-GDI/genericViewer/src/main/java/de/conterra/bsh/gdi/gnviewer/datasources/Row.java,v $ tim@129: * created by: Stefan Blume (blume) tim@129: * erstellt am: 21.11.2007 tim@129: * Copyright: con terra GmbH, 2005 tim@129: * tim@129: * modified by: $Author: blume $ tim@129: * modified on: $Date: 2008/01/30 12:38:34 $ tim@129: * Version: $Revision: 1.4 $ tim@129: * TAG: $Name: $ tim@129: * locked from: $Locker: $ tim@129: * CVS State: $State: Exp $ tim@129: * Project: $ProjectName$ tim@129: */ tim@129: package de.intevation.gnv.geobackend.sde.datasources; tim@129: tim@129: import java.util.ArrayList; tim@129: import java.util.Calendar; tim@129: import java.util.Date; tim@129: tim@129: import org.apache.log4j.Logger; tim@129: tim@129: import com.esri.sde.sdk.client.SDEPoint; tim@129: import com.esri.sde.sdk.client.SeException; tim@129: import com.esri.sde.sdk.client.SeShape; tim@129: tim@129: import de.intevation.gnv.geobackend.sde.datasources.exception.TechnicalException; tim@129: import de.intevation.gnv.geobackend.util.DateUtils; tim@129: tim@129: /** tim@129: * A Row represents a set of values. tim@129: * In a technical manner (e.g. database manner) a row contains all attributes of a single "hit". tim@129: * tim@129: * @author blume tim@129: * @version 1.0 tim@129: * @serial 1.0 tim@129: * @see tim@129: * @since 21.11.2007 11:00:54 tim@129: */ tim@129: public class Row { tim@129: /** tim@129: * Default Logging instance tim@129: */ tim@129: private static Logger sLogger = Logger.getLogger(Row.class); tim@129: tim@129: /** tim@129: * Data container. tim@129: */ tim@129: private Object[] mObjects; tim@129: tim@129: /** tim@129: * Constructor. tim@129: * tim@129: * @param pRowSize the number of attributes contained by this row. tim@129: */ tim@129: public Row(int pRowSize) { tim@129: mObjects = new Object[pRowSize]; tim@129: } tim@129: tim@129: /** tim@129: * Constructor. tim@129: * tim@129: * @param ArrayStr a line from CSV-File. tim@129: */ tim@129: public Row (String[] ArrayStr){ tim@129: this (ArrayStr.length); tim@129: int nLength = ArrayStr.length; tim@129: for (int i=0; i < nLength; i++){ tim@129: addObject(ArrayStr[i], i); tim@129: tim@129: } tim@129: tim@129: } tim@129: tim@129: tim@129: /** tim@129: * Adds an attribute value to a specific position of this row. tim@129: * tim@129: * @param pObject the object to be stored. tim@129: * @param pPos the postion the value to be saved tim@129: */ tim@129: public void addObject(Object pObject, int pPos) { tim@129: mObjects[pPos] = pObject; tim@129: } tim@129: tim@129: /** tim@129: * Returns a Value out of the Row. tim@129: * tim@129: * @param pPos the position of the value to be returned. tim@129: * @return an Object! (not strongly typed) tim@129: * @throws TechnicalException tim@129: */ tim@129: public Object getValue(int pPos) throws TechnicalException { tim@129: if (pPos < mObjects.length) { tim@263: tim@263: Object o = mObjects[pPos]; tim@263: if (o instanceof SeShape){ tim@263: return this.getPosValue(pPos); tim@263: }else{ tim@263: return o; tim@263: } tim@129: } else { tim@129: throw new TechnicalException("Cannot access this field position. Size is: " + mObjects.length); tim@129: } tim@129: } tim@129: tim@129: tim@129: /** tim@129: * This is a covenient method for getting strongly typed objects out of the row. tim@129: * It has to be ensured, that the type of the requested row position has been resolved out of the ColumnDefinition ({@link ResultSet#getColumnDefinitions()}). tim@129: * In fact, this method executes a simple cast to the desired type. tim@129: * tim@129: * @param pPos the position of the object to be resolved. tim@129: * @return a strongly typed Date tim@129: * @throws TechnicalException tim@129: * @see #getValue(int) tim@129: */ tim@129: public Date getDateValue(int pPos) throws TechnicalException { tim@129: Date date = null; tim@129: try { tim@129: Calendar lCalendar = (Calendar) getValue(pPos); tim@129: date = lCalendar.getTime(); tim@129: } tim@129: catch (ClassCastException e) { tim@129: try{ tim@129: //SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss"); tim@129: date = DateUtils.getDateFromString ((String)getValue(pPos)); //(Date)formatter.parse((String)getValue(pPos)); tim@129: tim@129: tim@129: } tim@129: catch (Exception ex){ tim@129: sLogger.error(getValue(pPos) + " " + ex.getMessage(), ex); tim@129: throw new TechnicalException("Could not cast this value to the Date Type. Object is of value type: " + getValue(pPos).getClass().getName()); tim@129: } tim@129: } tim@129: tim@129: return date; tim@129: } tim@129: tim@129: /** tim@129: * This is a covenient method for getting strongly typed objects out of the row. tim@129: * It has to be ensured, that the type of the requested row position has been resolved out of the ColumnDefinition ({@link ResultSet#getColumnDefinitions()}). tim@129: * In fact, this method executes a simple cast to the desired type. tim@129: * tim@129: * @param pPos the position of the object to be resolved. tim@129: * @return a strongly typed String tim@129: * @throws TechnicalException tim@129: * @see #getValue(int) tim@129: */ tim@129: public String getStringValue(int pPos) throws TechnicalException { tim@129: try { tim@263: Object o = this.getValue(pPos); tim@263: String returnValue = null; tim@263: if (o instanceof SeShape){ tim@263: returnValue = this.getPosValue(pPos); tim@263: }else{ tim@263: returnValue = (String)o; tim@263: } tim@263: return returnValue; tim@129: } catch (ClassCastException e) { tim@129: throw new TechnicalException("Could not cast this value to the String Type. Object is of value type: " + getValue(pPos).getClass().getName()); tim@129: } tim@129: } tim@129: tim@129: /** tim@129: * This is a covenient method for getting strongly typed objects out of the row. tim@129: * It has to be ensured, that the type of the requested row position has been resolved out of the ColumnDefinition ({@link ResultSet#getColumnDefinitions()}). tim@129: * In fact, this method executes a simple cast to the desired type. tim@129: * tim@129: * @param pPos the position of the object to be resolved. tim@129: * @throws TechnicalException tim@129: * @see #getValue(int) tim@129: * * @return a strongly typed int tim@129: */ tim@129: public int getIntValue(int pPos) throws TechnicalException { tim@129: try { tim@129: return (Integer) getValue(pPos); tim@129: } catch (ClassCastException e) { tim@129: throw new TechnicalException("Could not cast this value to the Integer Type. Object is of value type: " + getValue(pPos).getClass().getName()); tim@129: } tim@129: } tim@129: tim@129: /** tim@129: * This is a covenient method for getting strongly typed objects out of the row. tim@129: * It has to be ensured, that the type of the requested row position has been resolved out of the ColumnDefinition ({@link ResultSet#getColumnDefinitions()}). tim@129: * In fact, this method executes a simple cast to the desired type. tim@129: * tim@129: * @param pPos the position of the object to be resolved. tim@129: * @throws TechnicalException tim@129: * @see #getValue(int) tim@129: * * @return a strongly typed Double tim@129: */ tim@129: public Double getDoubleValue(int pPos) throws TechnicalException { tim@129: try { tim@129: return (Double) getValue(pPos); tim@129: } catch (ClassCastException e) { tim@129: try{ tim@129: return new Double ((String)getValue(pPos)); tim@129: } tim@129: catch(Exception ex){ tim@129: sLogger.error(getValue(pPos) + " " + ex.getMessage(), ex); tim@129: throw new TechnicalException("Could not cast this value to the Double Type. Object is of value type: " + getValue(pPos).getClass().getName()); tim@129: } tim@129: } tim@129: } tim@129: tim@129: /** tim@129: * This is a covenient method for getting strongly typed objects out of the row. tim@129: * It has to be ensured, that the type of the requested row position has been resolved out of the ColumnDefinition ({@link ResultSet#getColumnDefinitions()}). tim@129: * In fact, this method executes a simple cast to the desired type. tim@129: * tim@129: * @param pPos the position of the object to be resolved. tim@129: * @return a strongly typed Float tim@129: * @throws TechnicalException tim@129: * @see #getValue(int) tim@129: */ tim@129: public Float getFloatValue(int pPos) throws TechnicalException { tim@129: try { tim@129: return (Float) getValue(pPos); tim@129: } catch (ClassCastException e) { tim@129: throw new TechnicalException("Could not cast this value to the Float Type. Object is of value type: " + getValue(pPos).getClass().getName()); tim@129: } tim@129: } tim@129: /** tim@129: * This is a covenient method for getting strongly typed objects out of the row. tim@129: * It has to be ensured, that the type of the requested row position has been resolved out of the ColumnDefinition ({@link ResultSet#getColumnDefinitions()}). tim@129: * In fact, this method executes a simple cast to the desired type. tim@129: * tim@129: * @param pPos the position of the object to be resolved. tim@129: * @return a strongly typed Float tim@129: * @throws TechnicalException tim@129: * @see #getValue(int) tim@129: */ tim@129: public String getPosValue(int pPos)throws TechnicalException{ tim@268: StringBuffer returnValue = new StringBuffer(); tim@268: synchronized (returnValue) { tim@268: try { tim@876: SeShape val = (SeShape) this.mObjects[pPos]; tim@273: if (val.isPoint()){ tim@876: // Cannot use val.asText() because the tim@876: // generated WKT is invalid. tim@876: ArrayList aList = val.getAllPoints(0,false); tim@876: SDEPoint[] mPoint = (SDEPoint[])aList.get(0); tim@273: returnValue.append("POINT(") tim@273: .append(mPoint[0].getX()) tim@273: .append(" ") tim@275: .append(mPoint[0].getY()); tim@876: if (mPoint[0].is3D()){ tim@876: returnValue.append(" ").append(mPoint[0].getZ()); tim@876: } tim@876: returnValue.append(")"); tim@660: }else if (val.isLine() || val.isSimpleLine()){ tim@876: // Cannot use val.asText() because the tim@876: // generated WKT is invalid. tim@876: ArrayList aList = val.getAllPoints(0,false); tim@876: SDEPoint[] mPoint = (SDEPoint[])aList.get(0); tim@878: boolean isMultiLineString = val.isMultiPart(); tim@878: int length = mPoint.length; tim@878: int nextOffset = length; tim@878: int[] offsets = (int[])aList.get(1); tim@878: int offsetPos = 1; tim@878: tim@878: if(isMultiLineString){ tim@878: returnValue.append("MULTILINESTRING(("); tim@878: nextOffset = offsets.length-1 >= offsetPos ? offsets[offsetPos++] : length; tim@878: }else{ tim@878: returnValue.append("LINESTRING("); tim@878: } tim@878: tim@878: tim@878: tim@878: for (int i = 0; i< length;i++){ tim@878: tim@878: if (i == nextOffset){ tim@878: returnValue.append("),("); tim@878: nextOffset = offsets.length-1 >= offsetPos ? offsets[offsetPos++] : length; tim@878: } tim@878: tim@273: returnValue.append(mPoint[i].getX()) tim@273: .append(" ") tim@275: .append(mPoint[i].getY()); tim@876: if (mPoint[i].is3D()){ tim@876: returnValue.append(" ").append(mPoint[i].getZ()); tim@876: } tim@878: if (i < length-1 && i < nextOffset -1){ tim@268: returnValue.append(" , "); tim@268: } tim@268: } tim@878: tim@878: if(isMultiLineString){ tim@878: returnValue.append("))"); tim@878: }else{ tim@878: returnValue.append(")"); tim@878: } tim@878: tim@876: } else{ tim@876: returnValue.append(val.asText(val.getTextSize())); tim@268: } tim@268: } catch (SeException e) { tim@876: throw new TechnicalException("Could not cast this value to the " + tim@876: "Float Type. Object is of value " + tim@876: "type: " + tim@876: getValue(pPos).getClass().getName()); tim@268: } tim@129: } tim@268: return returnValue.toString(); tim@129: } tim@129: }