com.fivefaces.structureclient.domain.Scaffold 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
package com.fivefaces.structureclient.domain;
import org.json.JSONObject;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
public class Scaffold {
public static void main(String[] args) throws Exception {
Scaffold scaffold = new Scaffold();
scaffold.scaffoldBDD();
}
public void scaffold(Class utilsClass) throws Exception {
StructureDefinition def = new StructureDefinition(utilsClass);
String classname = utilsClass.getDeclaredClasses()[0].getSimpleName();
if (!def.getTableName().isEmpty()) {
String validateCreateMethod = getValidateCreateMethod(classname, def.getTableName(), def.getRequiredRoleNames(), def.getStringFields(), def.getIntegerFields(), def.getBooleanFields(), def.getArrayFields(), def.getDateTimeFields());
Files.writeString(Paths.get(String.format("src/main/resources/functions/sf_validate_create_%s.tf", classname.toLowerCase(Locale.ROOT))), validateCreateMethod, StandardCharsets.UTF_8);
String createMethod = getCreateMethod(classname, def.getTableName(), def.isWarehouse(), def.getInsertText(), def.getStringFields(), def.getIntegerFields(), def.getBooleanFields(), def.getArrayFields(), def.getDateTimeFields());
Files.writeString(Paths.get(String.format("src/main/resources/functions/sf_create_%s.tf", classname.toLowerCase(Locale.ROOT))), createMethod, StandardCharsets.UTF_8);
if (def.isCreateUpdate()) {
String validateUpdateMethod = getValidateUpdateMethod(classname, def.getTableName(), def.getRequiredRoleNames(), def.getStringFields(), def.getIntegerFields(), def.getBooleanFields(), def.getArrayFields(), def.getDateTimeFields());
Files.writeString(Paths.get(String.format("src/main/resources/functions/sf_validate_update_%s.tf", classname.toLowerCase(Locale.ROOT))), validateUpdateMethod, StandardCharsets.UTF_8);
String updateMethod = getUpdateMethod(classname, def.getTableName(), def.isWarehouse(), def.getStringFields(), def.getIntegerFields(), def.getBooleanFields(), def.getArrayFields(), def.getDateTimeFields());
Files.writeString(Paths.get(String.format("src/main/resources/functions/sf_update_%s.tf", classname.toLowerCase(Locale.ROOT))), updateMethod, StandardCharsets.UTF_8);
}
String validateDeleteMethod = getValidateDeleteMethod(classname, def.getRequiredRoleNames());
Files.writeString(Paths.get(String.format("src/main/resources/functions/sf_validate_delete_%s.tf", classname.toLowerCase(Locale.ROOT))), validateDeleteMethod, StandardCharsets.UTF_8);
String deleteMethod = getDeleteMethod(classname, def.getTableName(), def.isWarehouse());
Files.writeString(Paths.get(String.format("src/main/resources/functions/sf_delete_%s.tf", classname.toLowerCase(Locale.ROOT))), deleteMethod, StandardCharsets.UTF_8);
Files.createDirectories(Paths.get(String.format("src/test/java/com/fivefaces/structuredpjapi/state/integration/%s", classname.toLowerCase(Locale.ROOT))));
Files.createDirectories(Paths.get(String.format("src/test/java/com/fivefaces/structuredpjapi/state/integration/awsonprem/%s", classname.toLowerCase(Locale.ROOT))));
String createTest = getCreateTest(classname, def.getRequiredRoleNames(), def.getStringFields(), def.getIntegerFields(), def.getBooleanFields(), def.getArrayFields(), def.getDateTimeFields());
Files.writeString(Paths.get(String.format("src/test/java/com/fivefaces/structuredpjapi/state/integration/%s/Create%sTest.java", classname.toLowerCase(Locale.ROOT), classname)), createTest, StandardCharsets.UTF_8);
String updateTest = getUpdateTest(utilsClass.getDeclaredClasses()[0].getSimpleName(), def.getTableName(), def.getRequiredRoleNames(), def.getStringFields(), def.getIntegerFields(), def.getBooleanFields(), def.getArrayFields(), def.getDateTimeFields());
Files.writeString(Paths.get(String.format("src/test/java/com/fivefaces/structuredpjapi/state/integration/%s/Update%sTest.java", classname.toLowerCase(Locale.ROOT), classname)), updateTest, StandardCharsets.UTF_8);
String deleteTest = getDeleteTest(utilsClass.getDeclaredClasses()[0].getSimpleName(), def.getTableName(), def.getRequiredRoleNames());
Files.writeString(Paths.get(String.format("src/test/java/com/fivefaces/structuredpjapi/state/integration/%s/Delete%sTest.java", classname.toLowerCase(Locale.ROOT), classname)), deleteTest, StandardCharsets.UTF_8);
String createAWSOnPremTest = getCreateAWSOnPremTest(utilsClass.getDeclaredClasses()[0].getSimpleName());
Files.writeString(Paths.get(String.format("src/test/java/com/fivefaces/structuredpjapi/state/integration/awsonprem/%s/AWSOnPremiseCreate%sTest.java", classname.toLowerCase(Locale.ROOT), classname)),
createAWSOnPremTest, StandardCharsets.UTF_8);
String updateAWSOnPremTest = getUpdateAWSOnPremTest(utilsClass.getDeclaredClasses()[0].getSimpleName());
Files.writeString(Paths.get(String.format("src/test/java/com/fivefaces/structuredpjapi/state/integration/awsonprem/%s/AWSOnPremiseUpdate%sTest.java", classname.toLowerCase(Locale.ROOT), classname)),
updateAWSOnPremTest, StandardCharsets.UTF_8);
String deleteAWSOnPremTest = getDeleteAWSOnPremTest(utilsClass.getDeclaredClasses()[0].getSimpleName());
Files.writeString(Paths.get(String.format("src/test/java/com/fivefaces/structuredpjapi/state/integration/awsonprem/%s/AWSOnPremiseDelete%sTest.java", classname.toLowerCase(Locale.ROOT), classname)),
deleteAWSOnPremTest, StandardCharsets.UTF_8);
System.out.println("#DROP TABLE if exists `" + def.getTableName() + "`;\n\n");
System.out.println("CREATE TABLE `" + def.getTableName() + "` (\n" +
" `version` int NOT NULL AUTO_INCREMENT,\n" +
" `id` varchar(36) NOT NULL,\n" +
" `json_data` json DEFAULT NULL,\n" +
" `created_at_utc` datetime DEFAULT NULL,\n" +
" `created_by` varchar(45) DEFAULT NULL,\n" +
" `last_modified_at_utc` datetime DEFAULT NULL,\n" +
" `last_modified_by` varchar(45) DEFAULT NULL,\n" +
" PRIMARY KEY (`version`,`id`)\n" +
") ENGINE=InnoDB AUTO_INCREMENT=8588 DEFAULT CHARSET=latin1;\n\n");
if (def.isWarehouse()) {
System.out.println("#DROP TABLE if exists `" + def.getWarehouseSchema() + "`.`" + def.getTableName() + "`;\n\n");
System.out.println(createWarehouseTable(def.getStringFields(), def.getIntegerFields(), def.getBooleanFields(), def.getArrayFields(), def.getDateTimeFields(), def.getWarehouseSchema(), def.getTableName()));
}
System.out.println(getIndexProperties(def.getTableName(), def.getWarehouseSchema(), def.getStringFields(), def.getIntegerFields()));
} else {
String validateCallMethod = getValidateCallMethod(classname, def.getCallName(), def.getRequiredRoleNames(), def.getStringFields(), def.getIntegerFields(), def.getBooleanFields(), def.getArrayFields(), def.getDateTimeFields());
Files.writeString(Paths.get(String.format("src/main/resources/functions/sf_validate_%s.tf", def.getCallName().toLowerCase(Locale.ROOT))), validateCallMethod, StandardCharsets.UTF_8);
Files.createDirectories(Paths.get("src/test/java/com/fivefaces/structuredpjapi/state/integration/calls"));
Files.createDirectories(Paths.get("src/test/java/com/fivefaces/structuredpjapi/state/integration/awsonprem/calls"));
if (!Files.exists(Paths.get(String.format("src/test/java/com/fivefaces/structuredpjapi/state/integration/calls/Call%sTest.java", classname.toLowerCase(Locale.ROOT))))) {
String callTest = getCallTest(classname, def.getCallName());
Files.writeString(Paths.get(String.format("src/test/java/com/fivefaces/structuredpjapi/state/integration/calls/Call%sTest.java", classname)), callTest, StandardCharsets.UTF_8);
String callAWSOnPremTest = getCallAWSOnPremTest(utilsClass.getDeclaredClasses()[0].getSimpleName());
Files.writeString(Paths.get(String.format("src/test/java/com/fivefaces/structuredpjapi/state/integration/awsonprem/calls/AWSOnPremiseCall%sTest.java", classname)), callAWSOnPremTest, StandardCharsets.UTF_8);
}
}
}
public void tearDown(Class utilsClass) throws Exception {
String classname = utilsClass.getDeclaredClasses()[0].getSimpleName();
Files.deleteIfExists(Paths.get(String.format("src/main/resources/functions/sf_validate_create_%s.tf", classname.toLowerCase(Locale.ROOT))));
Files.deleteIfExists(Paths.get(String.format("src/main/resources/functions/sf_create_%s.tf", classname.toLowerCase(Locale.ROOT))));
Files.deleteIfExists(Paths.get(String.format("src/main/resources/functions/sf_validate_update_%s.tf", classname.toLowerCase(Locale.ROOT))));
Files.deleteIfExists(Paths.get(String.format("src/main/resources/functions/sf_update_%s.tf", classname.toLowerCase(Locale.ROOT))));
Files.deleteIfExists(Paths.get(String.format("src/main/resources/functions/sf_validate_delete_%s.tf", classname.toLowerCase(Locale.ROOT))));
Files.deleteIfExists(Paths.get(String.format("src/main/resources/functions/sf_delete_%s.tf", classname.toLowerCase(Locale.ROOT))));
try {
Files.walk(Paths.get(String.format("src/test/java/com/fivefaces/structuredpjapi/state/integration/%s", classname.toLowerCase(Locale.ROOT))))
.map(Path::toFile)
.forEach(File::delete);
} catch (Exception ignore){};
Files.deleteIfExists(Paths.get(String.format("src/test/java/com/fivefaces/structuredpjapi/state/integration/%s", classname.toLowerCase(Locale.ROOT))));
try {
Files.walk(Paths.get(String.format("src/test/java/com/fivefaces/structuredpjapi/state/integration/awsonprem/%s", classname.toLowerCase(Locale.ROOT))))
.map(Path::toFile)
.forEach(File::delete);
} catch (Exception ignore){};
Files.deleteIfExists(Paths.get(String.format("src/test/java/com/fivefaces/structuredpjapi/state/integration/awsonprem/%s", classname.toLowerCase(Locale.ROOT))));
Files.deleteIfExists(Paths.get(String.format("src/test/java/com/fivefaces/structuredpjapi/state/createtable/Create%sWarehouseTest.java", classname)));
}
public void scaffoldBDD() throws IOException {
final StringBuilder pythonFunctions = new StringBuilder();
final StringBuilder steps = new StringBuilder();
try {
Files.walk(Paths.get("/Users/fivefaces/dev/replatform-ui/src/pages"))
.map(Path::toFile)
.filter(file -> file.getName().endsWith(".tsx")
&& !file.getName().endsWith(".test.tsx")
&& !file.getName().equals("index.tsx")
&& !file.getName().equals("PermitedRoute.tsx")
&& !file.getName().equals("FilterComponent.tsx")
&& !file.getName().equals("AntdDataTable.tsx")
&& !file.getName().equals("CRUDDataTable.tsx")
&& !file.getName().equals("MsAuthPage.tsx"))
.forEach(f -> {
try {
System.out.println(f.getName());
processBDD(f, pythonFunctions, steps);
pythonFunctions.append("\n");
steps.append("\n");
} catch (Exception exception) {
try {
throw exception;
} catch (Exception e) {
e.printStackTrace();
}
}
});
} catch (Exception ignore){
ignore.printStackTrace();
};
Files.createDirectories(Paths.get("src/main/resources/bdd"));
Files.writeString(Paths.get("src/main/resources/bdd/pythonFunctions.txt"), pythonFunctions, StandardCharsets.UTF_8);
Files.writeString(Paths.get("src/main/resources/bdd/stepFunctions.txt"), steps, StandardCharsets.UTF_8);
}
public static final String TEXTAREA = "