Skip to content

Commit

Permalink
Minor cleanup #1989
Browse files Browse the repository at this point in the history
  • Loading branch information
hansva committed May 27, 2023
1 parent 4fd40d9 commit 8396aee
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@

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

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.apache.hop.core.ICheckResult;
import org.apache.hop.core.Result;
import org.apache.hop.core.annotations.Action;
Expand All @@ -41,12 +47,6 @@
import org.apache.hop.workflow.action.validator.AndValidator;
import org.apache.hop.workflow.action.validator.ValidatorContext;
import org.apache.hop.workflow.engine.IWorkflowEngine;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/** This defines a 'Set variables' action. */
@Action(
Expand All @@ -63,9 +63,9 @@ public class ActionSetVariables extends ActionBase implements Cloneable, IAction
@HopMetadataProperty(key = "replacevars")
private boolean replaceVars;

@HopMetadataProperty(groupKey = "fields", key = "field")
@HopMetadataProperty(groupKey = "fields", key = "field")
private List<VariableDefinition> variableDefinitions;

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

Expand All @@ -74,12 +74,13 @@ public class ActionSetVariables extends ActionBase implements Cloneable, IAction

public enum VariableType implements IEnumHasCodeAndDescription {
JVM(BaseMessages.getString(PKG, "ActionSetVariables.VariableType.JVM")),
CURRENT_WORKFLOW(BaseMessages.getString(PKG, "ActionSetVariables.VariableType.CurrentWorkflow")),
CURRENT_WORKFLOW(
BaseMessages.getString(PKG, "ActionSetVariables.VariableType.CurrentWorkflow")),
PARENT_WORKFLOW(BaseMessages.getString(PKG, "ActionSetVariables.VariableType.ParentWorkflow")),
ROOT_WORKFLOW(BaseMessages.getString(PKG, "ActionSetVariables.VariableType.RootWorkflow"));

private final String description;

VariableType(String description) {
this.description = description;
}
Expand All @@ -106,13 +107,12 @@ public String getDescription() {
return description;
}
}


public static final class VariableDefinition {

public static final class VariableDefinition {
public VariableDefinition() {
super();
}

public VariableDefinition(String name, String value, VariableType type) {
super();
this.name = name;
Expand All @@ -125,14 +125,14 @@ public VariableDefinition(String name, String value, VariableType type) {

@HopMetadataProperty(key = "variable_value")
private String value;

@HopMetadataProperty(key = "variable_type")
private VariableType type;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
Expand All @@ -153,7 +153,7 @@ public void setType(VariableType type) {
this.type = type;
}
}

public ActionSetVariables(String n) {
super(n, "");
replaceVars = true;
Expand Down Expand Up @@ -195,7 +195,9 @@ public Result execute(Result result, int nr) throws HopException {
Properties properties = new Properties();
properties.load(reader);
for (Object key : properties.keySet()) {
definitions.add(new VariableDefinition((String) key, (String) properties.get(key), fileVariableType));
definitions.add(
new VariableDefinition(
(String) key, (String) properties.get(key), fileVariableType));
}
} catch (Exception e) {
throw new HopException(
Expand Down Expand Up @@ -272,7 +274,7 @@ public Result execute(Result result, int nr) throws HopException {
// if parameter, save the initial parameter value for use in reset/clear variables
// in future calls
if (parameterValue != null
&& parameterValue != value
&& !parameterValue.equals(value)
&& !entryTransformSetVariablesMap.containsKey(name)) {
setEntryTransformSetVariable(name, parameterValue);
}
Expand Down Expand Up @@ -362,7 +364,7 @@ public void check(
remarks,
AndValidator.putValidators(ActionValidatorUtils.notNullValidator()));

if (res == false) {
if (!res) {
return;
}

Expand All @@ -376,34 +378,42 @@ public void check(
public List<ResourceReference> getResourceDependencies(
IVariables variables, WorkflowMeta workflowMeta) {
List<ResourceReference> references = super.getResourceDependencies(variables, workflowMeta);
String realFilename = resolve(this.filename);

String realFilename = resolve(this.filename);
if (!Utils.isEmpty(realFilename)) {
ResourceReference reference = new ResourceReference(this);
references.add(reference);
references.add(reference);
reference.getEntries().add(new ResourceEntry(realFilename, ResourceType.FILE));
}

return references;
}

/** @return the filename */
/**
* @return the filename
*/
@Override
public String getFilename() {
return filename;
}

/** @param filename the filename to set */
/**
* @param filename the filename to set
*/
public void setFilename(String filename) {
this.filename = filename;
}

/** @return the fileVariableType */
/**
* @return the fileVariableType
*/
public VariableType getFileVariableType() {
return fileVariableType;
}

/** @param scope the fileVariableType to set */
/**
* @param scope the fileVariableType to set
*/
public void setFileVariableType(VariableType scope) {
this.fileVariableType = scope;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

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

import java.util.ArrayList;
import java.util.List;
import org.apache.hop.core.Const;
import org.apache.hop.core.util.Utils;
import org.apache.hop.core.variables.IVariables;
Expand Down Expand Up @@ -47,8 +49,6 @@
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import java.util.ArrayList;
import java.util.List;

/** This dialog allows you to edit the Set variables action settings. */
public class ActionSetVariablesDialog extends ActionDialog implements IActionDialog {
Expand Down Expand Up @@ -242,7 +242,9 @@ public IAction open() {
false),
new ColumnInfo(
BaseMessages.getString(PKG, "ActionSetVariables.Fields.Column.VariableType"),
ColumnInfo.COLUMN_TYPE_CCOMBO,VariableType.getDescriptions(), false),
ColumnInfo.COLUMN_TYPE_CCOMBO,
VariableType.getDescriptions(),
false),
};
colinf[0].setUsingVariables(true);
colinf[1].setUsingVariables(true);
Expand Down Expand Up @@ -323,11 +325,11 @@ private void ok() {
if (name != null && name.length() != 0) {
String value = wFields.getNonEmpty(i).getText(2);
VariableType scope = VariableType.lookupDescription(wFields.getNonEmpty(i).getText(3));
list.add(new VariableDefinition(name, value, scope));
list.add(new VariableDefinition(name, value, scope));
}
}
action.setVariableDefinitions(list);

dispose();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.nio.charset.StandardCharsets;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.hop.core.HopClientEnvironment;
import org.apache.hop.core.Result;
import org.apache.hop.core.logging.HopLogStore;
Expand All @@ -45,15 +55,6 @@
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.nio.charset.StandardCharsets;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;

public class ActionSetVariablesTest {
private IWorkflowEngine<WorkflowMeta> workflow;
Expand Down Expand Up @@ -91,10 +92,10 @@ public void testSerialization() throws Exception {
// Properties file variables
assertEquals(VariableType.CURRENT_WORKFLOW, action.getFileVariableType());
assertEquals("filename.txt", action.getFilename());

// Settings
assertTrue(action.isReplaceVars());

// Static variables
VariableDefinition definition = action.getVariableDefinitions().get(0);
assertEquals("VAR_CURRENT", definition.getName());
Expand All @@ -113,8 +114,7 @@ public void testSerialization() throws Exception {
assertEquals("jvm", definition.getValue());
assertEquals(VariableType.JVM, definition.getType());
}



@Test
public void testASCIIText() throws Exception {
// properties file with native2ascii
Expand Down

0 comments on commit 8396aee

Please sign in to comment.