From f224335c967a2dea7f4e276e011e2c95f2131a8a Mon Sep 17 00:00:00 2001 From: hongwei Date: Mon, 6 Mar 2023 13:20:08 +0100 Subject: [PATCH] feature/added new i180n fields for resourceDocs --- .../main/scala/code/api/util/I18NUtil.scala | 17 ++----- .../code/api/v1_4_0/JSONFactory1_4_0.scala | 46 ++++++++++++++----- .../api/v1_4_0/JSONFactory1_4_0Test.scala | 14 +++--- .../commons/model/enums/Enumerations.scala | 5 ++ 4 files changed, 52 insertions(+), 30 deletions(-) diff --git a/obp-api/src/main/scala/code/api/util/I18NUtil.scala b/obp-api/src/main/scala/code/api/util/I18NUtil.scala index 56ca68399..89421f599 100644 --- a/obp-api/src/main/scala/code/api/util/I18NUtil.scala +++ b/obp-api/src/main/scala/code/api/util/I18NUtil.scala @@ -5,6 +5,7 @@ import code.api.Constant.PARAM_LOCALE import java.util.{Date, Locale} import code.webuiprops.MappedWebUiPropsProvider.getWebUiPropsValue +import com.openbankproject.commons.model.enums.I18NResourceDocField import net.liftweb.common.Full import net.liftweb.http.S import net.liftweb.http.provider.HTTPCookie @@ -48,19 +49,11 @@ object I18NUtil { } object ResourceDocTranslation { - def summary(operationId: String, locale: Option[String], default: String): String = { + def translate(fieldName: I18NResourceDocField.Value, operationId: String, locale: Option[String], default: String): String = { locale match { - case Some(locale) => - val webUiKey = s"webui_resource_doc_operation_id_${operationId}_summary_${locale}" - getWebUiPropsValue(webUiKey, default) - case None => - default - } - } - def description(operationId: String, language: Option[String], default: String): String = { - language match { - case Some(locale) => - val webUiKey = s"webui_resource_doc_operation_id_${operationId}_description_${locale}" + case Some(locale)=> + val webUiKeyString = "webui_resource_doc_operation_id_" + val webUiKey = s"$webUiKeyString${operationId}_${fieldName.toString.toLowerCase}_${locale}" getWebUiPropsValue(webUiKey, default) case None => default diff --git a/obp-api/src/main/scala/code/api/v1_4_0/JSONFactory1_4_0.scala b/obp-api/src/main/scala/code/api/v1_4_0/JSONFactory1_4_0.scala index a2c08cf34..43e1cf85e 100644 --- a/obp-api/src/main/scala/code/api/v1_4_0/JSONFactory1_4_0.scala +++ b/obp-api/src/main/scala/code/api/v1_4_0/JSONFactory1_4_0.scala @@ -12,6 +12,7 @@ import com.openbankproject.commons.model.ListResult import code.crm.CrmEvent.CrmEvent import com.openbankproject.commons.model.TransactionRequestTypeCharge import com.openbankproject.commons.model.{Product, _} +import com.openbankproject.commons.model.enums.I18NResourceDocField import com.openbankproject.commons.util.{EnumValue, JsonUtils, OBPEnumeration, ReflectUtils} import net.liftweb.common.Full import net.liftweb.json @@ -423,7 +424,7 @@ object JSONFactory1_4_0 extends MdcLoggable{ * **URL Parameters**: * [BANK_ID](/glossary#Bank.bank_id):gh.29.uk */ - def prepareUrlParameterDescription(requestUrl: String): String = { + def prepareUrlParameterDescription(requestUrl: String, urlParametersI18n: String): String = { val noQueryParamUrl = StringUtils.substringBefore(requestUrl, "?") //1rd: get the parameters from URL: val findMatches = StringUtils.split(noQueryParamUrl, "/") @@ -432,7 +433,7 @@ object JSONFactory1_4_0 extends MdcLoggable{ if(findMatches.nonEmpty) { val urlParameters: List[String] = findMatches.toList.sorted val parametersDescription: List[String] = urlParameters.map(i => prepareDescription(i, Nil)) - parametersDescription.mkString("\n\n\n**URL Parameters:**", "", "\n") + parametersDescription.mkString(s"\n\n\n**$urlParametersI18n**", "", "\n") } else { "" } @@ -497,7 +498,7 @@ object JSONFactory1_4_0 extends MdcLoggable{ } - def prepareJsonFieldDescription(jsonBody: scala.Product, jsonType: String): String = { + def prepareJsonFieldDescription(jsonBody: scala.Product, jsonType: String, jsonRequestBodyFieldsI18n: String, jsonResponseBodyFieldsI18n: String): String = { val allFields = getAllFields(jsonBody) val (jsonBodyJValue: json.JValue, allFieldsAndOptionStatus) = checkFieldOption(jsonBody, allFields) // Group by is mandatory criteria and sort those 2 groups by name of the field @@ -509,14 +510,14 @@ object JSONFactory1_4_0 extends MdcLoggable{ val jsonFieldsDescription = jsonBodyFields.map(i => prepareDescription(i, allFieldsAndOptionStatus)) - val jsonTitleType = if (jsonType.contains("request")) "\n\n\n**JSON request body fields:**\n\n" else "\n\n\n**JSON response body fields:**\n\n" + val jsonTitleType = if (jsonType.contains("request")) s"\n\n\n**$jsonRequestBodyFieldsI18n**\n\n" else s"\n\n\n**$jsonResponseBodyFieldsI18n**\n\n" jsonFieldsDescription.mkString(jsonTitleType,"","\n") } private val createResourceDocJsonMemo = new ConcurrentHashMap[ResourceDoc, ResourceDocJson] - def createResourceDocJson(rd: ResourceDoc, isVersion4OrHigher:Boolean, locale: Option[String]) : ResourceDocJson = { + def createResourceDocJson(rd: ResourceDoc, isVersion4OrHigher:Boolean, locale: Option[String], urlParametersI18n:String ,jsonRequestBodyFieldsI18n:String, jsonResponseBodyFieldsI18n:String) : ResourceDocJson = { // We MUST recompute all resource doc values due to translation via Web UI props val endpointTags = getAllEndpointTagsBox(rd.operationId).map(endpointTag =>ResourceDocTag(endpointTag.tagName)) val resourceDocUpdatedTags: ResourceDoc = rd.copy(tags = endpointTags++ rd.tags) @@ -539,28 +540,29 @@ object JSONFactory1_4_0 extends MdcLoggable{ "" } else{ //1st: prepare the description from URL - val urlParametersDescription: String = prepareUrlParameterDescription(resourceDocUpdatedTags.requestUrl) + val urlParametersDescription: String = prepareUrlParameterDescription(resourceDocUpdatedTags.requestUrl, urlParametersI18n) //2rd: get the fields description from the post json body: val exampleRequestBodyFieldsDescription = if (resourceDocUpdatedTags.requestVerb=="POST" ){ - prepareJsonFieldDescription(resourceDocUpdatedTags.exampleRequestBody,"request") + prepareJsonFieldDescription(resourceDocUpdatedTags.exampleRequestBody,"request", jsonRequestBodyFieldsI18n, jsonResponseBodyFieldsI18n) } else { "" } //3rd: get the fields description from the response body: //response body can be a nest class, need to loop all the fields. - val responseFieldsDescription = prepareJsonFieldDescription(resourceDocUpdatedTags.successResponseBody,"response") + val responseFieldsDescription = prepareJsonFieldDescription(resourceDocUpdatedTags.successResponseBody,"response", jsonRequestBodyFieldsI18n, jsonResponseBodyFieldsI18n) urlParametersDescription ++ exampleRequestBodyFieldsDescription ++ responseFieldsDescription } - val resourceDocDescription = I18NUtil.ResourceDocTranslation.description( + val resourceDocDescription = I18NUtil.ResourceDocTranslation.translate( + I18NResourceDocField.DESCRIPTION, resourceDocUpdatedTags.operationId, locale, resourceDocUpdatedTags.description.stripMargin.trim ) val description = resourceDocDescription ++ fieldsDescription val summary = resourceDocUpdatedTags.summary.replaceFirst("""\.(\s*)$""", "$1") // remove the ending dot in summary - val translatedSummary = I18NUtil.ResourceDocTranslation.summary(resourceDocUpdatedTags.operationId, locale, summary) + val translatedSummary = I18NUtil.ResourceDocTranslation.translate(I18NResourceDocField.SUMMARY, resourceDocUpdatedTags.operationId, locale, summary) ResourceDocJson( operation_id = resourceDocUpdatedTags.operationId, @@ -588,13 +590,33 @@ object JSONFactory1_4_0 extends MdcLoggable{ } def createResourceDocsJson(resourceDocList: List[ResourceDoc], isVersion4OrHigher:Boolean, locale: Option[String]) : ResourceDocsJson = { + val urlParametersI18n = I18NUtil.ResourceDocTranslation.translate( + I18NResourceDocField.URL_PARAMETERS, + "resourceDocUrlParametersString_i180n", + locale, + "URL Parameters:" + ) + + val jsonRequestBodyFields = I18NUtil.ResourceDocTranslation.translate( + I18NResourceDocField.JSON_REQUEST_BODY_FIELDS, + "resourceDocJsonRequestBodyFieldsString_i180n", + locale, + "JSON request body fields:" + ) + val jsonResponseBodyFields = I18NUtil.ResourceDocTranslation.translate( + I18NResourceDocField.JSON_RESPONSE_BODY_FIELDS, + "resourceDocJsonResponseBodyFieldsString_i180n", + locale, + "JSON response body fields:" + ) + if(isVersion4OrHigher){ ResourceDocsJson( - resourceDocList.map(createResourceDocJson(_,isVersion4OrHigher, locale)), + resourceDocList.map(createResourceDocJson(_,isVersion4OrHigher, locale, urlParametersI18n, jsonRequestBodyFields, jsonResponseBodyFields)), meta=Some(ResourceDocMeta(new Date(), resourceDocList.length)) ) } else { - ResourceDocsJson(resourceDocList.map(createResourceDocJson(_,false, locale))) + ResourceDocsJson(resourceDocList.map(createResourceDocJson(_,false, locale, urlParametersI18n, jsonRequestBodyFields, jsonResponseBodyFields))) } } diff --git a/obp-api/src/test/scala/code/api/v1_4_0/JSONFactory1_4_0Test.scala b/obp-api/src/test/scala/code/api/v1_4_0/JSONFactory1_4_0Test.scala index 23b8de0c0..81f3311b2 100644 --- a/obp-api/src/test/scala/code/api/v1_4_0/JSONFactory1_4_0Test.scala +++ b/obp-api/src/test/scala/code/api/v1_4_0/JSONFactory1_4_0Test.scala @@ -54,7 +54,7 @@ class JSONFactory1_4_0Test extends V140ServerSetup with DefaultUsers { scenario("prepareJsonFieldDescription should work well - users object") { val usersJson = usersJsonV400 - val description = JSONFactory1_4_0.prepareJsonFieldDescription(usersJson, "response") + val description = JSONFactory1_4_0.prepareJsonFieldDescription(usersJson, "response", "JSON request body fields:", "JSON response body fields:") description.contains( """ |JSON response body fields: @@ -85,19 +85,20 @@ class JSONFactory1_4_0Test extends V140ServerSetup with DefaultUsers { ) should be (false) println(description) } - + + val urlParameters = "URL Parameters:" scenario("PrepareUrlParameterDescription should work well, extract the parameters from URL") { val requestUrl1 = "/obp/v4.0.0/banks/BANK_ID/accounts/account_ids/private" - val requestUrl1Description = JSONFactory1_4_0.prepareUrlParameterDescription(requestUrl1) + val requestUrl1Description = JSONFactory1_4_0.prepareUrlParameterDescription(requestUrl1,urlParameters) requestUrl1Description contains ("[BANK_ID]") should be (true) val requestUrl2 = "/obp/v4.0.0/banks/BANK_ID/accounts/ACCOUNT_ID/VIEW_ID" - val requestUrl2Description = JSONFactory1_4_0.prepareUrlParameterDescription(requestUrl2) + val requestUrl2Description = JSONFactory1_4_0.prepareUrlParameterDescription(requestUrl2, urlParameters) requestUrl2Description contains ("[BANK_ID]") should be (true) requestUrl2Description contains ("[ACCOUNT_ID]") should be (true) requestUrl2Description contains ("[VIEW_ID]") should be (true) val requestUrl3 = "/obp/v4.0.0/banks/BANK_ID/accounts/ACCOUNT_ID/VIEW_ID?date=2020-11-11" - val requestUrl3Description = JSONFactory1_4_0.prepareUrlParameterDescription(requestUrl3) + val requestUrl3Description = JSONFactory1_4_0.prepareUrlParameterDescription(requestUrl3, urlParameters) requestUrl2Description shouldEqual(requestUrl3Description) } @@ -114,7 +115,8 @@ class JSONFactory1_4_0Test extends V140ServerSetup with DefaultUsers { scenario("createResourceDocJson should work well, no exception is good enough") { val resourceDoc: ResourceDoc = OBPAPI3_0_0.allResourceDocs(5) - val result: ResourceDocJson = JSONFactory1_4_0.createResourceDocJson(resourceDoc,false, None) + val result: ResourceDocJson = JSONFactory1_4_0.createResourceDocJson(resourceDoc,false, None, + urlParameters, "JSON request body fields:", "JSON response body fields:") } scenario("createResourceDocsJson should work well, no exception is good enough") { diff --git a/obp-commons/src/main/scala/com/openbankproject/commons/model/enums/Enumerations.scala b/obp-commons/src/main/scala/com/openbankproject/commons/model/enums/Enumerations.scala index 06741be75..d242d4870 100644 --- a/obp-commons/src/main/scala/com/openbankproject/commons/model/enums/Enumerations.scala +++ b/obp-commons/src/main/scala/com/openbankproject/commons/model/enums/Enumerations.scala @@ -227,6 +227,11 @@ object AccountRoutingScheme extends Enumeration { } +object I18NResourceDocField extends Enumeration { + type I18NResourceDocsField = Value + val SUMMARY, DESCRIPTION, URL_PARAMETERS, JSON_REQUEST_BODY_FIELDS, JSON_RESPONSE_BODY_FIELDS = Value +} + //-------------------simple enum definition, just some sealed trait way, start------------- trait SimpleEnum extends JsonAble { override def toJValue(implicit format: Formats): JValue = {