Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Oliver Eichhorn
JSF, Primefaces and PortletPreferences - UnsupportedOperationException
April 4, 2016 6:53 AM
Answer

Oliver Eichhorn

Rank: New Member

Posts: 6

Join Date: February 25, 2015

Recent Posts

I'am developing an JSF-Portlet with DataTable from Primefaces and like to store the state of the columns like the "Stateful Column Toggler" at http://blog.primefaces.org/?p=3341 .
I have the following source bound to the toggle-event :

 1
 2    @ProcessAction(name="doToggle")
 3    public void doToggle(ToggleEvent e) {
 4        System.out.println("ToggleEvent from column : " + e.getData() + " Visibility : " + e.getVisibility());
 5        list.set((Integer) e.getData(), e.getVisibility() == Visibility.VISIBLE);
 6        String[] array = ArrayUtil.toStringArray(list.toArray());
 7        FacesContext facesContext = FacesContext.getCurrentInstance();
 8        ExternalContext externalContext = facesContext.getExternalContext();
 9        PortletRequest portletRequest = (PortletRequest) externalContext.getRequest();
10        PortletPreferences portletPreferences = portletRequest.getPreferences();
11        try {
12            System.out.println("try setValues");
13            portletPreferences.setValues("visibilities", array);
14            System.out.println("setValues done");
15            System.out.println("visibilities is " + Arrays.toString(portletPreferences.getValues("visibilities",new String[]{})));
16        } catch (ReadOnlyException ex) {
17            System.out.println("Can't set values : " + ex);
18        }
19        System.out.println("end try setValues");
20        try {
21            System.out.println("try store");
22            portletPreferences.store();
23            System.out.println("store done ");
24        } catch (IOException ex) {
25            System.out.println("Can't store preferences: " + ex);
26        } catch (ValidatorException ex) {
27            System.out.println("Can't validate : " + ex);
28        } catch (UnsupportedOperationException ex) {
29            System.out.println("Can't do portletPreferences.store() : " + ex);
30        }
31        System.out.println("end onToggle");
32
33    }


When executing the portlet and toggling an entry, i get an UnsupportedOperationException :

 1
 214:31:23,442 DEBUG [DebugPhaseListener:64] BEFORE phaseId=[RESTORE_VIEW 1] viewId=[null]
 314:31:23,444 DEBUG [DebugPhaseListener:48] AFTER phaseId=[RESTORE_VIEW 1] viewId=[/views/offeneposten/view.xhtml]
 414:31:23,445 DEBUG [DebugPhaseListener:64] BEFORE phaseId=[RENDER_RESPONSE 6] viewId=[/views/offeneposten/view.xhtml]
 514:31:23,509 DEBUG [DebugPhaseListener:48] AFTER phaseId=[RENDER_RESPONSE 6] viewId=[/views/offeneposten/view.xhtml]
 614:31:31,238 DEBUG [DebugPhaseListener:64] BEFORE phaseId=[RESTORE_VIEW 1] viewId=[null]
 714:31:31,255 DEBUG [DebugPhaseListener:48] AFTER phaseId=[RESTORE_VIEW 1] viewId=[/views/offeneposten/view.xhtml]
 814:31:31,261 DEBUG [DebugPhaseListener:64] BEFORE phaseId=[APPLY_REQUEST_VALUES 2] viewId=[/views/offeneposten/view.xhtml]
 9ToggleEvent from column : 11 Visibility : VISIBLE
