Skip to content

Commit

Permalink
fix apache#2908 Enhanced JSON Output error when does not receive any …
Browse files Browse the repository at this point in the history
…rows
  • Loading branch information
sramazzina authored and hansva committed May 17, 2023
1 parent 8c29029 commit 3eab7e2
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,11 @@ public boolean init() {
// Init global json items array only if output to file is needed
data.jsonItems = new ArrayList<>();
data.isWriteToFile = true;
if (!meta.isDoNotOpenNewFileInit()) {
if (data.isWriteToFile && !openNewFile()) {
logError(BaseMessages.getString(PKG, "JsonOutput.Error.OpenNewFile", buildFilename()));
stopAll();
setErrors(1);
return false;
}
if (!meta.isDoNotOpenNewFileInit() && data.isWriteToFile && !openNewFile()) {
logError(BaseMessages.getString(PKG, "JsonOutput.Error.OpenNewFile", buildFilename()));
stopAll();
setErrors(1);
return false;
}
}

Expand All @@ -107,7 +105,7 @@ public boolean processRow() throws HopException {
if (r == null) {
// no more input to be expected...
// Let's output the remaining unsafe data
outPutRow(prevRow);
outputRow(prevRow);
// only attempt writing to file when the first row is not empty
if (data.isWriteToFile && !first) writeJsonFile();
setOutputDone();
Expand All @@ -133,7 +131,7 @@ public void manageRowItems(Object[] row) throws HopException {
if (!sameGroup(prevRow, row) && data.jsonKeyGroupItems.size() > 0) {
// Output the new row
logDebug("Record Num: " + data.nrRow + " - Generating JSON chunk");
outPutRow(prevRow);
outputRow(prevRow);
data.jsonKeyGroupItems = new ArrayList<>();
}

Expand Down Expand Up @@ -190,7 +188,8 @@ else if (!outputField.isRemoveIfBlank())
itemNode.put(getJsonAttributeName(outputField), jsonNode);
}
} catch (IOException e) {
throw new HopTransformException(BaseMessages.getString(PKG, "JsonOutput.Error.Casting"), e);
throw new HopTransformException(
BaseMessages.getString(PKG, "JsonOutput.Error.Casting"), e);
}
} else {
itemNode.put(getJsonAttributeName(outputField), value);
Expand Down Expand Up @@ -228,7 +227,7 @@ private String getKeyJsonAttributeName(JsonOutputKeyField field) {
}

@SuppressWarnings("unchecked")
private void outPutRow(Object[] rowData) throws HopException {
private void outputRow(Object[] rowData) throws HopException {
// We can now output an object
ObjectNode globalItemNode = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ public class JsonOutputDialog extends BaseTransformDialog implements ITransformD
private static final Class<?> PKG = JsonOutputMeta.class; // needed by Translator!!

public static final String STRING_SORT_WARNING_PARAMETER = "JSONSortWarning";
public static final String SYSTEM_COMBO_NO = "System.Combo.No"
public static final String SYSTEM_COMBO_YES = "System.Combo.Yes"

private Label wlEncoding;
private ComboVar wEncoding;
Expand All @@ -96,7 +98,7 @@ public class JsonOutputDialog extends BaseTransformDialog implements ITransformD
private Label wlAddToResult;
private Button wAddToResult;

private Group wFileName;
private Group wFilenameGroup;

private Label wlFilename;
private Button wbFilename;
Expand Down Expand Up @@ -346,24 +348,24 @@ public void widgetSelected(SelectionEvent e) {
BaseMessages.getString(PKG, "JsonOutputDialog.JSONFragment.Column"),
ColumnInfo.COLUMN_TYPE_CCOMBO,
new String[] {
BaseMessages.getString(PKG, "System.Combo.Yes"),
BaseMessages.getString(PKG, "System.Combo.No")
BaseMessages.getString(PKG, SYSTEM_COMBO_YES),
BaseMessages.getString(PKG, SYSTEM_COMBO_NO)
},
true),
new ColumnInfo(
BaseMessages.getString(PKG, "JsonOutputDialog.NoEnclosure.Column"),
ColumnInfo.COLUMN_TYPE_CCOMBO,
new String[] {
BaseMessages.getString(PKG, "System.Combo.Yes"),
BaseMessages.getString(PKG, "System.Combo.No")
BaseMessages.getString(PKG, SYSTEM_COMBO_YES),
BaseMessages.getString(PKG, SYSTEM_COMBO_NO)
},
false),
new ColumnInfo(
BaseMessages.getString(PKG, "JsonOutputDialog.RemoveIfBlank.Column"),
ColumnInfo.COLUMN_TYPE_CCOMBO,
new String[] {
BaseMessages.getString(PKG, "System.Combo.Yes"),
BaseMessages.getString(PKG, "System.Combo.No")
BaseMessages.getString(PKG, SYSTEM_COMBO_YES),
BaseMessages.getString(PKG, SYSTEM_COMBO_NO)
},
true),
};
Expand Down Expand Up @@ -611,17 +613,17 @@ private void createFilenameGroup(
// START OF FileName GROUP
//

wFileName = new Group(wGeneralComp, SWT.SHADOW_NONE);
PropsUi.setLook(wFileName);
wFileName.setText(BaseMessages.getString(PKG, "JsonOutputDialog.Group.File.Label"));
wFilenameGroup = new Group(wGeneralComp, SWT.SHADOW_NONE);
PropsUi.setLook(wFilenameGroup);
wFilenameGroup.setText(BaseMessages.getString(PKG, "JsonOutputDialog.Group.File.Label"));

FormLayout groupfilenameayout = new FormLayout();
groupfilenameayout.marginWidth = 10;
groupfilenameayout.marginHeight = 10;
wFileName.setLayout(groupfilenameayout);
wFilenameGroup.setLayout(groupfilenameayout);

// Filename line
wlFilename = new Label(wFileName, SWT.RIGHT);
wlFilename = new Label(wFilenameGroup, SWT.RIGHT);
wlFilename.setText(BaseMessages.getString(PKG, "JsonOutputDialog.Filename.Label"));
PropsUi.setLook(wlFilename);
FormData fdlFilename = new FormData();
Expand All @@ -630,7 +632,7 @@ private void createFilenameGroup(
fdlFilename.right = new FormAttachment(middle, -margin);
wlFilename.setLayoutData(fdlFilename);

wbFilename = new Button(wFileName, SWT.PUSH | SWT.CENTER);
wbFilename = new Button(wFilenameGroup, SWT.PUSH | SWT.CENTER);
PropsUi.setLook(wbFilename);
wbFilename.setText(BaseMessages.getString(PKG, "System.Button.Browse"));
FormData fdbFilename = new FormData();
Expand All @@ -653,7 +655,7 @@ private void createFilenameGroup(
},
true));

wFilename = new TextVar(variables, wFileName, SWT.SINGLE | SWT.LEFT | SWT.BORDER);
wFilename = new TextVar(variables, wFilenameGroup, SWT.SINGLE | SWT.LEFT | SWT.BORDER);
PropsUi.setLook(wFilename);
wFilename.addModifyListener(lsMod);
FormData fdFilename = new FormData();
Expand All @@ -663,15 +665,15 @@ private void createFilenameGroup(
wFilename.setLayoutData(fdFilename);

// Append to end of file?
wlAppend = new Label(wFileName, SWT.RIGHT);
wlAppend = new Label(wFilenameGroup, SWT.RIGHT);
wlAppend.setText(BaseMessages.getString(PKG, "JsonOutputDialog.Append.Label"));
PropsUi.setLook(wlAppend);
FormData fdlAppend = new FormData();
fdlAppend.left = new FormAttachment(0, 0);
fdlAppend.top = new FormAttachment(wFilename, margin);
fdlAppend.right = new FormAttachment(middle, -margin);
wlAppend.setLayoutData(fdlAppend);
wAppend = new Button(wFileName, SWT.CHECK);
wAppend = new Button(wFilenameGroup, SWT.CHECK);
wAppend.setToolTipText(BaseMessages.getString(PKG, "JsonOutputDialog.Append.Tooltip"));
PropsUi.setLook(wAppend);
FormData fdAppend = new FormData();
Expand All @@ -687,7 +689,7 @@ public void widgetSelected(SelectionEvent e) {
}
});

wlSplitOutputAfter = new Label(wFileName, SWT.RIGHT);
wlSplitOutputAfter = new Label(wFilenameGroup, SWT.RIGHT);
wlSplitOutputAfter.setText(
BaseMessages.getString(PKG, "JsonOutputDialog.splitOutputAfter.Label"));
PropsUi.setLook(wlSplitOutputAfter);
Expand All @@ -696,7 +698,7 @@ public void widgetSelected(SelectionEvent e) {
fdlSplitOutputAfter.top = new FormAttachment(wlAppend, 2 * margin);
fdlSplitOutputAfter.right = new FormAttachment(middle, -margin);
wlSplitOutputAfter.setLayoutData(fdlSplitOutputAfter);
wSplitOutputAfter = new TextVar(variables, wFileName, SWT.BORDER | SWT.READ_ONLY);
wSplitOutputAfter = new TextVar(variables, wFilenameGroup, SWT.BORDER | SWT.READ_ONLY);
wSplitOutputAfter.setEditable(true);
wSplitOutputAfter.setToolTipText("JsonOutputDialog.splitOutputAfter.Tooltip");
PropsUi.setLook(wSplitOutputAfter);
Expand All @@ -714,7 +716,7 @@ public void widgetSelected(SelectionEvent e) {
});

// Create Parent Folder
wlCreateParentFolder = new Label(wFileName, SWT.RIGHT);
wlCreateParentFolder = new Label(wFilenameGroup, SWT.RIGHT);
wlCreateParentFolder.setText(
BaseMessages.getString(PKG, "JsonOutputDialog.CreateParentFolder.Label"));
PropsUi.setLook(wlCreateParentFolder);
Expand All @@ -723,7 +725,7 @@ public void widgetSelected(SelectionEvent e) {
fdlCreateParentFolder.top = new FormAttachment(wlSplitOutputAfter, 2 * margin);
fdlCreateParentFolder.right = new FormAttachment(middle, -margin);
wlCreateParentFolder.setLayoutData(fdlCreateParentFolder);
wCreateParentFolder = new Button(wFileName, SWT.CHECK);
wCreateParentFolder = new Button(wFilenameGroup, SWT.CHECK);
wCreateParentFolder.setToolTipText(
BaseMessages.getString(PKG, "JsonOutputDialog.CreateParentFolder.Tooltip"));
PropsUi.setLook(wCreateParentFolder);
Expand All @@ -741,7 +743,7 @@ public void widgetSelected(SelectionEvent e) {
});

// Open new File at Init
wlDoNotOpenNewFileInit = new Label(wFileName, SWT.RIGHT);
wlDoNotOpenNewFileInit = new Label(wFilenameGroup, SWT.RIGHT);
wlDoNotOpenNewFileInit.setText(
BaseMessages.getString(PKG, "JsonOutputDialog.DoNotOpenNewFileInit.Label"));
PropsUi.setLook(wlDoNotOpenNewFileInit);
Expand All @@ -750,7 +752,7 @@ public void widgetSelected(SelectionEvent e) {
fdlDoNotOpenNewFileInit.top = new FormAttachment(wlCreateParentFolder, 2 * margin);
fdlDoNotOpenNewFileInit.right = new FormAttachment(middle, -margin);
wlDoNotOpenNewFileInit.setLayoutData(fdlDoNotOpenNewFileInit);
wDoNotOpenNewFileInit = new Button(wFileName, SWT.CHECK);
wDoNotOpenNewFileInit = new Button(wFilenameGroup, SWT.CHECK);
wDoNotOpenNewFileInit.setToolTipText(
BaseMessages.getString(PKG, "JsonOutputDialog.DoNotOpenNewFileInit.Tooltip"));
PropsUi.setLook(wDoNotOpenNewFileInit);
Expand All @@ -770,15 +772,15 @@ public void widgetSelected(SelectionEvent e) {
createExtensionLine(lsMod, middle, margin);

// Create multi-part file?
wlAddDate = new Label(wFileName, SWT.RIGHT);
wlAddDate = new Label(wFilenameGroup, SWT.RIGHT);
wlAddDate.setText(BaseMessages.getString(PKG, "JsonOutputDialog.AddDate.Label"));
PropsUi.setLook(wlAddDate);
FormData fdlAddDate = new FormData();
fdlAddDate.left = new FormAttachment(0, 0);
fdlAddDate.top = new FormAttachment(wEncoding, margin);
fdlAddDate.right = new FormAttachment(middle, -margin);
wlAddDate.setLayoutData(fdlAddDate);
wAddDate = new Button(wFileName, SWT.CHECK);
wAddDate = new Button(wFilenameGroup, SWT.CHECK);
PropsUi.setLook(wAddDate);
FormData fdAddDate = new FormData();
fdAddDate.left = new FormAttachment(middle, 0);
Expand All @@ -794,15 +796,15 @@ public void widgetSelected(SelectionEvent e) {
});

// Create multi-part file?
wlAddTime = new Label(wFileName, SWT.RIGHT);
wlAddTime = new Label(wFilenameGroup, SWT.RIGHT);
wlAddTime.setText(BaseMessages.getString(PKG, "JsonOutputDialog.AddTime.Label"));
PropsUi.setLook(wlAddTime);
FormData fdlAddTime = new FormData();
fdlAddTime.left = new FormAttachment(0, 0);
fdlAddTime.top = new FormAttachment(wlAddDate, 2 * margin);
fdlAddTime.right = new FormAttachment(middle, -margin);
wlAddTime.setLayoutData(fdlAddTime);
wAddTime = new Button(wFileName, SWT.CHECK);
wAddTime = new Button(wFilenameGroup, SWT.CHECK);
PropsUi.setLook(wAddTime);
FormData fdAddTime = new FormData();
fdAddTime.left = new FormAttachment(middle, 0);
Expand All @@ -817,7 +819,7 @@ public void widgetSelected(SelectionEvent e) {
}
});

wbShowFiles = new Button(wFileName, SWT.PUSH | SWT.CENTER);
wbShowFiles = new Button(wFilenameGroup, SWT.PUSH | SWT.CENTER);
PropsUi.setLook(wbShowFiles);
wbShowFiles.setText(BaseMessages.getString(PKG, "JsonOutputDialog.ShowFiles.Button"));
FormData fdbShowFiles = new FormData();
Expand Down Expand Up @@ -852,15 +854,15 @@ public void widgetSelected(SelectionEvent e) {
});

