From 8973d52d544fb05baccf9e93863c7082e63ad8e8 Mon Sep 17 00:00:00 2001 From: AnjanaSenanayake Date: Tue, 5 Dec 2023 10:34:18 +0530 Subject: [PATCH] Fix Issues - Remove special characters from resource profiles. - Remove special characters from strings. - Skip generating templates for resource Bundle, CodeSystem, DomainResource, OperationOutcome, Resource and ValueSet. --- .../project/tool/BallerinaProjectTool.java | 9 +++++++-- .../project/tool/model/FHIRProfile.java | 3 ++- .../tool/util/BallerinaProjectUtil.java | 20 +++++++++++++++++++ .../src/main/resources/template/balService.vm | 2 +- 4 files changed, 30 insertions(+), 4 deletions(-) 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 9eeccda..c43e598 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.Arrays; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -49,6 +50,9 @@ public class BallerinaProjectTool extends AbstractFHIRTool { private final Map igMap = new HashMap<>(); private final Map serviceMap = new HashMap<>(); private final Map dependenciesMap = new HashMap<>(); + + private final List EXCLUDED_FHIR_APIS = new ArrayList<>(Arrays.asList("Bundle", + "CodeSystem", "DomainResource", "OperationOutcome", "Resource", "ValueSet")); private BallerinaProjectToolConfig ballerinaProjectToolConfig; @Override @@ -121,7 +125,8 @@ private void populateBalService() { // extract structure definitions of resource types Map resourceDefMap = new HashMap<>(); entry.getValue().getResources().forEach((k, resourceDef) -> { - if (resourceDef.getDefinition().getKind().toCode().equalsIgnoreCase("RESOURCE")) { + if (!EXCLUDED_FHIR_APIS.contains(resourceDef.getDefinition().getName()) && resourceDef.getDefinition() + .getKind().toCode().equalsIgnoreCase("RESOURCE")) { resourceDefMap.put(k, resourceDef); } }); @@ -201,7 +206,7 @@ private SearchParam getSearchParam(Map.Entry paramet SearchParam param = new SearchParam(parameter.getValue().getSearchParameter().getName(), parameter.getValue().getSearchParameter().getCode()); param.setSearchParamDef(parameter.getValue().getSearchParameter()); - param.setDescription(parameter.getValue().getSearchParameter().getDescription()); + param.setDescription(parameter.getValue().getSearchParameter().getDescription().replace("\"","")); param.setDocumentation(parameter.getValue().getSearchParameter().getUrl()); param.setTargetResource(apiName); if (ballerinaProjectToolConfig.getSearchParamConfigs().contains(param.getSearchParamDef().getCode())) { diff --git a/native/fhir-to-bal-template/src/main/java/org/wso2/healthcare/fhir/codegen/ballerina/project/tool/model/FHIRProfile.java b/native/fhir-to-bal-template/src/main/java/org/wso2/healthcare/fhir/codegen/ballerina/project/tool/model/FHIRProfile.java index ce0bea9..7e870b2 100644 --- a/native/fhir-to-bal-template/src/main/java/org/wso2/healthcare/fhir/codegen/ballerina/project/tool/model/FHIRProfile.java +++ b/native/fhir-to-bal-template/src/main/java/org/wso2/healthcare/fhir/codegen/ballerina/project/tool/model/FHIRProfile.java @@ -22,6 +22,7 @@ import org.apache.commons.text.CaseUtils; import org.hl7.fhir.r4.model.StructureDefinition; import org.wso2.healthcare.fhir.codegen.ballerina.project.tool.config.BallerinaProjectToolConfig; +import org.wso2.healthcare.fhir.codegen.ballerina.project.tool.util.BallerinaProjectUtil; import java.util.*; @@ -47,7 +48,7 @@ public FHIRProfile(StructureDefinition profileDef, String url, String igName, St this.profileDef = profileDef; this.url = url; this.importsList = new HashSet<>(); - this.name = profileDef.getName(); + this.name = BallerinaProjectUtil.resolveSpecialCharacters(profileDef.getName()); } public StructureDefinition getProfileDef() { diff --git a/native/fhir-to-bal-template/src/main/java/org/wso2/healthcare/fhir/codegen/ballerina/project/tool/util/BallerinaProjectUtil.java b/native/fhir-to-bal-template/src/main/java/org/wso2/healthcare/fhir/codegen/ballerina/project/tool/util/BallerinaProjectUtil.java index 3f58644..5afd564 100644 --- a/native/fhir-to-bal-template/src/main/java/org/wso2/healthcare/fhir/codegen/ballerina/project/tool/util/BallerinaProjectUtil.java +++ b/native/fhir-to-bal-template/src/main/java/org/wso2/healthcare/fhir/codegen/ballerina/project/tool/util/BallerinaProjectUtil.java @@ -22,12 +22,19 @@ import java.util.List; import java.util.Map; +import java.util.regex.Pattern; /** * Utility class for Ballerina project generator. */ public class BallerinaProjectUtil { + /** + * Aggregate substrings to a camelcase string. + * + * @param substrings list of substrings + * @return aggregated string + */ public static String aggregateCamelcase(List substrings) { String aggregatedName = substrings.get(0).toLowerCase(); @@ -41,4 +48,17 @@ public static String aggregateCamelcase(List substrings) { } return aggregatedName; } + + /** + * Resolve for special character + * + * @param specialChar special character + * @return preferred string replacement + */ + public static String resolveSpecialCharacters(String specialChar) { + return specialChar.replaceAll(Pattern.quote("[x]"), "") + .replaceAll(Pattern.quote("/"), "") + .replaceAll("-", "_") + .replaceAll("\\s+", ""); + } } diff --git a/native/fhir-to-bal-template/src/main/resources/template/balService.vm b/native/fhir-to-bal-template/src/main/resources/template/balService.vm index 5934a3d..36671ce 100644 --- a/native/fhir-to-bal-template/src/main/resources/template/balService.vm +++ b/native/fhir-to-bal-template/src/main/resources/template/balService.vm @@ -24,7 +24,7 @@ import $import.toLowerCase(); # Generic type to wrap all implemented profiles. # Add required profile types here. # public type $service.getName() $basePackageImportIdentifier:$service.getName()|; -public type $service.getName()#**##foreach($profile in $service.getProfileList()) $profile.getPackagePrefix():$profile.getName()#if($foreach.hasNext)| #end#end; +public type $service.getName() #**##foreach($profile in $service.getProfileList())$profile.getPackagePrefix():$profile.getName()#if($foreach.hasNext)|#end#end; # initialize source system endpoint here