001: /*
002: * This file is part of the GeOxygene project source files.
003: *
004: * GeOxygene aims at providing an open framework which implements OGC/ISO specifications for
005: * the development and deployment of geographic (GIS) applications. It is a open source
006: * contribution of the COGIT laboratory at the Institut Géographique National (the French
007: * National Mapping Agency).
008: *
009: * See: http://oxygene-project.sourceforge.net
010: *
011: * Copyright (C) 2005 Institut Géographique National
012: *
013: * This library is free software; you can redistribute it and/or modify it under the terms
014: * of the GNU Lesser General Public License as published by the Free Software Foundation;
015: * either version 2.1 of the License, or any later version.
016: *
017: * This library is distributed in the hope that it will be useful, but WITHOUT ANY
018: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
019: * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
020: *
021: * You should have received a copy of the GNU Lesser General Public License along with
022: * this library (see file LICENSE if present); if not, write to the Free Software
023: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024: *
025: */
026:
027: package fr.ign.cogit.geoxygene.datatools.oracle;
028:
029: import java.math.BigDecimal;
030: import java.sql.Connection;
031:
032: import oracle.jdbc.driver.OracleConnection;
033: import oracle.sql.ARRAY;
034: import oracle.sql.ArrayDescriptor;
035:
036: import org.apache.ojb.broker.accesslayer.conversions.FieldConversion;
037: import org.apache.ojb.broker.util.batch.BatchConnection;
038:
039: /**
040: * Routine de conversion des tableaux java en VARRAY Oracle.
041: * On impose le nom du tableau dans Oracle.
042: * double[] -> "VARRAY_OF_DOUBLE".
043: * int[] -> "VARRAY_OF_INTEGER".
044: * boolean[] -> "VARRAY_OF_BOOLEAN" (défini comme un varray de CHAR(1))
045: * string[] -> "VARRAY_OF_STRING"
046: *
047: * @author Thierry Badard & Arnaud Braun
048: * @version 1.1
049: *
050: */
051:
052: /* Remarque : on pourrait coder comme GeomGeOxygene2Oracle avec la connection definie statique
053: * et initialisee a la construction de GeodatabaseOjbOracle.
054: */
055:
056: public class ArrayGeOxygene2Oracle implements FieldConversion {
057:
058: // nom des types Oracle
059: public static String typeArrayDouble = "VARRAY_OF_DOUBLE";
060: public static String typeArrayInt = "VARRAY_OF_INTEGER";
061: public static String typeArrayBoolean = "VARRAY_OF_BOOLEAN";
062: public static String typeArrayString = "VARRAY_OF_STRING";
063:
064: public Object sqlToJava(Object object) {
065: try {
066: Object array = ((ARRAY) object).getArray();
067: String type = ((ARRAY) object).getDescriptor().descType();
068: if (type.startsWith(typeArrayDouble)) {
069: BigDecimal[] bigdecimals = (BigDecimal[]) array;
070: int n = bigdecimals.length;
071: double[] doubles = new double[n];
072: for (int i = 0; i < n; i++)
073: doubles[i] = bigdecimals[i].doubleValue();
074: return doubles;
075: } else if (type.startsWith(typeArrayInt)) {
076: BigDecimal[] bigdecimals = (BigDecimal[]) array;
077: int n = bigdecimals.length;
078: int[] ints = new int[n];
079: for (int i = 0; i < n; i++)
080: ints[i] = bigdecimals[i].intValue();
081: return ints;
082: } else if (type.startsWith(typeArrayBoolean)) {
083: String[] strings = (String[]) array;
084: int n = strings.length;
085: boolean[] bools = new boolean[n];
086: for (int i = 0; i < n; i++) {
087: String value = strings[i];
088: boolean bool = false;
089: if (value.compareToIgnoreCase("0") == 0)
090: bool = false;
091: else if (value.compareToIgnoreCase("1") == 0)
092: bool = true;
093: else
094: System.out
095: .println("## PROBLEME VARRAY_OF_BOOLEAN -> ARRAY ##");
096: bools[i] = bool;
097: }
098: return bools;
099: } else if (type.startsWith(typeArrayString)) {
100: String[] strings = (String[]) array;
101: return strings;
102: } else {
103: System.out.println("## PROBLEME VARRAY -> ARRAY ##");
104: return null;
105: }
106: } catch (Exception e1) {
107: e1.printStackTrace();
108: return null;
109: }
110: }
111:
112: public Object javaToSql(Object object, Connection conn) {
113: String param = null;
114: Object objectToConvert = null;
115: ;
116: if (object instanceof double[]) {
117: param = typeArrayDouble;
118: objectToConvert = object;
119: } else if (object instanceof int[]) {
120: param = typeArrayInt;
121: objectToConvert = object;
122: } else if (object instanceof boolean[]) {
123: param = typeArrayBoolean;
124: boolean[] bools = (boolean[]) object;
125: String[] chars = new String[bools.length];
126: for (int i = 0; i < bools.length; i++)
127: if (bools[i] == true)
128: chars[i] = "1";
129: else
130: chars[i] = "0";
131: objectToConvert = chars;
132: } else if (object instanceof String[]) {
133: param = typeArrayString;
134: objectToConvert = object;
135: } else
136: System.out
137: .println("## ARRAY EN VARRAY : CAS NON TRAITE ##");
138: try {
139: ArrayDescriptor arraydesc;
140: ARRAY array;
141: if (conn instanceof BatchConnection) {
142: OracleConnection oConn = (OracleConnection) ((BatchConnection) conn)
143: .getDelegate();
144: arraydesc = ArrayDescriptor.createDescriptor(param,
145: oConn);
146: array = new ARRAY(arraydesc, oConn, objectToConvert);
147: } else {
148: arraydesc = ArrayDescriptor.createDescriptor(param,
149: conn);
150: array = new ARRAY(arraydesc, conn, objectToConvert);
151: }
152: return array;
153: } catch (Exception e) {
154: e.printStackTrace();
155: return null;
156: }
157: }
158:
159: public Object javaToSql(Object arg0) {
160: System.out
161: .println("[#### [ERREUR GeOxygene] L'IMPOSSIBLE EST ARRIVE !! ##### ");
162: System.out
163: .println("Probleme de conversion GeOxygene -> Oracle");
164: return null;
165: }
166:
167: }
|