Liferay: get PortletID and companyID from init()

北城以北 提交于 2019-12-13 16:22:05

问题


Maybe trough PortletConfig in init(PortletConfig)

The thing is that using

((PortletConfigImpl) portletConfig).getPortletId();

is not allowed anymore because adding portal-impl.jar in package.properties gives throws an exception when trying to execute build ant target, saying that this is not allowed anymore

For companyID I directly have no idea where to start. I am using currently

long companyId = CompanyLocalServiceUtil.getCompanies().get(0).getCompanyId();

but as soon as I got more than one company it will fail

If only I could get Portlet object somehow, I think it would be enough to get both portletId and companyId


回答1:


You can get the Portlet from PortletConfig through the portletId got from the method Portletconfig.getPortletName() and then using PortletLocalServiceUtil.getPortletById(String):

// I have not tried this but I think it should work
String portletName = Portletconfig.getPortletName();
Portlet portlet = PortletLocalServiceUtil.getPortletById(portletName);
long companyId = portlet.getCompanyId();

And then from the Portlet you can easily get the companyId.

A word of caution as stated by Olaf Kock in his answer, hence here is another answer which gives you some alternative to fetch the companyId if you know some aspect of the company. This approach does not fetch the companyId from the portlet instance for the reasons as explained by Olaf Kock.

Try this approach and see if this helps.




回答2:


I'm not one 100% sure what you need, but if you have just one company, you can use the following code to get the company id:

PortalUtil.getDefaultCompanyId()



回答3:


Adding portal-impl.jar to plugins has always been a source of problems and has never been the correct way to do. With that I'm quite happy to see that it actually seems to be actively prohibited now by the build scripts. As discussed in this question you should not do this and the portlet API does not support what you want. You strictly don't have the context necessary for knowing which company a portlet instance has been added to.

Keep in mind: There might (and typically is) only one instance of your portlet class ever, no matter if a portlet instance is placed on any page - and independent of how many portlet instances are around in the whole portal, across all companies and sites.

You should not follow this path, instead explain what you're trying to achieve with it. We might help you with that, but cannot help you learning how to implement something that is impossible, or, if you find a loophole, will be unmaintainable and randomly break with the next update because it's far outside the specification.



来源:https://stackoverflow.com/questions/12322904/liferay-get-portletid-and-companyid-from-init

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!