Skip to content

Commit

Permalink
Add option to create parent folders in "Serialize to file"
Browse files Browse the repository at this point in the history
transform apache#3677
  • Loading branch information
nadment committed Mar 5, 2024
1 parent 919e7bb commit bc814c8
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@

package org.apache.hop.pipeline.transforms.cubeoutput;

import org.apache.commons.vfs2.FileObject;
import org.apache.hop.core.ResultFile;
import org.apache.hop.core.exception.HopException;
import org.apache.hop.core.exception.HopFileException;
import org.apache.hop.core.exception.HopTransformException;
import org.apache.hop.core.vfs.HopVfs;
import org.apache.hop.i18n.BaseMessages;
import org.apache.hop.pipeline.Pipeline;
Expand Down Expand Up @@ -170,12 +172,21 @@ public boolean init() {
private void prepareFile() throws HopFileException {
try {
String filename = resolve(meta.getFilename());

FileObject fileObject = HopVfs.getFileObject(filename);

// See if we need to create the parent folder(s)...
//
if (meta.isFilenameCreatingParentFolders()) {
createParentFolder(fileObject.getParent());
}

if (meta.isAddToResultFilenames()) {
// Add this to the result file names...
ResultFile resultFile =
new ResultFile(
ResultFile.FILE_TYPE_GENERAL,
HopVfs.getFileObject(filename),
fileObject,
getPipelineMeta().getName(),
getTransformName());
resultFile.setComment("This file was created with a cube file output transform");
Expand All @@ -189,7 +200,37 @@ private void prepareFile() throws HopFileException {
throw new HopFileException(e);
}
}

private void createParentFolder(FileObject parentFolder) throws HopTransformException {
if ( parentFolder==null )
return;

try {
// See if we need to create the parent folder(s)...
if (!parentFolder.exists()) {

createParentFolder(parentFolder.getParent());

// Try to create the parent folder...
parentFolder.createFolder();
if (log.isDebug()) {
logDebug(BaseMessages.getString(PKG, "CubeOutput.Log.ParentFolderCreated", parentFolder.getName()));
}
}
} catch (Exception e) {
throw new HopTransformException(BaseMessages.getString(PKG,
"CubeOutput.Error.ErrorCreatingParentFolder", parentFolder.getName()));
} finally {
if (parentFolder != null) {
try {
parentFolder.close();
} catch (Exception ex) {
// Ignore
}
}
}
}

@Override
public void dispose() {
if (data.oneFileOpened) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
import org.apache.hop.ui.pipeline.transform.BaseTransformDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
Expand All @@ -43,8 +41,8 @@ public class CubeOutputDialog extends BaseTransformDialog implements ITransformD
private static final Class<?> PKG = CubeOutputMeta.class; // For Translator

private TextVar wFilename;
private Button wCreatingParentFolders;
private Button wAddToResult;

private Button wDoNotOpenNewFileInit;

private final CubeOutputMeta input;
Expand All @@ -66,6 +64,7 @@ public String open() {
Shell parent = getParent();

shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.RESIZE | SWT.MAX | SWT.MIN);
shell.setMinimumSize(400, 200);
PropsUi.setLook(shell);
setShellImage(shell, input);

Expand All @@ -80,7 +79,7 @@ public String open() {
shell.setText(BaseMessages.getString(PKG, "CubeOutputDialog.Shell.Text"));

int middle = props.getMiddlePct();
int margin = props.getMargin();
int margin = PropsUi.getMargin();

// TransformName line
wlTransformName = new Label(shell, SWT.RIGHT);
Expand Down Expand Up @@ -127,14 +126,37 @@ public String open() {
fdFilename.right = new FormAttachment(wbFilename, -margin);
wFilename.setLayoutData(fdFilename);

// Creating parent folders
//
Label wlCreatingParentFolders = new Label(shell, SWT.RIGHT);
wlCreatingParentFolders.setText(
BaseMessages.getString(PKG, "CubeOutputDialog.CreatingParentFolders.Label"));
PropsUi.setLook(wlCreatingParentFolders);
FormData fdlCreatingParentFolders = new FormData();
fdlCreatingParentFolders.left = new FormAttachment(0, 0);
fdlCreatingParentFolders.top = new FormAttachment(wFilename, 2 * margin);
fdlCreatingParentFolders.right = new FormAttachment(middle, -margin);
wlCreatingParentFolders.setLayoutData(fdlCreatingParentFolders);
wCreatingParentFolders = new Button(shell, SWT.CHECK);
wCreatingParentFolders.setToolTipText(
BaseMessages.getString(PKG, "CubeOutputDialog.CreatingParentFolders.Tooltip"));
PropsUi.setLook(wDoNotOpenNewFileInit);
FormData fdCreatingParentFolders = new FormData();
fdCreatingParentFolders.left = new FormAttachment(middle, 0);
fdCreatingParentFolders.top = new FormAttachment(wlCreatingParentFolders, 0, SWT.CENTER);
fdCreatingParentFolders.right = new FormAttachment(100, 0);
wCreatingParentFolders.setLayoutData(fdCreatingParentFolders);
wCreatingParentFolders.addListener(SWT.Selection, e -> input.setChanged());

// Open new File at Init
//
Label wlDoNotOpenNewFileInit = new Label(shell, SWT.RIGHT);
wlDoNotOpenNewFileInit.setText(
BaseMessages.getString(PKG, "CubeOutputDialog.DoNotOpenNewFileInit.Label"));
PropsUi.setLook(wlDoNotOpenNewFileInit);
FormData fdlDoNotOpenNewFileInit = new FormData();
fdlDoNotOpenNewFileInit.left = new FormAttachment(0, 0);
fdlDoNotOpenNewFileInit.top = new FormAttachment(wFilename, 2 * margin);
fdlDoNotOpenNewFileInit.top = new FormAttachment(wCreatingParentFolders, margin);
fdlDoNotOpenNewFileInit.right = new FormAttachment(middle, -margin);
wlDoNotOpenNewFileInit.setLayoutData(fdlDoNotOpenNewFileInit);
wDoNotOpenNewFileInit = new Button(shell, SWT.CHECK);
Expand All @@ -146,15 +168,10 @@ public String open() {
fdDoNotOpenNewFileInit.top = new FormAttachment(wlDoNotOpenNewFileInit, 0, SWT.CENTER);
fdDoNotOpenNewFileInit.right = new FormAttachment(100, 0);
wDoNotOpenNewFileInit.setLayoutData(fdDoNotOpenNewFileInit);
wDoNotOpenNewFileInit.addSelectionListener(
new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
input.setChanged();
}
});
wDoNotOpenNewFileInit.addListener(SWT.Selection, e -> input.setChanged());

// Add File to the result files name
//
Label wlAddToResult = new Label(shell, SWT.RIGHT);
wlAddToResult.setText(BaseMessages.getString(PKG, "CubeOutputDialog.AddFileToResult.Label"));
PropsUi.setLook(wlAddToResult);
Expand All @@ -172,21 +189,14 @@ public void widgetSelected(SelectionEvent e) {
fdAddToResult.top = new FormAttachment(wlAddToResult, 0, SWT.CENTER);
fdAddToResult.right = new FormAttachment(100, 0);
wAddToResult.setLayoutData(fdAddToResult);
SelectionAdapter lsSelR =
new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent arg0) {
input.setChanged();
}
};
wAddToResult.addSelectionListener(lsSelR);
wAddToResult.addListener(SWT.Selection, e -> input.setChanged());

wOk = new Button(shell, SWT.PUSH);
wOk.setText(BaseMessages.getString(PKG, "System.Button.OK"));
wCancel = new Button(shell, SWT.PUSH);
wCancel.setText(BaseMessages.getString(PKG, "System.Button.Cancel"));

setButtonPositions(new Button[] {wOk, wCancel}, margin, wAddToResult);
setButtonPositions(new Button[] {wOk, wCancel}, margin, null);

// Add listeners
wOk.addListener(SWT.Selection, e -> ok());
Expand Down Expand Up @@ -218,6 +228,7 @@ public void getData() {
if (input.getFilename() != null) {
wFilename.setText(input.getFilename());
}
wCreatingParentFolders.setSelection(input.isFilenameCreatingParentFolders());
wDoNotOpenNewFileInit.setSelection(input.isDoNotOpenNewFileInit());
wAddToResult.setSelection(input.isAddToResultFilenames());

Expand All @@ -238,6 +249,7 @@ private void ok() {
}

transformName = wTransformName.getText(); // return value
input.setFilenameCreatingParentFolders(wCreatingParentFolders.getSelection());
input.setAddToResultFilenames(wAddToResult.getSelection());
input.setDoNotOpenNewFileInit(wDoNotOpenNewFileInit.getSelection());
input.setFilename(wFilename.getText());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public class CubeOutputMeta extends BaseTransformMeta<CubeOutput, CubeOutputData
@HopMetadataProperty(key = "name")
private String filename;

@HopMetadataProperty(key = "filename_create_parent_folders")
private boolean filenameCreatingParentFolders;

/** Flag: add the filenames to result filenames */
@HopMetadataProperty(key = "add_to_result_filenames")
private boolean addToResultFilenames;
Expand All @@ -67,6 +70,7 @@ public CubeOutputMeta() {

public CubeOutputMeta(CubeOutputMeta m) {
this.filename = m.filename;
this.filenameCreatingParentFolders = m.filenameCreatingParentFolders;
this.addToResultFilenames = m.addToResultFilenames;
this.doNotOpenNewFileInit = m.doNotOpenNewFileInit;
}
Expand Down Expand Up @@ -171,6 +175,18 @@ public void setFilename(String filename) {
this.filename = filename;
}

/**
* Gets filename creating parent folders
*/
public boolean isFilenameCreatingParentFolders() {
return filenameCreatingParentFolders;
}

/** @param filenameCreatingParentFolders The filenameCreatingParentFolders to set */
public void setFilenameCreatingParentFolders(boolean filenameCreatingParentFolders) {
this.filenameCreatingParentFolders = filenameCreatingParentFolders;
}

/**
* Gets addToResultFilenames
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@ CubeOutput.Description=Write rows of data to a data cube
CubeOutput.Log.LineNumber=linenr
CubeOutput.Log.ErrorWritingLine=Error writing line :
CubeOutput.Log.ErrorOpeningCubeOutputFile=Error opening cube output file:
CubeOutput.Log.ErrorClosingFile=Error closing file
CubeOutput.Log.ErrorClosingFile=Error closing file
CubeOutput.Log.ParentFolderCreated=Folder parent [{0}] was created
CubeOutput.Error.ErrorCreatingParentFolder=Couldn't create parent folder [{0}]!
CubeOutputDialog.DefaultTransformName=write to cube file
CubeOutputDialog.Shell.Text=Serialize to file
CubeOutputDialog.TransformName.Label=Transform name
CubeOutputDialog.Filename.Label=Filename
CubeOutputDialog.Browse.Button=&Browse...
CubeOutputDialog.FilterNames.Options.CubeFiles=Cube files
CubeOutputDialog.FilterNames.Options.AllFiles=All files
CubeOutputDialog.CreatingParentFolders.Label=Create parent folders
CubeOutputDialog.CreatingParentFolders.Tooltip=Create parent folders if they don''t exist
CubeOutputDialog.AddFileToResult.Label=Add filenames to result
CubeOutputDialog.AddFileToResult.Tooltip=Check this if you wan to add filenames to result filenames.
CubeOutputDialog.DoNotOpenNewFileInit.Label=Do not create file at start
Expand Down

0 comments on commit bc814c8

Please sign in to comment.