// Add File to the result files name
wlAddToResult = new Label(wFileName, SWT.RIGHT);
wlAddToResult = new Label(wFilenameGroup, SWT.RIGHT);
wlAddToResult.setText(BaseMessages.getString(PKG, "JsonOutputDialog.AddFileToResult.Label"));
PropsUi.setLook(wlAddToResult);
FormData fdlAddToResult = new FormData();
fdlAddToResult.left = new FormAttachment(0, 0);
fdlAddToResult.top = new FormAttachment(wbShowFiles, margin);
fdlAddToResult.right = new FormAttachment(middle, -margin);
wlAddToResult.setLayoutData(fdlAddToResult);
wAddToResult = new Button(wFileName, SWT.CHECK);
wAddToResult = new Button(wFilenameGroup, SWT.CHECK);
wAddToResult.setToolTipText(
BaseMessages.getString(PKG, "JsonOutputDialog.AddFileToResult.Tooltip"));
PropsUi.setLook(wAddToResult);
Expand All @@ -882,7 +884,7 @@ public void widgetSelected(SelectionEvent arg0) {
fdFileName.left = new FormAttachment(0, margin);
fdFileName.top = new FormAttachment(wSettings, 2 * margin);
fdFileName.right = new FormAttachment(100, -margin);
wFileName.setLayoutData(fdFileName);
wFilenameGroup.setLayoutData(fdFileName);

// ///////////////////////////////////////////////////////////
// / END OF FileName GROUP
Expand All @@ -891,7 +893,7 @@ public void widgetSelected(SelectionEvent arg0) {

private void createExtensionLine(ModifyListener lsMod, int middle, int margin) {
// Extension line
wlExtension = new Label(wFileName, SWT.RIGHT);
wlExtension = new Label(wFilenameGroup, SWT.RIGHT);
wlExtension.setText(BaseMessages.getString(PKG, "System.Label.Extension"));
PropsUi.setLook(wlExtension);
FormData fdlExtension = new FormData();
Expand All @@ -900,7 +902,7 @@ private void createExtensionLine(ModifyListener lsMod, int middle, int margin) {
fdlExtension.right = new FormAttachment(middle, -margin);
wlExtension.setLayoutData(fdlExtension);

wExtension = new TextVar(variables, wFileName, SWT.SINGLE | SWT.LEFT | SWT.BORDER);
wExtension = new TextVar(variables, wFilenameGroup, SWT.SINGLE | SWT.LEFT | SWT.BORDER);
PropsUi.setLook(wExtension);
wExtension.addModifyListener(lsMod);
FormData fdExtension = new FormData();
Expand All @@ -909,15 +911,15 @@ private void createExtensionLine(ModifyListener lsMod, int middle, int margin) {
fdExtension.right = new FormAttachment(100, -margin);
wExtension.setLayoutData(fdExtension);

wlEncoding = new Label(wFileName, SWT.RIGHT);
wlEncoding = new Label(wFilenameGroup, SWT.RIGHT);
wlEncoding.setText(BaseMessages.getString(PKG, "JsonOutputDialog.Encoding.Label"));
PropsUi.setLook(wlEncoding);
FormData fdlEncoding = new FormData();
fdlEncoding.left = new FormAttachment(0, 0);
fdlEncoding.top = new FormAttachment(wExtension, margin);
fdlEncoding.right = new FormAttachment(middle, -margin);
wlEncoding.setLayoutData(fdlEncoding);
wEncoding = new ComboVar(variables, wFileName, SWT.BORDER | SWT.READ_ONLY);
wEncoding = new ComboVar(variables, wFilenameGroup, SWT.BORDER | SWT.READ_ONLY);
wEncoding.setEditable(true);
PropsUi.setLook(wEncoding);
wEncoding.addModifyListener(lsMod);
Expand All @@ -929,7 +931,9 @@ private void createExtensionLine(ModifyListener lsMod, int middle, int margin) {
wEncoding.addFocusListener(
new FocusListener() {
@Override
public void focusLost(FocusEvent e) {}
public void focusLost(FocusEvent e) {
//Ignore event
}

@Override
public void focusGained(FocusEvent e) {
Expand Down Expand Up @@ -1017,22 +1021,22 @@ private void getData() {
item.setText(2, Const.NVL(field.getElementName(), ""));
String jsonFragment =
field.isJSONFragment()
? BaseMessages.getString(PKG, "System.Combo.Yes")
: BaseMessages.getString(PKG, "System.Combo.No");
? BaseMessages.getString(PKG, SYSTEM_COMBO_YES)
: BaseMessages.getString(PKG, SYSTEM_COMBO_NO);
if (jsonFragment != null) {
item.setText(3, jsonFragment);
}
String withoutEnclosing =
field.isWithoutEnclosing()
? BaseMessages.getString(PKG, "System.Combo.Yes")
: BaseMessages.getString(PKG, "System.Combo.No");
? BaseMessages.getString(PKG, SYSTEM_COMBO_YES)
: BaseMessages.getString(PKG, SYSTEM_COMBO_NO);
if (withoutEnclosing != null) {
item.setText(4, withoutEnclosing);
}
String removeIfBlank =
field.isRemoveIfBlank()
? BaseMessages.getString(PKG, "System.Combo.Yes")
: BaseMessages.getString(PKG, "System.Combo.No");
? BaseMessages.getString(PKG, SYSTEM_COMBO_YES)
: BaseMessages.getString(PKG, SYSTEM_COMBO_NO);
if (removeIfBlank != null) {
item.setText(5, removeIfBlank);
}
Expand Down Expand Up @@ -1099,11 +1103,11 @@ private void getInfo(JsonOutputMeta jsometa) {
field.setFieldName(item.getText(1));
field.setElementName(item.getText(2));
field.setJSONFragment(
BaseMessages.getString(PKG, "System.Combo.Yes").equalsIgnoreCase(item.getText(3)));
BaseMessages.getString(PKG, SYSTEM_COMBO_YES).equalsIgnoreCase(item.getText(3)));
field.setWithoutEnclosing(
BaseMessages.getString(PKG, "System.Combo.Yes").equalsIgnoreCase(item.getText(4)));
BaseMessages.getString(PKG, SYSTEM_COMBO_YES).equalsIgnoreCase(item.getText(4)));
field.setRemoveIfBlank(
BaseMessages.getString(PKG, "System.Combo.Yes").equalsIgnoreCase(item.getText(5)));
BaseMessages.getString(PKG, SYSTEM_COMBO_YES).equalsIgnoreCase(item.getText(5)));
// CHECKSTYLE:Indentation:OFF
jsometa.getOutputFields()[i] = field;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ public boolean equal(Object obj) {
@Override
public Object clone() {
try {
Object retval = super.clone();
return retval;
return super.clone();
} catch (CloneNotSupportedException e) {
return null;
}
Expand Down
Loading

0 comments on commit 3eab7e2

Please sign in to comment.