From: <rfl...@us...> - 2007-02-07 17:39:38
|
Revision: 244 http://svn.sourceforge.net/salto-db/?rev=244&view=rev Author: rflament Date: 2007-02-07 09:39:29 -0800 (Wed, 07 Feb 2007) Log Message: ----------- Modified Paths: -------------- salto-db-generator/trunk/src/site/apt/plugins/ejb3hibernatedao.apt salto-db-generator/trunk/src/site/apt/plugins/ejb3hibernatedaotests.apt salto-db-generator/trunk/src/site/apt/tutorials/tuto-doc.apt salto-db-generator/trunk/src/site/site.xml Added Paths: ----------- salto-db-generator/trunk/src/com/salto/db/ant/RegisterPluginTask.java salto-db-generator/trunk/src/site/apt/tutorials/customplugin.apt Added: salto-db-generator/trunk/src/com/salto/db/ant/RegisterPluginTask.java =================================================================== --- salto-db-generator/trunk/src/com/salto/db/ant/RegisterPluginTask.java (rev 0) +++ salto-db-generator/trunk/src/com/salto/db/ant/RegisterPluginTask.java 2007-02-07 17:39:29 UTC (rev 244) @@ -0,0 +1,45 @@ +package com.salto.db.ant; + +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.Task; + +import com.salto.db.generator.plugin.IGeneratorPlugin; +import com.salto.db.generator.plugin.Plugins; + +/** + * Register a plugin + * + * @author rfl...@sa... + * + */ +public class RegisterPluginTask extends Task { + + private String className; + + public String getClassName() { + return className; + } + + public void setClassName(String className) { + this.className = className; + } + + public void execute() throws BuildException { + log("registering plugin from classname '" + className + "'"); + + try { + Class c = Class.forName(className); + IGeneratorPlugin plugin = (IGeneratorPlugin) c.newInstance(); + Plugins.getInstance().addPlugin(plugin); + + } catch (ClassNotFoundException e) { + throw new BuildException("cannot find class '" + className + + "' in classpath", e); + + } catch (Exception e) { + throw new BuildException(e); + } + + } + +} Modified: salto-db-generator/trunk/src/site/apt/plugins/ejb3hibernatedao.apt =================================================================== --- salto-db-generator/trunk/src/site/apt/plugins/ejb3hibernatedao.apt 2007-02-06 16:11:01 UTC (rev 243) +++ salto-db-generator/trunk/src/site/apt/plugins/ejb3hibernatedao.apt 2007-02-07 17:39:29 UTC (rev 244) @@ -14,6 +14,8 @@ In each dao there is a getById() method, a save() method, an update() method, a delete() method and a finder for each different column. + If you want something equivalebt but without ejb3 annotation, have a look at the {{{pojohibernatedao.html}"Pojos and Hibernate DAOs" plugin}}. + * Example Suppose we have two tables in a database : Modified: salto-db-generator/trunk/src/site/apt/plugins/ejb3hibernatedaotests.apt =================================================================== --- salto-db-generator/trunk/src/site/apt/plugins/ejb3hibernatedaotests.apt 2007-02-06 16:11:01 UTC (rev 243) +++ salto-db-generator/trunk/src/site/apt/plugins/ejb3hibernatedaotests.apt 2007-02-07 17:39:29 UTC (rev 244) @@ -1,8 +1,163 @@ --- - EJB3, Hibernate DAOs and unit tests plugin + EJB3, Hibernate DAOs and Junit tests plugin --- -EJB3, Hibernate DAOs and unit tests plugin +EJB3, Hibernate DAOs and JUnit tests plugin - This plugin generated the same files as the "EJB3 And Hibernate Daos" plugin, plus JUnit testcases for every generated daos. + This plugin generates the same files as the {{{ejb3hibernatedao.html}"EJB3 And Hibernate Daos" plugin}}, plus JUnit testcases for every generated daos. + + Thanks to this plugin, you can directly test generated daos and have an example of how to use them. + + Here is a sample generated testcase : + ++----------------------------------------------------------+ +package com.salto.ejb3dao.test; + +import junit.framework.TestCase; +import java.util.List; +import java.sql.Timestamp; + +import com.salto.ejb3dao.DAOFactory; +import com.salto.ejb3dao.pojo.Person; +import com.salto.ejb3dao.PersonDAO; + +/** + * <p>Unit test for com.salto.ejb3dao.PersonDAO</p> + * <p>Generated at Wed Feb 07 16:08:05 CET 2007</p> + * + * @author Salto-db Generator Ant v1.0.15 / EJB3 + Hibernate DAO + TestCases + */ +public class PersonDAOTest extends TestCase { + + private PersonDAO dao; + + private Integer existingId; + + private Person testEntityPerson; + + private String testName; + + private String testFirstName; + + private int testAge; + + private int testIdCity; + + + public void setUp() throws Exception { + setPersonDAO(DAOFactory.DEFAULT.buildPersonDAO()); + //TODO: call setExistingId() with an id that exists in database + //TODO: call setTestPerson() with an Person instance + } + + public void tearDown() { + existingId = null; + dao = null; + testEntityPerson = null; + } + + public void setPersonDAO(PersonDAO dao) { + this.dao = dao; + } + + public PersonDAO getPersonDAO() { + return this.dao; + } + + public void setExistingId(Integer id) { + this.existingId = id; + } + + public Integer getExistingId() { + return this.existingId; + } + + public Person getTestEntityPerson() { + return testEntityPerson; + } + + public void setTestEntityPerson(Person testEntityPerson) { + this.testEntityPerson = testEntityPerson; + } + + public void testGetById() { + Person test = dao.getById(existingId); + assertNotNull(test); + } + + public void testFindAll() { + List<Person> all = dao.findAll(); + assertNotNull(all); + assertFalse(all.isEmpty()); + } + + public void testSaveAndRemove() { + dao.save(testEntityPerson); + Person test = dao.getById(testEntityPerson.getIdPerson()); + assertNotNull(testEntityPerson.getIdPerson()); + assertNotNull(test); + dao.delete(testEntityPerson); + test = dao.getById(testEntityPerson.getIdPerson()); + assertNull(test); + } + + public String getTestName() { + return testName; + } + + public void setTestName(String testName) { + this.testName = testName; + } + + public void testFindByName() { + List<Person> list = dao.findByName(testName); + assertNotNull(list); + assertFalse(list.isEmpty()); + } + + public String getTestFirstName() { + return testFirstName; + } + + public void setTestFirstName(String testFirstName) { + this.testFirstName = testFirstName; + } + + public void testFindByFirstName() { + List<Person> list = dao.findByFirstName(testFirstName); + assertNotNull(list); + assertFalse(list.isEmpty()); + } + + public int getTestAge() { + return testAge; + } + + public void setTestAge(int testAge) { + this.testAge = testAge; + } + + public void testFindByAge() { + List<Person> list = dao.findByAge(testAge); + assertNotNull(list); + assertFalse(list.isEmpty()); + } + + public int getTestIdCity() { + return testIdCity; + } + + public void setTestIdCity(int testIdCity) { + this.testIdCity = testIdCity; + } + + public void testFindByIdCity() { + List<Person> list = dao.findByIdCity(testIdCity); + assertNotNull(list); + assertFalse(list.isEmpty()); + } + + +} ++----------------------------------------------------------+ \ No newline at end of file Added: salto-db-generator/trunk/src/site/apt/tutorials/customplugin.apt =================================================================== --- salto-db-generator/trunk/src/site/apt/tutorials/customplugin.apt (rev 0) +++ salto-db-generator/trunk/src/site/apt/tutorials/customplugin.apt 2007-02-07 17:39:29 UTC (rev 244) @@ -0,0 +1,148 @@ + --- + How to write a custom plugin for Salto-db Generator + --- + R\xE9mi Flament + --- + --- + +How to write a custom plugin for Salto-db Generator + + It is possible to write your own plugins and to use them from the Ant task. It is not yet possible to use custom + plugins from the Eclipse plugin. + + To write a plugin you need to create class that implements the {{{../apidocs/com/salto/db/generator/plugin/IGeneratorPlugin.html}IGeneratorPlugin}} interface. + + I advise you to extend the abstract class {{{../apidocs/com/salto/db/generator/plugin/DefaultAbstractGeneratorPlugin.html}DefaultAbstractGeneratorPlugin}}. + + We're going to create a plugin called "myPlugin" which create a text file for each table. In each of those file we will write the name of the table, the name of the columns and their types. + + Here is the code of our custom plugin : + ++------------------------------------------------------------------------+ +package com.test; + +import java.io.FileOutputStream; +import java.io.PrintWriter; + +import salto.tool.jdo.data.JdoInfo; +import salto.tool.sql.data.TableColInfo; + +import com.salto.db.generator.plugin.DefaultAbstractGeneratorPlugin; + +public class MyPlugin extends DefaultAbstractGeneratorPlugin { + + /** + * Create a file for each table that contains column names and types + * + * @see com.salto.db.generator.plugin.IGeneratorPlugin#execute(java.lang.String, + * salto.tool.jdo.data.JdoInfo) + */ + public void execute(String fileName, JdoInfo jdoInfo) throws Exception { + + FileOutputStream fos = null; + PrintWriter printWriter = null; + + try { + fos = new FileOutputStream(fileName + ".txt"); + printWriter = new PrintWriter(fos); + printWriter.println("----------------------------------------"); + printWriter.println("Table " + jdoInfo.getTableName()); + printWriter.println("----------------------------------------"); + for (int i = 0; i < jdoInfo.getColInfos().length; i++) { + TableColInfo info = jdoInfo.getColInfos()[i]; + printWriter.println(info.getColName() + " " + + info.getColTypName()); + } + } + + catch (Exception e) { + e.printStackTrace(); + + } finally { + try { + printWriter.close(); + } catch (Exception e) { + + } + + try { + fos.close(); + } catch (Exception e) { + + } + } + } + + /** + * This prefix will be added to the name that we are passed as a parameter + * in the execute() method. + * + * @see com.salto.db.generator.plugin.IGeneratorPlugin#getDefaultPrefix() + */ + public String getDefaultPrefix() { + return "MyCustomPrefix"; + } + + /** + * Long description show in the eclipse plugin. + * + * @see com.salto.db.generator.plugin.IGeneratorPlugin#getLongDescription() + */ + public String getLongDescription() { + return "This plugin has been written to show how to develop a custom plugin and how to register it." + + " It will generate a simple text for each table containing column names and types."; + } + + /** + * Plugin identifier + * + * @see com.salto.db.generator.plugin.IGeneratorPlugin#getName() + */ + public String getName() { + return "myplugin"; + } + + /** + * short description show in the eclipse plugin. + * + * @see com.salto.db.generator.plugin.IGeneratorPlugin#getShortDescription() + */ + public String getShortDescription() { + return "tutorial custom plugin"; + } + +} ++------------------------------------------------------------------------+ + + Next we need to compile our plugin. Once this is done we can use the salto-db-register task to use it from the + salto-db-generate task : + ++------------------------------------------------------------------+ +<salto-db-register className="com.test.MyPlugin"/> + +<salto-db-generate nameOffset="0" generateView="${generateView}" plugin="myPlugin" schema="${schema}" + tableName="${tableName}" login="${login}" password="${password}" jdbcUrl="${jdbcUrl}" + driverClassName="${driverClassName}" outputDir="${outputDir}" packageName="${packageName}"/> ++------------------------------------------------------------------+ + + After running the Ant build, we end up with as many MyCustomPrefix*.txt files as there are tables + in the database. + + Each generated file looks like that : + ++------------------------------------------------------------------+ +---------------------------------------- +Table person +---------------------------------------- +id_person INTEGER +name VARCHAR +first_name VARCHAR +age INTEGER +id_city INTEGER + ++------------------------------------------------------------------+ + + For people familiar with the Velocity template engine, it might be easier to extend the {{{../apidocs/com/salto/db/generator/plugin/VelocityAbstractPlugin.html}VelocityAbstractPlugin}} when you write a plugin. + + If you write a custom plugin and if you think it could be useful to other people, don't hesitate to send it to us, we'll include it + on next releases. \ No newline at end of file Modified: salto-db-generator/trunk/src/site/apt/tutorials/tuto-doc.apt =================================================================== --- salto-db-generator/trunk/src/site/apt/tutorials/tuto-doc.apt 2007-02-06 16:11:01 UTC (rev 243) +++ salto-db-generator/trunk/src/site/apt/tutorials/tuto-doc.apt 2007-02-07 17:39:29 UTC (rev 244) @@ -4,6 +4,8 @@ Manuel Verriez --- --- + +Salto-db generator: installation & use Salto-db generator is an Eclipse plug-in that allows you to generate JAVA classes in order to interact between your application and a datasource. @@ -13,7 +15,7 @@ * hibernate: Plain Old Java Objects (POJO) generation + xml mapping files or EJB3 annotations (since Hibernate 3.2) -Installation +* Installation Installation is straight forward, once you have Eclipse. You need to add a new update site (Eclipse -> Help -> Software Updates -> Find and install -> Search for new features to install -> New remote site) @@ -23,7 +25,7 @@ [../images/tutorials/doc/eclipse.jpg] Salto-db perspective -Use +* Use Here is an example to illustrate functionalities provided by this plug-in. Modified: salto-db-generator/trunk/src/site/site.xml =================================================================== --- salto-db-generator/trunk/src/site/site.xml 2007-02-06 16:11:01 UTC (rev 243) +++ salto-db-generator/trunk/src/site/site.xml 2007-02-07 17:39:29 UTC (rev 244) @@ -30,6 +30,7 @@ <menu name="Tutorials"> <item name="Eclipse plugin tutorial" href="tutorials/tuto-doc.html"/> + <item name="How to write a custom plugin" href="tutorials/customplugin.html"/> </menu> ${reports} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |