Foros de discusión

How to convert from liferay.PortletPreferences to javax.PortletPreferences?

thumbnail
Cameron McBride, modificado hace 12 años.

How to convert from liferay.PortletPreferences to javax.PortletPreferences?

Expert Mensajes: 269 Fecha de incorporación: 8/02/11 Mensajes recientes
The normal way to set and retrieve portlet preferences is with javax.portlet.PortletPreferences. I want to look up some preferences outside of that portlet. To do that it seems like I need to use the liferay objects and classes like com.liferay.portal.model.PortletPreferences. There are lots of goodies like PortalPreferencesLocalServiceUtil, PortalPreferencesFinderUtil, PortalPreferencesFactoryUtil.

How do I take a liferay PortletPreferences object and convert it to the javax PortletPreferences to easily pull some preferences out or set them?

I can do a getPreferences() - String and see my preferences, which are returned in an xml format:
<portlet-preferences><preference><name>my_name</name><value>1,2</value></preference></portlet-preferences>

I could parse that xml but that doesn't seem like the best way.
thumbnail
Toni Pérez Rodil, modificado hace 12 años.

RE: How to convert from liferay.PortletPreferences to javax.PortletPreferen

New Member Mensajes: 9 Fecha de incorporación: 2/08/10 Mensajes recientes
Hello, Cameron!

If you have access to the "companyId", I would give a try to "com.liferay.portal.service.PortletPreferencesLocalServiceUtil#getPreferences( long companyId, long ownerId, int ownerType, long plid,java.lang.String portletId) throws com.liferay.portal.kernel.exception.SystemException".
Apart of the"companyId", you can get the needed parameters from the com.liferay.portal.model.PortletPreferences object.

HTH.
Regards.
--
Toni Pérez Rodil
B2B 2000
thumbnail
Cameron McBride, modificado hace 12 años.

RE: How to convert from liferay.PortletPreferences to javax.PortletPreferen

Expert Mensajes: 269 Fecha de incorporación: 8/02/11 Mensajes recientes
That worked great, thanks! I couldn't tell from Eclipse which of those methods returned a javax object. Since most of them return liferay objects I didn't look at the source, my fault.

Here is some gnarly code that takes a group and a portlet type, loops through every page in the group, checks for that portlet, gets the preferences and prints a value from them. Maybe this thing will help someone one day.

		Group group = null;
		try {
			group = GroupLocalServiceUtil.getGroup(groupId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		if (group == null) {
			return retMap;
		}
		
		List<layout> layouts = new ArrayList<layout>();
		
		if (group.getPublicLayoutsPageCount() &gt; 0) {
			System.out.println("0. page count: "+group.getPublicLayoutsPageCount());
			try {
				layouts.addAll(LayoutLocalServiceUtil.getLayouts(group.getGroupId(), false));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		if (group.getPrivateLayoutsPageCount() &gt; 0) {
			try {
				layouts.addAll(LayoutLocalServiceUtil.getLayouts(group.getGroupId(), true));
			} catch (SystemException e) {
				e.printStackTrace();
			}
		}
		
		for (Layout layout : layouts) 
		{		
			System.out.println("has a layout");
			
			LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet) layout.getLayoutType();
			if (layoutTypePortlet != null) 
			{	
				System.out.println("1");
				
				List<portlet> portlets = null;
				try {
					portlets = layoutTypePortlet.getAllPortlets();										
				} catch (Exception e) {
					e.printStackTrace();
				}
				
				if (portlets != null) {
					
					for (Portlet portlet : portlets)
					{
						System.out.println("3 - " + portlet.getPluginId() + " - " + portlet.getPortletId());
						if (portlet.getPluginId().equals(portletId)) {
							
							System.out.println("4");
							List<com.liferay.portal.model.portletpreferences> prefsList = null;
							try {
								prefsList = PortletPreferencesLocalServiceUtil.getPortletPreferences(layout.getPlid(), portlet.getPortletId());
							} catch (Exception e) {
								e.printStackTrace();
							}
							
							if (prefsList != null) {
								
								for (com.liferay.portal.model.PortletPreferences prefs : prefsList) {
									
									PortletPreferences portletPrefs = null;
									try {
										portletPrefs = PortletPreferencesLocalServiceUtil.getPreferences(companyId, prefs.getOwnerId(), prefs.getOwnerType(), layout.getPlid(), portlet.getPortletId());
									} catch (Exception e) {
										e.printStackTrace();
									}
									
									if (portletPrefs != null) {
										String links = portletPrefs.getValue(QuickLinksTools.QUICK_LINKS, "");
										System.out.println("Links: " + links);
									}
								}
							}
						}
					}
				}
			}
		}</com.liferay.portal.model.portletpreferences></portlet></layout></layout>