10try setValues
11setValues done
12visibilities is [true, true, true, true, true, true, true, true, true, true, true, true, false, false, false, false]
13end try setValues
14try store
15Can't do portletPreferences.store() : java.lang.UnsupportedOperationException
16end onToggle
1714:31:31,290 DEBUG [DebugPhaseListener:48] AFTER phaseId=[APPLY_REQUEST_VALUES 2] viewId=[/views/offeneposten/view.xhtml]
1814:31:31,291 DEBUG [DebugPhaseListener:64] BEFORE phaseId=[PROCESS_VALIDATIONS 3] viewId=[/views/offeneposten/view.xhtml]
1914:31:31,292 DEBUG [DebugPhaseListener:48] AFTER phaseId=[PROCESS_VALIDATIONS 3] viewId=[/views/offeneposten/view.xhtml]
2014:31:31,293 DEBUG [DebugPhaseListener:64] BEFORE phaseId=[UPDATE_MODEL_VALUES 4] viewId=[/views/offeneposten/view.xhtml]
2114:31:31,294 DEBUG [DebugPhaseListener:48] AFTER phaseId=[UPDATE_MODEL_VALUES 4] viewId=[/views/offeneposten/view.xhtml]
2214:31:31,294 DEBUG [DebugPhaseListener:64] BEFORE phaseId=[INVOKE_APPLICATION 5] viewId=[/views/offeneposten/view.xhtml]
2314:31:31,295 DEBUG [DebugPhaseListener:48] AFTER phaseId=[INVOKE_APPLICATION 5] viewId=[/views/offeneposten/view.xhtml]
2414:31:31,295 DEBUG [DebugPhaseListener:64] BEFORE phaseId=[RENDER_RESPONSE 6] viewId=[/views/offeneposten/view.xhtml]
2514:31:31,374 DEBUG [DebugPhaseListener:48] AFTER phaseId=[RENDER_RESPONSE 6] viewId=[/views/offeneposten/view.xhtml]


Thanks for any hints

Oliver
Martin Vaněk
RE: JSF, Primefaces and PortletPreferences - UnsupportedOperationException
April 5, 2016 6:17 AM
Answer

Martin Vaněk

Rank: New Member

Posts: 8

Join Date: March 12, 2016

Recent Posts

Hi,

I am not so sure about this one, but to me it seems like annotation ProcessAction is used elswhere (in PortletClass) and it could perhaps make a difference in finding the right preferences for the portlet. Code for handling this event should be in ManagedBean, even in PF example without annotation too.

When I looked into code store() method code, it throws UnsupportedOperationException when its private propery portletId is null.

Perharps you could provide whole class when you have this method for handling the event?
Oliver Eichhorn
RE: JSF, Primefaces and PortletPreferences - UnsupportedOperationException
April 6, 2016 2:06 AM
Answer

Oliver Eichhorn

Rank: New Member

Posts: 6

Join Date: February 25, 2015

Recent Posts

Hi Martin,

i have used the annotations @ProcessAction because of the portlet specification, which states :
All changes made to PortletPreferences object not followed by a call to the store method must be discarded when the portlet finishes the processAction method.
If the store method is invoked within the scope of a render method invocation, it must throw an IllegalStateException.
.


