Skip to content

Commit

Permalink
Merge pull request #2956 from sramazzina/1995
Browse files Browse the repository at this point in the history
fix #1995
  • Loading branch information
hansva authored May 27, 2023
2 parents e64a5f9 + fe256e0 commit 12d4c73
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@

package org.apache.hop.workflow.actions.tableexists;

import java.util.List;
import org.apache.hop.core.ICheckResult;
import org.apache.hop.core.Result;
import org.apache.hop.core.annotations.Action;
import org.apache.hop.core.database.Database;
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.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.IHopMetadataProvider;
import org.apache.hop.resource.ResourceEntry;
import org.apache.hop.resource.ResourceEntry.ResourceType;
Expand All @@ -37,9 +37,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.List;

/** This defines a table exists action. */
@Action(
Expand All @@ -53,13 +50,18 @@
public class ActionTableExists extends ActionBase implements Cloneable, IAction {
private static final Class<?> PKG = ActionTableExists.class; // For Translator

@HopMetadataProperty(key = "tablename")
private String tableName;
private String schemaname;
private DatabaseMeta connection;

@HopMetadataProperty(key = "schemaname")
private String schemaName;

@HopMetadataProperty(key = "connection")
private String connection;

public ActionTableExists(String n) {
super(n, "");
schemaname = null;
schemaName = null;
tableName = null;
connection = null;
}
Expand All @@ -74,59 +76,28 @@ public Object clone() {
return je;
}

@Override
public String getXml() {
StringBuilder retval = new StringBuilder(200);

retval.append(super.getXml());

retval.append(" ").append(XmlHandler.addTagValue("tablename", tableName));
retval.append(" ").append(XmlHandler.addTagValue("schemaname", schemaname));
retval
.append(" ")
.append(
XmlHandler.addTagValue("connection", connection == null ? null : connection.getName()));

return retval.toString();
}

@Override
public void loadXml(Node entrynode, IHopMetadataProvider metadataProvider, IVariables variables)
throws HopXmlException {
try {
super.loadXml(entrynode);

tableName = XmlHandler.getTagValue(entrynode, "tablename");
schemaname = XmlHandler.getTagValue(entrynode, "schemaname");
String dbname = XmlHandler.getTagValue(entrynode, "connection");
connection = DatabaseMeta.loadDatabase(metadataProvider, dbname);
} catch (HopException e) {
throw new HopXmlException(BaseMessages.getString(PKG, "TableExists.Meta.UnableLoadXml"), e);
}
}

public void setTablename(String tableName) {
public void setTableName(String tableName) {
this.tableName = tableName;
}

public String getTablename() {
public String getTableName() {
return tableName;
}

public String getSchemaname() {
return schemaname;
public String getSchemaName() {
return schemaName;
}

public void setSchemaname(String schemaname) {
this.schemaname = schemaname;
public void setSchemaName(String schemaName) {
this.schemaName = schemaName;
}

public void setDatabase(DatabaseMeta database) {
this.connection = database;
public String getConnection() {
return connection;
}

public DatabaseMeta getDatabase() {
return connection;
public void setConnection(String connection) {
this.connection = connection;
}

@Override
Expand All @@ -144,33 +115,38 @@ public Result execute(Result previousResult, int nr) {
Result result = previousResult;
result.setResult(false);

if (connection != null) {
Database db = new Database(this, this, connection);
try {
db.connect();
String realTablename = resolve(tableName);
String realSchemaname = resolve(schemaname);

if (db.checkTableExists(realSchemaname, realTablename)) {
if (log.isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "TableExists.Log.TableExists", realTablename));
if (!Utils.isEmpty(connection)) {
DatabaseMeta dbMeta = parentWorkflowMeta.findDatabase(connection, getVariables());
if (dbMeta != null) {
Database db = new Database(this, this, dbMeta);
try {
db.connect();
String realTableName = resolve(tableName);
String realSchemaName = resolve(schemaName);

if (db.checkTableExists(realSchemaName, realTableName)) {
if (log.isDetailed()) {
logDetailed(
BaseMessages.getString(PKG, "TableExists.Log.TableExists", realTableName));
}
result.setResult(true);
} else {
if (log.isDetailed()) {
logDetailed(
BaseMessages.getString(PKG, "TableExists.Log.TableNotExists", realTableName));
}
}
result.setResult(true);
} else {
if (log.isDetailed()) {
logDetailed(
BaseMessages.getString(PKG, "TableExists.Log.TableNotExists", realTablename));
}
}
} catch (HopDatabaseException dbe) {
result.setNrErrors(1);
logError(BaseMessages.getString(PKG, "TableExists.Error.RunningAction", dbe.getMessage()));
} finally {
if (db != null) {
try {
db.disconnect();
} catch (Exception e) {
/* Ignore */
} catch (HopDatabaseException dbe) {
result.setNrErrors(1);
logError(
BaseMessages.getString(PKG, "TableExists.Error.RunningAction", dbe.getMessage()));
} finally {
if (db != null) {
try {
db.disconnect();
} catch (Exception e) {
/* Ignore */
}
}
}
}
Expand All @@ -182,23 +158,17 @@ public Result execute(Result previousResult, int nr) {
return result;
}

@Override
public DatabaseMeta[] getUsedDatabaseConnections() {
return new DatabaseMeta[] {
connection,
};
}

@Override
public List<ResourceReference> getResourceDependencies(
IVariables variables, WorkflowMeta workflowMeta) {
List<ResourceReference> references = super.getResourceDependencies(variables, workflowMeta);
if (connection != null) {
DatabaseMeta dbMeta = parentWorkflowMeta.findDatabase(connection, getVariables());
if (dbMeta != null) {
ResourceReference reference = new ResourceReference(this);
reference.getEntries().add(new ResourceEntry(connection.getHostname(), ResourceType.SERVER));
reference.getEntries().add(new ResourceEntry(dbMeta.getHostname(), ResourceType.SERVER));
reference
.getEntries()
.add(new ResourceEntry(connection.getDatabaseName(), ResourceType.DATABASENAME));
.add(new ResourceEntry(dbMeta.getDatabaseName(), ResourceType.DATABASENAME));
references.add(reference);
}
return references;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,17 @@ public IAction open() {
wName.setLayoutData(fdName);

// Connection line
wConnection = addConnectionLine(shell, wName, action.getDatabase(), lsMod);
wConnection = addConnectionLine(shell, wName, action.getConnection(), lsMod);

// Schema name line
Label wlSchemaname = new Label(shell, SWT.RIGHT);
wlSchemaname.setText(BaseMessages.getString(PKG, "ActionTableExists.Schemaname.Label"));
PropsUi.setLook(wlSchemaname);
FormData fdlSchemaname = new FormData();
fdlSchemaname.left = new FormAttachment(0, 0);
fdlSchemaname.right = new FormAttachment(middle, -margin);
fdlSchemaname.top = new FormAttachment(wConnection, 2 * margin);
wlSchemaname.setLayoutData(fdlSchemaname);
Label wlSchemaName = new Label(shell, SWT.RIGHT);
wlSchemaName.setText(BaseMessages.getString(PKG, "ActionTableExists.Schemaname.Label"));
PropsUi.setLook(wlSchemaName);
FormData fdlSchemaName = new FormData();
fdlSchemaName.left = new FormAttachment(0, 0);
fdlSchemaName.right = new FormAttachment(middle, -margin);
fdlSchemaName.top = new FormAttachment(wConnection, 2 * margin);
wlSchemaName.setLayoutData(fdlSchemaName);

Button wbSchema = new Button(shell, SWT.PUSH | SWT.CENTER);
PropsUi.setLook(wbSchema);
Expand Down Expand Up @@ -209,10 +209,10 @@ public void widgetSelected(SelectionEvent e) {
/** Copy information from the meta-data input to the dialog fields. */
public void getData() {
wName.setText(Const.nullToEmpty(action.getName()));
wTablename.setText(Const.nullToEmpty(action.getTablename()));
wSchemaname.setText(Const.nullToEmpty(action.getSchemaname()));
if (action.getDatabase() != null) {
wConnection.setText(action.getDatabase().getName());
wTablename.setText(Const.nullToEmpty(action.getTableName()));
wSchemaname.setText(Const.nullToEmpty(action.getSchemaName()));
if (action.getConnection() != null) {
wConnection.setText(action.getConnection());
}

wName.selectAll();
Expand All @@ -234,9 +234,9 @@ private void ok() {
return;
}
action.setName(wName.getText());
action.setDatabase(getWorkflowMeta().findDatabase(wConnection.getText(), variables));
action.setTablename(wTablename.getText());
action.setSchemaname(wSchemaname.getText());
action.setConnection(wConnection.getText());
action.setTableName(wTablename.getText());
action.setSchemaName(wSchemaname.getText());

dispose();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
ActionTableExists.Name=La tabella esiste
TableExists.Meta.UnableLoadXml=Impossibile caricare la action di tipo 'tabella esiste' dal nodo XML
ActionTableExists.Tablename.Label=Nome tabella\:
TableExists.Error.RunningAction=Si \u00E8 verificato un errore durante l''esecuzione di questo transform\: {0}
TableExists.Error.NoConnectionDefined=Nessuna connessione di database \u00E8 stata definita.
TableExists.Log.TableNotExists=La tabella [{0}] nion esiste\!

ActionTableExists.Tablename.Label=Tabella\:
TableExists.Error.RunningAction=Si \u00E8 verificato un errore durante l''esecuzione di questa action\: {0}
TableExists.Error.NoConnectionDefined=Non \u00E8 stata definita alcuna connessione al database .
TableExists.Log.TableNotExists=La tabella [{0}] non esiste\!
TableExists.Log.TableExists=La tabella [{0}] esiste.
ActionTableExists.Title=La tabella esiste
ActionTableExists.Title=Table exists
ActionTableExists.Name.Label=Nome action\:
ActionTableExists.Schemaname.Label=Nome dello schema\:
ActionTableExists.Name.Default=La tabella esiste
ActionTableExists.Schemaname.Label=Schema\:
ActionTableExists.Name.Default=Table exists
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ protected Class<ActionTableExists> getActionClass() {

@Override
protected List<String> listAttributes() {
return Arrays.asList("tablename", "schemaname", "database");
return Arrays.asList("tableName", "schemaName", "connection");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1266,7 +1266,7 @@ public MetaSelectionLine<DatabaseMeta> addConnectionLine(
mb.open();
}
}
return addConnectionLine(shell, wTransformName, databaseMeta, lsMod);
return addConnectionLine(parent, previous, databaseMeta, lsMod);
}

public interface IFieldsChoiceDialogProvider {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.hop.metadata.api.IHopMetadataProvider;
import org.apache.hop.pipeline.transform.ITransform;
import org.apache.hop.ui.core.PropsUi;
import org.apache.hop.ui.core.dialog.MessageBox;
import org.apache.hop.ui.core.gui.WindowProperty;
import org.apache.hop.ui.core.widget.MetaSelectionLine;
import org.apache.hop.workflow.WorkflowMeta;
Expand Down Expand Up @@ -127,6 +128,36 @@ public MetaSelectionLine<DatabaseMeta> addConnectionLine(
return wConnection;
}

/**
* Adds the connection line for the given parent and previous control, and returns a meta
* selection manager control
*
* @param parent the parent composite object
* @param previous the previous control
* @param connection
* @param lsMod
* @return the combo box UI component
*/
public MetaSelectionLine<DatabaseMeta> addConnectionLine(
Composite parent, Control previous, String connection, ModifyListener lsMod) {

DatabaseMeta databaseMeta = getWorkflowMeta().findDatabase(connection, variables);
// If we are unable to find the database metadata, display only a warning message so that the
// user
// can proceed to correct the issue in the affected pipeline
if (databaseMeta == null) {
MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ICON_WARNING);
mb.setMessage(
BaseMessages.getString(
PKG,
"BaseTransformDialog.InvalidConnection.DialogMessage",
variables.resolve(connection)));
mb.setText(BaseMessages.getString(PKG, "BaseTransformDialog.InvalidConnection.DialogTitle"));
mb.open();
}
return addConnectionLine(parent, previous, databaseMeta, lsMod);
}

public IHopMetadataProvider getMetadataProvider() {
return metadataProvider;
}
Expand Down

0 comments on commit 12d4c73

Please sign in to comment.