From e43b262d878c27fb4bf9c17f4c5cbcea5e06fd72 Mon Sep 17 00:00:00 2001 From: ThishaniLucas Date: Tue, 21 Nov 2023 10:20:34 +0530 Subject: [PATCH 1/2] Add validations for user inputs and handle invalid included/excluded * https://github.com/wso2-enterprise/open-healthcare/issues/1453 * https://github.com/wso2-enterprise/open-healthcare/issues/1451 * https://github.com/wso2-enterprise/open-healthcare/issues/1452 --- .../packagegen/tool/config/PackageConfig.java | 15 ++- .../project/tool/BallerinaProjectTool.java | 93 +++++++++++++------ .../project/tool/config/MetadataConfig.java | 15 ++- .../ballerina/health/cmd/fhir/FhirSubCmd.java | 8 +- .../src/main/resources/ballerina-health.help | 36 ++++--- 5 files changed, 121 insertions(+), 46 deletions(-) diff --git a/native/fhir-to-bal-lib/src/main/java/org/wso2/healthcare/fhir/ballerina/packagegen/tool/config/PackageConfig.java b/native/fhir-to-bal-lib/src/main/java/org/wso2/healthcare/fhir/ballerina/packagegen/tool/config/PackageConfig.java index 1d449a0..70b8e8d 100644 --- a/native/fhir-to-bal-lib/src/main/java/org/wso2/healthcare/fhir/ballerina/packagegen/tool/config/PackageConfig.java +++ b/native/fhir-to-bal-lib/src/main/java/org/wso2/healthcare/fhir/ballerina/packagegen/tool/config/PackageConfig.java @@ -113,7 +113,20 @@ public List getDependencyConfigList() { } public void setOrg(String org) { - this.org = org; + // org name of a ballerina package can have only alhpa-numeric chars and '_' + // it cannot have consecutive '_' + // and it cannot start/end with '_' + String normalizedOrg = org.replaceAll("[^a-zA-Z0-9_]", "_"); + if (normalizedOrg.contains("__")) { + normalizedOrg = normalizedOrg.replaceAll("_{2,}", "_"); + } + if (normalizedOrg.startsWith("_")) { + normalizedOrg = ProjectUtils.removeFirstChar(normalizedOrg); + } + if (normalizedOrg.endsWith("_")) { + normalizedOrg = ProjectUtils.removeLastChar(normalizedOrg); + } + this.org = normalizedOrg; } public void setName(String name) { diff --git a/native/fhir-to-bal-template/src/main/java/org/wso2/healthcare/fhir/codegen/ballerina/project/tool/BallerinaProjectTool.java b/native/fhir-to-bal-template/src/main/java/org/wso2/healthcare/fhir/codegen/ballerina/project/tool/BallerinaProjectTool.java index bffffca..13449fd 100644 --- a/native/fhir-to-bal-template/src/main/java/org/wso2/healthcare/fhir/codegen/ballerina/project/tool/BallerinaProjectTool.java +++ b/native/fhir-to-bal-template/src/main/java/org/wso2/healthcare/fhir/codegen/ballerina/project/tool/BallerinaProjectTool.java @@ -37,6 +37,7 @@ import org.wso2.healthcare.fhir.codegen.ballerina.project.tool.model.SearchParam; import java.io.File; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -117,11 +118,18 @@ private void populateIGs(ToolContext toolContext) { private void populateBalService() { for (Map.Entry entry : igMap.entrySet()) { String igName = entry.getKey(); - for (Map.Entry definitionEntry : entry.getValue().getResources().entrySet()) { - if (definitionEntry.getValue().getDefinition().getKind().toCode().equalsIgnoreCase("RESOURCE")) { - validateAndAddFHIRResource(definitionEntry.getValue().getDefinition(), igName); + // extract structure definitions of resource types + Map resourceDefMap = new HashMap<>(); + entry.getValue().getResources().forEach((k, resourceDef) -> { + if (resourceDef.getDefinition().getKind().toCode().equalsIgnoreCase("RESOURCE")) { + resourceDefMap.put(k, resourceDef); } - } + }); + // filter structure definitions based on included/excluded + List structureDefinitions = retrieveStructureDef(igName, resourceDefMap); + structureDefinitions.forEach(definition -> { + addResourceProfile(definition, definition.getType(), definition.getName(), definition.getUrl(), igName); + }); //adding Search parameters for (Map.Entry parameter : entry.getValue().getSearchParameters().entrySet()) { List baseResources = parameter.getValue().getSearchParameter().getBase(); @@ -137,6 +145,58 @@ private void populateBalService() { } } + private List retrieveStructureDef(String igName, Map resourceDefMap) { + List structureDefinitions = new ArrayList<>(); + List includedProfiles = + ballerinaProjectToolConfig.getIncludedIGConfigs().get(igName).getIncludedProfiles(); + List excludedProfiles = + ballerinaProjectToolConfig.getIncludedIGConfigs().get(igName).getExcludedProfiles(); + if (!includedProfiles.isEmpty()) { + for (String profile : includedProfiles) { + if (resourceDefMap.containsKey(profile)) { + structureDefinitions.add(resourceDefMap.get(profile).getDefinition()); + } else { + // invalid url + System.out.println("Invalid fhir profile to include: " + profile); + } + } + if (structureDefinitions.isEmpty()) { + // nothing included + // generate template for all the profiles + System.out.println("Generating templates for all FHIR profiles..."); + resourceDefMap.forEach((k, resourceDef) -> { + structureDefinitions.add(resourceDef.getDefinition()); + }); + } + return structureDefinitions; + } + if (!excludedProfiles.isEmpty()) { + Map resourceDefMapCopy = new HashMap<>(resourceDefMap); + for (String profile : excludedProfiles) { + if (resourceDefMapCopy.containsKey(profile)) { + resourceDefMapCopy.remove(profile); + } else { + // invalid url + System.out.println("Invalid fhir profile to exclude: " + profile); + } + } + resourceDefMapCopy.forEach((k, resourceDef) -> { + structureDefinitions.add(resourceDef.getDefinition()); + }); + if (resourceDefMap.size() == resourceDefMapCopy.size()) { + System.out.println("Generating templates for all FHIR profiles..."); + } + return structureDefinitions; + } + // nothing included or excluded + // generate templates for all the profiles + System.out.println("Generating templates for all FHIR profiles..."); + resourceDefMap.forEach((k, v) -> { + structureDefinitions.add(v.getDefinition()); + }); + return structureDefinitions; + } + private SearchParam getSearchParam(Map.Entry parameter, String apiName) { SearchParam param = new SearchParam(parameter.getValue().getSearchParameter().getName(), parameter.getValue().getSearchParameter().getCode()); @@ -150,31 +210,6 @@ private SearchParam getSearchParam(Map.Entry paramet return param; } - /** - * Validate Ballerina service based on include-exclude configs. - * - * @param structureDefinition FHIR StructureDefinition - * @param igName IG name - */ - public void validateAndAddFHIRResource(StructureDefinition structureDefinition, String igName) { - - String resourceType = structureDefinition.getType(); - String profile = structureDefinition.getName(); - String url = structureDefinition.getUrl(); - - if (ballerinaProjectToolConfig.getIncludedIGConfigs().get(igName).getIncludedProfiles().isEmpty()) { - //add all resources of the IG except ones listed in excluded list - if (!ballerinaProjectToolConfig.getIncludedIGConfigs().get(igName).getExcludedProfiles().contains(url)) { - addResourceProfile(structureDefinition, resourceType, profile, url, igName); - } - } else { - //add resources listed in included list. Neglect excluded list - if (ballerinaProjectToolConfig.getIncludedIGConfigs().get(igName).getIncludedProfiles().contains(url)) { - addResourceProfile(structureDefinition, resourceType, profile, url, igName); - } - } - } - /** * Adding Ballerina service model to a common map. * diff --git a/native/fhir-to-bal-template/src/main/java/org/wso2/healthcare/fhir/codegen/ballerina/project/tool/config/MetadataConfig.java b/native/fhir-to-bal-template/src/main/java/org/wso2/healthcare/fhir/codegen/ballerina/project/tool/config/MetadataConfig.java index 4093a0d..c0c40d4 100644 --- a/native/fhir-to-bal-template/src/main/java/org/wso2/healthcare/fhir/codegen/ballerina/project/tool/config/MetadataConfig.java +++ b/native/fhir-to-bal-template/src/main/java/org/wso2/healthcare/fhir/codegen/ballerina/project/tool/config/MetadataConfig.java @@ -72,7 +72,20 @@ public List getKeywords() { } public void setOrg(String org) { - this.org = org; + // org name of a ballerina package can have only alhpa-numeric chars and '_' + // it cannot have consecutive '_' + // and it cannot start/end with '_' + String normalizedOrg = org.replaceAll("[^a-zA-Z0-9_]", "_"); + if (normalizedOrg.contains("__")) { + normalizedOrg = normalizedOrg.replaceAll("_{2,}", "_"); + } + if (normalizedOrg.startsWith("_")) { + normalizedOrg = normalizedOrg.substring(1); + } + if (normalizedOrg.endsWith("_")) { + normalizedOrg = normalizedOrg.substring(0, normalizedOrg.length() - 1); + } + this.org = normalizedOrg; } public void setNamePrefix(String namePrefix) { diff --git a/native/health-cli/src/main/java/io/ballerina/health/cmd/fhir/FhirSubCmd.java b/native/health-cli/src/main/java/io/ballerina/health/cmd/fhir/FhirSubCmd.java index ff820c0..ff0f48c 100644 --- a/native/health-cli/src/main/java/io/ballerina/health/cmd/fhir/FhirSubCmd.java +++ b/native/health-cli/src/main/java/io/ballerina/health/cmd/fhir/FhirSubCmd.java @@ -156,12 +156,18 @@ public void execute() { HealthCmdUtils.exitError(exitWhenFinish); } if (dependentPackage != null && !dependentPackage.isEmpty()) { - if (!dependentPackage.matches("^[^/]+/[^/]+$")) { + // regex matching ballerinax/health.fhir.r4 + if (!dependentPackage.matches("^(?!.*__)[a-zA-Z0-9][a-zA-Z0-9_]+[a-zA-Z0-9]/[a-zA-Z0-9][a-zA-Z0-9._]+[a-zA-Z0-9]$")) { printStream.println("Format of the dependent package is incorrect."); printStream.println("Try bal health --help for more information."); HealthCmdUtils.exitError(exitWhenFinish); } } + if (includedProfiles != null && excludedProfiles != null) { + printStream.println("Both --included-profile and --excluded-profile cannot be used together."); + printStream.println("Try bal health --help for more information."); + HealthCmdUtils.exitError(exitWhenFinish); + } if (this.engageSubCommand(argList)) { if (CMD_MODE_TEMPLATE.equals(mode)) { printStream.println("Ballerina FHIR API templates generation completed successfully. Generated templates can be found at " + targetOutputPath); diff --git a/native/health-cli/src/main/resources/ballerina-health.help b/native/health-cli/src/main/resources/ballerina-health.help index 24eeab4..bd44e94 100644 --- a/native/health-cli/src/main/resources/ballerina-health.help +++ b/native/health-cli/src/main/resources/ballerina-health.help @@ -13,8 +13,8 @@ DESCRIPTION from a given health specification (eg.:FHIR implementation guides) files. The generated Ballerina sources will be written into the provided - output location. Make sure to add the directory path which contains FHIR specification, - as the last argument. + output location. Make sure to add the directory path which contains + FHIR specification, as the last argument. You can download FHIR specification files from the respective Implementation Guide's official website. (Published list of @@ -36,36 +36,44 @@ COMMANDS OPTIONS -m, --mode - Mode can be 'package' or 'template'. If the mode is set to 'package', - a Ballerina package will be generated including all the records - and types. If the mode is set to ‘template’, tool will generate - Ballerina templates for each FHIR resource definition available - in the specified path. This is a MANDATORY input for fhir command. + Mode can be 'package' or 'template'. If the mode is set to + 'package', a Ballerina package will be generated including all the + records and types. If the mode is set to ‘template’, tool will + generate Ballerina templates for each FHIR resource definition + available in the specified path. This is a MANDATORY input for fhir + command. + --package-name Only applicable in ‘package’ mode. Name of the Ballerina package to be generated. This is a MANDATORY input in ‘package’ mode. Refer https://ballerina.io/learn/package-references/#the-name-field + --dependent-package Only applicable in ‘template’ mode. Fully qualified name of the - published Ballerina package containing IG resources [eg: /]. - This option can be used to generate templates specifically for the - resources in the given IG. The package name part of this value - will be added as a prefix to the template name. - This is a MANDATORY input in ‘template’ mode. + published Ballerina package containing IG resources + [eg: /]. This option can be used to generate + templates specifically for the resources in the given IG. The + package name part of this value will be added as a prefix to the + template name. This is a MANDATORY input in ‘template’ mode. + -o, --output Location of the generated Ballerina artifacts. If this path is not specified, the output will be written to the same directory from which the command is run. + --org-name Organization name of the Ballerina package/template to be generated. Refer https://ballerina.io/learn/package-references/#the-org-field + --package-version Version of the Ballerina package/template to be generated. Refer https://semver.org/ + --included-profile Only applicable in ‘template’ mode. If only a specific profile/s - needs to be generated as templates, specify the profile URL - as the value of this parameter. This argument can be used more than once. + needs to be generated as templates, specify the profile URL as the + value of this parameter. This argument can be used more than once. + --excluded-profile Only applicable in ‘template’ mode. If only a specific profile/s needs to be skipped when generating templates, specify the From c2f9112c3fd114defe36142eba528181bd63cde3 Mon Sep 17 00:00:00 2001 From: ThishaniLucas Date: Wed, 29 Nov 2023 14:48:43 +0530 Subject: [PATCH 2/2] Add flags to print strings --- .../packagegen/tool/ToolConstants.java | 5 +++ .../templategen/PackageTemplateGenerator.java | 4 +- .../tool/BallerinaProjectConstants.java | 9 ++++ .../project/tool/BallerinaProjectTool.java | 10 ++--- .../generator/BallerinaProjectGenerator.java | 4 +- .../io/ballerina/health/cmd/HealthCmd.java | 4 +- .../cmd/core/utils/HealthCmdConstants.java | 22 ++++++++++ .../ballerina/health/cmd/fhir/FhirSubCmd.java | 42 +++++++++---------- 8 files changed, 67 insertions(+), 33 deletions(-) diff --git a/native/fhir-to-bal-lib/src/main/java/org/wso2/healthcare/fhir/ballerina/packagegen/tool/ToolConstants.java b/native/fhir-to-bal-lib/src/main/java/org/wso2/healthcare/fhir/ballerina/packagegen/tool/ToolConstants.java index 89925c1..ab0ec79 100644 --- a/native/fhir-to-bal-lib/src/main/java/org/wso2/healthcare/fhir/ballerina/packagegen/tool/ToolConstants.java +++ b/native/fhir-to-bal-lib/src/main/java/org/wso2/healthcare/fhir/ballerina/packagegen/tool/ToolConstants.java @@ -79,4 +79,9 @@ public enum TokenPosition { public static final String LICENSE_YEAR = "2023"; public static final String DATA_TYPE_EXTENSION = "Extension"; + + public class PrintStrings { + public static final String OVERWRITING_EXISTING_PACKAGE = "[INFO] Overwriting the existing package."; + public static final String INVALID_INPUT = "[ERROR] Invalid input. Exiting the tool."; + } } diff --git a/native/fhir-to-bal-lib/src/main/java/org/wso2/healthcare/fhir/ballerina/packagegen/tool/templategen/PackageTemplateGenerator.java b/native/fhir-to-bal-lib/src/main/java/org/wso2/healthcare/fhir/ballerina/packagegen/tool/templategen/PackageTemplateGenerator.java index e80fe66..6f6fdb5 100644 --- a/native/fhir-to-bal-lib/src/main/java/org/wso2/healthcare/fhir/ballerina/packagegen/tool/templategen/PackageTemplateGenerator.java +++ b/native/fhir-to-bal-lib/src/main/java/org/wso2/healthcare/fhir/ballerina/packagegen/tool/templategen/PackageTemplateGenerator.java @@ -79,9 +79,9 @@ public void generate(ToolContext toolContext, Map generatorPrope if ("n".equalsIgnoreCase(input)) { System.exit(0); } else if ("y".equalsIgnoreCase(input)) { - System.out.println("Overwriting the existing package."); + System.out.println(ToolConstants.PrintStrings.OVERWRITING_EXISTING_PACKAGE); } else { - System.out.println("Invalid input. Exiting the tool."); + System.out.println(ToolConstants.PrintStrings.INVALID_INPUT); System.exit(0); } } diff --git a/native/fhir-to-bal-template/src/main/java/org/wso2/healthcare/fhir/codegen/ballerina/project/tool/BallerinaProjectConstants.java b/native/fhir-to-bal-template/src/main/java/org/wso2/healthcare/fhir/codegen/ballerina/project/tool/BallerinaProjectConstants.java index 6270daf..1dbcde2 100644 --- a/native/fhir-to-bal-template/src/main/java/org/wso2/healthcare/fhir/codegen/ballerina/project/tool/BallerinaProjectConstants.java +++ b/native/fhir-to-bal-template/src/main/java/org/wso2/healthcare/fhir/codegen/ballerina/project/tool/BallerinaProjectConstants.java @@ -30,4 +30,13 @@ public class BallerinaProjectConstants { public static final String SERVICE_PACKAGE_IMPORT_SUFFIX = "ballerinax/health.fhir"; public static final String INTERNATIONAL_PACKAGE_IMPORT_SUFFIX = "ballerinax/health.fhir.r4.international401"; + public class PrintStrings { + + public static final String OVERWRITING_EXISTING_TEMPLATES = "[INFO] Overwriting the existing templates."; + public static final String TEMPLATES_FOR_ALL_PROFILES = "[INFO] Generating templates for all FHIR profiles..."; + + public static final String INVALID_PROFILE = "[WARN] Invalid FHIR profile: "; + + public static final String INVALID_INPUT = "[ERROR] Invalid input. Exiting the tool."; + } } diff --git a/native/fhir-to-bal-template/src/main/java/org/wso2/healthcare/fhir/codegen/ballerina/project/tool/BallerinaProjectTool.java b/native/fhir-to-bal-template/src/main/java/org/wso2/healthcare/fhir/codegen/ballerina/project/tool/BallerinaProjectTool.java index 13449fd..9eeccda 100644 --- a/native/fhir-to-bal-template/src/main/java/org/wso2/healthcare/fhir/codegen/ballerina/project/tool/BallerinaProjectTool.java +++ b/native/fhir-to-bal-template/src/main/java/org/wso2/healthcare/fhir/codegen/ballerina/project/tool/BallerinaProjectTool.java @@ -157,13 +157,13 @@ private List retrieveStructureDef(String igName, Map { structureDefinitions.add(resourceDef.getDefinition()); }); @@ -177,20 +177,20 @@ private List retrieveStructureDef(String igName, Map { structureDefinitions.add(resourceDef.getDefinition()); }); if (resourceDefMap.size() == resourceDefMapCopy.size()) { - System.out.println("Generating templates for all FHIR profiles..."); + System.out.println(BallerinaProjectConstants.PrintStrings.TEMPLATES_FOR_ALL_PROFILES); } return structureDefinitions; } // nothing included or excluded // generate templates for all the profiles - System.out.println("Generating templates for all FHIR profiles..."); + System.out.println(BallerinaProjectConstants.PrintStrings.TEMPLATES_FOR_ALL_PROFILES); resourceDefMap.forEach((k, v) -> { structureDefinitions.add(v.getDefinition()); }); diff --git a/native/fhir-to-bal-template/src/main/java/org/wso2/healthcare/fhir/codegen/ballerina/project/tool/generator/BallerinaProjectGenerator.java b/native/fhir-to-bal-template/src/main/java/org/wso2/healthcare/fhir/codegen/ballerina/project/tool/generator/BallerinaProjectGenerator.java index 8f730eb..e733359 100644 --- a/native/fhir-to-bal-template/src/main/java/org/wso2/healthcare/fhir/codegen/ballerina/project/tool/generator/BallerinaProjectGenerator.java +++ b/native/fhir-to-bal-template/src/main/java/org/wso2/healthcare/fhir/codegen/ballerina/project/tool/generator/BallerinaProjectGenerator.java @@ -56,9 +56,9 @@ public void generate(ToolContext toolContext, Map generatorPrope if ("n".equalsIgnoreCase(input)) { System.exit(0); } else if ("y".equalsIgnoreCase(input)) { - System.out.println("Overwriting the existing templates."); + System.out.println(BallerinaProjectConstants.PrintStrings.OVERWRITING_EXISTING_TEMPLATES); } else { - System.out.println("Invalid input. Exiting the tool."); + System.out.println(BallerinaProjectConstants.PrintStrings.INVALID_INPUT); System.exit(0); } } diff --git a/native/health-cli/src/main/java/io/ballerina/health/cmd/HealthCmd.java b/native/health-cli/src/main/java/io/ballerina/health/cmd/HealthCmd.java index 20413e2..1227744 100644 --- a/native/health-cli/src/main/java/io/ballerina/health/cmd/HealthCmd.java +++ b/native/health-cli/src/main/java/io/ballerina/health/cmd/HealthCmd.java @@ -98,11 +98,11 @@ private void printHelpTextAsStream() { } return; } catch (IOException e) { - printStream.println("Helper text is not available."); + printStream.println(HealthCmdConstants.PrintStrings.HELP_NOT_AVAILABLE); HealthCmdUtils.throwLauncherException(e); } } - printStream.println("An Error occurred internally while fetching the Help text."); + printStream.println(HealthCmdConstants.PrintStrings.HELP_ERROR); HealthCmdUtils.exitError(true); } diff --git a/native/health-cli/src/main/java/io/ballerina/health/cmd/core/utils/HealthCmdConstants.java b/native/health-cli/src/main/java/io/ballerina/health/cmd/core/utils/HealthCmdConstants.java index 92eb5ad..9886da1 100644 --- a/native/health-cli/src/main/java/io/ballerina/health/cmd/core/utils/HealthCmdConstants.java +++ b/native/health-cli/src/main/java/io/ballerina/health/cmd/core/utils/HealthCmdConstants.java @@ -31,5 +31,27 @@ public class HealthCmdConstants { public static final String CMD_DEFAULT_IG_NAME = "healthcare.fhir"; public static final String CMD_DEFAULT_ORG_NAME = "healthcare"; + public class PrintStrings { + + public static final String HELP_FOR_MORE_INFO = "[INFO] Try bal health --help for more information."; + public static final String TEMPLATE_GEN_SUCCESS = "[INFO] Ballerina FHIR API templates generation completed " + + "successfully. Generated templates can be found at: "; + public static final String PKG_GEN_SUCCESS = "[INFO] Ballerina FHIR package generation completed successfully." + + " Generated package can be found at: "; + + public static final String INVALID_SPEC_PATH = "[ERROR] Invalid specification path received for FHIR tool command."; + public static final String HELP_NOT_AVAILABLE = "[ERROR] Helper text is not available."; + public static final String HELP_ERROR = "[ERROR] An Error occurred internally while fetching the Help text."; + public static final String INVALID_NUM_OF_ARGS = "[ERROR] Invalid number of arguments received for FHIR tool " + + "command."; + public static final String INVALID_MODE = "[ERROR] Invalid mode received for FHIR tool command."; + public static final String PKG_NAME_REQUIRED = "[ERROR] Package name [--package-name] is required for package " + + "generation."; + public static final String DEPENDENT_REQUIRED = "[ERROR] Dependent package [--dependent-package] is required " + + "for template generation."; + public static final String DEPENDENT_INCORRECT = "[ERROR] Format of the dependent package is incorrect."; + public static final String INCLUDED_EXCLUDED_TOGETHER = "[ERROR] Both --included-profile and " + + "--excluded-profile cannot be used together."; + } } diff --git a/native/health-cli/src/main/java/io/ballerina/health/cmd/fhir/FhirSubCmd.java b/native/health-cli/src/main/java/io/ballerina/health/cmd/fhir/FhirSubCmd.java index ff0f48c..2d63155 100644 --- a/native/health-cli/src/main/java/io/ballerina/health/cmd/fhir/FhirSubCmd.java +++ b/native/health-cli/src/main/java/io/ballerina/health/cmd/fhir/FhirSubCmd.java @@ -18,13 +18,11 @@ package io.ballerina.health.cmd.fhir; -import com.google.gson.JsonElement; import io.ballerina.cli.BLauncherCmd; import io.ballerina.cli.launcher.BLauncherException; import io.ballerina.health.cmd.core.exception.BallerinaHealthException; import io.ballerina.health.cmd.core.utils.HealthCmdConstants; import io.ballerina.health.cmd.core.utils.HealthCmdUtils; -import io.ballerina.health.cmd.core.utils.JsonTypeConverter; import io.ballerina.health.cmd.handler.Handler; import io.ballerina.health.cmd.handler.HandlerFactory; import picocli.CommandLine; @@ -36,7 +34,6 @@ import java.io.InputStreamReader; import java.io.PrintStream; import java.nio.charset.StandardCharsets; -import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.HashMap; @@ -50,6 +47,7 @@ @CommandLine.Command(name = "fhir", description = "Generates Ballerina service/client for FHIR contract " + "for Ballerina service.") public class FhirSubCmd implements BLauncherCmd { + private final PrintStream printStream; private final boolean exitWhenFinish; private final String toolName = "fhir"; @@ -124,59 +122,59 @@ public void execute() { } return; } catch (IOException e) { - printStream.println("Helper text is not available."); + printStream.println(HealthCmdConstants.PrintStrings.HELP_NOT_AVAILABLE); HealthCmdUtils.throwLauncherException(e); } } - printStream.println("An Error occurred internally while fetching the Help text."); + printStream.println(HealthCmdConstants.PrintStrings.HELP_ERROR); HealthCmdUtils.exitError(exitWhenFinish); } if (argList == null || argList.isEmpty()) { //at minimum arg count is 1 (spec path) - printStream.println("Invalid number of arguments received for FHIR tool command."); - printStream.println("Try bal health --help for more information."); + printStream.println(HealthCmdConstants.PrintStrings.INVALID_NUM_OF_ARGS); + printStream.println(HealthCmdConstants.PrintStrings.HELP_FOR_MORE_INFO); HealthCmdUtils.exitError(exitWhenFinish); } if (mode == null || mode.isEmpty()) { //mode is required param - printStream.println("Invalid mode received for FHIR tool command."); - printStream.println("Try bal health --help for more information."); + printStream.println(HealthCmdConstants.PrintStrings.INVALID_MODE); + printStream.println(HealthCmdConstants.PrintStrings.HELP_FOR_MORE_INFO); HealthCmdUtils.exitError(exitWhenFinish); } if (CMD_MODE_PACKAGE.equals(mode) && (packageName == null || packageName.isEmpty())) { // package name is a required param in package mode - printStream.println("Package name [--package-name] is required for package generation."); - printStream.println("Try bal health --help for more information."); + printStream.println(HealthCmdConstants.PrintStrings.PKG_NAME_REQUIRED); + printStream.println(HealthCmdConstants.PrintStrings.HELP_FOR_MORE_INFO); HealthCmdUtils.exitError(exitWhenFinish); } if (CMD_MODE_TEMPLATE.equals(mode) && (dependentPackage == null || dependentPackage.isEmpty())) { // dependent package is a required param in template mode - printStream.println("Dependent package [--dependent-package] is required for template generation."); - printStream.println("Try bal health --help for more information."); + printStream.println(HealthCmdConstants.PrintStrings.DEPENDENT_REQUIRED); + printStream.println(HealthCmdConstants.PrintStrings.HELP_FOR_MORE_INFO); HealthCmdUtils.exitError(exitWhenFinish); } if (dependentPackage != null && !dependentPackage.isEmpty()) { // regex matching ballerinax/health.fhir.r4 if (!dependentPackage.matches("^(?!.*__)[a-zA-Z0-9][a-zA-Z0-9_]+[a-zA-Z0-9]/[a-zA-Z0-9][a-zA-Z0-9._]+[a-zA-Z0-9]$")) { - printStream.println("Format of the dependent package is incorrect."); - printStream.println("Try bal health --help for more information."); + printStream.println(HealthCmdConstants.PrintStrings.DEPENDENT_INCORRECT); + printStream.println(HealthCmdConstants.PrintStrings.HELP_FOR_MORE_INFO); HealthCmdUtils.exitError(exitWhenFinish); } } if (includedProfiles != null && excludedProfiles != null) { - printStream.println("Both --included-profile and --excluded-profile cannot be used together."); - printStream.println("Try bal health --help for more information."); + printStream.println(HealthCmdConstants.PrintStrings.INCLUDED_EXCLUDED_TOGETHER); + printStream.println(HealthCmdConstants.PrintStrings.HELP_FOR_MORE_INFO); HealthCmdUtils.exitError(exitWhenFinish); } if (this.engageSubCommand(argList)) { if (CMD_MODE_TEMPLATE.equals(mode)) { - printStream.println("Ballerina FHIR API templates generation completed successfully. Generated templates can be found at " + targetOutputPath); + printStream.println(HealthCmdConstants.PrintStrings.TEMPLATE_GEN_SUCCESS + targetOutputPath); } else { - printStream.println("Ballerina FHIR package generation completed successfully. Generated package can be found at " + targetOutputPath); + printStream.println(HealthCmdConstants.PrintStrings.PKG_GEN_SUCCESS + targetOutputPath); } } else { - printStream.println("Invalid mode received for FHIR tool command."); - printStream.println("Try bal health --help for more information."); + printStream.println(HealthCmdConstants.PrintStrings.INVALID_MODE); + printStream.println(HealthCmdConstants.PrintStrings.HELP_FOR_MORE_INFO); } HealthCmdUtils.exitError(exitWhenFinish); @@ -216,7 +214,7 @@ public boolean engageSubCommand(List argList) { try { specificationPath = HealthCmdUtils.validateAndSetSpecificationPath(argList.get(argList.size() - 1), executionPath.toString()); } catch (BallerinaHealthException e) { - printStream.println("Invalid specification path received for FHIR tool command."); + printStream.println(HealthCmdConstants.PrintStrings.INVALID_SPEC_PATH); throw new BLauncherException(); } Handler toolHandler = null;