IDEMPIERE-3057 2Pack handler for SQL Statements that must not fail

This commit is contained in:
Carlos Ruiz 2016-03-22 19:12:26 +01:00
parent e51ad389e7
commit 18e494fd12
7 changed files with 189 additions and 4 deletions

View File

@ -0,0 +1,19 @@
SET SQLBLANKLINES ON
SET DEFINE OFF
-- IDEMPIERE-3057 2Pack handler for SQL Statements that must not fail
-- Mar 22, 2016 5:34:50 PM CET
INSERT INTO AD_Ref_List (AD_Ref_List_ID,Name,AD_Reference_ID,Value,AD_Client_ID,AD_Org_ID,IsActive,Created,CreatedBy,Updated,UpdatedBy,EntityType,AD_Ref_List_UU) VALUES (200356,'SQL Mandatory',50004,'SQM',0,0,'Y',TO_DATE('2016-03-22 17:34:50','YYYY-MM-DD HH24:MI:SS'),100,TO_DATE('2016-03-22 17:34:50','YYYY-MM-DD HH24:MI:SS'),100,'D','63605262-3ba3-4140-b839-4e5b0f8ad23d')
;
-- Mar 22, 2016 5:35:42 PM CET
UPDATE AD_Field SET DisplayLogic='@Type@=''D''|Type@=''SQL''|Type@=''SQM''',Updated=TO_DATE('2016-03-22 17:35:42','YYYY-MM-DD HH24:MI:SS'),UpdatedBy=100 WHERE AD_Field_ID=50105
;
-- Mar 22, 2016 5:39:53 PM CET
UPDATE AD_Field SET DisplayLogic='@Type@=''SQL''|@Type@=''SQM''',Updated=TO_DATE('2016-03-22 17:39:53','YYYY-MM-DD HH24:MI:SS'),UpdatedBy=100 WHERE AD_Field_ID=50104
;
SELECT register_migration_script('201603221737_IDEMPIERE-3057.sql') FROM dual
;

View File

@ -0,0 +1,16 @@
-- IDEMPIERE-3057 2Pack handler for SQL Statements that must not fail
-- Mar 22, 2016 5:34:50 PM CET
INSERT INTO AD_Ref_List (AD_Ref_List_ID,Name,AD_Reference_ID,Value,AD_Client_ID,AD_Org_ID,IsActive,Created,CreatedBy,Updated,UpdatedBy,EntityType,AD_Ref_List_UU) VALUES (200356,'SQL Mandatory',50004,'SQM',0,0,'Y',TO_TIMESTAMP('2016-03-22 17:34:50','YYYY-MM-DD HH24:MI:SS'),100,TO_TIMESTAMP('2016-03-22 17:34:50','YYYY-MM-DD HH24:MI:SS'),100,'D','63605262-3ba3-4140-b839-4e5b0f8ad23d')
;
-- Mar 22, 2016 5:35:42 PM CET
UPDATE AD_Field SET DisplayLogic='@Type@=''D''|Type@=''SQL''|Type@=''SQM''',Updated=TO_TIMESTAMP('2016-03-22 17:35:42','YYYY-MM-DD HH24:MI:SS'),UpdatedBy=100 WHERE AD_Field_ID=50105
;
-- Mar 22, 2016 5:39:53 PM CET
UPDATE AD_Field SET DisplayLogic='@Type@=''SQL''|@Type@=''SQM''',Updated=TO_TIMESTAMP('2016-03-22 17:39:53','YYYY-MM-DD HH24:MI:SS'),UpdatedBy=100 WHERE AD_Field_ID=50104
;
SELECT register_migration_script('201603221737_IDEMPIERE-3057.sql') FROM dual
;

View File

