Message Boards

Sorting/ordering of localized entities in Liferay 7.1

Mirko Romstadt, modified 3 Years ago.

Sorting/ordering of localized entities in Liferay 7.1

New Member Posts: 17 Join Date: 7/23/14 Recent Posts
Hello everybody,

I am currently working on localizing a portlet for Liferay 7.1. I followed https://liferay.dev/blogs/-/blogs/new-since-liferay-7-1-localized-entities and it worked out pretty well, I now have (beside other columns) a localized title. But sorting does not work anymore. If I use the old OrderByComparator (something like EntityTitleOrderByComparator extends OrderByComparator<Entity>), I get this error:

ERROR [http-nio-8080-exec-3][PortletServlet:112] javax.portlet.PortletException: org.apache.jasper.JasperException: javax.el.ELException: java.lang.IllegalArgumentException: Unknown column name title


This makes sense, there is no column called "title" anymore. So I thought, let's see how Liferay does it. I checked for JournalArticle because localization is used here and I checked it is working fine. If a language for an entry is not present for the users selected language, it takes the default language and sorts the titles correctly. So I had a look into https://github.com/liferay/liferay-portal/blob/master/modules/apps/journal/journal-web/src/main/java/com/liferay/journal/web/internal/display/context/JournalDisplayContext.java#L1197 which gets its OrderByComparator from https://github.com/liferay/liferay-portal/blob/master/modules/apps/journal/journal-web/src/main/java/com/liferay/journal/web/internal/util/JournalPortletUtil.java#L78.

On line 105 you can see that it creates a new ArticleTitleComparator, so I had a look into this one: https://github.com/liferay/liferay-portal/blob/master/modules/apps/journal/journal-api/src/main/java/com/liferay/journal/util/comparator/ArticleTitleComparator.java#L26.

This class is "@deprecated As of Judson (7.1.x), with no direct replacement", so I wonder how (and if) it is working on Liferay 7.1. My class looks exactly the same but is not working. I would like to know how to properly implement sorting with Liferay 7.1 and the new localization.

If needed, I could create a small example portlet to show my not-working implementation with the new localization and the old OrderByComparator. But I hope, someone could give me a hint of how to implement the sorting, maybe even with a code example from Liferay's source code.

Btw.: I also had a look into https://github.com/liferay/liferay-portal/blob/master/modules/apps/journal/journal-service/src/main/java/com/liferay/journal/service/persistence/impl/JournalArticlePersistenceImpl.java and https://github.com/liferay/liferay-portal/blob/master/modules/apps/journal/journal-service/src/main/java/com/liferay/journal/service/persistence/impl/JournalArticleLocalizationPersistenceImpl.java to see if there are any JOINs in the database query, but I couldn't figure how the Service Builder is managing the localization part.

Thanks,
Mirko
thumbnail
David H Nebinger, modified 3 Years ago.

RE: Sorting/ordering of localized entities in Liferay 7.1

Liferay Legend Posts: 14919 Join Date: 9/2/06 Recent Posts
I'll see what I can find Mirko...
Mirko Romstadt, modified 3 Years ago.

RE: Sorting/ordering of localized entities in Liferay 7.1

New Member Posts: 17 Join Date: 7/23/14 Recent Posts
Hey David,

Did you have a chance to look into this? It would actually help me if you can tell me where I have to look emoticon

Greetings,
Mirko
thumbnail
David H Nebinger, modified 3 Years ago.

RE: Sorting/ordering of localized entities in Liferay 7.1

Liferay Legend Posts: 14919 Join Date: 9/2/06 Recent Posts
For DQ access to the localized columns, you have to get a reference to the XxxLocalizationPersistence service for the entity so you can get access to the findWithDynamicQuery() method.

For sorting, they may call that class deprecated since 7.1 but it still exists in master (7.3). Perhaps they want to deprecate it, but until there's a solution it is probably going to stay in as-is, so you should be safe to follow the same.
Mirko Romstadt, modified 3 Years ago.

RE: Sorting/ordering of localized entities in Liferay 7.1

New Member Posts: 17 Join Date: 7/23/14 Recent Posts
Hey David,

Thanks for your help and sorry for my late reply, I had no time to look into this until now.

I checked the findWithDynamicQuery() method, but this is actually not what I am looking for. For the XxxLocalizationPersistence this gives me a list of localizations for one specific entity, but I am interested in a list of entities that are sorted based on the localization. As an example:

I have a site that supports english and german with english as the default language. Then I have a user that has set his language to german. Furthermore I have two entities. The first entity has set its english title to "B" and no translation for the german title. The second entity has set its english title to "C" and its german translation to "A".

The given user should now have a list that shows the second entity first as "A" and then the first entity as "B", while another user who has set his language to english would have the first entity "B" first and then the second entity shown as "C".

So I invested some time to get this working, but I cannot manage to "reproduce" the way the localization is done in Liferay's JournalArticle. I created a small example portlet that focuses solely on the localization and demonstrates my problem with sorting localized titles.

First I created a service with the service builder that has an entity "MyEntity". The interesting part in the service.xml:


<column name="unlocalizedDescription" type="String" />

<localized-entity>
    <localized-column name="localizedTitle" />
    
    <order by="asc">
        <order-column name="localizedTitle" />
    </order>
</localized-entity>


So the entity has one normal string called "unlocalizedDescription" and one localized string that is stored in the MyEntityLocalization table called "localizedTitle".

I added the necessary methods to the MyEntityLocalServiceImpl to add, update and get the entities. Then I created a simple web module to demonstrate my issue called "Localization Example". I created two display context classes for the search container and for the clay management toolbar, one util class to get the OrderByComparator, and some action classes for the render and action commands. I tried to be as close as possible to the JournalArticle implementation while simplifying as much as possible it to my needs, of course. Then I added the necessary jsp's to render the frontend.

Finally I added three OrderByComparators to the "localization-example-api" module which are the core of my issue. While the MyEntityCreateDateComparator (which is used by default) and the MyEntityDescriptionComparator work fine, the MyEntityTitleComparator doesn't, it throws the given error in my first post.

You can test this by deploying the "Localization Example" portlet, then adding some entities, and finally changing the "Filter and Order" to "Title".
The only real difference I see to the Liferay implementation is that they manually add the JournalArticleLocalization table in their service.xml instead of using the <localized-entity> syntax. But the result should be the same and also the latter one should be the way to go, shouldn't it?

I am really out of ideas here and would appreciate any help or hints for this issue!

You will find the zipped example portlet here: https://we.tl/t-9UQ3WZSTyP

Thanks in advance,
Mirko