KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > webdocwf > util > loader > transformation > Transformation


1 /**
2   Transformation - Transformations in Octopus Loader.
3     Copyright (C) 2002-2004 Together
4     This library is free software; you can redistribute it and/or
5     modify it under the terms of the GNU Lesser General Public
6     License as published by the Free Software Foundation; either
7     version 2.1 of the License, or (at your option) any later version.
8     This library is distributed in the hope that it will be useful,
9     but WITHOUT ANY WARRANTY; without even the implied warranty of
10     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11     Lesser General Public License for more details.
12     You should have received a copy of the GNU Lesser General Public
13     License along with this library; if not, write to the Free Software
14     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  Transformation.java
16  Date: 05.04.2004.
17  @version 1.0
18  @author: Zoran Milakovic zoran@prozone.co.yu
19  @author: Milosevic Sinisa sinisa@prozone.co.yu
20  */

21
22 package org.webdocwf.util.loader.transformation;
23
24 import java.lang.reflect.Constructor JavaDoc;
25 import java.sql.Connection JavaDoc;
26 import java.sql.ResultSet JavaDoc;
27 import java.sql.SQLException JavaDoc;
28 import java.sql.Statement JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Vector JavaDoc;
32
33 import org.w3c.dom.Element JavaDoc;
34 import org.w3c.dom.Node JavaDoc;
35 import org.w3c.dom.NodeList JavaDoc;
36 import org.webdocwf.util.loader.ConfigReader;
37 import org.webdocwf.util.loader.LoaderException;
38 import org.webdocwf.util.loader.OctopusXMLUtil;
39 import org.webdocwf.util.loader.logging.Logger;
40
41 /**
42  *
43  * Transformation - transformations in Octopus Loader.
44  */