@ -30,7 +30,7 @@ public class X_AD_Package_Exp_Detail extends PO implements I_AD_Package_Exp_Deta
/** /**
* *
*/ */
private static final long serialVersionUID = 20151030L; private static final long serialVersionUID = 20160322L;
/** Standard Constructor */ /** Standard Constructor */
public X_AD_Package_Exp_Detail (Properties ctx, int AD_Package_Exp_Detail_ID, String trxName) public X_AD_Package_Exp_Detail (Properties ctx, int AD_Package_Exp_Detail_ID, String trxName)
@ -913,6 +913,8 @@ public class X_AD_Package_Exp_Detail extends PO implements I_AD_Package_Exp_Deta
public static final String TYPE_ModelValidator = "MV"; public static final String TYPE_ModelValidator = "MV";
/** Entity Type = ET */ /** Entity Type = ET */
public static final String TYPE_EntityType = "ET"; public static final String TYPE_EntityType = "ET";
/** SQL Mandatory = SQM */
public static final String TYPE_SQLMandatory = "SQM";
/** Set Type. /** Set Type.
@param Type @param Type
Type of Validation (SQL, Java Script, Java Language) Type of Validation (SQL, Java Script, Java Language)

View File

@ -152,6 +152,10 @@
class="org.adempiere.pipo2.handler.SQLStatementElementHandler" class="org.adempiere.pipo2.handler.SQLStatementElementHandler"
id="SQLStatement"> id="SQLStatement">
</handler> </handler>
<handler
class="org.adempiere.pipo2.handler.SQLMandatoryElementHandler"
id="SQLMandatory">
</handler>
<handler <handler
class="org.adempiere.pipo2.handler.ReferenceElementHandler" class="org.adempiere.pipo2.handler.ReferenceElementHandler"
id="AD_Reference"> id="AD_Reference">

View File

@ -0,0 +1,142 @@
/**********************************************************************
* This file is part of iDempiere ERP Open Source *
* http://www.idempiere.org *
* *
* Copyright (C) Contributors *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU 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 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., 51 Franklin Street, Fifth Floor, Boston, *
* MA 02110-1301, USA. *
* *
* Contributors: *
* - Trek Global *
* - Carlos Ruiz - globalqss *
**********************************************************************/
package org.adempiere.pipo2.handler;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.logging.Level;
import javax.xml.transform.sax.TransformerHandler;
import org.adempiere.exceptions.AdempiereException;
import org.adempiere.pipo2.AbstractElementHandler;
import org.adempiere.pipo2.Element;
import org.adempiere.pipo2.PIPOContext;
import org.adempiere.pipo2.PackOut;
import org.adempiere.pipo2.PackoutItem;
import org.adempiere.pipo2.SQLElementParameters;
import org.compiere.model.X_AD_Package_Imp_Detail;
import org.compiere.util.DB;
import org.compiere.util.Env;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
public class SQLMandatoryElementHandler extends AbstractElementHandler {
public void startElement(PIPOContext ctx, Element element) throws SAXException {
String elementValue = element.getElementValue();
log.info(elementValue);
String DBType = getStringValue(element, "DBType");
String sql = getStringValue(element, "statement");
if (sql.endsWith(";") && !(sql.toLowerCase().endsWith("end;")))
sql = sql.substring(0, sql.length() - 1);
sql = Env.parseContext(Env.getCtx(), 0, sql, false);
PreparedStatement pstmt = null;
X_AD_Package_Imp_Detail impDetail = createImportDetail(ctx, element.qName, "", 0);
try {
pstmt = DB.prepareStatement(sql, getTrxName(ctx));
if (DBType.equals("ALL")) {
int n = pstmt.executeUpdate();
if (log.isLoggable(Level.INFO)) log.info("Executed SQL Mandatory: "+ getStringValue(element, "statement") + " ReturnValue="+n);
} else if (DB.isOracle() && DBType.equals("Oracle")) {
int n = pstmt.executeUpdate();
if (log.isLoggable(Level.INFO)) log.info("Executed SQL Mandatory for Oracle: "+ getStringValue(element, "statement") + " ReturnValue="+n);
} else if (DB.isPostgreSQL()
&& ( DBType.equals("Postgres")
|| DBType.equals("PostgreSQL") // backward compatibility with old packages developed by hand
)
) {
// Avoid convert layer - command specific for postgresql
//
// pstmt = DB.prepareStatement(sql, getTrxName(ctx));
// pstmt.executeUpdate();
//
Statement stmt = null;
try {
stmt = pstmt.getConnection().createStatement();
int n = stmt.executeUpdate (sql);
if (log.isLoggable(Level.INFO)) log.info("Executed SQL Mandatory for PostgreSQL: "+ getStringValue(element,"statement") + " ReturnValue="+n);
} finally {
DB.close(stmt);
stmt = null;
}
}
logImportDetail (ctx, impDetail, 1, "SQLMandatory",1,"Execute");
ctx.packIn.getNotifier().addSuccessLine("-> " + sql);
} catch (Exception e) {
ctx.packIn.getNotifier().addFailureLine("SQL Mandatory failed, error (" + e.getLocalizedMessage() + "):");
logImportDetail (ctx, impDetail, 0, "SQLMandatory",1,"Execute");
ctx.packIn.getNotifier().addFailureLine("-> " + sql);
log.log(Level.SEVERE,"SQLMandatory", e);
throw new AdempiereException(e);
} finally {
DB.close(pstmt);
pstmt = null;
}
}
public void endElement(PIPOContext ctx, Element element) throws SAXException {
}
public void create(PIPOContext ctx, TransformerHandler document)
throws SAXException {
String SQLMandatory = Env.getContext(ctx.ctx, SQLElementParameters.SQL_STATEMENT);
String DBType = Env.getContext(ctx.ctx, SQLElementParameters.DB_TYPE);
AttributesImpl atts = new AttributesImpl();
addTypeName(atts, "custom");
document.startElement("","","SQLMandatory",atts);
createSQLStatmentBinding(document, SQLMandatory, DBType);
document.endElement("","","SQLMandatory");
}
private void createSQLStatmentBinding( TransformerHandler document, String sqlMandatory, String DBType) throws SAXException
{
document.startElement("","","DBType", new AttributesImpl());
char[] contents = DBType.toCharArray();
document.characters(contents,0,contents.length);
document.endElement("","","DBType");
document.startElement("","","statement", new AttributesImpl());
contents = sqlMandatory.toCharArray();
document.startCDATA();
document.characters(contents,0,contents.length);
document.endCDATA();
document.endElement("","","statement");
}
public void packOut(PackOut packout, TransformerHandler packoutHandler, TransformerHandler docHandler,int field) throws Exception
{
PackoutItem detail = packout.getCurrentPackoutItem();
Env.setContext(packout.getCtx().ctx, SQLElementParameters.SQL_STATEMENT, (String)detail.getProperty(SQLElementParameters.SQL_STATEMENT));
Env.setContext(packout.getCtx().ctx, SQLElementParameters.DB_TYPE, (String)detail.getProperty(SQLElementParameters.DB_TYPE));
this.create(packout.getCtx(), packoutHandler);
packout.getCtx().ctx.remove(SQLElementParameters.SQL_STATEMENT);
packout.getCtx().ctx.remove(SQLElementParameters.DB_TYPE);
}
}

