Parametrize portlets and access these params from vm-side - liferay

I have portlets, and I want to separate them on two groups by some static property. By static I mean this property is the same for all instances (so, it is not the same like portlet configuration as far as I understand it - quite possible, I'm wrong).
So, what is the best way to differ this portlets in liferay vm-based theme?
Off the top of my head, I can imagine that I'm putting a properties file which I can access from servlet context. This solution is terrifying me (reading file on each request?!!), besides, I don't know how I exactly can access servlet context from velocity template.
I would appreciate any help with humility.

Related

Access a property defined in portal-ext.properties from a theme that uses Freemarker - Liferay 7

I need to obtain a property that is set in portal-ext.properties from my theme's portal_normal.ftl file. Using Liferay 7.
All my searches have come up short, but I can't believe I'm the first one to have this need.
For background, I need to display a web content that is stored in the global site. This needs to be displayed on every site, which is why it's going into the theme. It also needs to be updated by the content editors, so the content can't be included in the theme.
I can display the web content easily, using the journalArticleLocalService like this
journalArticleLocalService.fetchArticle(10197,"1260480")
but I obviously don't want to hard code the group ID and article ID like this; and they are already stored in portal-ext.properties for another reason.
Also, I know about theme configuration values, I use them for a lot of other configuration needs, but those require the values to be entered for each site and there are hundreds of sites in our implementation so this is not an option.
Thanks in advance for any solutions or advice you can provide.
Properties are (as far as I know) not available in Liferay's Freemarker Templates - and as everything in Liferay tries to phase them out in favor of OSGi based configuration) I'd not recommend keeping up the configuration there. Any change requires a restart of the portal - proper configuration would make them hot-changeable.
However, as you now have this situation and probably look for a quick solution: You can create a TemplateContextContributor and make the required values available for your template.

Liferay Service Builder and portlet instance specific data

Does Liferay Service Builder peristence provide mechanism for isolating stored data into separate portlet instance specific (portlet added into two separate pages) tables?
If it is not possible what is the preferred approach to avoid mixing portlet data on two different portal pages?
As this is stackoverflow and related to anything with source code: You can create that single scope yourself - Liferay's scope is just on "group". Any other criteria that you add to the data can be there, but the filtering needs to be handled by you.
A non-programming way to achieve your goal is to open a subscope for your portlet. This can be inspected with (for example) the MessageBoards portlet: In its configuration you can select if you want the scope to be the "Site" or the "Page". Effectively this creates a new "group" (the API name for the scope) and you can just handle the data as if they were in a completely different site.
This illustrates one of the reasons why the API calls it groupId and not siteId, because it's not limited to denote a site.

When to go for getViewForPage in Hybris

I have seen methods in Hybris returning a JSP or dynamically doing getViewForPage.What is the difference between the two approaches.Is it the static or dynamic difference?What should be used when?Can I add new components with just returning a JSP using return statement?
I would strongly recommend to use the getViewForPage method every time. There are a lot of reasons why. Here are just the 2 most important from my point of view:
For every CMS Page Template you can define the "frontendTemplateName", which is the reference to the JSP view. The advantage here is that it can be maintained in the database. So if you decide to use a different JSP to display a CMS Page Template, all you need is to change the frontend template name and hybris uses a different JSP to render this template. This advantage is gone if you return just the JSP file. It also helps to avoid repetition of the JSP name in your code.
Additionally consider restrictions to pages. hybris is able to display different pages depending on maintained restrictions. It is not possible to use restrictions on CMS Pages when using the JSP name in your code.
As long as you use the "storeCmsPageInModel" method, you can use CMS Components in your template. No matter if you use JSP or getViewForPage. However with the getViewForPage method you make sure, that the right JSP is used and that the configured frontend template name has the right content slots to display your components.
Maybe this question is not intended, but I would strongly recommend to use CMS Pages for every possible purpose. We developers have seen requirements change over time and CMS Pages offer all the flexibility you need to respond to this change.

Liferay: How can I get the pages of the site in a web content?

I have a portal in Liferay 6.2, and need to design the velocity template of a web content that must have a menu listing the pages (linked names) of the site where is present.
My questions are:
Is this possible?
What would be the correct way to do this?
Would it be better to make a portlet instead of a web content for this purpose?
Thanks for the help.
It feels a bit like you are trying to solve many problems in a single template - consider to compose the UI from many different elements (e.g. custom portlets) rather than building the one structure/template that fits all requirements.
That being said, as there's also the chance that your template doesn't do more than just displaying the current navigation: You have two options: The out-of-the-box Navigation portlet is quite configurable, you might be able to utilize that one instead of implementing anything yourself (check the configuration options).
And lastly, if you want to implement for yourself: Get hold of the themeDisplay object. With getLayout() you'll get the current page, while getLayouts() you'll get all pages of the current site and can enumerate them. However, there's one problem: You typically don't have access to the themeDisplay object from a CMS template. But there are several ways to still get to the data (search the Liferay forums for cms template themedisplay). Also, an Application Display Template will be a lot more powerful - and you can also check how the layouts collection is built - just search for usage of ThemeDisplay.setLayouts in Liferay's source code. But with ADT we're diverting from your original question.
Liferay offers a sitemap portlet out-of-the-box which lists pages of a site. You can configure it and define your own application display template (ADT).

Utilize Organization selection in custom portlet

I want to use Liferay's Organization select/search feature such as when adding a new user through the Control Panel (under Users and Organizations).
Does anyone know where that code lives? Trying to get a grasp on navigating their source code but can't find it.
Thanks
All the liferay OOTB portlets' JSPs lie in the folder portal-web/docroot/html/portlet.
And all the implementation classes (service implementation) reside in the portal-impl/src/com/liferay/portlet for respective portlets.
So keeping this convention in mind, the JSPs for the Users & Organization would reside in portal-web/docroot/html/portlet/users_admin since you can't see any folder with the name users or organizations or users_organization, so your best bet is to look inside the users_admin folder, also because this folder has user & organization folder which include specific JSPs for Users and Organizations.
In this folder you would find different *search.jsp & *search_results.jsp which would be what you might be looking for.
I would suggest always start looking from the view.jsp for a particular portlet since this is the first landing page when we add the portlet to the page or click on any Control Panel portlets the first time. Most probably this view.jsp would include all the other JSPs which you might want to customize or take an idea from.
Most of the time the JSPs lead you to the implementation classes, we can't use liferay's implementation classes (since they live in portal-impl.jar and using this jar in plugin-portlets is prohibited) but we can use the different *Util.java classes which are exposed for our use like the UserLocalServiceUtil, OrganizationLocalServiceUtil, OrganizationServiceUtil etc.
Hope this gives a clue to finding out Liferay implementations.

Resources