Foren

User Preferences

Chris Dailey, geändert vor 8 Jahren.

User Preferences

Junior Member Beiträge: 30 Beitrittsdatum: 24.11.14 Neueste Beiträge
Hi,
I have a Liferay Faces portlet that displays events in a calendar. The portlet has a portlet-level preference that is configurable in the edit.xhtml to set a flag for display more types of events for internal people. The portlet is then included on a more public page configured with the flag off, and on a protected page with the flag turned on.

Here is a relevant excerpt from the edit.xhtml file:


<p:selectbooleancheckbox id="maintEditCkbox" value="#{calendarBean.maintEditFlag}" />
<p:commandbutton id="submitLoginButton" value="Submit" ajax="false" action="#{calendarBean.updateMaintPref}" />


And here is an excerpt from the CalendarBean (scheduleModel is where the calendar data comes from which needs to know which mode it is in, and everything else should be self-explanatory):


@ManagedBean(name="calendarBean")
@ViewScoped
public class CalendarBean
	implements Serializable
{
	private static final long serialVersionUID = 1L;
	private static final Logger logger = Logger.getLogger(CalendarBean.class.getName());

	private boolean maintEditFlag;
	public boolean isMaintEditFlag() { return maintEditFlag; }
	public void setMaintEditFlag(boolean value) { maintEditFlag = value; }

	public CalendarBean()
	{
	}

	@PostConstruct
	public void init()
	{
		PortletPreferences preferences = getPortletPreferences();

		maintEditFlag = "TRUE".equals(preferences.getValue("maintenance", "FALSE"));
		scheduleModel.setMaintenanceView(maintEditFlag);
	}

	public void updateMaintPref()
	{
		PortletPreferences preferences = getPortletPreferences();
		try
		{
			preferences.setValue("maintenance", maintEditFlag ? "TRUE" : "FALSE");
			preferences.store();
		}
		catch (ReadOnlyException | ValidatorException | IOException e)
		{
			logger.log(Level.SEVERE, "Failed to update maintenance property", e);
		}
		scheduleModel.setMaintenanceView(maintEditFlag);
	}

       { ...... Other portions removed ...... the following two methods are really in another class ...... }

	static PortletPreferences getPortletPreferences()
	{
		PortletRequest portletRequest = getPortletRequest();
		PortletPreferences preferences = portletRequest.getPreferences();
		return preferences;
	}

	static PortletRequest getPortletRequest()
	{
		FacesContext facesContext = FacesContext.getCurrentInstance();
		ExternalContext externalContext = facesContext.getExternalContext();
		PortletRequest portletRequest = (PortletRequest)externalContext.getRequest();
		return portletRequest;
	}
}


This works very well. However, there are filters in the calendar with a bunch of checkboxes. I'd like to be able to persist which checkboxes are set so when the user returns to the page, the same checkboxes are checked. In other words, user-based preferences.

I think I need a "User Preferences" feature. Obviously I'm already successfully using portlet-level preferences. I found these two posts: first part second part

Perhaps I need to use this ScopedPreferencesService object referenced in those posts, however my IDE (Eclipse) has no idea about it. For dependencies, under Ivy, there are the usual liferay-faces-bridge-api-3.2.4-ga5.jar, liferay-faces-bridge-impl-3.2.4-ga5.jar, liferay-faces-util-3.2.4-ga5.jar, liferay-faces-portal-3.2.4-ga5.jar, primefaces-3.5.jar; under Liferay Portlet Plugin API there are util-bridges.jar, util-java.jar, util-taglib.jar.

Is that the way to go, or is there some other way?

Thanks in advance!
Chris
thumbnail
Neil Griffin, geändert vor 8 Jahren.

RE: User Preferences

Liferay Legend Beiträge: 2655 Beitrittsdatum: 27.07.05 Neueste Beiträge
The scope of preferences can be controlled by using options like preferences-company-wide, preferences-unique-per-layout, and preferences-owned-by-group in liferay-portlet.xml (see the DTD for descriptions).
Chris Dailey, geändert vor 8 Jahren.

RE: User Preferences - multiple scopes in one portlet?

Junior Member Beiträge: 30 Beitrittsdatum: 24.11.14 Neueste Beiträge
Neil Griffin:
The scope of preferences can be controlled by using options like preferences-company-wide, preferences-unique-per-layout, and preferences-owned-by-group in liferay-portlet.xml (see the DTD for descriptions).


Hi, Neil,

Thanks for the ideas! Looking at them, it seems like these liferay-portlet.xml properties apply to ALL of the preferences. In my case, I have one preference that is specific to the portlet instance -- whether it is the full data set for internal eyes or the limited customer data set. The new preferences need to be a user-specific scope.

I suppose I could just split the portlet into two portlets, each with the different behavior instead of using a portlet property, and then keep the user specific properties (what Peter Mesotten refered to as Per-user, per-portlet -- preferences-company-wide=false, preferences-unique-per-layout=true, preferences-owned-by-group=false).

Ideally, I could use the same view.xhtml for both portlets (and thus the same ManagedBeans) and let the code differentiate between them by an init-param in either liferay-portlet.xml or portlet.xml. However, I don't seem to be able to retrieve the init parameter from there; the "getInitParameter" methods I've found (both in PortletContext and in FacesContext.getExternalContext()) return the init-param values from the web.xml.

Thanks again!
Chris
thumbnail
Neil Griffin, geändert vor 8 Jahren.

RE: User Preferences - multiple scopes in one portlet?

Liferay Legend Beiträge: 2655 Beitrittsdatum: 27.07.05 Neueste Beiträge
Chris Dailey:
However, I don't seem to be able to retrieve the init parameter from there; the "getInitParameter" methods I've found (both in PortletContext and in FacesContext.getExternalContext()) return the init-param values from the web.xml.

If you specify the configuration values only in WEB-INF/portlet.xml (and not inside of WEB-INF/web.xml) then does FacesContext.getExternalContext().getInitParameter(String name) work correctly?