cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

how to get localized value from component by using java code

Former Member
0 Likes
2,588

Hi Expert,

I need to get content from model. I have written the query as below: Select {content} from {PostalContentComponent} where {uid}='AboutEgyptPostContentComponent'

I'm able to get the result in hac as well as code level.

But I need to fetch the localized value for which query is working in hac but not in code level. HAC Query is: Select {content[ar]} from {PostalContentComponent} where {uid}='AboutEgyptPostContentComponent'

i'm getting below exception: Caused by: java.sql.SQLSyntaxErrorException: incompatible data type in conversion: from SQL type VARCHAR to java.lang.Long, value:

Could you please help how to get the localized value by using java code.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Likes

just write a query in HAC and click on SQL query and you will understand how localized attributes work in Hybris.

for example, below is a query where I am fetching product code

 select {code} from {SizeVariantProduct!} where {code} = '617850'

its SQL query translates into

 SELECT  item_t0.p_code  FROM sizevariantproduct item_t0 WHERE ( item_t0.p_code  = '617850') AND (item_t0.TypePkString=? )

Now if I search for name which is a localized attribute, my flexi query and sql query translates into the following. Flexi:

 select {name} from {SizeVariantProduct!} where {code} = '617850'

SQL:

 SELECT  lp_t0.p_name  FROM sizevariantproduct item_t0 JOIN sizevariantproductlp lp_t0 ON item_t0.PK = lp_t0.ITEMPK AND lp_t0.LANGPK =?  WHERE ( item_t0.p_code  = '617850') AND (item_t0.TypePkString=? )

See how the query that contains a localized attribute has a join with a table 'sizevariantproductlp'

So I think what you can do is write your query the following way:

 Select {pk} from {PostalContentComponent} where {uid}='AboutEgyptPostContentComponent'


This query would return PostalContentComponentModel, once you get the model, you can fetch the content by passing the locale as 'AR'

 // Inject CommonI18NService as a spring bean
 Locale locale = i18NService.getLocaleForIsocode('AR');
 
 PostalContentComponentModel model = // fetch model from the above query
 
 String content = model.getContent(locale);
 
 

Former Member
0 Likes

Hi Saurabh,

I'm able to get localized content by passing "Locale". Thanks for your response.

Answers (1)

Answers (1)

0 Likes

In case, if I understand the problem correctly, you want to retrieve String content of the component, hence, in Java code, you need to explicitly point that:

 final String q = "SELECT {content[ar]} FROM {PostalContentComponent} WHERE {uid} = 'AboutEgyptPostContentComponent'";
 final FlexibleSearchQuery fq = new FlexibleSearchQuery(q);
 fq.setResultClassList(Collections.singletonList(String.class));
 final List<String> result = flexibleSearchService.<String>search(fq).getResult()

Also, there is an alternative: retrieve localized content from the model itself:

 final String q = "SELECT {pk} FROM {PostalContentComponent} WHERE {uid} = 'AboutEgyptPostContentComponent'";
 final List<PostalContentComponentModel> components = flexibleSearchService.<PostalContentComponentModel>search(q).getResult();
 final Locale arLocale = new Locale("ar");
 final List<String> result = components.stream().map(compoment -> compoment.getContent(arLocale))
         .collect(Collectors.toList());
Former Member
0 Likes

Hi Sergii,

Thanks for your response. It's working.