Need a simple persistence method for Liferay portlet development

I just need to persist couple parameter to be used in a simple liferay portlet. What are my choices other than full scale database support?

Can I just simply use property files? What is the downside of doing this? In this case, the portlet will be used by all visitors in read-only mode. Only one or two administrators will have write privilege.

Has anyone done this before? Where should the property files be created?

Thanks.
Jason.

One option that comes to my mind is to use
PortletPreferences
to store the values. I’ve done this in a few portlets where I needed some parameters that would be configured by administrators.

  • Teemu

Here’s a simple example on how to use
PortletPreferences
in a Vaadin application. Basically you should implement the
PortletRequestListener
interface and use a
ThreadLocal
to store the preferences instance for later use.


public class PreferencesExampleApplication extends Application implements
		PortletRequestListener {

    private static ThreadLocal<PortletPreferences> portletPreferences = new ThreadLocal<PortletPreferences>();

    @Override
    public void init() {
        Window mainWindow = new Window("Example Application");
        setMainWindow(mainWindow);

        final DateField dateField = new DateField();
        mainWindow.addComponent(dateField);

        Button b = new Button("Save", new Button.ClickListener() {

            @Override
            public void buttonClick(ClickEvent event) {
                try {
                    PortletPreferences prefs = PreferencesExampleApplication
                            .getPortletPreferences();
                    prefs.setValue("date", dateField.getValue().toString());
                    prefs.store();
                } catch (ReadOnlyException e) {
                    e.printStackTrace();
                } catch (ValidatorException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        mainWindow.addComponent(b);
    }

    @Override
    public void onRequestStart(PortletRequest request, PortletResponse response) {
        portletPreferences.set(request.getPreferences());
    }

    @Override
    public void onRequestEnd(PortletRequest request, PortletResponse response) {
        System.out.println("Current date value: "
                + portletPreferences.get().getValue("date", "not set"));
        portletPreferences.remove();
    }

    public static PortletPreferences getPortletPreferences() {
        return portletPreferences.get();
    }

}

Looks good. Thanks. I will give it a try.

I just did some improvements to the code example above.


  • ThreadLocal

    portletPreferences
    is now static, as it should have been in the first place.
  • Added static
    getPortletPreferences
    method so that you can easily gain access to the
    PortletPreferences
    .

Thanks. It works great.

I love Vaadin.

Jason

This was very helpful.
I have managed to store and retrieve my preferences like this but the portlet request listener keeps receiving unwanted requests when I haven’t sent a store preferences request. I want to distinguish between store preferences request and other unwanted requests.
I’ve been trying to come up with a condition to do that but so far no luck.
What i want is a condition in
onRequestEnd
method that returns true only if the
PortletRequest
was initiated by
PortletPrefrences.store()

Cheers!
Nabeel

Where are the preference stored in Liferay? Are they stored in some XML file?

in the LIferay database, and If I remeber right they are identified by portlet instance id.

Yes, there is a
PortletPreferences
table where the actual preferences values are stored in the
preferences
column in a XML format.