This is the managed-bean-class :

  1
  2package de.bitfabrik.portal.ef3service.ernst.flexireport.bean;
  3
  4import java.io.IOException;
  5import java.util.ArrayList;
  6import java.util.Arrays;
  7import java.util.Collections;
  8import java.util.Enumeration;
  9import java.util.List;
 10
 11import javax.annotation.PostConstruct;
 12import javax.faces.bean.ManagedBean;
 13import javax.faces.bean.ViewScoped;
 14import javax.faces.context.ExternalContext;
 15import javax.faces.context.FacesContext;
 16import javax.portlet.PortletPreferences;
 17import javax.portlet.PortletRequest;
 18import javax.portlet.ProcessAction;
 19import javax.portlet.ReadOnlyException;
 20import javax.portlet.ValidatorException;
 21
 22import org.primefaces.event.ToggleEvent;
 23import org.primefaces.model.Visibility;
 24
 25import com.liferay.compat.portal.kernel.util.ListUtil;
 26import com.liferay.faces.portal.context.LiferayFacesContext;
 27import com.liferay.portal.kernel.exception.PortalException;
 28import com.liferay.portal.kernel.exception.SystemException;
 29import com.liferay.portal.kernel.util.ArrayUtil;
 30import com.liferay.portal.kernel.util.GetterUtil;
 31import com.liferay.portal.kernel.util.ParamUtil;
 32import com.liferay.portal.kernel.util.Validator;
 33import com.liferay.portlet.PortletPreferencesFactoryUtil;
 34
 35import de.bitfabrik.portal.ef3service.ernst.flexireport.model.dataOffeneposten;
 36import de.bitfabrik.portal.ef3service.ernst.flexireport.service.dataOffenepostenLocalServiceUtil;
 37
 38@ManagedBean(name="dataOffenepostenBacking")
 39@ViewScoped
 40public class DataOffenepostenBacking extends AbstractBacking {
 41
 42    private List<dataOffeneposten> dataoffenepostens;
 43    private List<Boolean> list;
 44    public List<dataOffeneposten> getDataOffeneposten() {
 45
 46        if (dataoffenepostens == null) {
 47            long scopeGroupId = LiferayFacesContext.getInstance().getScopeGroupId();
 48
 49            try {
 50                dataoffenepostens = new ArrayList<dataOffeneposten>();
 51
 52                List<de.bitfabrik.portal.ef3service.ernst.flexireport.model.dataOffeneposten> list = dataOffenepostenLocalServiceUtil.getDataOffeneposten(scopeGroupId);
 53
 54                for (de.bitfabrik.portal.ef3service.ernst.flexireport.model.dataOffeneposten datadebitor : list) {
 55                    dataoffenepostens.add(datadebitor);
 56                }
 57            }
 58            catch (SystemException e) {
 59                logger.error(e);
 60            }
 61        }
 62
 63        return dataoffenepostens;
 64    }
 65    @PostConstruct
 66    public void init() {
 67        getDataOffeneposten();
 68        getList();
 69    }
 70   
 71    public List<Boolean> getList() {
 72        FacesContext facesContext = FacesContext.getCurrentInstance();
 73        ExternalContext externalContext = facesContext.getExternalContext();
 74        PortletRequest portletRequest = (PortletRequest) externalContext.getRequest();
 75        PortletPreferences portletPreferences = portletRequest.getPreferences();
 76        String[] visibilities = portletPreferences.getValues("visibilities",new String[]{});
 77        if (visibilities != null && visibilities.length > 0) {
 78            for(String visibility:visibilities){
 79                list.add(GetterUtil.getBoolean(visibility));
 80            }
 81        } else {
 82            list = Arrays.asList(true,true,true,true,true,true,true,true,true,true,true, false, false, false, false, false);
 83        }
 84        return list;    }
 85   
 86    @ProcessAction(name="doToggle")
 87    public void doToggle(ToggleEvent e) {
 88        System.out.println("ToggleEvent from column : " + e.getData() + " Visibility : " + e.getVisibility());
 89        list.set((Integer) e.getData(), e.getVisibility() == Visibility.VISIBLE);
 90        String[] array = ArrayUtil.toStringArray(list.toArray());
 91        FacesContext facesContext = FacesContext.getCurrentInstance();
 92        ExternalContext externalContext = facesContext.getExternalContext();
 93        PortletRequest portletRequest = (PortletRequest) externalContext.getRequest();
 94        PortletPreferences portletPreferences = portletRequest.getPreferences();
 95        try {
 96            System.out.println("try setValues");
 97            portletPreferences.setValues("visibilities", array);
 98            System.out.println("setValues done");
 99            System.out.println("visibilities is " + Arrays.toString(portletPreferences.getValues("visibilities",new String[]{})));
100        } catch (ReadOnlyException ex) {
101            System.out.println("Can't set values : " + ex);
102        }
103        System.out.println("end try setValues");
104        try {
105            System.out.println("try store");
106            portletPreferences.store();
107            System.out.println("store done ");
108        } catch (IOException ex) {
109            System.out.println("Can't store preferences: " + ex);
110        } catch (ValidatorException ex) {
111            System.out.println("Can't validate : " + ex);
112        } catch (UnsupportedOperationException ex) {
113            System.out.println("Can't do portletPreferences.store() : " + ex);
114        }
115        System.out.println("end onToggle");
116
117    }
118
119}


Are these properties like portletId not managed by the portlet-container ???

This is the xhtml-snippet where the callback is defined :

 1
 2                    <f:facet name="header">
 3                                             Offeneposten-Report
 4                    <p:commandButton value="Export alles nach Excel" style="float:right" ajax="false" icon="fa fa-file-excel-o" >
 5                        <p:dataExporter type="xls" target="dataOffeneposten" fileName="Offeneposten" />
 6                    </p:commandButton>
 7                        <p:commandButton id="toggler" type="button" value="Spalten auswählen" style="float:left" icon="fa fa-gear" />
 8                        <p:columnToggler datasource="dataOffeneposten" trigger="toggler" >
 9                            <p:ajax event="toggle" listener="#{dataOffenepostenBacking.doToggle}" />
