IDEMPIERE-5796: Generate Model Template for several tables in a row (#2244)
* IDEMPIERE-5796: for several tables in a row https://idempiere.atlassian.net/browse/IDEMPIERE-5796 * IDEMPIERE-5796: for several tables in a row - changes done in ModelInterfaceGenerator / ModelClassGenerator Implement suggesion from @hengsin * IDEMPIERE-5796: for several tables in a row - changes done in ModelInterfaceGenerator / ModelClassGenerator Implement suggesion from @hengsin * IDEMPIERE-5796: for several tables in a row - changes done in ModelInterfaceGenerator / ModelClassGenerator Implement suggestion from @CarlosRuiz-globalqss removing duplicated code * IDEMPIERE-5796: for several tables in a row - changes done in ModelInterfaceGenerator / ModelClassGenerator fix error
This commit is contained in:
parent
c47db344e8
commit
ecfb71bd7d
|
|
@ -31,7 +31,6 @@ import java.sql.ResultSet;
|
|||
import java.sql.SQLException;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.Collection;
|
||||
import java.util.StringTokenizer;
|
||||
import java.util.TreeSet;
|
||||
import java.util.logging.Level;
|
||||
|
||||
|
|
@ -914,121 +913,6 @@ public class ModelClassGenerator
|
|||
*/
|
||||
public static void generateSource(String sourceFolder, String packageName, String entityType, String tableName, String columnEntityType)
|
||||
{
|
||||
if (sourceFolder == null || sourceFolder.trim().length() == 0)
|
||||
throw new IllegalArgumentException("Must specify source folder");
|
||||
|
||||
File file = new File(sourceFolder);
|
||||
if (!file.exists())
|
||||
throw new IllegalArgumentException("Source folder doesn't exists. sourceFolder="+sourceFolder);
|
||||
|
||||
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");
|
||||
|
||||
StringBuilder tableLike = new StringBuilder().append(tableName.trim());
|
||||
if (!tableLike.toString().startsWith("'") || !tableLike.toString().endsWith("'"))
|
||||
tableLike = new StringBuilder("'").append(tableLike).append("'");
|
||||
|
||||
StringBuilder entityTypeFilter = new StringBuilder();
|
||||
if (entityType != null && entityType.trim().length() > 0)
|
||||
{
|
||||
entityTypeFilter.append("EntityType IN (");
|
||||
StringTokenizer tokenizer = new StringTokenizer(entityType, ",");
|
||||
int i = 0;
|
||||
while(tokenizer.hasMoreTokens()) {
|
||||
StringBuilder token = new StringBuilder().append(tokenizer.nextToken().trim());
|
||||
if (!token.toString().startsWith("'") || !token.toString().endsWith("'"))
|
||||
token = new StringBuilder("'").append(token).append("'");
|
||||
if (i > 0)
|
||||
entityTypeFilter.append(",");
|
||||
entityTypeFilter.append(token);
|
||||
i++;
|
||||
}
|
||||
entityTypeFilter.append(")");
|
||||
}
|
||||
else
|
||||
{
|
||||
entityTypeFilter.append("EntityType IN ('U','A')");
|
||||
}
|
||||
|
||||
StringBuilder directory = new StringBuilder().append(sourceFolder.trim());
|
||||
String packagePath = packageName.replace(".", File.separator);
|
||||
if (!(directory.toString().endsWith("/") || directory.toString().endsWith("\\")))
|
||||
{
|
||||
directory.append(File.separator);
|
||||
}
|
||||
if (File.separator.equals("/"))
|
||||
directory = new StringBuilder(directory.toString().replaceAll("[\\\\]", File.separator));
|
||||
else
|
||||
directory = new StringBuilder(directory.toString().replaceAll("[/]", File.separator));
|
||||
directory.append(packagePath);
|
||||
file = new File(directory.toString());
|
||||
if (!file.exists())
|
||||
file.mkdirs();
|
||||
|
||||
// complete sql
|
||||
String filterViews = null;
|
||||
if (tableLike.toString().contains("%")) {
|
||||
filterViews = " AND (TableName IN ('RV_WarehousePrice','RV_BPartner') OR IsView='N')"; // special views
|
||||
}
|
||||
if (tableLike.toString().equals("'%'")) {
|
||||
filterViews += " AND TableName NOT LIKE 'W|_%' ESCAPE '|'"; // exclude webstore from general model generator
|
||||
}
|
||||
StringBuilder sql = new StringBuilder();
|
||||
sql.append("SELECT AD_Table_ID ")
|
||||
.append("FROM AD_Table ")
|
||||
.append("WHERE IsActive = 'Y' AND TableName NOT LIKE '%_Trl' ");
|
||||
// Autodetect if we need to use IN or LIKE clause - teo_sarca [ 3020640 ]
|
||||
if (tableLike.indexOf(",") == -1)
|
||||
sql.append(" AND TableName LIKE ").append(tableLike);
|
||||
else
|
||||
sql.append(" AND TableName IN (").append(tableLike).append(")"); // only specific tables
|
||||
sql.append(" AND ").append(entityTypeFilter.toString());
|
||||
if (filterViews != null) {
|
||||
sql.append(filterViews);
|
||||
}
|
||||
sql.append(" ORDER BY TableName");
|
||||
//
|
||||
StringBuilder columnFilterBuilder = new StringBuilder();
|
||||
if (!Util.isEmpty(columnEntityType, true))
|
||||
{
|
||||
columnFilterBuilder.append("EntityType IN (");
|
||||
StringTokenizer tokenizer = new StringTokenizer(columnEntityType, ",");
|
||||
int i = 0;
|
||||
while(tokenizer.hasMoreTokens()) {
|
||||
StringBuilder token = new StringBuilder().append(tokenizer.nextToken().trim());
|
||||
if (!token.toString().startsWith("'") || !token.toString().endsWith("'"))
|
||||
token = new StringBuilder("'").append(token).append("'");
|
||||
if (i > 0)
|
||||
columnFilterBuilder.append(",");
|
||||
columnFilterBuilder.append(token);
|
||||
i++;
|
||||
}
|
||||
columnFilterBuilder.append(")");
|
||||
}
|
||||
String columnFilter = columnFilterBuilder.length() > 0 ? columnFilterBuilder.toString() : null;
|
||||
|
||||
PreparedStatement pstmt = null;
|
||||
ResultSet rs = null;
|
||||
try
|
||||
{
|
||||
pstmt = DB.prepareStatement(sql.toString(), null);
|
||||
rs = pstmt.executeQuery();
|
||||
while (rs.next())
|
||||
{
|
||||
new ModelClassGenerator(rs.getInt(1), directory.toString(), packageName, columnFilter);
|
||||
}
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
throw new DBException(e, sql.toString());
|
||||
}
|
||||
finally
|
||||
{
|
||||
DB.close(rs, pstmt);
|
||||
rs = null; pstmt = null;
|
||||
}
|
||||
ModelInterfaceGenerator.generateSource(ModelInterfaceGenerator.GEN_SOURCE_CLASS, sourceFolder, packageName, entityType, tableName, columnEntityType);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -106,6 +106,9 @@ public class ModelInterfaceGenerator
|
|||
/** Logger */
|
||||
private static final CLogger log = CLogger.getCLogger(ModelInterfaceGenerator.class);
|
||||
|
||||
public final static String GEN_SOURCE_INTERFACE = "I";
|
||||
public final static String GEN_SOURCE_CLASS = "C";
|
||||
|
||||
/**
|
||||
* @param AD_Table_ID
|
||||
* @param directory
|
||||
|
|
@ -778,6 +781,18 @@ public class ModelInterfaceGenerator
|
|||
* @param columnEntityType
|
||||
*/
|
||||
public static void generateSource(String sourceFolder, String packageName, String entityType, String tableName, String columnEntityType)
|
||||
{
|
||||
generateSource(GEN_SOURCE_INTERFACE, sourceFolder, packageName, entityType, tableName, columnEntityType);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param sourceFolder
|
||||
* @param packageName
|
||||
* @param entityType
|
||||
* @param tableName table Like
|
||||
* @param columnEntityType
|
||||
*/
|
||||
public static void generateSource(String type, String sourceFolder, String packageName, String entityType, String tableName, String columnEntityType)
|
||||
{
|
||||
if (sourceFolder == null || sourceFolder.trim().length() == 0)
|
||||
throw new IllegalArgumentException("Must specify source folder");
|
||||
|
|
@ -792,9 +807,7 @@ public class ModelInterfaceGenerator
|
|||
if (tableName == null || tableName.trim().length() == 0)
|
||||
throw new IllegalArgumentException("Must specify table name");
|
||||
|
||||
StringBuilder tableLike = new StringBuilder().append(tableName.trim());
|
||||
if (!tableLike.toString().startsWith("'") || !tableLike.toString().endsWith("'"))
|
||||
tableLike = new StringBuilder("'").append(tableLike).append("'");
|
||||
StringBuilder tableLike = new StringBuilder().append(tableName.trim().toUpperCase().replaceAll("'", ""));
|
||||
|
||||
StringBuilder entityTypeFilter = new StringBuilder();
|
||||
if (entityType != null && entityType.trim().length() > 0)
|
||||
|
|
@ -803,7 +816,7 @@ public class ModelInterfaceGenerator
|
|||
StringTokenizer tokenizer = new StringTokenizer(entityType, ",");
|
||||
int i = 0;
|
||||
while(tokenizer.hasMoreTokens()) {
|
||||
StringBuilder token = new StringBuilder(tokenizer.nextToken().trim());
|
||||
StringBuilder token = new StringBuilder().append(tokenizer.nextToken().trim());
|
||||
if (!token.toString().startsWith("'") || !token.toString().endsWith("'"))
|
||||
token = new StringBuilder("'").append(token).append("'");
|
||||
if (i > 0)
|
||||
|
|
@ -828,7 +841,7 @@ public class ModelInterfaceGenerator
|
|||
directory = new StringBuilder(directory.toString().replaceAll("[\\\\]", File.separator));
|
||||
else
|
||||
directory = new StringBuilder(directory.toString().replaceAll("[/]", File.separator));
|
||||
directory = new StringBuilder(directory).append(packagePath);
|
||||
directory.append(packagePath);
|
||||
file = new File(directory.toString());
|
||||
if (!file.exists())
|
||||
file.mkdirs();
|
||||
|
|
@ -847,9 +860,19 @@ public class ModelInterfaceGenerator
|
|||
.append("WHERE IsActive = 'Y' AND TableName NOT LIKE '%_Trl' ");
|
||||
// Autodetect if we need to use IN or LIKE clause - teo_sarca [ 3020640 ]
|
||||
if (tableLike.indexOf(",") == -1)
|
||||
sql.append(" AND TableName LIKE ").append(tableLike);
|
||||
else
|
||||
sql.append(" AND TableName IN (").append(tableLike).append(")"); // only specific tables
|
||||
sql.append(" AND UPPER(TableName) LIKE ").append(DB.TO_STRING(tableLike.toString()));
|
||||
else { // only specific tables
|
||||
StringBuilder finalTableLike = new StringBuilder("");
|
||||
for (String table : tableLike.toString().split(",")) {
|
||||
if (finalTableLike.length() > 0)
|
||||
finalTableLike.append(", ");
|
||||
|
||||
finalTableLike.append(DB.TO_STRING(table.replaceAll("'", "").trim()));
|
||||
}
|
||||
|
||||
sql.append(" AND UPPER(TableName) IN (").append(finalTableLike).append(")");
|
||||
}
|
||||
|
||||
sql.append(" AND ").append(entityTypeFilter.toString());
|
||||
if (filterViews != null) {
|
||||
sql.append(filterViews);
|
||||
|
|
@ -884,7 +907,10 @@ public class ModelInterfaceGenerator
|
|||
rs = pstmt.executeQuery();
|
||||
while (rs.next())
|
||||
{
|
||||
if (type.equals(GEN_SOURCE_INTERFACE))
|
||||
new ModelInterfaceGenerator(rs.getInt(1), directory.toString(), packageName, columnFilter);
|
||||
else if (type.equals(GEN_SOURCE_CLASS))
|
||||
new ModelClassGenerator(rs.getInt(1), directory.toString(), packageName, columnFilter);
|
||||
}
|
||||
}
|
||||
catch (SQLException e)
|
||||
|
|
|
|||
Loading…
Reference in New Issue