Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write only target input to file #241

Merged
merged 1 commit into from
Mar 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions src/main/java/org/folio/rest/delegate/FileDelegate.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public class FileDelegate extends AbstractWorkflowIODelegate {

private Expression op;

private Expression target;

@Override
public void execute(DelegateExecution execution) throws Exception {
long startTime = System.nanoTime();
Expand Down Expand Up @@ -107,14 +109,26 @@ public void execute(DelegateExecution execution) throws Exception {
}
break;
case WRITE:
// iterate over `target` input varaible
// writing entry per line
String targetInputVariable = this.target.getValue(execution).toString();
StringBuilder content = new StringBuilder();
for (Object value : inputs.values()) {
if (value instanceof String) {
content.append(value);
} else {
content.append(objectMapper.writeValueAsString(value));
}
content.append("\n");
Object obj = inputs.get(targetInputVariable);
if (obj instanceof List) {
List<Object> objects = (List<Object>) obj;
logger.info("{} {} has {} entries to write",
obj.getClass().getSimpleName(), targetInputVariable, objects.size());
for (Object value : (List<Object>) objects) {
if (value instanceof String) {
content.append(value);
} else {
content.append(objectMapper.writeValueAsString(value));
}
content.append("\n");
}
} else {
logger.warn("{} {} unsupported input type for target parameter of WRITE operation",
obj.getClass().getSimpleName(), targetInputVariable);
}
FileUtils.writeStringToFile(file, content.toString(), StandardCharsets.UTF_8);
logger.info("{} written", filePath);
Expand Down Expand Up @@ -148,6 +162,10 @@ public void setOp(Expression op) {
this.op = op;
}

public void setTarget(Expression target) {
this.target = target;
}

@Override
public Class<?> fromTask() {
return FileTask.class;
Expand Down