MetaDataTask.java :  » Database-ORM » transformica-2.3.0 » biz » hammurapi » transformica » jdbc » Java Open Source

Java Open Source » Database ORM » transformica 2.3.0 
transformica 2.3.0 » biz » hammurapi » transformica » jdbc » MetaDataTask.java
/*
 * transformica 2
 * Code generator 
 * Copyright (C) 2004  Hammurapi Group
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * URL: http://www.pavelvlasov.com/pv/content/menu.show@id=products.transformica.html
 * e-Mail: support@hammurapi.biz
 */
package biz.hammurapi.transformica.jdbc;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;

import org.apache.tools.ant.BuildException;

import biz.hammurapi.sql.ConnectionPerThreadDataSource;
import biz.hammurapi.sql.SQLProcessor;
import biz.hammurapi.sql.metadata.DefaultGenerationPolicy;
import biz.hammurapi.sql.metadata.Metadata;
import biz.hammurapi.transformica.TransformSession;
import biz.hammurapi.transformica.TransformTask;
import biz.hammurapi.util.Visitable;


/**
 * Traverses database metadata. Transformations are defined by nested channel elements.
 * Velocity template engine is used for code generation. Current metadata object is accessible through 'element' context object.
 * 'session' context object of type biz.hammurapi.transformica.TransformSession gives access to current Ant project and provides
 * methods to include other templates or channels and to store information.   
 * <section name="Example" suppress-description="yes">
 <pre>
 &lt;taskdef name="dbtransform" classname="biz.hammurapi.transformica.jdbc.MetaDataTask"&gt;<br/>
 <tab/>&lt;classpath&gt;<br/>
 <tab/><tab/>&lt;fileset dir="lib" includes="*.jar"/&gt;<br/>
 <tab/>&lt;/classpath&gt;<br/>
 &lt;/taskdef&gt;<br/>
 <br/>
 &lt;dbtransform<br/>
 <tab/>driver="org.hsqldb.jdbcDriver"<br/>
 <tab/>url="jdbc:hsqldb:hsql://localhost"<br/>
 <tab/>user="sa"<br/>
 <tab/>password=""<br/>
 &gt;<br/>
 <tab/>&lt;channel <br/>
 <tab/><tab/>outputDir="generated"<br/> 
 <tab/><tab/>template="templates/jdbc.java"<br/>
 <tab/><tab/>fileNameTemplate='$${element.name}.java'  <br/>
 <tab/>&gt;<br/>
 <tab/><tab/>&lt;filetouchdetector fileInfoFile="fileInfo.txt" genRoot="generated"/&gt;<br/>
 <tab/>&lt;/channel&gt;<br/>
 &lt;/jdbctransform&gt;<br/>
 </pre></section>
 * @ant.element name="jdbcmetadatatransform"
 * @author Pavel Vlasov
 */
public class MetaDataTask extends TransformTask {
  private String driver;
  private String url;
  private String user;
  private String password;
  
    private File metadataFile;
    
    /**
     * Metadata file
     * @ant.non-required
     * @param metadata
     */
    public void setMetadata(File metadataFile) {
      this.metadataFile=metadataFile;
    }
  
    /**
     * Set it to true to avoid loading index metadata 
     * (e.g. if you don't have sufficient privileges)
     * @ant.non-required
     * @param skipIndices
     */
  /**
   * Connection string
   * @ant.required
   * @param url The connectionString to set.
   */
  public void setURL(String url) {
    this.url = url;
  }

  /**
   * JDBC Driver class name
   * @ant.required
   * @param driver The driver to set.
   */
  public void setDriver(String driver) {
    this.driver = driver;
  }

  /**
   * Password
   * @ant.non-required
   * @param password The password to set.
   */
  public void setPassword(String password) {
    this.password = password;
  }

  /**
   * User name
   * @ant.non-required
   * @param userName The userName to set.
   */
  public void setUser(String userName) {
    this.user = userName;
  }
  
  protected Visitable getModel(TransformSession session) {    
    try {
      if (metadataFile==null) {
        if (driver==null) {
          throw new BuildException("Driver not set");      
        }
        if (url==null) {
          throw new BuildException("Connection URL is not set");        
        }
              
        // TODO - make it better.    
        return new Metadata(
            newProcessor(), 
            new String[] {"TABLE", "VIEW"}, 
            new DefaultGenerationPolicy(), 
            null);
      } else {
        return Metadata.load(metadataFile);
      }
      
    } catch (ClassNotFoundException e) {
      throw new BuildException("Class not found: "+e, e);
    } catch (SQLException e) {
      throw new BuildException(e);
    } catch (IOException e) {
      throw new BuildException("Could not load metadata file: "+e, e);
    }
  }  
  
  /**
   * @return
   * @throws ClassNotFoundException
   */
  SQLProcessor newProcessor() throws ClassNotFoundException {
    return new SQLProcessor(new ConnectionPerThreadDataSource(driver, url, user, password, null), null);
  }

  /**
   * Defines transformation channel.
   * @ant.required
   * @return
   */
  public TableChannel createTableChannel() {
    TableChannel ret=new TableChannel(session);
    channels.add(ret);
    return ret;
  }
  
  /**
   * Defines transformation channel.
   * @ant.required
   * @return
   */
  public SchemaChannel createSchemaChannel() {
    SchemaChannel ret=new SchemaChannel(session);
    channels.add(ret);
    return ret;
  }  
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.