Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IDEMPIERE-5796: for several tables in a row #2244

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
118 changes: 1 addition & 117 deletions org.adempiere.base/src/org/adempiere/util/ModelClassGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
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;

Expand Down Expand Up @@ -914,121 +913,6 @@ public String toString()
*/
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_INTERFACE, sourceFolder, packageName, entityType, tableName, columnEntityType);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here probably is GEN_SOURCE_CLASS ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, fixed

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -778,6 +781,18 @@ public String toString() {
* @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");
Expand All @@ -792,9 +807,7 @@ public static void generateSource(String sourceFolder, String packageName, Strin
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)
Expand All @@ -803,7 +816,7 @@ public static void generateSource(String sourceFolder, String packageName, Strin
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)
Expand All @@ -828,7 +841,7 @@ public static void generateSource(String sourceFolder, String packageName, Strin
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();
Expand All @@ -847,9 +860,19 @@ public static void generateSource(String sourceFolder, String packageName, Strin
.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);
Expand Down Expand Up @@ -884,7 +907,10 @@ public static void generateSource(String sourceFolder, String packageName, Strin
rs = pstmt.executeQuery();
while (rs.next())
{
new ModelInterfaceGenerator(rs.getInt(1), directory.toString(), packageName, columnFilter);
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)
Expand Down