10                        </p:columnToggler>
11                    <br />   
12                    </f:facet>


Thanks Oliver
Juan Gonzalez
RE: JSF, Primefaces and PortletPreferences - UnsupportedOperationException
April 6, 2016 1:37 AM
Answer

Juan Gonzalez

Rank: Liferay Legend

Posts: 3057

Join Date: October 28, 2008

Recent Posts

Hi Oliver,

can you paste the xhtml snippet where this action is invoked?

Thanks.
Oliver Eichhorn
RE: JSF, Primefaces and PortletPreferences - UnsupportedOperationException
April 6, 2016 2:09 AM
Answer

Oliver Eichhorn

Rank: New Member

Posts: 6

Join Date: February 25, 2015

Recent Posts

Added the snipped to the post
Juan Gonzalez
RE: JSF, Primefaces and PortletPreferences - UnsupportedOperationException
April 6, 2016 6:40 AM
Answer

Juan Gonzalez

Rank: Liferay Legend

Posts: 3057

Join Date: October 28, 2008

Recent Posts

Hi Oliver,

as what I see seems you're mixing Portlet and JSF concepts.

Why do you think doToggle method will be executed as a portlet action?
Oliver Eichhorn
RE: JSF, Primefaces and PortletPreferences - UnsupportedOperationException
April 6, 2016 8:15 AM
Answer

Oliver Eichhorn

Rank: New Member

Posts: 6

Join Date: February 25, 2015

Recent Posts

I think the annotation will do the trick.
I tried this way because i don't have an EDIT-Mode xhtml as in other samples which saved preferences or am i totaly wrong.
This is my first JSF/Primeface Liferay-plugin ....
Which is the "Liferay JSF"-way ??

Thanks in advance

Oliver
Juan Gonzalez
RE: JSF, Primefaces and PortletPreferences - UnsupportedOperationException
April 6, 2016 8:20 AM
Answer

Juan Gonzalez

Rank: Liferay Legend

Posts: 3057

Join Date: October 28, 2008

Recent Posts

Hi Oliver,

here (https://github.com/liferay/liferay-faces/tree/master/demos/bridge/jsf2-portlet) you have a complete example for changing portlet preferences (with EDIT mode).

Hope it helps. You know where we are if you need help on this great world of JSF and portlets.

Thanks for using Liferay Faces ;-)
Oliver Eichhorn
RE: JSF, Primefaces and PortletPreferences - UnsupportedOperationException
April 6, 2016 10:37 AM
Answer

Oliver Eichhorn

Rank: New Member

Posts: 6

Join Date: February 25, 2015

Recent Posts

Thanks Juan,

i have to understand the example, will be tomorrow back to the topic after analysis.
I thought it can be done direct from toogle event without another roundtrip to edit-mode.
This is imho an transparent solution for users from selection to persistence ;-)
But this seems not usual way of processing

Best regards Oliver
Juan Gonzalez
RE: JSF, Primefaces and PortletPreferences - UnsupportedOperationException
April 6, 2016 12:08 PM
Answer

Juan Gonzalez

Rank: Liferay Legend

Posts: 3057

Join Date: October 28, 2008

Recent Posts

Hi Oliver,

in order to achieve that, you should remove the p:ajax from your actionListener so ACTION_PHASE is executed, instead of the RESOURCE_PHASE (as it's an AJAX request).

Can you check that?

Thanks.
Oliver Eichhorn
RE: JSF, Primefaces and PortletPreferences - UnsupportedOperationException
April 6, 2016 11:21 PM
Answer

Oliver Eichhorn

Rank: New Member

Posts: 6

Join Date: February 25, 2015

Recent Posts

Hello Juan,

i will try your suggestion today.

Thanks Oliver
Kyle Joseph Stiemann
Thread Split
December 22, 2016 7:38 AM
Answer

Kyle Joseph Stiemann

LIFERAY STAFF

Rank: Liferay Master

Posts: 567

Join Date: January 14, 2013

Recent Posts

Please do not resurrect dead threads.

The new thread can be found at https://web.liferay.com/community/forums/-/message_boards/view_message/84050525.