Skip to content

Commit

Permalink
Merge pull request #241 from TAMULib/240-file-delegate-write-update
Browse files Browse the repository at this point in the history
Write only target input to file
  • Loading branch information
wwelling authored Mar 26, 2024
2 parents 6d9e2f5 + a99f64f commit a4c47c0
Showing 1 changed file with 25 additions and 7 deletions.
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

0 comments on commit a4c47c0

Please sign in to comment.