diff --git a/org.adempiere.base-feature/model.generator.launch b/org.adempiere.base-feature/model.generator.launch
new file mode 100644
index 0000000000..86a786888f
--- /dev/null
+++ b/org.adempiere.base-feature/model.generator.launch
@@ -0,0 +1,32 @@
+
+
- * Example: java GenerateModel.class mydirectory myPackage 'U','A' - * would generate entity type User and Application classes into mydirectory. - * Without parameters, the default is used: - * C:\Compiere\compiere-all\extend\src\compiere\model\ compiere.model 'U','A' - *- * @param args directory package entityType - * - directory where to save the generated file - * - package of the classes to be generated - * - entityType to be generated + /** + * @param sourceFolder + * @param packageName + * @param entityType + * @param tableLike */ - public static void main (String[] args) + public static void generateSource(String sourceFolder, String packageName, String entityType, String tableName) { - Adempiere.startupEnvironment(true); - CLogMgt.setLevel(Level.FINE); - log.info("Generate Model $Revision: 1.42 $"); - log.info("----------------------------------"); - // first parameter - String directory = "C:\\Adempiere\\adempiere-all\\extend\\src\\compiere\\model\\"; - if (args.length > 0) - directory = args[0]; - if (directory == null || directory.length() == 0) - { - System.err.println("No Directory"); - System.exit(1); - } - log.info("Directory: " + directory); + if (sourceFolder == null || sourceFolder.trim().length() == 0) + throw new IllegalArgumentException("Must specify source folder"); - // second parameter - String packageName = "compiere.model"; - if (args.length > 1) - packageName = args[1]; - if (packageName == null || packageName.length() == 0) - { - System.err.println("No package"); - System.exit(1); - } - log.info("Package: " + packageName); + File file = new File(sourceFolder); + if (!file.exists()) + throw new IllegalArgumentException("Source folder doesn't exists. sourceFolder="+sourceFolder); - // third parameter - String entityType = "'U','A'"; // User, Application - if (args.length > 2) - entityType = args[2]; - if (entityType == null || entityType.length() == 0) - { - System.err.println("No EntityType"); - System.exit(1); - } - StringBuffer sql = new StringBuffer("EntityType IN (") - .append(entityType).append(")"); - log.info(sql.toString()); - log.info("----------------------------------"); + if (packageName == null || packageName.trim().length() == 0) + throw new IllegalArgumentException("Must specify package name"); - String tableLike = "'%'"; // All tables - //tableLike = "'AD_OrgInfo', 'AD_Role', 'C_CashLine', 'C_Currency', 'C_Invoice', 'C_Order', 'C_Payment', 'M_InventoryLine', 'M_PriceList', 'M_Product', 'U_POSTerminal'"; - if (args.length > 3) - tableLike = args[3]; - log.info("Table Like: " + tableLike); + if (tableName == null || tableName.trim().length() == 0) + throw new IllegalArgumentException("Must specify table name"); + + String tableLike = tableName.trim(); + if (!tableLike.startsWith("'") || !tableLike.endsWith("'")) + tableLike = "'" + tableLike + "'"; + + String entityTypeFilter = null; + if (entityType != null && entityType.trim().length() > 0) + { + entityTypeFilter = "EntityType IN ("; + StringTokenizer tokenizer = new StringTokenizer(entityType); + int i = 0; + while(tokenizer.hasMoreTokens()) { + String token = tokenizer.nextToken().trim(); + if (!token.startsWith("'") || !token.endsWith("'")) + token = "'" + token + "'"; + if (i > 0) + entityTypeFilter = entityTypeFilter + ","; + entityTypeFilter = entityTypeFilter + token; + i++; + } + entityTypeFilter = entityTypeFilter+")"; + } + else + { + entityTypeFilter = "EntityType IN ('U','A')"; + } + + String directory = sourceFolder.trim(); + String packagePath = packageName.replaceAll("[.]", File.separator); + if (!(directory.endsWith("/") || directory.endsWith("\\"))) + { + directory = directory + File.separator; + } + if (File.separator.equals("/")) + directory = directory.replaceAll("[\\\\]", File.separator); + else + directory = directory.replaceAll("[/]", File.separator); + directory = directory + packagePath; + file = new File(directory); + if (!file.exists()) + file.mkdirs(); // complete sql - sql.insert(0, "SELECT AD_Table_ID " - + "FROM AD_Table " - + "WHERE (TableName IN ('RV_WarehousePrice','RV_BPartner')" // special views - + " OR IsView='N')" - + " AND IsActive = 'Y' AND TableName NOT LIKE '%_Trl' AND "); + StringBuffer sql = new StringBuffer(); + sql.append("SELECT AD_Table_ID ") + .append("FROM AD_Table ") + .append("WHERE (TableName IN ('RV_WarehousePrice','RV_BPartner')") // special views + .append(" OR IsView='N')") + .append(" AND IsActive = 'Y' AND TableName NOT LIKE '%_Trl' "); sql.append(" AND TableName LIKE ").append(tableLike); - + sql.append(" AND ").append(entityTypeFilter); sql.append(" ORDER BY TableName"); // @@ -910,7 +910,5 @@ public class ModelClassGenerator DB.close(rs, pstmt); rs = null; pstmt = null; } - log.info("Generated = " + count); } - } diff --git a/org.adempiere.base/src/org/adempiere/util/ModelGeneratorDialog.java b/org.adempiere.base/src/org/adempiere/util/ModelGeneratorDialog.java new file mode 100644 index 0000000000..7945b18392 --- /dev/null +++ b/org.adempiere.base/src/org/adempiere/util/ModelGeneratorDialog.java @@ -0,0 +1,149 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 2010 Heng Sin Low * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. 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 General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + *****************************************************************************/ +package org.adempiere.util; + +import java.awt.BorderLayout; +import java.awt.Cursor; +import java.awt.GridLayout; +import java.awt.Insets; +import java.awt.Panel; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.JButton; +import javax.swing.JCheckBox; +import javax.swing.JDialog; +import javax.swing.JFileChooser; +import javax.swing.JLabel; +import javax.swing.JOptionPane; +import javax.swing.JTextField; + +/** + * + * @author hengsin + * + */ +public class ModelGeneratorDialog extends JDialog implements ActionListener { + + /** + * default generated serial version Id + */ + private static final long serialVersionUID = 3546051609729699491L; + private JButton bGenerate; + private JButton bCancel; + private JButton bFolder; + private JTextField fFolderName; + private JTextField fPackageName; + private JTextField fTableName; + private JCheckBox fGenerateInterface; + private JCheckBox fGenerateClass; + private JTextField fEntityType; + + public ModelGeneratorDialog() { + super(); + setTitle("Model Class Generator"); + BorderLayout layout = new BorderLayout(); + this.getContentPane().setLayout(layout); + Panel confirmPanel = new Panel(); + this.getContentPane().add(confirmPanel, BorderLayout.SOUTH); + Panel mainPanel = new Panel(); + this.getContentPane().add(mainPanel, BorderLayout.CENTER); + mainPanel.setLayout(new GridLayout(5, 2)); + + Panel filePanel = new Panel(); + filePanel.setLayout(new BorderLayout()); + fFolderName = new JTextField(); + filePanel.add(fFolderName, BorderLayout.CENTER); + bFolder = new JButton("..."); + bFolder.setMargin(new Insets(0, 0, 0, 0)); + filePanel.add(bFolder, BorderLayout.EAST); + mainPanel.add(new JLabel("Source Folder")); + mainPanel.add(filePanel); + bFolder.addActionListener(this); + + mainPanel.add(new JLabel("Package Name")); + fPackageName = new JTextField("org.adempiere.model"); + mainPanel.add(fPackageName); + + mainPanel.add(new JLabel("Table Name")); + fTableName = new JTextField("%"); + mainPanel.add(fTableName); + + mainPanel.add(new JLabel("Entity Type")); + fEntityType = new JTextField("U,A"); + mainPanel.add(fEntityType); + + fGenerateInterface = new JCheckBox("Generate Interface"); + fGenerateInterface.setSelected(true); + mainPanel.add(fGenerateInterface); + fGenerateClass = new JCheckBox("Generate Class"); + fGenerateClass.setSelected(false); + mainPanel.add(fGenerateClass); + + bGenerate = new JButton("Generate Source"); + confirmPanel.add(bGenerate); + bCancel = new JButton("Cancel"); + confirmPanel.add(bCancel); + bGenerate.addActionListener(this); + bCancel.addActionListener(this); + } + + @Override + public void actionPerformed(ActionEvent e) { + if (e.getSource() == bGenerate) { + String folder = fFolderName.getText(); + if (folder == null || folder.trim().length() == 0) { + JOptionPane.showMessageDialog(this, "Please enter source folder name", "Error", JOptionPane.ERROR_MESSAGE); + return; + } + String packageName = fPackageName.getText(); + if (packageName == null || packageName.trim().length() == 0) { + JOptionPane.showMessageDialog(this, "Please enter package name", "Error", JOptionPane.ERROR_MESSAGE); + return; + } + String tableName = fTableName.getText(); + if (tableName == null || tableName.trim().length() == 0) { + JOptionPane.showMessageDialog(this, "Please enter table name", "Error", JOptionPane.ERROR_MESSAGE); + return; + } + String entityType = fEntityType.getText(); + if (entityType == null || entityType.trim().length() == 0) { + JOptionPane.showMessageDialog(this, "Please enter entity type", "Error", JOptionPane.ERROR_MESSAGE); + return; + } + if (!fGenerateClass.isSelected() && !fGenerateInterface.isSelected()) { + JOptionPane.showMessageDialog(this, "Must select at least one of generate interface or generate class", "Error", JOptionPane.ERROR_MESSAGE); + return; + } + this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); + if (fGenerateClass.isSelected()) { + ModelClassGenerator.generateSource(folder, packageName, entityType, tableName); + } + if (fGenerateInterface.isSelected()) { + ModelInterfaceGenerator.generateSource(folder, packageName, entityType, tableName); + } + this.dispose(); + } else if (e.getSource() == bCancel) { + this.dispose(); + } else if (e.getSource() == bFolder) { + JFileChooser fileChooser = new JFileChooser(); + fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); + int state = fileChooser.showOpenDialog(this); + if (state == JFileChooser.APPROVE_OPTION) { + fFolderName.setText(fileChooser.getSelectedFile().getAbsolutePath()); + } + } + } + +} diff --git a/org.adempiere.base/src/org/adempiere/util/ModelInterfaceGenerator.java b/org.adempiere.base/src/org/adempiere/util/ModelInterfaceGenerator.java index f27cc104d1..8b95d36d28 100644 --- a/org.adempiere.base/src/org/adempiere/util/ModelInterfaceGenerator.java +++ b/org.adempiere.base/src/org/adempiere/util/ModelInterfaceGenerator.java @@ -39,6 +39,7 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Collection; +import java.util.StringTokenizer; import java.util.TreeSet; import java.util.logging.Level; @@ -47,7 +48,6 @@ import org.compiere.Adempiere; import org.compiere.model.MEntityType; import org.compiere.model.MQuery; import org.compiere.model.MTable; -import org.compiere.util.CLogMgt; import org.compiere.util.CLogger; import org.compiere.util.DB; import org.compiere.util.DisplayType; @@ -56,12 +56,12 @@ import org.compiere.util.Env; /** * @author Trifon Trifonov * @version $Id$ - * + * * @author Teo Sarca, SC ARHIPAC SERVICE SRL *
- * Example: java GenerateInterafce.class mydirectory myPackage 'U','A' - * would generate entity type User and Application classes into mydirectory. - * Without parameters, the default is used: - * C:\extend\src\compiere\model\ compiere.model 'U','A' - * - *- * - * @param args - * directory package entityType - directory where to save the - * generated file - package of the classes to be generated - - * entityType to be generated + /** + * @param sourceFolder + * @param packageName + * @param entityType + * @param tableLike */ - public static void main(String[] args) { - Adempiere.startupEnvironment(true); - CLogMgt.setLevel(Level.FINE); - log.info("Generate Interface $Revision: 1.0 $"); - log.info("----------------------------------"); - // first parameter - String directory = "C:\\Adempiere\\adempiere-all\\extend\\src\\compiere\\model\\"; - if (args.length > 0) - directory = args[0]; - if (directory == null || directory.length() == 0) { - System.err.println("No Directory"); - System.exit(1); - } - log.info("Directory: " + directory); + public static void generateSource(String sourceFolder, String packageName, String entityType, String tableName) + { + if (sourceFolder == null || sourceFolder.trim().length() == 0) + throw new IllegalArgumentException("Must specify source folder"); - // second parameter - String packageName = "compiere.model"; - if (args.length > 1) - packageName = args[1]; - if (packageName == null || packageName.length() == 0) { - System.err.println("No package"); - System.exit(1); - } - log.info("Package: " + packageName); + File file = new File(sourceFolder); + if (!file.exists()) + throw new IllegalArgumentException("Source folder doesn't exists. sourceFolder="+sourceFolder); - // third parameter - String entityType = "'U','A'"; // User, Application - if (args.length > 2) - entityType = args[2]; - if (entityType == null || entityType.length() == 0) { - System.err.println("No EntityType"); - System.exit(1); + if (packageName == null || packageName.trim().length() == 0) + throw new IllegalArgumentException("Must specify package name"); + + if (tableName == null || tableName.trim().length() == 0) + throw new IllegalArgumentException("Must specify table name"); + + String tableLike = tableName.trim(); + if (!tableLike.startsWith("'") || !tableLike.endsWith("'")) + tableLike = "'" + tableLike + "'"; + + String entityTypeFilter = null; + if (entityType != null && entityType.trim().length() > 0) + { + entityTypeFilter = "EntityType IN ("; + StringTokenizer tokenizer = new StringTokenizer(entityType); + int i = 0; + while(tokenizer.hasMoreTokens()) { + String token = tokenizer.nextToken().trim(); + if (!token.startsWith("'") || !token.endsWith("'")) + token = "'" + token + "'"; + if (i > 0) + entityTypeFilter = entityTypeFilter + ","; + entityTypeFilter = entityTypeFilter + token; + i++; + } + entityTypeFilter = entityTypeFilter+")"; } - StringBuffer sql = new StringBuffer("EntityType IN (").append( - entityType).append(")"); - log.info(sql.toString()); - log.info("----------------------------------"); - - // Table name like - String tableLike = "'%'"; // All tables - //tableLike = "'AD_OrgInfo', 'AD_Role', 'C_CashLine', 'C_Currency', 'C_Invoice', 'C_Order', 'C_Payment', 'M_InventoryLine', 'M_PriceList', 'M_Product', 'U_POSTerminal'"; // only specific tables - if (args.length > 3) - tableLike = args[3]; - log.info("Table Like: " + tableLike); - - // complete sql - sql.insert(0, "SELECT AD_Table_ID " + "FROM AD_Table " - + "WHERE (TableName IN ('RV_WarehousePrice','RV_BPartner')" // special views - + " OR IsView='N') AND IsActive = 'Y' AND TableName NOT LIKE '%_Trl' AND "); + else + { + entityTypeFilter = "EntityType IN ('U','A')"; + } + + String directory = sourceFolder.trim(); + String packagePath = packageName.replaceAll("[.]", File.separator); + if (!(directory.endsWith("/") || directory.endsWith("\\"))) + { + directory = directory + File.separator; + } + if (File.separator.equals("/")) + directory = directory.replaceAll("[\\\\]", File.separator); + else + directory = directory.replaceAll("[/]", File.separator); + directory = directory + packagePath; + file = new File(directory); + if (!file.exists()) + file.mkdirs(); + + // complete sql + StringBuffer sql = new StringBuffer(); + sql.append("SELECT AD_Table_ID ") + .append("FROM AD_Table ") + .append("WHERE (TableName IN ('RV_WarehousePrice','RV_BPartner')") // special views + .append(" OR IsView='N')") + .append(" AND IsActive = 'Y' AND TableName NOT LIKE '%_Trl' "); sql.append(" AND TableName LIKE ").append(tableLike); - //sql.append(" AND TableName IN (").append( tableLike ).append(")"); + sql.append(" AND ").append(entityTypeFilter); sql.append(" ORDER BY TableName"); // @@ -826,7 +830,8 @@ public class ModelInterfaceGenerator { pstmt = DB.prepareStatement(sql.toString(), null); rs = pstmt.executeQuery(); - while (rs.next()) { + while (rs.next()) + { new ModelInterfaceGenerator(rs.getInt(1), directory, packageName); count++; } @@ -840,8 +845,5 @@ public class ModelInterfaceGenerator DB.close(rs, pstmt); rs = null; pstmt = null; } - log.info("Generated = " + count); - } - } \ No newline at end of file