ingo@1127: /* ingo@1127: * Copyright (c) 2010 by Intevation GmbH ingo@1127: * ingo@1127: * This program is free software under the LGPL (>=v2.1) ingo@1127: * Read the file LGPL.txt coming with the software for details ingo@1127: * or visit http://www.gnu.org/licenses/ if it does not exist. ingo@1127: */ ingo@1127: sascha@894: package de.intevation.gnv.geobackend.sde.datasources; sascha@894: sascha@894: import com.esri.sde.sdk.client.SDEPoint; sascha@894: import com.esri.sde.sdk.client.SeException; sascha@894: import com.esri.sde.sdk.client.SeShape; sascha@894: sascha@894: import de.intevation.gnv.geobackend.sde.datasources.exception.TechnicalException; sascha@894: sascha@894: import de.intevation.gnv.geobackend.util.DateUtils; sascha@894: sascha@894: import java.util.ArrayList; sascha@894: import java.util.Calendar; sascha@894: import java.util.Date; sascha@894: sascha@894: import org.apache.log4j.Logger; sascha@894: sascha@894: /** sascha@894: * A Row represents a set of values. sascha@894: * In a technical manner (e.g. database manner) a row contains all attributes of a single "hit". sascha@894: * sascha@894: * @author blume sascha@894: * @author Tim Englich sascha@894: */ sascha@894: public class Row { sascha@894: /** sascha@894: * Default Logging instance sascha@894: */ sascha@894: private static Logger sLogger = Logger.getLogger(Row.class); sascha@894: sascha@894: /** sascha@894: * Data container. sascha@894: */ sascha@894: private Object[] mObjects; sascha@894: sascha@894: /** sascha@894: * Constructor. sascha@894: * @param pRowSize the number of attributes contained by this row. sascha@894: */ sascha@894: public Row(int pRowSize) { sascha@894: mObjects = new Object[pRowSize]; sascha@894: } sascha@894: sascha@894: /** sascha@894: * Constructor. sascha@894: * @param ArrayStr a line from CSV-File. sascha@894: */ sascha@894: public Row (String[] ArrayStr){ sascha@894: this (ArrayStr.length); sascha@894: int nLength = ArrayStr.length; sascha@894: for (int i=0; i < nLength; i++){ sascha@894: addObject(ArrayStr[i], i); ingo@897: ingo@897: } ingo@897: sascha@894: } sascha@894: sascha@894: /** sascha@894: * Adds an attribute value to a specific position of this row. sascha@894: * sascha@894: * @param pObject the object to be stored. sascha@894: * @param pPos the postion the value to be saved sascha@894: */ sascha@894: public void addObject(Object pObject, int pPos) { sascha@894: mObjects[pPos] = pObject; sascha@894: } sascha@894: sascha@894: /** sascha@894: * Returns a Value out of the Row. sascha@894: * sascha@894: * @param pPos the position of the value to be returned. sascha@894: * @return an Object! (not strongly typed) sascha@894: * @throws TechnicalException sascha@894: */ sascha@894: public Object getValue(int pPos) throws TechnicalException { sascha@894: if (pPos < mObjects.length) { ingo@897: sascha@894: Object o = mObjects[pPos]; sascha@894: if (o instanceof SeShape){ sascha@894: return this.getPosValue(pPos); sascha@894: }else{ sascha@894: return o; sascha@894: } sascha@894: } else { sascha@894: throw new TechnicalException("Cannot access this field position. Size is: " + mObjects.length); sascha@894: } sascha@894: } sascha@894: sascha@894: /** sascha@894: * This is a covenient method for getting strongly typed objects out of the row. sascha@894: * It has to be ensured, that the type of the requested row position has been resolved out of the ColumnDefinition ({@link ResultSet#getColumnDefinitions()}). sascha@894: * In fact, this method executes a simple cast to the desired type. sascha@894: * sascha@894: * @param pPos the position of the object to be resolved. sascha@894: * @return a strongly typed Date sascha@894: * @throws TechnicalException sascha@894: * @see #getValue(int) sascha@894: */ sascha@894: public Date getDateValue(int pPos) throws TechnicalException { sascha@894: Date date = null; sascha@894: try { sascha@894: Calendar lCalendar = (Calendar) getValue(pPos); sascha@894: date = lCalendar.getTime(); ingo@897: } sascha@894: catch (ClassCastException e) { sascha@894: try{ sascha@894: //SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss"); sascha@894: date = DateUtils.getDateFromString ((String)getValue(pPos)); //(Date)formatter.parse((String)getValue(pPos)); ingo@897: ingo@897: sascha@894: } sascha@894: catch (Exception ex){ sascha@894: sLogger.error(getValue(pPos) + " " + ex.getMessage(), ex); sascha@894: throw new TechnicalException("Could not cast this value to the Date Type. Object is of value type: " + getValue(pPos).getClass().getName()); sascha@894: } ingo@897: } ingo@897: sascha@894: return date; sascha@894: } sascha@894: sascha@894: /** sascha@894: * This is a covenient method for getting strongly typed objects out of the row. sascha@894: * It has to be ensured, that the type of the requested row position has been resolved out of the ColumnDefinition ({@link ResultSet#getColumnDefinitions()}). sascha@894: * In fact, this method executes a simple cast to the desired type. sascha@894: * sascha@894: * @param pPos the position of the object to be resolved. sascha@894: * @return a strongly typed String sascha@894: * @throws TechnicalException sascha@894: * @see #getValue(int) sascha@894: */ sascha@894: public String getStringValue(int pPos) throws TechnicalException { sascha@894: try { sascha@894: Object o = this.getValue(pPos); sascha@894: String returnValue = null; sascha@894: if (o instanceof SeShape){ sascha@894: returnValue = this.getPosValue(pPos); sascha@894: }else{ sascha@894: returnValue = (String)o; sascha@894: } sascha@894: return returnValue; sascha@894: } catch (ClassCastException e) { sascha@894: throw new TechnicalException("Could not cast this value to the String Type. Object is of value type: " + getValue(pPos).getClass().getName()); sascha@894: } sascha@894: } sascha@894: sascha@894: /** sascha@894: * This is a covenient method for getting strongly typed objects out of the row. sascha@894: * It has to be ensured, that the type of the requested row position has been resolved out of the ColumnDefinition ({@link ResultSet#getColumnDefinitions()}). sascha@894: * In fact, this method executes a simple cast to the desired type. sascha@894: * sascha@894: * @param pPos the position of the object to be resolved. sascha@894: * @throws TechnicalException sascha@894: * @see #getValue(int) sascha@894: * * @return a strongly typed int sascha@894: */ sascha@894: public int getIntValue(int pPos) throws TechnicalException { sascha@894: try { sascha@894: return (Integer) getValue(pPos); sascha@894: } catch (ClassCastException e) { sascha@894: throw new TechnicalException("Could not cast this value to the Integer Type. Object is of value type: " + getValue(pPos).getClass().getName()); sascha@894: } sascha@894: } sascha@894: sascha@894: /** sascha@894: * This is a covenient method for getting strongly typed objects out of the row. sascha@894: * It has to be ensured, that the type of the requested row position has been resolved out of the ColumnDefinition ({@link ResultSet#getColumnDefinitions()}). sascha@894: * In fact, this method executes a simple cast to the desired type. sascha@894: * sascha@894: * @param pPos the position of the object to be resolved. sascha@894: * @throws TechnicalException sascha@894: * @see #getValue(int) sascha@894: * * @return a strongly typed Double sascha@894: */ sascha@894: public Double getDoubleValue(int pPos) throws TechnicalException { sascha@894: try { sascha@894: return (Double) getValue(pPos); sascha@894: } catch (ClassCastException e) { sascha@894: try{ sascha@894: return new Double ((String)getValue(pPos)); sascha@894: } sascha@894: catch(Exception ex){ sascha@894: sLogger.error(getValue(pPos) + " " + ex.getMessage(), ex); sascha@894: throw new TechnicalException("Could not cast this value to the Double Type. Object is of value type: " + getValue(pPos).getClass().getName()); sascha@894: } sascha@894: } sascha@894: } sascha@894: sascha@894: /** sascha@894: * This is a covenient method for getting strongly typed objects out of the row. sascha@894: * It has to be ensured, that the type of the requested row position has been resolved out of the ColumnDefinition ({@link ResultSet#getColumnDefinitions()}). sascha@894: * In fact, this method executes a simple cast to the desired type. sascha@894: * sascha@894: * @param pPos the position of the object to be resolved. sascha@894: * @return a strongly typed Float sascha@894: * @throws TechnicalException sascha@894: * @see #getValue(int) sascha@894: */ sascha@894: public Float getFloatValue(int pPos) throws TechnicalException { sascha@894: try { sascha@894: return (Float) getValue(pPos); sascha@894: } catch (ClassCastException e) { sascha@894: throw new TechnicalException("Could not cast this value to the Float Type. Object is of value type: " + getValue(pPos).getClass().getName()); sascha@894: } sascha@894: } sascha@894: /** sascha@894: * This is a covenient method for getting strongly typed objects out of the row. sascha@894: * It has to be ensured, that the type of the requested row position has been resolved out of the ColumnDefinition ({@link ResultSet#getColumnDefinitions()}). sascha@894: * In fact, this method executes a simple cast to the desired type. sascha@894: * sascha@894: * @param pPos the position of the object to be resolved. sascha@894: * @return a strongly typed Float sascha@894: * @throws TechnicalException sascha@894: * @see #getValue(int) ingo@897: */ sascha@894: public String getPosValue(int pPos)throws TechnicalException{ sascha@894: StringBuffer returnValue = new StringBuffer(); sascha@894: synchronized (returnValue) { sascha@894: try { sascha@894: SeShape val = (SeShape) this.mObjects[pPos]; sascha@894: if (val.isPoint()){ ingo@897: // Cannot use val.asText() because the sascha@894: // generated WKT is invalid. sascha@894: ArrayList aList = val.getAllPoints(0,false); sascha@894: SDEPoint[] mPoint = (SDEPoint[])aList.get(0); sascha@894: returnValue.append("POINT(") sascha@894: .append(mPoint[0].getX()) sascha@894: .append(" ") sascha@894: .append(mPoint[0].getY()); sascha@894: if (mPoint[0].is3D()){ sascha@894: returnValue.append(" ").append(mPoint[0].getZ()); sascha@894: } sascha@894: returnValue.append(")"); sascha@894: }else if (val.isLine() || val.isSimpleLine()){ ingo@897: // Cannot use val.asText() because the sascha@894: // generated WKT is invalid. sascha@894: ArrayList aList = val.getAllPoints(0,false); sascha@894: SDEPoint[] mPoint = (SDEPoint[])aList.get(0); sascha@894: boolean isMultiLineString = val.isMultiPart(); sascha@894: int length = mPoint.length; sascha@894: int nextOffset = length; sascha@894: int[] offsets = (int[])aList.get(1); sascha@894: int offsetPos = 1; ingo@897: sascha@894: if(isMultiLineString){ sascha@894: returnValue.append("MULTILINESTRING(("); sascha@894: nextOffset = offsets.length-1 >= offsetPos ? offsets[offsetPos++] : length; sascha@894: }else{ sascha@894: returnValue.append("LINESTRING("); sascha@894: } sascha@894: sascha@894: for (int i = 0; i< length;i++){ ingo@897: sascha@894: if (i == nextOffset){ sascha@894: returnValue.append("),("); sascha@894: nextOffset = offsets.length-1 >= offsetPos ? offsets[offsetPos++] : length; sascha@894: } ingo@897: sascha@894: returnValue.append(mPoint[i].getX()) sascha@894: .append(" ") sascha@894: .append(mPoint[i].getY()); sascha@894: if (mPoint[i].is3D()){ sascha@894: returnValue.append(" ").append(mPoint[i].getZ()); sascha@894: } sascha@894: if (i < length-1 && i < nextOffset -1){ sascha@894: returnValue.append(" , "); sascha@894: } sascha@894: } sascha@894: sascha@894: if(isMultiLineString){ sascha@894: returnValue.append("))"); sascha@894: }else{ sascha@894: returnValue.append(")"); sascha@894: } ingo@897: sascha@894: } else{ sascha@894: returnValue.append(val.asText(val.getTextSize())); sascha@894: } sascha@894: } catch (SeException e) { sascha@894: throw new TechnicalException("Could not cast this value to the " + sascha@894: "Float Type. Object is of value " + ingo@897: "type: " + sascha@894: getValue(pPos).getClass().getName()); sascha@894: } sascha@894: } sascha@894: return returnValue.toString(); sascha@894: } sascha@894: }