From d541c3261b0a327ebda020415562abdffa7c9921 Mon Sep 17 00:00:00 2001 From: Nicolas Adment <39568358+nadment@users.noreply.github.com> Date: Wed, 3 May 2023 23:33:27 +0200 Subject: [PATCH 1/3] Cleanup XML of action Wait for SQL #2000 --- .../actions/waitforsql/ActionWaitForSql.java | 418 +++++++----------- .../waitforsql/ActionWaitForSqlDialog.java | 87 ++-- .../waitforsql/ActionWaitForSqlTest.java | 60 +++ .../WorkflowActionWaitForSqlLoadSaveTest.java | 68 --- .../test/resources/wait-for-sql-action.xml | 35 ++ 5 files changed, 309 insertions(+), 359 deletions(-) create mode 100644 plugins/actions/waitforsql/src/test/java/org/apache/hop/workflow/actions/waitforsql/ActionWaitForSqlTest.java delete mode 100644 plugins/actions/waitforsql/src/test/java/org/apache/hop/workflow/actions/waitforsql/WorkflowActionWaitForSqlLoadSaveTest.java create mode 100644 plugins/actions/waitforsql/src/test/resources/wait-for-sql-action.xml diff --git a/plugins/actions/waitforsql/src/main/java/org/apache/hop/workflow/actions/waitforsql/ActionWaitForSql.java b/plugins/actions/waitforsql/src/main/java/org/apache/hop/workflow/actions/waitforsql/ActionWaitForSql.java index 46d6b9ab156..1982c47ca07 100644 --- a/plugins/actions/waitforsql/src/main/java/org/apache/hop/workflow/actions/waitforsql/ActionWaitForSql.java +++ b/plugins/actions/waitforsql/src/main/java/org/apache/hop/workflow/actions/waitforsql/ActionWaitForSql.java @@ -26,12 +26,13 @@ import org.apache.hop.core.database.DatabaseMeta; import org.apache.hop.core.exception.HopDatabaseException; import org.apache.hop.core.exception.HopException; -import org.apache.hop.core.exception.HopXmlException; import org.apache.hop.core.row.IRowMeta; import org.apache.hop.core.util.Utils; import org.apache.hop.core.variables.IVariables; -import org.apache.hop.core.xml.XmlHandler; import org.apache.hop.i18n.BaseMessages; +import org.apache.hop.metadata.api.HopMetadataProperty; +import org.apache.hop.metadata.api.IEnumHasCode; +import org.apache.hop.metadata.api.IEnumHasCodeAndDescription; import org.apache.hop.metadata.api.IHopMetadataProvider; import org.apache.hop.resource.ResourceEntry; import org.apache.hop.resource.ResourceEntry.ResourceType; @@ -41,8 +42,6 @@ import org.apache.hop.workflow.action.IAction; import org.apache.hop.workflow.action.validator.ActionValidatorUtils; import org.apache.hop.workflow.action.validator.AndValidator; -import org.w3c.dom.Node; - import java.util.ArrayList; import java.util.List; @@ -57,72 +56,121 @@ documentationUrl = "/workflow/actions/waitforsql.html") public class ActionWaitForSql extends ActionBase implements Cloneable, IAction { private static final Class PKG = ActionWaitForSql.class; // For Translator - - public boolean isClearResultList; - - public boolean isAddRowsResult; - - public boolean isUseVars; - - public boolean isCustomSql; - - public String customSql; - - private DatabaseMeta connection; - - public String tableName; - - public String schemaName; - - private String maximumTimeout; // maximum timeout in seconds - private String checkCycleTime; // cycle time in seconds + + @HopMetadataProperty(key = "clear_result_rows") + private boolean clearResultList; + + @HopMetadataProperty(key = "add_rows_result") + private boolean addRowsResult; + + @HopMetadataProperty(key = "is_usevars") + private boolean useVars; + + @HopMetadataProperty(key = "is_custom_sql") + private boolean customSqlEnabled; + + @HopMetadataProperty(key = "custom_sql") + private String customSql; + + @HopMetadataProperty(key = "connection") + private String connection; + + @HopMetadataProperty(key = "tablename") + private String tableName; + + @HopMetadataProperty(key = "schemaname") + private String schemaName; + + /** Maximum timeout in seconds */ + @HopMetadataProperty(key = "maximum_timeout") + private String maximumTimeout; + + /** Cycle time in seconds */ + @HopMetadataProperty(key = "check_cycle_time") + private String checkCycleTime; + + @HopMetadataProperty(key = "success_on_timeout") private boolean successOnTimeout; + @HopMetadataProperty(key = "rows_count_value") + private String rowsCountValue; + + @HopMetadataProperty(key = "success_condition", storeWithCode = true) + private SuccessCondition successCondition; + private static final String selectCount = "SELECT count(*) FROM "; - - public static final String[] successConditionsDesc = - new String[] { - BaseMessages.getString(PKG, "ActionWaitForSQL.SuccessWhenRowCountEqual.Label"), - BaseMessages.getString(PKG, "ActionWaitForSQL.SuccessWhenRowCountDifferent.Label"), - BaseMessages.getString(PKG, "ActionWaitForSQL.SuccessWhenRowCountSmallerThan.Label"), - BaseMessages.getString(PKG, "ActionWaitForSQL.SuccessWhenRowCountSmallerOrEqualThan.Label"), - BaseMessages.getString(PKG, "ActionWaitForSQL.SuccessWhenRowCountGreaterThan.Label"), - BaseMessages.getString(PKG, "ActionWaitForSQL.SuccessWhenRowCountGreaterOrEqual.Label") - }; - public static final String[] successConditionsCode = - new String[] { - "rows_count_equal", - "rows_count_different", - "rows_count_smaller", - "rows_count_smaller_equal", - "rows_count_greater", - "rows_count_greater_equal" - }; - + public static final int SUCCESS_CONDITION_ROWS_COUNT_EQUAL = 0; public static final int SUCCESS_CONDITION_ROWS_COUNT_DIFFERENT = 1; public static final int SUCCESS_CONDITION_ROWS_COUNT_SMALLER = 2; public static final int SUCCESS_CONDITION_ROWS_COUNT_SMALLER_EQUAL = 3; public static final int SUCCESS_CONDITION_ROWS_COUNT_GREATER = 4; public static final int SUCCESS_CONDITION_ROWS_COUNT_GREATER_EQUAL = 5; + + public enum SuccessCondition implements IEnumHasCodeAndDescription { + ROWS_COUNT_EQUAL("rows_count_equal", BaseMessages.getString(PKG, "ActionWaitForSQL.SuccessWhenRowCountEqual.Label")), + ROWS_COUNT_DIFFERENT("rows_count_different",BaseMessages.getString(PKG, "ActionWaitForSQL.SuccessWhenRowCountDifferent.Label")), + ROWS_COUNT_SMALLER("rows_count_smaller", BaseMessages.getString(PKG, "ActionWaitForSQL.SuccessWhenRowCountSmallerThan.Label")), + ROWS_COUNT_SMALLER_EQUAL("rows_count_smaller_equal", BaseMessages.getString(PKG, "ActionWaitForSQL.SuccessWhenRowCountSmallerOrEqualThan.Label")), + ROWS_COUNT_GREATER("rows_count_greater", BaseMessages.getString(PKG, "ActionWaitForSQL.SuccessWhenRowCountGreaterThan.Label")), + ROWS_COUNT_GREATER_EQUAL("rows_count_greater_equal", BaseMessages.getString(PKG, "ActionWaitForSQL.SuccessWhenRowCountGreaterOrEqual.Label")); + + private final String code; + private final String description; + + SuccessCondition(String code, String description) { + this.code = code; + this.description = description; + } + + public static String[] getDescriptions() { + return IEnumHasCodeAndDescription.getDescriptions(SuccessCondition.class); + } + + public static SuccessCondition lookupDescription(String description) { + return IEnumHasCodeAndDescription.lookupDescription(SuccessCondition.class, description, ROWS_COUNT_EQUAL); + } + + public static SuccessCondition lookupCode(String code) { + return IEnumHasCode.lookupCode(SuccessCondition.class, code, ROWS_COUNT_EQUAL); + } + + /** + * Gets code + * + * @return value of code + */ + @Override + public String getCode() { + return code; + } + + /** + * Gets description + * + * @return value of description + */ + @Override + public String getDescription() { + return description; + } + } - public String rowsCountValue; - public int successCondition; private static String DEFAULT_MAXIMUM_TIMEOUT = "0"; // infinite timeout private static String DEFAULT_CHECK_CYCLE_TIME = "60"; // 1 minute public ActionWaitForSql(String n) { super(n, ""); - isClearResultList = true; + clearResultList = true; rowsCountValue = "0"; - successCondition = SUCCESS_CONDITION_ROWS_COUNT_GREATER; - isCustomSql = false; - isUseVars = false; - isAddRowsResult = false; + successCondition = SuccessCondition.ROWS_COUNT_GREATER; + customSqlEnabled = false; + useVars = false; + addRowsResult = false; customSql = null; + connection = null; schemaName = null; tableName = null; - connection = null; maximumTimeout = DEFAULT_MAXIMUM_TIMEOUT; checkCycleTime = DEFAULT_CHECK_CYCLE_TIME; successOnTimeout = false; @@ -138,129 +186,10 @@ public Object clone() { return je; } - public int getSuccessCondition() { + public SuccessCondition getSuccessCondition() { return successCondition; } - public static int getSuccessConditionByDesc(String tt) { - if (tt == null) { - return 0; - } - - for (int i = 0; i < successConditionsDesc.length; i++) { - if (successConditionsDesc[i].equalsIgnoreCase(tt)) { - return i; - } - } - - // If this fails, try to match using the code. - return getSuccessConditionByCode(tt); - } - - @Override - public String getXml() { - StringBuilder retval = new StringBuilder(200); - - retval.append(super.getXml()); - retval - .append(" ") - .append( - XmlHandler.addTagValue("connection", connection == null ? null : connection.getName())); - retval.append(" ").append(XmlHandler.addTagValue("schemaname", schemaName)); - retval.append(" ").append(XmlHandler.addTagValue("tablename", tableName)); - retval - .append(" ") - .append( - XmlHandler.addTagValue("success_condition", getSuccessConditionCode(successCondition))); - retval.append(" ").append(XmlHandler.addTagValue("rows_count_value", rowsCountValue)); - retval.append(" ").append(XmlHandler.addTagValue("is_custom_sql", isCustomSql)); - retval.append(" ").append(XmlHandler.addTagValue("is_usevars", isUseVars)); - retval.append(" ").append(XmlHandler.addTagValue("custom_sql", customSql)); - retval.append(" ").append(XmlHandler.addTagValue("add_rows_result", isAddRowsResult)); - retval.append(" ").append(XmlHandler.addTagValue("maximum_timeout", maximumTimeout)); - retval.append(" ").append(XmlHandler.addTagValue("check_cycle_time", checkCycleTime)); - retval.append(" ").append(XmlHandler.addTagValue("success_on_timeout", successOnTimeout)); - retval.append(" ").append(XmlHandler.addTagValue("clear_result_rows", isClearResultList)); - return retval.toString(); - } - - private static String getSuccessConditionCode(int i) { - if (i < 0 || i >= successConditionsCode.length) { - return successConditionsCode[0]; - } - return successConditionsCode[i]; - } - - private static int getSucessConditionByCode(String tt) { - if (tt == null) { - return 0; - } - - for (int i = 0; i < successConditionsCode.length; i++) { - if (successConditionsCode[i].equalsIgnoreCase(tt)) { - return i; - } - } - return 0; - } - - public static String getSuccessConditionDesc(int i) { - if (i < 0 || i >= successConditionsDesc.length) { - return successConditionsDesc[0]; - } - return successConditionsDesc[i]; - } - - @Override - public void loadXml(Node entrynode, IHopMetadataProvider metadataProvider, IVariables variables) - throws HopXmlException { - try { - super.loadXml(entrynode); - String dbname = XmlHandler.getTagValue(entrynode, "connection"); - connection = DatabaseMeta.loadDatabase(metadataProvider, dbname); - schemaName = XmlHandler.getTagValue(entrynode, "schemaname"); - tableName = XmlHandler.getTagValue(entrynode, "tablename"); - successCondition = - getSucessConditionByCode( - Const.NVL(XmlHandler.getTagValue(entrynode, "success_condition"), "")); - rowsCountValue = Const.NVL(XmlHandler.getTagValue(entrynode, "rows_count_value"), "0"); - isCustomSql = "Y".equalsIgnoreCase(XmlHandler.getTagValue(entrynode, "is_custom_sql")); - isUseVars = "Y".equalsIgnoreCase(XmlHandler.getTagValue(entrynode, "is_usevars")); - customSql = XmlHandler.getTagValue(entrynode, "custom_sql"); - isAddRowsResult = "Y".equalsIgnoreCase(XmlHandler.getTagValue(entrynode, "add_rows_result")); - maximumTimeout = XmlHandler.getTagValue(entrynode, "maximum_timeout"); - checkCycleTime = XmlHandler.getTagValue(entrynode, "check_cycle_time"); - successOnTimeout = - "Y".equalsIgnoreCase(XmlHandler.getTagValue(entrynode, "success_on_timeout")); - isClearResultList = - "Y".equalsIgnoreCase(XmlHandler.getTagValue(entrynode, "clear_result_rows")); - - } catch (HopException e) { - throw new HopXmlException(BaseMessages.getString(PKG, "ActionWaitForSQL.UnableLoadXML"), e); - } - } - - private static int getSuccessConditionByCode(String tt) { - if (tt == null) { - return 0; - } - - for (int i = 0; i < successConditionsCode.length; i++) { - if (successConditionsCode[i].equalsIgnoreCase(tt)) { - return i; - } - } - return 0; - } - - public void setDatabase(DatabaseMeta database) { - this.connection = database; - } - - public DatabaseMeta getDatabase() { - return connection; - } - @Override public boolean isEvaluation() { return true; @@ -275,8 +204,15 @@ public boolean isUnconditional() { protected void checkConnection() throws HopDatabaseException { // check connection // connect and disconnect - try (Database dbchecked = new Database(this, this, connection)) { - dbchecked.connect(); + try { + IHopMetadataProvider metadataProvider = this.getParentWorkflow().getMetadataProvider(); + DatabaseMeta databaseMeta = metadataProvider.getSerializer(DatabaseMeta.class).load(resolve(connection)); + + try ( Database database = new Database(this, this, databaseMeta)) { + database.connect(); + } + } catch (HopException e) { + throw new HopDatabaseException(e.getMessage(), e); } } @@ -294,14 +230,14 @@ public Result execute(Result previousResult, int nr) { return result; } - if (isCustomSql) { + if (customSqlEnabled) { // clear result list rows - if (isClearResultList) { + if (clearResultList) { result.getRows().clear(); } realCustomSql = customSql; - if (isUseVars) { + if (useVars) { realCustomSql = resolve(realCustomSql); } if (log.isDebug()) { @@ -434,10 +370,12 @@ protected boolean sqlDataOK( List ar = null; IRowMeta rowMeta = null; - Database db = new Database(this, this, connection); - try { + IHopMetadataProvider metadataProvider = this.getParentWorkflow().getMetadataProvider(); + DatabaseMeta databaseMeta = metadataProvider.getSerializer(DatabaseMeta.class).load(resolve(connection)); + + try (Database db = new Database(this, this, databaseMeta)) { db.connect(); - if (isCustomSql) { + if (customSqlEnabled) { countStatement = customSql; } else { if (!Utils.isEmpty(realSchemaName)) { @@ -456,7 +394,7 @@ protected boolean sqlDataOK( BaseMessages.getString(PKG, "ActionWaitForSQL.Log.RunSQLStatement", countStatement)); } - if (isCustomSql) { + if (customSqlEnabled) { ar = db.getRows(countStatement, 0); if (ar != null) { rowsCount = ar.size(); @@ -480,43 +418,40 @@ protected boolean sqlDataOK( } switch (successCondition) { - case ActionWaitForSql.SUCCESS_CONDITION_ROWS_COUNT_EQUAL: + case ROWS_COUNT_EQUAL: successOK = (rowsCount == nrRowsLimit); break; - case ActionWaitForSql.SUCCESS_CONDITION_ROWS_COUNT_DIFFERENT: + case ROWS_COUNT_DIFFERENT: successOK = (rowsCount != nrRowsLimit); break; - case ActionWaitForSql.SUCCESS_CONDITION_ROWS_COUNT_SMALLER: + case ROWS_COUNT_SMALLER: successOK = (rowsCount < nrRowsLimit); break; - case ActionWaitForSql.SUCCESS_CONDITION_ROWS_COUNT_SMALLER_EQUAL: + case ROWS_COUNT_SMALLER_EQUAL: successOK = (rowsCount <= nrRowsLimit); break; - case ActionWaitForSql.SUCCESS_CONDITION_ROWS_COUNT_GREATER: + case ROWS_COUNT_GREATER: successOK = (rowsCount > nrRowsLimit); break; - case ActionWaitForSql.SUCCESS_CONDITION_ROWS_COUNT_GREATER_EQUAL: + case ROWS_COUNT_GREATER_EQUAL: successOK = (rowsCount >= nrRowsLimit); break; default: break; } } // end if countStatement!=null + + if (addRowsResult && customSqlEnabled && ar != null) { + rowMeta = db.getQueryFields(countStatement, false); + } } catch (HopDatabaseException dbe) { logError( BaseMessages.getString(PKG, "ActionWaitForSQL.Error.RunningEntry", dbe.getMessage())); - } finally { - if (db != null) { - if (isAddRowsResult && isCustomSql && ar != null) { - rowMeta = db.getQueryFields(countStatement, false); - } - db.disconnect(); - } } if (successOK) { // ad rows to result - if (isAddRowsResult && isCustomSql && ar != null) { + if (addRowsResult && customSqlEnabled && ar != null) { List rows = new ArrayList<>(); for (int i = 0; i < ar.size(); i++) { rows.add(new RowMetaAndData(rowMeta, ar.get(i))); @@ -531,23 +466,31 @@ protected boolean sqlDataOK( @Override public DatabaseMeta[] getUsedDatabaseConnections() { - return new DatabaseMeta[] { - connection, - }; + return new DatabaseMeta[0]; } @Override - public List getResourceDependencies( - IVariables variables, WorkflowMeta workflowMeta) { + public List getResourceDependencies(IVariables variables, + WorkflowMeta workflowMeta) { List references = super.getResourceDependencies(variables, workflowMeta); - if (connection != null) { + + DatabaseMeta databaseMeta = null; + try { + IHopMetadataProvider metadataProvider = workflowMeta.getMetadataProvider(); + databaseMeta = metadataProvider.getSerializer(DatabaseMeta.class).load(variables.resolve(connection)); + } catch (HopException e) { + // Ignore error loading metadata + } + + if (databaseMeta != null) { ResourceReference reference = new ResourceReference(this); - reference.getEntries().add(new ResourceEntry(connection.getHostname(), ResourceType.SERVER)); - reference - .getEntries() - .add(new ResourceEntry(connection.getDatabaseName(), ResourceType.DATABASENAME)); + reference.getEntries() + .add(new ResourceEntry(databaseMeta.getHostname(), ResourceType.SERVER)); + reference.getEntries() + .add(new ResourceEntry(databaseMeta.getDatabaseName(), ResourceType.DATABASENAME)); references.add(reference); } + return references; } @@ -571,12 +514,12 @@ public void check( * @return value of isClearResultList */ public boolean isClearResultList() { - return isClearResultList; + return clearResultList; } /** @param isClearResultList The isClearResultList to set */ public void setClearResultList(boolean isClearResultList) { - this.isClearResultList = isClearResultList; + this.clearResultList = isClearResultList; } /** @@ -585,12 +528,12 @@ public void setClearResultList(boolean isClearResultList) { * @return value of isAddRowsResult */ public boolean isAddRowsResult() { - return isAddRowsResult; + return addRowsResult; } /** @param isAddRowsResult The isAddRowsResult to set */ public void setAddRowsResult(boolean isAddRowsResult) { - this.isAddRowsResult = isAddRowsResult; + this.addRowsResult = isAddRowsResult; } /** @@ -599,12 +542,12 @@ public void setAddRowsResult(boolean isAddRowsResult) { * @return value of isUseVars */ public boolean isUseVars() { - return isUseVars; + return useVars; } /** @param isUseVars The isUseVars to set */ public void setUseVars(boolean isUseVars) { - this.isUseVars = isUseVars; + this.useVars = isUseVars; } /** @@ -612,13 +555,13 @@ public void setUseVars(boolean isUseVars) { * * @return value of isCustomSql */ - public boolean isCustomSql() { - return isCustomSql; + public boolean isCustomSqlEnabled() { + return customSqlEnabled; } /** @param isCustomSql The isCustomSql to set */ - public void setCustomSql(boolean isCustomSql) { - this.isCustomSql = isCustomSql; + public void setCustomSqlEnabled(boolean isCustomSql) { + this.customSqlEnabled = isCustomSql; } /** @@ -635,20 +578,6 @@ public void setCustomSql(String customSql) { this.customSql = customSql; } - /** - * Gets connection - * - * @return value of connection - */ - public DatabaseMeta getConnection() { - return connection; - } - - /** @param connection The connection to set */ - public void setConnection(DatabaseMeta connection) { - this.connection = connection; - } - /** * Gets tableName * @@ -728,23 +657,6 @@ public static String getSelectCount() { return selectCount; } - /** - * Gets successConditionsDesc - * - * @return value of successConditionsDesc - */ - public static String[] getSuccessConditionsDesc() { - return successConditionsDesc; - } - - /** - * Gets successConditionsCode - * - * @return value of successConditionsCode - */ - public static String[] getSuccessConditionsCode() { - return successConditionsCode; - } /** * Gets rowsCountValue @@ -761,7 +673,21 @@ public void setRowsCountValue(String rowsCountValue) { } /** @param successCondition The successCondition to set */ - public void setSuccessCondition(int successCondition) { + public void setSuccessCondition(SuccessCondition successCondition) { this.successCondition = successCondition; } + + /** + * Gets connection + * + * @return value of connection + */ + public String getConnection() { + return connection; + } + + /** @param connection The connection to set */ + public void setConnection(String connection) { + this.connection = connection; + } } diff --git a/plugins/actions/waitforsql/src/main/java/org/apache/hop/workflow/actions/waitforsql/ActionWaitForSqlDialog.java b/plugins/actions/waitforsql/src/main/java/org/apache/hop/workflow/actions/waitforsql/ActionWaitForSqlDialog.java index 0e68f668859..8cf1ea6a59b 100644 --- a/plugins/actions/waitforsql/src/main/java/org/apache/hop/workflow/actions/waitforsql/ActionWaitForSqlDialog.java +++ b/plugins/actions/waitforsql/src/main/java/org/apache/hop/workflow/actions/waitforsql/ActionWaitForSqlDialog.java @@ -41,6 +41,7 @@ import org.apache.hop.workflow.WorkflowMeta; import org.apache.hop.workflow.action.IAction; import org.apache.hop.workflow.action.IActionDialog; +import org.apache.hop.workflow.actions.waitforsql.ActionWaitForSql.SuccessCondition; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.CCombo; import org.eclipse.swt.events.FocusAdapter; @@ -171,7 +172,8 @@ public IAction open() { wName.setLayoutData(fdName); // Connection line - wConnection = addConnectionLine(shell, wName, action.getDatabase(), lsMod); + DatabaseMeta databaseMeta = workflowMeta.findDatabase(action.getConnection(), variables); + wConnection = addConnectionLine(shell, wName, databaseMeta, lsMod); // Schema name line wlSchemaname = new Label(shell, SWT.RIGHT); @@ -251,7 +253,7 @@ public void widgetSelected(SelectionEvent e) { fdlSuccessCondition.top = new FormAttachment(0, margin); wlSuccessCondition.setLayoutData(fdlSuccessCondition); wSuccessCondition = new CCombo(wSuccessGroup, SWT.SINGLE | SWT.READ_ONLY | SWT.BORDER); - wSuccessCondition.setItems(ActionWaitForSql.successConditionsDesc); + wSuccessCondition.setItems(SuccessCondition.getDescriptions()); wSuccessCondition.select(0); // +1: starts at -1 PropsUi.setLook(wSuccessCondition); @@ -588,17 +590,21 @@ public void mouseUp(MouseEvent e) { } private void getSql() { - DatabaseMeta inf = getWorkflowMeta().findDatabase(wConnection.getText(), variables); - if (inf != null) { + if (StringUtils.isEmpty(wConnection.getText())) { + return; + } + + DatabaseMeta databaseMeta = this.workflowMeta.findDatabase(variables.resolve(wConnection.getText()), variables); + if (databaseMeta != null) { DatabaseExplorerDialog std = new DatabaseExplorerDialog( - shell, SWT.NONE, variables, inf, getWorkflowMeta().getDatabases()); + shell, SWT.NONE, variables, databaseMeta, getWorkflowMeta().getDatabases()); if (std.open()) { String sql = "SELECT *" + Const.CR + "FROM " - + inf.getQuotedSchemaTableCombination( + + databaseMeta.getQuotedSchemaTableCombination( variables, std.getSchemaName(), std.getTableName()) + Const.CR; wSql.setText(sql); @@ -614,7 +620,7 @@ private void getSql() { wSql.setText(sql); break; case SWT.YES: - Database db = new Database(loggingObject, variables, inf); + Database db = new Database(loggingObject, variables, databaseMeta); try { db.connect(); IRowMeta fields = db.getQueryFields(sql, false); @@ -627,11 +633,11 @@ private void getSql() { } else { sql += ", "; } - sql += inf.quoteField(field.getName()) + Const.CR; + sql += databaseMeta.quoteField(field.getName()) + Const.CR; } sql += "FROM " - + inf.getQuotedSchemaTableCombination( + + databaseMeta.getQuotedSchemaTableCombination( variables, std.getSchemaName(), std.getTableName()) + Const.CR; wSql.setText(sql); @@ -665,7 +671,7 @@ private void getSql() { mb.setMessage(BaseMessages.getString(PKG, "ActionWaitForSQL.ConnectionNoLongerAvailable")); mb.setText(BaseMessages.getString(PKG, "ActionWaitForSQL.DialogCaptionError4")); mb.open(); - } + } } public void setPosition() { @@ -696,23 +702,18 @@ private void setCustomerSql() { /** Copy information from the meta-data input to the dialog fields. */ public void getData() { wName.setText(Const.nullToEmpty(action.getName())); - - if (action.getDatabase() != null) { - wConnection.setText(action.getDatabase().getName()); - } - - wSchemaname.setText(Const.nullToEmpty(action.schemaName)); - wTablename.setText(Const.nullToEmpty(action.tableName)); - - wSuccessCondition.setText(ActionWaitForSql.getSuccessConditionDesc(action.successCondition)); - wRowsCountValue.setText(Const.NVL(action.rowsCountValue, "0")); - wCustomSql.setSelection(action.isCustomSql); - wUseSubs.setSelection(action.isUseVars); - wAddRowsToResult.setSelection(action.isAddRowsResult); - wClearResultList.setSelection(action.isClearResultList); - wSql.setText(Const.nullToEmpty(action.customSql)); - wMaximumTimeout.setText(Const.NVL(action.getMaximumTimeout(), "")); - wCheckCycleTime.setText(Const.NVL(action.getCheckCycleTime(), "")); + wConnection.setText(Const.nullToEmpty(action.getConnection())); + wSchemaname.setText(Const.nullToEmpty(action.getSchemaName())); + wTablename.setText(Const.nullToEmpty(action.getTableName())); + wSuccessCondition.setText(action.getSuccessCondition().getDescription()); + wRowsCountValue.setText(Const.NVL(action.getRowsCountValue(), "0")); + wCustomSql.setSelection(action.isCustomSqlEnabled()); + wUseSubs.setSelection(action.isUseVars()); + wAddRowsToResult.setSelection(action.isAddRowsResult()); + wClearResultList.setSelection(action.isClearResultList()); + wSql.setText(Const.nullToEmpty(action.getCustomSql())); + wMaximumTimeout.setText(Const.nullToEmpty(action.getMaximumTimeout())); + wCheckCycleTime.setText(Const.nullToEmpty(action.getCheckCycleTime())); wSuccessOnTimeout.setSelection(action.isSuccessOnTimeout()); wName.selectAll(); @@ -734,18 +735,16 @@ private void ok() { return; } action.setName(wName.getText()); - action.setDatabase(getWorkflowMeta().findDatabase(wConnection.getText(), variables)); - - action.schemaName = wSchemaname.getText(); - action.tableName = wTablename.getText(); - action.successCondition = - ActionWaitForSql.getSuccessConditionByDesc(wSuccessCondition.getText()); - action.rowsCountValue = wRowsCountValue.getText(); - action.isCustomSql = wCustomSql.getSelection(); - action.isUseVars = wUseSubs.getSelection(); - action.isAddRowsResult = wAddRowsToResult.getSelection(); - action.isClearResultList = wClearResultList.getSelection(); - action.customSql = wSql.getText(); + action.setConnection(wConnection.getText()); + action.setSchemaName(wSchemaname.getText()); + action.setTableName(wTablename.getText()); + action.setSuccessCondition(SuccessCondition.lookupDescription(wSuccessCondition.getText())); + action.setRowsCountValue(wRowsCountValue.getText()); + action.setCustomSqlEnabled(wCustomSql.getSelection()); + action.setUseVars(wUseSubs.getSelection()); + action.setAddRowsResult(wAddRowsToResult.getSelection()); + action.setClearResultList(wClearResultList.getSelection()); + action.setCustomSql(wSql.getText()); action.setMaximumTimeout(wMaximumTimeout.getText()); action.setCheckCycleTime(wCheckCycleTime.getText()); action.setSuccessOnTimeout(wSuccessOnTimeout.getSelection()); @@ -754,13 +753,11 @@ private void ok() { } private void getTableName() { - String databaseName = wConnection.getText(); - if (StringUtils.isNotEmpty(databaseName)) { - DatabaseMeta databaseMeta = getWorkflowMeta().findDatabase(databaseName, variables); + if (StringUtils.isNotEmpty(wConnection.getText())) { + DatabaseMeta databaseMeta = this.workflowMeta.findDatabase(variables.resolve(wConnection.getText()), variables); if (databaseMeta != null) { - DatabaseExplorerDialog std = - new DatabaseExplorerDialog( - shell, SWT.NONE, variables, databaseMeta, getWorkflowMeta().getDatabases()); + DatabaseExplorerDialog std = new DatabaseExplorerDialog(shell, SWT.NONE, variables, + databaseMeta, getWorkflowMeta().getDatabases()); std.setSelectedSchemaAndTable(wSchemaname.getText(), wTablename.getText()); if (std.open()) { wTablename.setText(Const.NVL(std.getTableName(), "")); diff --git a/plugins/actions/waitforsql/src/test/java/org/apache/hop/workflow/actions/waitforsql/ActionWaitForSqlTest.java b/plugins/actions/waitforsql/src/test/java/org/apache/hop/workflow/actions/waitforsql/ActionWaitForSqlTest.java new file mode 100644 index 00000000000..2d9bcdc0559 --- /dev/null +++ b/plugins/actions/waitforsql/src/test/java/org/apache/hop/workflow/actions/waitforsql/ActionWaitForSqlTest.java @@ -0,0 +1,60 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hop.workflow.actions.waitforsql; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import org.apache.hop.core.HopClientEnvironment; +import org.apache.hop.core.database.DatabaseMeta; +import org.apache.hop.metadata.serializer.memory.MemoryMetadataProvider; +import org.apache.hop.workflow.action.ActionSerializationTestUtil; +import org.apache.hop.workflow.actions.waitforsql.ActionWaitForSql.SuccessCondition; +import org.junit.Test; + +/** Unit tests for wait for sql action. */ +public class ActionWaitForSqlTest { + + @Test + public void testSerialization() throws Exception { + HopClientEnvironment.init(); + DatabaseMeta databaseMeta = new DatabaseMeta(); + databaseMeta.setName("unit-test-db"); + databaseMeta.setDatabaseType("NONE"); + MemoryMetadataProvider provider = new MemoryMetadataProvider(); + provider.getSerializer(DatabaseMeta.class).save(databaseMeta); + + ActionWaitForSql action = + ActionSerializationTestUtil.testSerialization( + "/wait-for-sql-action.xml", ActionWaitForSql.class, provider); + + assertEquals("unit-test-db", action.getConnection()); + assertEquals("SCHEMATEST", action.getSchemaName()); + assertEquals("TABLETEST", action.getTableName()); + assertEquals("select FIELD from TABLE", action.getCustomSql()); + assertEquals(SuccessCondition.ROWS_COUNT_DIFFERENT, action.getSuccessCondition()); + assertEquals("1", action.getRowsCountValue()); + assertEquals("60", action.getCheckCycleTime()); + assertEquals("99", action.getMaximumTimeout()); + assertFalse(action.isAddRowsResult()); + assertTrue(action.isCustomSqlEnabled()); + assertTrue(action.isUseVars()); + assertFalse(action.isSuccessOnTimeout()); + assertTrue(action.isClearResultList()); + } +} diff --git a/plugins/actions/waitforsql/src/test/java/org/apache/hop/workflow/actions/waitforsql/WorkflowActionWaitForSqlLoadSaveTest.java b/plugins/actions/waitforsql/src/test/java/org/apache/hop/workflow/actions/waitforsql/WorkflowActionWaitForSqlLoadSaveTest.java deleted file mode 100644 index 8f469ba3632..00000000000 --- a/plugins/actions/waitforsql/src/test/java/org/apache/hop/workflow/actions/waitforsql/WorkflowActionWaitForSqlLoadSaveTest.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.hop.workflow.actions.waitforsql; - -import org.apache.hop.junit.rules.RestoreHopEngineEnvironment; -import org.apache.hop.pipeline.transforms.loadsave.validator.IFieldLoadSaveValidator; -import org.apache.hop.pipeline.transforms.loadsave.validator.IntLoadSaveValidator; -import org.apache.hop.workflow.action.loadsave.WorkflowActionLoadSaveTestSupport; -import org.junit.ClassRule; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -public class WorkflowActionWaitForSqlLoadSaveTest - extends WorkflowActionLoadSaveTestSupport { - @ClassRule public static RestoreHopEngineEnvironment env = new RestoreHopEngineEnvironment(); - - @Override - protected Class getActionClass() { - return ActionWaitForSql.class; - } - - @Override - protected List listAttributes() { - return Arrays.asList( - new String[] { - "database", - "schemaName", - "tableName", - "successCondition", - "rowsCountValue", - "isCustomSql", - "isUseVars", - "customSql", - "isAddRowsResult", - "maximumTimeout", - "checkCycleTime", - "successOnTimeout", - "isClearResultList" - }); - } - - @Override - protected Map> createAttributeValidatorsMap() { - Map> validators = new HashMap<>(); - validators.put( - "successCondition", - new IntLoadSaveValidator(ActionWaitForSql.successConditionsCode.length)); - return validators; - } -} diff --git a/plugins/actions/waitforsql/src/test/resources/wait-for-sql-action.xml b/plugins/actions/waitforsql/src/test/resources/wait-for-sql-action.xml new file mode 100644 index 00000000000..496cdaf638f --- /dev/null +++ b/plugins/actions/waitforsql/src/test/resources/wait-for-sql-action.xml @@ -0,0 +1,35 @@ + + + + Wait for SQL + + WAIT_FOR_SQL + + unit-test-db + SCHEMATEST + TABLETEST + rows_count_different + 1 + Y + Y + select FIELD from TABLE + N + 99 + 60 + N + Y + N + 288 + 80 + + \ No newline at end of file From 7b13af91656176927da2002e5a2e1c3de2b2ead0 Mon Sep 17 00:00:00 2001 From: Nicolas Adment <39568358+nadment@users.noreply.github.com> Date: Wed, 10 May 2023 22:47:44 +0200 Subject: [PATCH 2/3] Cleanup XML of action Wait for SQL (no need to resolve) #2000 --- .../actions/waitforsql/ActionWaitForSql.java | 8 ++--- .../waitforsql/ActionWaitForSqlDialog.java | 29 +++++-------------- 2 files changed, 10 insertions(+), 27 deletions(-) diff --git a/plugins/actions/waitforsql/src/main/java/org/apache/hop/workflow/actions/waitforsql/ActionWaitForSql.java b/plugins/actions/waitforsql/src/main/java/org/apache/hop/workflow/actions/waitforsql/ActionWaitForSql.java index 1982c47ca07..d5a78149e92 100644 --- a/plugins/actions/waitforsql/src/main/java/org/apache/hop/workflow/actions/waitforsql/ActionWaitForSql.java +++ b/plugins/actions/waitforsql/src/main/java/org/apache/hop/workflow/actions/waitforsql/ActionWaitForSql.java @@ -205,9 +205,7 @@ protected void checkConnection() throws HopDatabaseException { // check connection // connect and disconnect try { - IHopMetadataProvider metadataProvider = this.getParentWorkflow().getMetadataProvider(); - DatabaseMeta databaseMeta = metadataProvider.getSerializer(DatabaseMeta.class).load(resolve(connection)); - + DatabaseMeta databaseMeta = parentWorkflowMeta.findDatabase(connection, getVariables()); try ( Database database = new Database(this, this, databaseMeta)) { database.connect(); } @@ -370,9 +368,7 @@ protected boolean sqlDataOK( List ar = null; IRowMeta rowMeta = null; - IHopMetadataProvider metadataProvider = this.getParentWorkflow().getMetadataProvider(); - DatabaseMeta databaseMeta = metadataProvider.getSerializer(DatabaseMeta.class).load(resolve(connection)); - + DatabaseMeta databaseMeta = parentWorkflowMeta.findDatabase(connection, getVariables()); try (Database db = new Database(this, this, databaseMeta)) { db.connect(); if (customSqlEnabled) { diff --git a/plugins/actions/waitforsql/src/main/java/org/apache/hop/workflow/actions/waitforsql/ActionWaitForSqlDialog.java b/plugins/actions/waitforsql/src/main/java/org/apache/hop/workflow/actions/waitforsql/ActionWaitForSqlDialog.java index 8cf1ea6a59b..0c90ea86162 100644 --- a/plugins/actions/waitforsql/src/main/java/org/apache/hop/workflow/actions/waitforsql/ActionWaitForSqlDialog.java +++ b/plugins/actions/waitforsql/src/main/java/org/apache/hop/workflow/actions/waitforsql/ActionWaitForSqlDialog.java @@ -17,7 +17,6 @@ package org.apache.hop.workflow.actions.waitforsql; -import org.apache.commons.lang.StringUtils; import org.apache.hop.core.Const; import org.apache.hop.core.Props; import org.apache.hop.core.database.Database; @@ -590,11 +589,7 @@ public void mouseUp(MouseEvent e) { } private void getSql() { - if (StringUtils.isEmpty(wConnection.getText())) { - return; - } - - DatabaseMeta databaseMeta = this.workflowMeta.findDatabase(variables.resolve(wConnection.getText()), variables); + DatabaseMeta databaseMeta = this.workflowMeta.findDatabase(wConnection.getText(), variables); if (databaseMeta != null) { DatabaseExplorerDialog std = new DatabaseExplorerDialog( @@ -753,21 +748,13 @@ private void ok() { } private void getTableName() { - if (StringUtils.isNotEmpty(wConnection.getText())) { - DatabaseMeta databaseMeta = this.workflowMeta.findDatabase(variables.resolve(wConnection.getText()), variables); - if (databaseMeta != null) { - DatabaseExplorerDialog std = new DatabaseExplorerDialog(shell, SWT.NONE, variables, - databaseMeta, getWorkflowMeta().getDatabases()); - std.setSelectedSchemaAndTable(wSchemaname.getText(), wTablename.getText()); - if (std.open()) { - wTablename.setText(Const.NVL(std.getTableName(), "")); - } - } else { - MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ICON_ERROR); - mb.setMessage( - BaseMessages.getString(PKG, "ActionWaitForSQL.ConnectionError2.DialogMessage")); - mb.setText(BaseMessages.getString(PKG, "System.Dialog.Error.Title")); - mb.open(); + DatabaseMeta databaseMeta = workflowMeta.findDatabase(wConnection.getText(), variables); + if (databaseMeta != null) { + DatabaseExplorerDialog std = new DatabaseExplorerDialog(shell, SWT.NONE, variables, + databaseMeta, getWorkflowMeta().getDatabases()); + std.setSelectedSchemaAndTable(wSchemaname.getText(), wTablename.getText()); + if (std.open()) { + wTablename.setText(Const.NVL(std.getTableName(), "")); } } } From af135c5c2cd79b311f83dd47396e710676df41a3 Mon Sep 17 00:00:00 2001 From: Hans Van Akelyen Date: Wed, 17 May 2023 16:34:45 +0200 Subject: [PATCH 3/3] Small cleanup for #2000, and small bugfix for #2923 --- .../main/java/org/apache/hop/run/HopRun.java | 42 ++--- .../actions/waitforsql/ActionWaitForSql.java | 157 +++++++++++------- 2 files changed, 120 insertions(+), 79 deletions(-) diff --git a/engine/src/main/java/org/apache/hop/run/HopRun.java b/engine/src/main/java/org/apache/hop/run/HopRun.java index 03bc5ed499a..b6867369551 100644 --- a/engine/src/main/java/org/apache/hop/run/HopRun.java +++ b/engine/src/main/java/org/apache/hop/run/HopRun.java @@ -101,7 +101,7 @@ public class HopRun implements Runnable, IHasHopMetadataProvider { @Option( names = {"-p", "--parameters"}, description = "A list of PARAMETER=VALUE pairs") - private String parameters = null; + private String[] parameters = null; @Option( names = {"-ps", "--parameters-separator"}, @@ -483,25 +483,27 @@ private void parseParametersAndVariables( if (parameters != null) { parametersSeparator = parametersSeparator == null ? "," : parametersSeparator; - for (String parameter : parameters.split(parametersSeparator)) { - String[] split = parameter.split("=", 2); - String key = split.length > 0 ? split[0] : null; - String value = split.length > 1 ? split[1] : null; - if (value != null && value.startsWith("\"") && value.endsWith("\"")) { - value = value.substring(1, value.length() - 1); - } + for (String parameter : parameters) { + for(String singleParameter : parameter.split(parametersSeparator)){ + String[] split = singleParameter.split("=", 2); + String key = split.length > 0 ? split[0] : null; + String value = split.length > 1 ? split[1] : null; + if (value != null && value.startsWith("\"") && value.endsWith("\"")) { + value = value.substring(1, value.length() - 1); + } - if (key != null) { - // We can work with this. - // - if (Const.indexOfString(key, availableParameters) < 0) { - // A variable - // - configuration.getVariablesMap().put(key, value); - } else { - // A parameter + if (key != null) { + // We can work with this. // - configuration.getParametersMap().put(key, value); + if (Const.indexOfString(key, availableParameters) < 0) { + // A variable + // + configuration.getVariablesMap().put(key, value); + } else { + // A parameter + // + configuration.getParametersMap().put(key, value); + } } } } @@ -674,14 +676,14 @@ public void setHelpRequested(boolean helpRequested) { * * @return value of parameters */ - public String getParameters() { + public String[] getParameters() { return parameters; } /** * @param parameters The parameters to set */ - public void setParameters(String parameters) { + public void setParameters(String[] parameters) { this.parameters = parameters; } diff --git a/plugins/actions/waitforsql/src/main/java/org/apache/hop/workflow/actions/waitforsql/ActionWaitForSql.java b/plugins/actions/waitforsql/src/main/java/org/apache/hop/workflow/actions/waitforsql/ActionWaitForSql.java index d5a78149e92..017cdfccb17 100644 --- a/plugins/actions/waitforsql/src/main/java/org/apache/hop/workflow/actions/waitforsql/ActionWaitForSql.java +++ b/plugins/actions/waitforsql/src/main/java/org/apache/hop/workflow/actions/waitforsql/ActionWaitForSql.java @@ -17,6 +17,8 @@ package org.apache.hop.workflow.actions.waitforsql; +import java.util.ArrayList; +import java.util.List; import org.apache.hop.core.Const; import org.apache.hop.core.ICheckResult; import org.apache.hop.core.Result; @@ -42,8 +44,6 @@ import org.apache.hop.workflow.action.IAction; import org.apache.hop.workflow.action.validator.ActionValidatorUtils; import org.apache.hop.workflow.action.validator.AndValidator; -import java.util.ArrayList; -import java.util.List; /** This defines a Wait for SQL data action */ @Action( @@ -56,11 +56,11 @@ documentationUrl = "/workflow/actions/waitforsql.html") public class ActionWaitForSql extends ActionBase implements Cloneable, IAction { private static final Class PKG = ActionWaitForSql.class; // For Translator - + @HopMetadataProperty(key = "clear_result_rows") private boolean clearResultList; - @HopMetadataProperty(key = "add_rows_result") + @HopMetadataProperty(key = "add_rows_result") private boolean addRowsResult; @HopMetadataProperty(key = "is_usevars") @@ -74,7 +74,7 @@ public class ActionWaitForSql extends ActionBase implements Cloneable, IAction { @HopMetadataProperty(key = "connection") private String connection; - + @HopMetadataProperty(key = "tablename") private String tableName; @@ -84,36 +84,49 @@ public class ActionWaitForSql extends ActionBase implements Cloneable, IAction { /** Maximum timeout in seconds */ @HopMetadataProperty(key = "maximum_timeout") private String maximumTimeout; - + /** Cycle time in seconds */ @HopMetadataProperty(key = "check_cycle_time") - private String checkCycleTime; - + private String checkCycleTime; + @HopMetadataProperty(key = "success_on_timeout") private boolean successOnTimeout; @HopMetadataProperty(key = "rows_count_value") private String rowsCountValue; - + @HopMetadataProperty(key = "success_condition", storeWithCode = true) private SuccessCondition successCondition; - - private static final String selectCount = "SELECT count(*) FROM "; - + + private static final String SELECT_COUNT = "SELECT count(*) FROM "; + public static final int SUCCESS_CONDITION_ROWS_COUNT_EQUAL = 0; public static final int SUCCESS_CONDITION_ROWS_COUNT_DIFFERENT = 1; public static final int SUCCESS_CONDITION_ROWS_COUNT_SMALLER = 2; public static final int SUCCESS_CONDITION_ROWS_COUNT_SMALLER_EQUAL = 3; public static final int SUCCESS_CONDITION_ROWS_COUNT_GREATER = 4; public static final int SUCCESS_CONDITION_ROWS_COUNT_GREATER_EQUAL = 5; - + public enum SuccessCondition implements IEnumHasCodeAndDescription { - ROWS_COUNT_EQUAL("rows_count_equal", BaseMessages.getString(PKG, "ActionWaitForSQL.SuccessWhenRowCountEqual.Label")), - ROWS_COUNT_DIFFERENT("rows_count_different",BaseMessages.getString(PKG, "ActionWaitForSQL.SuccessWhenRowCountDifferent.Label")), - ROWS_COUNT_SMALLER("rows_count_smaller", BaseMessages.getString(PKG, "ActionWaitForSQL.SuccessWhenRowCountSmallerThan.Label")), - ROWS_COUNT_SMALLER_EQUAL("rows_count_smaller_equal", BaseMessages.getString(PKG, "ActionWaitForSQL.SuccessWhenRowCountSmallerOrEqualThan.Label")), - ROWS_COUNT_GREATER("rows_count_greater", BaseMessages.getString(PKG, "ActionWaitForSQL.SuccessWhenRowCountGreaterThan.Label")), - ROWS_COUNT_GREATER_EQUAL("rows_count_greater_equal", BaseMessages.getString(PKG, "ActionWaitForSQL.SuccessWhenRowCountGreaterOrEqual.Label")); + ROWS_COUNT_EQUAL( + "rows_count_equal", + BaseMessages.getString(PKG, "ActionWaitForSQL.SuccessWhenRowCountEqual.Label")), + ROWS_COUNT_DIFFERENT( + "rows_count_different", + BaseMessages.getString(PKG, "ActionWaitForSQL.SuccessWhenRowCountDifferent.Label")), + ROWS_COUNT_SMALLER( + "rows_count_smaller", + BaseMessages.getString(PKG, "ActionWaitForSQL.SuccessWhenRowCountSmallerThan.Label")), + ROWS_COUNT_SMALLER_EQUAL( + "rows_count_smaller_equal", + BaseMessages.getString( + PKG, "ActionWaitForSQL.SuccessWhenRowCountSmallerOrEqualThan.Label")), + ROWS_COUNT_GREATER( + "rows_count_greater", + BaseMessages.getString(PKG, "ActionWaitForSQL.SuccessWhenRowCountGreaterThan.Label")), + ROWS_COUNT_GREATER_EQUAL( + "rows_count_greater_equal", + BaseMessages.getString(PKG, "ActionWaitForSQL.SuccessWhenRowCountGreaterOrEqual.Label")); private final String code; private final String description; @@ -128,7 +141,8 @@ public static String[] getDescriptions() { } public static SuccessCondition lookupDescription(String description) { - return IEnumHasCodeAndDescription.lookupDescription(SuccessCondition.class, description, ROWS_COUNT_EQUAL); + return IEnumHasCodeAndDescription.lookupDescription( + SuccessCondition.class, description, ROWS_COUNT_EQUAL); } public static SuccessCondition lookupCode(String code) { @@ -156,8 +170,8 @@ public String getDescription() { } } - private static String DEFAULT_MAXIMUM_TIMEOUT = "0"; // infinite timeout - private static String DEFAULT_CHECK_CYCLE_TIME = "60"; // 1 minute + private static final String DEFAULT_MAXIMUM_TIMEOUT = "0"; // infinite timeout + private static final String DEFAULT_CHECK_CYCLE_TIME = "60"; // 1 minute public ActionWaitForSql(String n) { super(n, ""); @@ -182,8 +196,7 @@ public ActionWaitForSql() { @Override public Object clone() { - ActionWaitForSql je = (ActionWaitForSql) super.clone(); - return je; + return super.clone(); } public SuccessCondition getSuccessCondition() { @@ -206,7 +219,7 @@ protected void checkConnection() throws HopDatabaseException { // connect and disconnect try { DatabaseMeta databaseMeta = parentWorkflowMeta.findDatabase(connection, getVariables()); - try ( Database database = new Database(this, this, databaseMeta)) { + try (Database database = new Database(this, this, databaseMeta)) { database.connect(); } } catch (HopException e) { @@ -367,7 +380,7 @@ protected boolean sqlDataOK( boolean successOK = false; List ar = null; IRowMeta rowMeta = null; - + DatabaseMeta databaseMeta = parentWorkflowMeta.findDatabase(connection, getVariables()); try (Database db = new Database(this, this, databaseMeta)) { db.connect(); @@ -376,11 +389,11 @@ protected boolean sqlDataOK( } else { if (!Utils.isEmpty(realSchemaName)) { countStatement = - selectCount + SELECT_COUNT + db.getDatabaseMeta() .getQuotedSchemaTableCombination(this, realSchemaName, realTableName); } else { - countStatement = selectCount + db.getDatabaseMeta().quoteField(realTableName); + countStatement = SELECT_COUNT + db.getDatabaseMeta().quoteField(realTableName); } } @@ -436,7 +449,7 @@ protected boolean sqlDataOK( break; } } // end if countStatement!=null - + if (addRowsResult && customSqlEnabled && ar != null) { rowMeta = db.getQueryFields(countStatement, false); } @@ -445,16 +458,14 @@ protected boolean sqlDataOK( BaseMessages.getString(PKG, "ActionWaitForSQL.Error.RunningEntry", dbe.getMessage())); } - if (successOK) { - // ad rows to result - if (addRowsResult && customSqlEnabled && ar != null) { - List rows = new ArrayList<>(); - for (int i = 0; i < ar.size(); i++) { - rows.add(new RowMetaAndData(rowMeta, ar.get(i))); - } - if (rows != null) { - result.getRows().addAll(rows); - } + // ad rows to result + if (successOK && addRowsResult && customSqlEnabled && ar != null) { + List rows = new ArrayList<>(); + for (int i = 0; i < ar.size(); i++) { + rows.add(new RowMetaAndData(rowMeta, ar.get(i))); + } + if (rows != null) { + result.getRows().addAll(rows); } } return successOK; @@ -466,23 +477,26 @@ public DatabaseMeta[] getUsedDatabaseConnections() { } @Override - public List getResourceDependencies(IVariables variables, - WorkflowMeta workflowMeta) { + public List getResourceDependencies( + IVariables variables, WorkflowMeta workflowMeta) { List references = super.getResourceDependencies(variables, workflowMeta); DatabaseMeta databaseMeta = null; try { IHopMetadataProvider metadataProvider = workflowMeta.getMetadataProvider(); - databaseMeta = metadataProvider.getSerializer(DatabaseMeta.class).load(variables.resolve(connection)); + databaseMeta = + metadataProvider.getSerializer(DatabaseMeta.class).load(variables.resolve(connection)); } catch (HopException e) { // Ignore error loading metadata } if (databaseMeta != null) { ResourceReference reference = new ResourceReference(this); - reference.getEntries() + reference + .getEntries() .add(new ResourceEntry(databaseMeta.getHostname(), ResourceType.SERVER)); - reference.getEntries() + reference + .getEntries() .add(new ResourceEntry(databaseMeta.getDatabaseName(), ResourceType.DATABASENAME)); references.add(reference); } @@ -513,7 +527,9 @@ public boolean isClearResultList() { return clearResultList; } - /** @param isClearResultList The isClearResultList to set */ + /** + * @param isClearResultList The isClearResultList to set + */ public void setClearResultList(boolean isClearResultList) { this.clearResultList = isClearResultList; } @@ -527,7 +543,9 @@ public boolean isAddRowsResult() { return addRowsResult; } - /** @param isAddRowsResult The isAddRowsResult to set */ + /** + * @param isAddRowsResult The isAddRowsResult to set + */ public void setAddRowsResult(boolean isAddRowsResult) { this.addRowsResult = isAddRowsResult; } @@ -541,7 +559,9 @@ public boolean isUseVars() { return useVars; } - /** @param isUseVars The isUseVars to set */ + /** + * @param isUseVars The isUseVars to set + */ public void setUseVars(boolean isUseVars) { this.useVars = isUseVars; } @@ -555,7 +575,9 @@ public boolean isCustomSqlEnabled() { return customSqlEnabled; } - /** @param isCustomSql The isCustomSql to set */ + /** + * @param isCustomSql The isCustomSql to set + */ public void setCustomSqlEnabled(boolean isCustomSql) { this.customSqlEnabled = isCustomSql; } @@ -569,7 +591,9 @@ public String getCustomSql() { return customSql; } - /** @param customSql The customSql to set */ + /** + * @param customSql The customSql to set + */ public void setCustomSql(String customSql) { this.customSql = customSql; } @@ -583,7 +607,9 @@ public String getTableName() { return tableName; } - /** @param tableName The tableName to set */ + /** + * @param tableName The tableName to set + */ public void setTableName(String tableName) { this.tableName = tableName; } @@ -597,7 +623,9 @@ public String getSchemaName() { return schemaName; } - /** @param schemaName The schemaName to set */ + /** + * @param schemaName The schemaName to set + */ public void setSchemaName(String schemaName) { this.schemaName = schemaName; } @@ -611,7 +639,9 @@ public String getMaximumTimeout() { return maximumTimeout; } - /** @param maximumTimeout The maximumTimeout to set */ + /** + * @param maximumTimeout The maximumTimeout to set + */ public void setMaximumTimeout(String maximumTimeout) { this.maximumTimeout = maximumTimeout; } @@ -625,7 +655,9 @@ public String getCheckCycleTime() { return checkCycleTime; } - /** @param checkCycleTime The checkCycleTime to set */ + /** + * @param checkCycleTime The checkCycleTime to set + */ public void setCheckCycleTime(String checkCycleTime) { this.checkCycleTime = checkCycleTime; } @@ -639,7 +671,9 @@ public boolean isSuccessOnTimeout() { return successOnTimeout; } - /** @param successOnTimeout The successOnTimeout to set */ + /** + * @param successOnTimeout The successOnTimeout to set + */ public void setSuccessOnTimeout(boolean successOnTimeout) { this.successOnTimeout = successOnTimeout; } @@ -650,10 +684,9 @@ public void setSuccessOnTimeout(boolean successOnTimeout) { * @return value of selectCount */ public static String getSelectCount() { - return selectCount; + return SELECT_COUNT; } - /** * Gets rowsCountValue * @@ -663,12 +696,16 @@ public String getRowsCountValue() { return rowsCountValue; } - /** @param rowsCountValue The rowsCountValue to set */ + /** + * @param rowsCountValue The rowsCountValue to set + */ public void setRowsCountValue(String rowsCountValue) { this.rowsCountValue = rowsCountValue; } - /** @param successCondition The successCondition to set */ + /** + * @param successCondition The successCondition to set + */ public void setSuccessCondition(SuccessCondition successCondition) { this.successCondition = successCondition; } @@ -682,7 +719,9 @@ public String getConnection() { return connection; } - /** @param connection The connection to set */ + /** + * @param connection The connection to set + */ public void setConnection(String connection) { this.connection = connection; }