com.fivefaces.structureclient.service.statemachine.impl.ScaffoldValidate Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of common-structure-client Show documentation
Show all versions of common-structure-client Show documentation
structure Client for Five Faces
The newest version!
package com.fivefaces.structureclient.service.statemachine.impl;
import com.fivefaces.structure.schema.StructureDefinition;
import com.fivefaces.structure.schema.StructureFieldDefinition;
import com.fivefaces.structure.schema.StructureFieldType;
import com.fivefaces.structure.service.StructureDefinitionSchemaLoader;
import com.fivefaces.structureclient.service.statemachine.ScaffoldStateMachineResult;
import com.fivefaces.structureclient.service.statemachine.ScaffoldUtils;
import lombok.AllArgsConstructor;
import org.apache.commons.text.CaseUtils;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
@Component
@AllArgsConstructor
public class ScaffoldValidate {
private final ScaffoldUtils scaffoldUtils;
private final StructureDefinitionSchemaLoader structureDefinitionSchemaLoader;
public String scaffold(String line, ScaffoldStateMachineResult result, String[] lines, int counter, String stateMachine, String arn) throws Exception {
final Map validateParameters = scaffoldUtils.getParameters(line, "name", "next", "entity", "roles", "property", "exclude");
final String validateEntity = validateParameters.get("entity");
StructureDefinition schema = null;
if (validateEntity != null && !validateEntity.toLowerCase(Locale.ROOT).equals("none")) {
if (!validateParameters.containsKey("roles")) {
result.getWarnings().add(String.format("%s step is missing roles", validateParameters.get("name")));
}
schema = structureDefinitionSchemaLoader.getTargetDefinitions().get(validateEntity);
// add any properties which are not included on the schema but are added in on the command line
final Map propertyParameters = scaffoldUtils.getParameters(line, "property");
schema.setFields(new HashMap<>());
StructureDefinition finalSchema = schema;
propertyParameters.forEach((a, b) -> {
StructureFieldDefinition structureFieldSchema = new StructureFieldDefinition();
structureFieldSchema.setType(StructureFieldType.OBJECT);
structureFieldSchema.setRequired(Boolean.TRUE);
if (!finalSchema.getFields().containsKey(b)) {
finalSchema.getFields().put(b, structureFieldSchema);
}
});
} else {
// we can hand craft the schema for this validate method
schema = new StructureDefinition();
schema.setType("madeup");
final Map propertyParameters = scaffoldUtils.getParameters(line, "property");
schema.setFields(new HashMap<>());
StructureDefinition finalSchema = schema;
propertyParameters.forEach((a, b) -> {
StructureFieldDefinition structureFieldSchema = new StructureFieldDefinition();
structureFieldSchema.setType(StructureFieldType.OBJECT);
structureFieldSchema.setRequired(Boolean.TRUE);
finalSchema.getFields().put(b, structureFieldSchema);
});
}
String name = CaseUtils.toCamelCase(validateParameters.get("name"), false, ' ');
final String validateStateMachine = scaffoldUtils.getValidateStateMachine(name,
"\"" + scaffoldUtils.getRoleNames(validateParameters).trim().replaceAll(" ", "\",\"") + "\"", schema, scaffoldUtils.getExclusions(validateParameters));
result.getStateMachines().put("Validate " + validateParameters.get("name"), validateStateMachine);
final String validateStep = String.format(" \"Type\": \"Task\",\n" +
" \"Resource\": \"arn:aws:states:::states:startExecution.sync:2\",\n" +
" \"ResultPath\": \"$.input.validate%s\",\n" +
" \"Parameters\": {\n" +
" \"StateMachineArn\": \"%s:stateMachine:${var.environment}_validate%s\",\n" +
" \"Input\": {\n" +
" \"NeedCallback \": true,\n" +
" \"query.$\": \"$.query\",\n" +
" \"AWS_STEP_FUNCTIONS_STARTED_BY_EXECUTION_ID.$\": \"$$.Execution.Id\"\n" +
" }\n" +
" },\n" +
" %s",
CaseUtils.toCamelCase(validateParameters.get("name"), true, ' '),
arn,
CaseUtils.toCamelCase(validateParameters.get("name"), true, ' '),
scaffoldUtils.addNextStep(result, lines, counter, validateParameters));
final String replaceString = "***" + validateParameters.get("name") + "***";
return stateMachine.replace(replaceString, validateStep);
}
}