View File

@ -104,10 +104,10 @@ public class SQLStatementElementHandler extends AbstractElementHandler {
} }
savepoint = null; savepoint = null;
} }
ctx.packIn.getNotifier().addFailureLine("SQL statement failed but ignored, error (" + e.getLocalizedMessage() + "): " + sql); ctx.packIn.getNotifier().addFailureLine("SQL statement failed but ignored, error (" + e.getLocalizedMessage() + "): ");
logImportDetail (ctx, impDetail, 0, "SQLStatement",1,"Execute"); logImportDetail (ctx, impDetail, 0, "SQLStatement",1,"Execute");
ctx.packIn.getNotifier().addFailureLine("-> " + sql); ctx.packIn.getNotifier().addFailureLine("-> " + sql);
log.log(Level.SEVERE,"SQLSatement", e); log.log(Level.SEVERE,"SQLStatement", e);
} finally { } finally {
DB.close(pstmt); DB.close(pstmt);
pstmt = null; pstmt = null;

View File

@ -183,6 +183,8 @@ public class PackOutProcess extends SvrProcess
return I_AD_Role.Table_Name; return I_AD_Role.Table_Name;
else if (X_AD_Package_Exp_Detail.TYPE_SQLStatement.equals(type)) else if (X_AD_Package_Exp_Detail.TYPE_SQLStatement.equals(type))
return "SQLStatement"; return "SQLStatement";
else if (X_AD_Package_Exp_Detail.TYPE_SQLMandatory.equals(type))
return "SQLMandatory";
else if (X_AD_Package_Exp_Detail.TYPE_Table.equals(type)) else if (X_AD_Package_Exp_Detail.TYPE_Table.equals(type))
return I_AD_Table.Table_Name; return I_AD_Table.Table_Name;
else if (X_AD_Package_Exp_Detail.TYPE_Window.equals(type)) else if (X_AD_Package_Exp_Detail.TYPE_Window.equals(type))
@ -202,7 +204,7 @@ public class PackOutProcess extends SvrProcess
if (MPackageExpDetail.TYPE_Data.equals(type)) { if (MPackageExpDetail.TYPE_Data.equals(type)) {
properties.put(DataElementParameters.AD_TABLE_ID, dtl.getAD_Table_ID()); properties.put(DataElementParameters.AD_TABLE_ID, dtl.getAD_Table_ID());
properties.put(DataElementParameters.SQL_STATEMENT, dtl.getSQLStatement()); properties.put(DataElementParameters.SQL_STATEMENT, dtl.getSQLStatement());
} else if (MPackageExpDetail.TYPE_SQLStatement.equals(type)) { } else if (MPackageExpDetail.TYPE_SQLStatement.equals(type) || MPackageExpDetail.TYPE_SQLMandatory.equals(type)) {
properties.put(SQLElementParameters.SQL_STATEMENT, dtl.getSQLStatement()); properties.put(SQLElementParameters.SQL_STATEMENT, dtl.getSQLStatement());
properties.put(SQLElementParameters.DB_TYPE, dtl.getDBType()); properties.put(SQLElementParameters.DB_TYPE, dtl.getDBType());
} else if (MPackageExpDetail.TYPE_File_CodeOrOther.equals(type)) { } else if (MPackageExpDetail.TYPE_File_CodeOrOther.equals(type)) {