掲示板

Setting preferences that apply to all users per instance of a portlet?

12年前 に Phoenix Zerin によって更新されました。

Setting preferences that apply to all users per instance of a portlet?

Junior Member 投稿: 60 参加年月日: 11/06/28 最新の投稿
I am building a plugin portlet that displays "related content" depending on what content is currently visible on the page that the user is looking at. The portlet has multiple "layouts"; depending on which layout a portlet instance is set to use, the related content will be rendered in a particular configuration.

I'm trying to figure out how best to go about making the layout configurable.

  • Each instance of the portlet has its own configuration.
  • Only administrators can change the configuration for the portlet.
  • Any changes an administrator makes to the portlet affect all users.


What is the best way to store the layout name for each instance of the portlet?
12年前 に Pushpinder Singh によって更新されました。

RE: Setting preferences that apply to all users per instance of a portlet?

Junior Member 投稿: 84 参加年月日: 10/07/21 最新の投稿
To generate an key/value pair for per instance of portlet use Window id.

javax.portlet.PortletPreferences prefs = renderRequest.getPreferences();
String windowKey = renderRequest.getWindowID();
12年前 に Phoenix Zerin によって更新されました。

RE: Setting preferences that apply to all users per instance of a portlet?

Junior Member 投稿: 60 参加年月日: 11/06/28 最新の投稿
Thanks Pushpinder. Doesn't renderRequest.getPreferences() return just the preferences for the current user, though?
12年前 に Phoenix Zerin によって更新されました。

RE: Setting preferences that apply to all users per instance of a portlet? (回答)

Junior Member 投稿: 60 参加年月日: 11/06/28 最新の投稿
I've seen this referenced in a lot of places:

String portletResource = ParamUtil.getString(actionRequest,"portletResource");
PortletPreferences preferences = PortletPreferencesFactoryUtil.getPortletSetup(actionRequest, portletResource);


It says "preferences" all over it, though, so I'm confused.
thumbnail
12年前 に Victor Zorin によって更新されました。

RE: Setting preferences that apply to all users per instance of a portlet?

Liferay Legend 投稿: 1228 参加年月日: 08/04/14 最新の投稿
It seems to me that what you are asking is a default/standard behavior of portlet preferences mechanism. During render() phase you load preferences and use them for your portlet rendering. Preferences for portlet instance are set during action() phase when admin clicks 'save' on portlet configuration screen. How you organise your work around it, depends on the framework you chose for your portlet.
Sometimes it is handy to create your own class that wraps up configuration properties for the portlet, see example below:
import javax.portlet.PortletPreferences;
import org.apache.struts.action.ActionForm;

@SuppressWarnings("serial")
public class ConfigForm extends ActionForm {
	private int pageSize;
	private int maxDays = 14;
	private String coverageType;

	public String getCoverageType() {
		return coverageType;
	}

	public void setCoverageType(String coverageType) {
		this.coverageType = coverageType;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public ConfigForm() {}
	
	public ConfigForm(PortletPreferences prefs) {
		pageSize = Integer.parseInt(prefs.getValue("pageSize", "5"));
		coverageType = prefs.getValue("coverageType", "groups");
	}

	public void update(PortletPreferences prefs)
			throws javax.portlet.ReadOnlyException {
		prefs.setValue("pageSize", String.valueOf(pageSize));
		prefs.setValue("coverageType", coverageType);
	}
}

In this example, maxDays is constant across all portlet instances, and pageSize and coverageType are specific to portlet instance with default values of 5 and 'groups'.

Code for render phase that builds up ConfigForm from preferences ( disregard processMyOfficeRender, this is our own struts framework) :
public ActionForward processMyOfficeRender(ActionMapping mapping,
			ActionForm form, PortletConfig config, RenderRequest req,
			RenderResponse res) throws Exception {
		ConfigForm configForm = new ConfigForm(req.getPreferences());
                logger.info("pageSize=" + configForm.getPageSize());
                // you can pass this config form for use by jsps
               req.setAttribute("configForm", configForm);


Code for saving portlet preferences:
public ActionForward processMyOfficeAction(ActionMapping mapping,
			ActionForm form, PortletConfig config, ActionRequest req,
			ActionResponse res) throws Exception {
                // loading form parameters from configuration screen ...
		ConfigForm cForm = (ConfigForm) form;
		PortletPreferences prefs = req.getPreferences();
                // passing parameters to preferences ...
		cForm.update(prefs);
                // storing preferences in liferay for this portlet instance
		prefs.store();
		req.setAttribute("message", "message.portlet-confguration-updated");
		return mapping.findForward("/updated");
	}