45 public class Transformation {
46
47   
48   
49   private String JavaDoc transformationName;
50   private String JavaDoc transformationClassName;
51   private String JavaDoc transformatorConfigName;
52   
53   private Vector JavaDoc sourceColumnNames = new Vector JavaDoc();
54
55   private List JavaDoc transformationTargetColumns = new ArrayList JavaDoc();
56   
57   private Transformer transformer;
58   private Logger logger;
59   private JavaScriptEvaluator jsEvaluator;
60   private Element JavaDoc transformationDocFragment;
61
62
63   public Transformation(String JavaDoc name, String JavaDoc className, String JavaDoc configString, Element JavaDoc doc) throws Exception JavaDoc {
64
65       this.transformationName = name;
66       this.transformationClassName = className;
67       this.transformatorConfigName = configString;
68       this.transformationDocFragment = doc;
69             String JavaDoc javaScriptExpression = "";
70             
71         NodeList JavaDoc childNodes = this.transformationDocFragment.getElementsByTagName("javaScript");
72         if ( childNodes.item(0) != null ){
73                     javaScriptExpression = childNodes.item(0).getFirstChild().getNodeValue();
74         }
75       try {
76                 init();
77               if (javaScriptExpression != ""){
78                     //instantiate JavaScriptEvaluator for transformation with javaScript
79
jsEvaluator = new JavaScriptEvaluator();
80                     jsEvaluator.setExpression(javaScriptExpression);
81                     jsEvaluator.setVariableNames(this.sourceColumnNames);
82                     this.transformer = (Transformer) jsEvaluator;
83         }else{
84                  //instantiate transformer
85
Class JavaDoc transformatorClass = Class.forName(this.transformationClassName);
86                Class JavaDoc[] ArgClassArray = new Class JavaDoc[] { };
87                Object JavaDoc[] ArgObject = new Object JavaDoc[] { };
88                Constructor JavaDoc transConstructor = transformatorClass.getDeclaredConstructor(ArgClassArray);
89                this.transformer = (Transformer)(transConstructor.newInstance(ArgObject));
90                 }
91              if(transformatorConfigName != null && this.transformer != null) {
92                 this.transformer.configure(transformatorConfigName);
93              }
94                 
95       }catch(Exception JavaDoc e) {
96         throw new LoaderException("Error during transformation!",e);
97       }
98   }
99
100   public Transformation(String JavaDoc name, String JavaDoc className, Element JavaDoc doc) throws Exception JavaDoc {
101       new Transformation(name,className,null,doc);
102   }
103
104   /**
105    * This method set logger object
106    */

107   public void setLogger(Logger logger) {
108         if (this.transformer instanceof JavaScriptEvaluator){
109             jsEvaluator.setLogger(logger);
110         }else{
111         this.logger = logger;
112         }
113
114   }
115   
116     private void init() {
117         initSourceColumns();
118         initTargetColumns();
119     }
120
121
122
123   /**
124    * @param doc represents Object document
125    * @param importJob represents current import job
126    */

127   private void initSourceColumns() {
128 //ZK change this because of problem with reading xml file, because of method importValue which reads all data from <importDefinition>tag
129
if (this.transformationDocFragment!=null){
130         this.sourceColumnNames = OctopusXMLUtil.importValueForTransform(this.transformationDocFragment, "sourceColumn","name");
131     }
132   }
133
134   private void initTargetColumns() {
135     Vector JavaDoc targetColumnNames = OctopusXMLUtil.importValueForTransform(this.transformationDocFragment, "targetColumn","name");
136     Vector JavaDoc targetTableNames = OctopusXMLUtil.importValueForTransform(this.transformationDocFragment, "targetColumn","tableName");
137     Vector JavaDoc targetTableIDs = OctopusXMLUtil.importValueForTransform(this.transformationDocFragment, "targetColumn","tableID");
138     Vector JavaDoc targetValueModes = OctopusXMLUtil.importValueForTransform(this.transformationDocFragment, "targetColumn","valueMode");
139     for(int i = 0; i < targetColumnNames.size(); i++) {
140         this.transformationTargetColumns.add(new TransformationTargetColumn(
141         (String JavaDoc)targetColumnNames.get(i),
142         (String JavaDoc)targetTableNames.get(i),
143         (String JavaDoc)targetTableIDs.get(i),
144         (String JavaDoc)targetValueModes.get(i)
145         ));
146     }
147     
148   }
149
150
151   /**
152    * Returns Vector with source column names
153    * @return vector
154    */

155   public Vector JavaDoc getSourceColumnNames() {
156     return this.sourceColumnNames;
157   }
158
159   /**
160    * Returns Vector with target column names
161    * @return vector
162    */

163   public Vector JavaDoc getTargetColumnNames() {
164     Vector JavaDoc retVal = new Vector JavaDoc();
165     for(int i = 0; i < this.transformationTargetColumns.size(); i++) {
166         retVal.add( ((TransformationTargetColumn)this.transformationTargetColumns.get(i)).getName() );
167     }
168     return retVal;
169   }
170   
171   /**
172      * Returns Vector with target column names
173      * @param index logical table index
174      * @return vector
175      */

176     public Vector JavaDoc getTargetColumnNames(int index) {
177       Vector JavaDoc retVal = new Vector JavaDoc();
178       for(int i = 0; i < this.transformationTargetColumns.size(); i++) {
179           if( ((TransformationTargetColumn)this.transformationTargetColumns.get(i)).getTableID().equals( String.valueOf(index) ) )
180             retVal.add( ((TransformationTargetColumn)this.transformationTargetColumns.get(i)).getName() );
181       }
182       
183       return retVal;
184     }
185     
186     /**
187     * Returns Vector with target column types
188     * @param index logical table index
189     * @return vector
190     */

191    public Vector JavaDoc getTargetColumnTypes(int index) {
192      Vector JavaDoc retVal = new Vector JavaDoc();
193      for(int i = 0; i < this.transformationTargetColumns.size(); i++) {
194          if( ((TransformationTargetColumn)this.transformationTargetColumns.get(i)).getTableID().equals( String.valueOf(index) ) )
195            retVal.add( ((TransformationTargetColumn)this.transformationTargetColumns.get(i)).getType() );
196 // else
197
// retVal.add(null);
198
}
199      return retVal;
200    }
201    
202    /**
203    * Returns Vector with target column types on ordered places, and
204    * add null if in this place logic table is different than specified
205    * @param index logical table index
206    * @return vector
207    */

208   public Vector JavaDoc getOrderedTargetColumnTypes(int index) {
209     Vector JavaDoc retVal = new Vector JavaDoc();
210     for(int i = 0; i < this.transformationTargetColumns.size(); i++) {
211         if( ((TransformationTargetColumn)this.transformationTargetColumns.get(i)).getTableID().equals( String.valueOf(index) ) )
212           retVal.add( ((TransformationTargetColumn)this.transformationTargetColumns.get(i)).getType() );
213         else
214           retVal.add(null);
215     }
216     return retVal;
217   }
218    
219    /**
220    * Returns Vector with target value modes
221    * @param index logical table index
222    * @return vector
223    */

224   public Vector JavaDoc getTargetValueModes(int index) {
225     Vector JavaDoc retVal = new Vector JavaDoc();
226     for(int i = 0; i < this.transformationTargetColumns.size(); i++) {
227         if( ((TransformationTargetColumn)this.transformationTargetColumns.get(i)).getTableID().equals( String.valueOf(index) ) )
228           retVal.add( ((TransformationTargetColumn)this.transformationTargetColumns.get(i)).getValueMode() );
229     }
230     return retVal;
231   }
232    
233    /**
234       * Returns Vector with target key column names
235       * @param index logical table index
236       * @return vector
237       */

238      public Vector JavaDoc getTargetKeyColumnNames(int index) {
239        Vector JavaDoc retVal = new Vector JavaDoc();
240        for(int i = 0; i < this.transformationTargetColumns.size(); i++) {
241             if( ((TransformationTargetColumn)this.transformationTargetColumns.get(i)).getTableID().equals( String.valueOf(index) ) ) {
242                if( ((TransformationTargetColumn)this.transformationTargetColumns.get(i)).getValueMode().equals("Key") )
243                    retVal.add( ((TransformationTargetColumn)this.transformationTargetColumns.get(i)).getName() );
244             }
245        }
246        return retVal;
247      }
248   
249   /**
250      * Returns Vector with target column names for specified table
251      * @param tableName name of table
252      * @return vector
253      */

254     public Vector JavaDoc getTargetColumnNames(String JavaDoc tableName) {
255       Vector JavaDoc retVal = new Vector JavaDoc();
256       for(int i = 0; i < this.transformationTargetColumns.size(); i++) {
257             if( ((TransformationTargetColumn)this.transformationTargetColumns.get(i)).getTableName().equals(tableName) )
258                 retVal.add( ((TransformationTargetColumn)this.transformationTargetColumns.get(i)).getName() );
259       }
260       return retVal;
261     }
262
263
264   /**
265    * Returns Vector with target key column names
266    * @return vector
267    */

268   public Vector JavaDoc getTargetKeyColumnNames() {
269     Vector JavaDoc retVal = new Vector JavaDoc();
270     for(int i = 0; i < this.transformationTargetColumns.size(); i++) {
271         if( ((TransformationTargetColumn)this.transformationTargetColumns.get(i)).getValueMode().equals("Key") )
272             retVal.add( ((TransformationTargetColumn)this.transformationTargetColumns.get(i)).getName() );
273     }
274     return retVal;
275   }
276
277   /**
278    * Returns Vector with target value mode
279    * @return vector
280    */

281   public Vector JavaDoc getTargetValueMode() {
282     Vector JavaDoc retVal = new Vector JavaDoc();
283     for(int i = 0; i < this.transformationTargetColumns.size(); i++) {
284         retVal.add( ((TransformationTargetColumn)this.transformationTargetColumns.get(i)).getValueMode() );
285     }
286     return retVal;
287   }
288
289
290   /**
291    * Returns Vector with target column types
292    * @return vector
293    */

294 // public Vector getTargetColumnTypes() {
295
// Vector retVal = new Vector();
296
// for(int i = 0; i < this.transformationTargetColumns.size(); i++) {
297
// retVal.add( ((TransformationTargetColumn)this.transformationTargetColumns.get(i)).getValueMode() );
298
// }
299
// return retVal;
300
// }
301

302  
303   /**
304    * This method read value of sub counter parameter
305    * @param sourceValues values to transform
306    * @return vector transformed values
307    * @throws SQLException
308    */

309   public Vector JavaDoc transformValues(Vector JavaDoc sourceValues) throws Exception JavaDoc{
310     Vector JavaDoc results = new Vector JavaDoc();
311     try {
312         results.addAll(this.transformer.transformValue(sourceValues));
313     } catch (Exception JavaDoc except) {
314       throw except;
315     }
316     return results;
317
318 }
319   /**
320    * This method returns transformation name
321    * @return String which represents transformation name
322    */

323   public String JavaDoc getName() {
324     return this.transformationName;
325   }
326
327
328   /**
329    * Method transformationColumnTypes is used to put types of transformation columns into
330    * global vector sorted in target tables. If there is an error, Exception
331    * "SQLException" or "NullPointerException" is thrown.
332    * @param c Connection to target database.
333    * @param firstColumn is first column
334    * @param columnsSuportedTarget is true if driver for target database supports getColumns method
335    * @param configReaderTarget is ConfigReader object for target database
336    * @throws SQLException Constructs an SQLException object with a reason.
337    * @throws NullPointerException Constructs a NullPointerException with the specified detail message.
338    */

339   public void transformationColumnTypes(
340         Connection JavaDoc c,
341       int firstColumn,
342       boolean columnsSuportedTarget, ConfigReader configReaderTarget) throws SQLException JavaDoc, NullPointerException JavaDoc {
343         int iCnt = 0;
344     try {
345     List JavaDoc tableNames = this.getTargetTableNames();
346     for(int k = 0; k < tableNames.size(); k++) {
347           Vector JavaDoc columnNames = this.getTargetColumnNames(tableNames.get(k).toString());
348           Statement JavaDoc stmtTrans = c.createStatement();
349           //Vector typs = new Vector();
350
Vector JavaDoc subTyps = new Vector JavaDoc();
351           String JavaDoc strQuery = "select ";
352                 ResultSet JavaDoc rsetTrans=null;
353           if (columnNames.size() != 0) {
354             for (int i = 0; i < columnNames.size(); i++) {
355                 strQuery += columnNames.get(i).toString() +
356                     ", ";
357               }
358               strQuery = strQuery.substring(0, strQuery.length() - 2);
359               strQuery += " from " + tableNames.get(k).toString();
360            if (columnsSuportedTarget){
361                         rsetTrans = c.getMetaData().getColumns( c.getCatalog(), null, tableNames.get(k).toString(), "%" );
362                         String JavaDoc columnName = "";
363                         String JavaDoc columnType = "";
364                         while(rsetTrans.next()){
365                             columnName = rsetTrans.getString(3+firstColumn);
366                             columnType = rsetTrans.getString(5+firstColumn);
367                             for (int j = 0; j < columnNames.size(); j++) {
368                                     if( columnNames.get(j).toString().equalsIgnoreCase( columnName ) ){
369                                     this.setType(tableNames.get(k).toString(),columnNames.get(j).toString(),columnType);
370                                     //typs.add(columnType);
371
}
372                             }
373                         }
374            }else{//TODO ZK ADDED stmtConstant.setMaxRows(1). Place this as parameter in conf file, like maxRowsSuported
375
if (configReaderTarget.getMaxRowsSupported()){
376                         stmtTrans.setMaxRows(1);
377                 }
378                         rsetTrans = stmtTrans.executeQuery(strQuery);
379                         for (int j = 0; j < columnNames.size(); j++) {
380                                 this.setType( tableNames.get(k).toString(),
381                                 columnNames.get(j).toString(),
382                                 rsetTrans.getMetaData().getColumnTypeName(j + firstColumn) );
383                             //typs.add(rsetConstant.getMetaData().getColumnTypeName(j + firstColumn));
384
}
385               }
386                 rsetTrans.close();
387         }
388                 stmtTrans.close();
389     }
390 // this.hmTargetColumnTypes.addAll(typs);
391
}
392     catch (SQLException JavaDoc ex) {
393       throw ex;
394     }
395     catch (NullPointerException JavaDoc ex) {
396       throw ex;
397     }
398   }
399   
400   public List JavaDoc getTargetTableNames() {
401     List JavaDoc retVal = new ArrayList JavaDoc();
402     Element JavaDoc transformation = this.transformationDocFragment;
403    NodeList JavaDoc nodeList = transformation.getChildNodes();
404    String JavaDoc tableName = "";
405    for(int i = 0; i < nodeList.getLength(); i++) {
406         if( nodeList.item(i).getNodeType() != Node.ELEMENT_NODE )
407             continue;
408            if( !nodeList.item(i).getNodeName().equals("targetColumns"))
409                 continue;
410         Element JavaDoc targetColumns = (Element JavaDoc)nodeList.item(i);
411         NodeList JavaDoc targetColumnElements = targetColumns.getElementsByTagName("targetColumn");
412         for(int j = 0; j < targetColumnElements.getLength();j++) {
413            Element JavaDoc targetColumn = (Element JavaDoc)targetColumnElements.item(j);
414            tableName = targetColumn.getAttribute("tableName");
415            if( !retVal.contains(tableName) )
416             retVal.add(tableName);
417         }
418    }
419    return retVal;
420   }
421   
422   public String JavaDoc getTargetTableID() {
423     Element JavaDoc transformation = this.transformationDocFragment;
424    NodeList JavaDoc nodeList = transformation.getChildNodes();
425    for(int i = 0; i < nodeList.getLength(); i++) {
426     if( nodeList.item(i).getNodeType() != Node.ELEMENT_NODE )
427         continue;
428        if( !nodeList.item(i).getNodeName().equals("targetColumns"))
429             continue;
430     Element JavaDoc targetColumns = (Element JavaDoc)nodeList.item(i);
431        Element JavaDoc targetColumn = (Element JavaDoc)targetColumns.getElementsByTagName("targetColumn").item(0);
432        return targetColumn.getAttribute("tableID");
433    }
434    return null;
435     }
436   
437    private void setType(String JavaDoc tableName, String JavaDoc columnName, String JavaDoc type) {
438         for(int i = 0; i < this.transformationTargetColumns.size(); i++) {
439             if( ((TransformationTargetColumn)this.transformationTargetColumns.get(i)).getTableName().equals(tableName) &&
440                  ((TransformationTargetColumn)this.transformationTargetColumns.get(i)).getName().equals(columnName) )
441             ((TransformationTargetColumn)this.transformationTargetColumns.get(i)).setType(type);
442        }
443    }
444   
445   /**
446    * This method reset all variables
447    */

448   public void reset() {
449       this.sourceColumnNames = new Vector JavaDoc();
450       this.transformationTargetColumns = new ArrayList JavaDoc();
451   }
452   
453   public void release() throws Exception JavaDoc{
454     this.transformer.release();
455   }
456 }
457
Popular Tags