Foros de discusión

JSF, Primefaces and PortletPreferences - UnsupportedOperationException

Oliver Eichhorn, modificado hace 7 años.

JSF, Primefaces and PortletPreferences - UnsupportedOperationException

New Member Mensajes: 6 Fecha de incorporación: 25/02/15 Mensajes recientes
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 :


	@ProcessAction(name="doToggle")
    public void doToggle(ToggleEvent e) {
    	System.out.println("ToggleEvent from column : " + e.getData() + " Visibility : " + e.getVisibility());
    	list.set((Integer) e.getData(), e.getVisibility() == Visibility.VISIBLE);
		String[] array = ArrayUtil.toStringArray(list.toArray());
		FacesContext facesContext = FacesContext.getCurrentInstance();
		ExternalContext externalContext = facesContext.getExternalContext();
		PortletRequest portletRequest = (PortletRequest) externalContext.getRequest();
		PortletPreferences portletPreferences = portletRequest.getPreferences();
		try {
			System.out.println("try setValues");
			portletPreferences.setValues("visibilities", array);
			System.out.println("setValues done");
			System.out.println("visibilities is " + Arrays.toString(portletPreferences.getValues("visibilities",new String[]{})));
		} catch (ReadOnlyException ex) {
			System.out.println("Can't set values : " + ex);
		} 
		System.out.println("end try setValues");
		try {
			System.out.println("try store");
			portletPreferences.store();
			System.out.println("store done ");
		} catch (IOException ex) {
			System.out.println("Can't store preferences: " + ex);
		} catch (ValidatorException ex) {
			System.out.println("Can't validate : " + ex);
		} catch (UnsupportedOperationException ex) {
			System.out.println("Can't do portletPreferences.store() : " + ex);
		}
		System.out.println("end onToggle");

    }


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


14:31:23,442 DEBUG [DebugPhaseListener:64] BEFORE phaseId=[RESTORE_VIEW 1] viewId=[null]
14:31:23,444 DEBUG [DebugPhaseListener:48] AFTER phaseId=[RESTORE_VIEW 1] viewId=[/views/offeneposten/view.xhtml]
14:31:23,445 DEBUG [DebugPhaseListener:64] BEFORE phaseId=[RENDER_RESPONSE 6] viewId=[/views/offeneposten/view.xhtml]
14:31:23,509 DEBUG [DebugPhaseListener:48] AFTER phaseId=[RENDER_RESPONSE 6] viewId=[/views/offeneposten/view.xhtml]
14:31:31,238 DEBUG [DebugPhaseListener:64] BEFORE phaseId=[RESTORE_VIEW 1] viewId=[null]
14:31:31,255 DEBUG [DebugPhaseListener:48] AFTER phaseId=[RESTORE_VIEW 1] viewId=[/views/offeneposten/view.xhtml]
14:31:31,261 DEBUG [DebugPhaseListener:64] BEFORE phaseId=[APPLY_REQUEST_VALUES 2] viewId=[/views/offeneposten/view.xhtml]
ToggleEvent from column : 11 Visibility : VISIBLE
try setValues
setValues done
visibilities is [true, true, true, true, true, true, true, true, true, true, true, true, false, false, false, false]
end try setValues
try store
Can't do portletPreferences.store() : java.lang.UnsupportedOperationException
end onToggle
14:31:31,290 DEBUG [DebugPhaseListener:48] AFTER phaseId=[APPLY_REQUEST_VALUES 2] viewId=[/views/offeneposten/view.xhtml]
14:31:31,291 DEBUG [DebugPhaseListener:64] BEFORE phaseId=[PROCESS_VALIDATIONS 3] viewId=[/views/offeneposten/view.xhtml]
14:31:31,292 DEBUG [DebugPhaseListener:48] AFTER phaseId=[PROCESS_VALIDATIONS 3] viewId=[/views/offeneposten/view.xhtml]
14:31:31,293 DEBUG [DebugPhaseListener:64] BEFORE phaseId=[UPDATE_MODEL_VALUES 4] viewId=[/views/offeneposten/view.xhtml]
14:31:31,294 DEBUG [DebugPhaseListener:48] AFTER phaseId=[UPDATE_MODEL_VALUES 4] viewId=[/views/offeneposten/view.xhtml]
14:31:31,294 DEBUG [DebugPhaseListener:64] BEFORE phaseId=[INVOKE_APPLICATION 5] viewId=[/views/offeneposten/view.xhtml]
14:31:31,295 DEBUG [DebugPhaseListener:48] AFTER phaseId=[INVOKE_APPLICATION 5] viewId=[/views/offeneposten/view.xhtml]
14:31:31,295 DEBUG [DebugPhaseListener:64] BEFORE phaseId=[RENDER_RESPONSE 6] viewId=[/views/offeneposten/view.xhtml]
14:31:31,374 DEBUG [DebugPhaseListener:48] AFTER phaseId=[RENDER_RESPONSE 6] viewId=[/views/offeneposten/view.xhtml]


Thanks for any hints

Oliver
thumbnail
Juan Gonzalez, modificado hace 7 años.

Moved to Liferay Faces category

Liferay Legend Mensajes: 3089 Fecha de incorporación: 28/10/08 Mensajes recientes
Moved to Liferay Faces category
Martin Vaněk, modificado hace 7 años.

RE: JSF, Primefaces and PortletPreferences - UnsupportedOperationException

New Member Mensajes: 11 Fecha de incorporación: 12/03/16 Mensajes recientes
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, modificado hace 7 años.

RE: JSF, Primefaces and PortletPreferences - UnsupportedOperationException

New Member Mensajes: 6 Fecha de incorporación: 25/02/15 Mensajes recientes
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 :


package de.bitfabrik.portal.ef3service.ernst.flexireport.bean;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.portlet.PortletPreferences;
import javax.portlet.PortletRequest;
import javax.portlet.ProcessAction;
import javax.portlet.ReadOnlyException;
import javax.portlet.ValidatorException;

import org.primefaces.event.ToggleEvent;
import org.primefaces.model.Visibility;

import com.liferay.compat.portal.kernel.util.ListUtil;
import com.liferay.faces.portal.context.LiferayFacesContext;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.ArrayUtil;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portlet.PortletPreferencesFactoryUtil;

import de.bitfabrik.portal.ef3service.ernst.flexireport.model.dataOffeneposten;
import de.bitfabrik.portal.ef3service.ernst.flexireport.service.dataOffenepostenLocalServiceUtil;

@ManagedBean(name="dataOffenepostenBacking")
@ViewScoped
public class DataOffenepostenBacking extends AbstractBacking {

	private List<dataoffeneposten> dataoffenepostens;
    private List<boolean> list;
	public List<dataoffeneposten> getDataOffeneposten() {

	    if (dataoffenepostens == null) {
	        long scopeGroupId = LiferayFacesContext.getInstance().getScopeGroupId();

	        try {
	            dataoffenepostens = new ArrayList<dataoffeneposten>();

	            List<de.bitfabrik.portal.ef3service.ernst.flexireport.model.dataoffeneposten> list = dataOffenepostenLocalServiceUtil.getDataOffeneposten(scopeGroupId);

	            for (de.bitfabrik.portal.ef3service.ernst.flexireport.model.dataOffeneposten datadebitor : list) {
	                dataoffenepostens.add(datadebitor);
	            }
	        }
	        catch (SystemException e) {
	            logger.error(e);
	        }
	    }

	    return dataoffenepostens;
	}
	@PostConstruct
	public void init() {
	    getDataOffeneposten();
	    getList();
	}
	
	public List<boolean> getList() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		ExternalContext externalContext = facesContext.getExternalContext();
		PortletRequest portletRequest = (PortletRequest) externalContext.getRequest();
		PortletPreferences portletPreferences = portletRequest.getPreferences();
		String[] visibilities = portletPreferences.getValues("visibilities",new String[]{});
		if (visibilities != null &amp;&amp; visibilities.length &gt; 0) {
			for(String visibility:visibilities){
				list.add(GetterUtil.getBoolean(visibility));
			}
		} else {
			list = Arrays.asList(true,true,true,true,true,true,true,true,true,true,true, false, false, false, false, false);
		}
		return list;	}
	
	@ProcessAction(name="doToggle")
    public void doToggle(ToggleEvent e) {
    	System.out.println("ToggleEvent from column : " + e.getData() + " Visibility : " + e.getVisibility());
    	list.set((Integer) e.getData(), e.getVisibility() == Visibility.VISIBLE);
		String[] array = ArrayUtil.toStringArray(list.toArray());
		FacesContext facesContext = FacesContext.getCurrentInstance();
		ExternalContext externalContext = facesContext.getExternalContext();
		PortletRequest portletRequest = (PortletRequest) externalContext.getRequest();
		PortletPreferences portletPreferences = portletRequest.getPreferences();
		try {
			System.out.println("try setValues");
			portletPreferences.setValues("visibilities", array);
			System.out.println("setValues done");
			System.out.println("visibilities is " + Arrays.toString(portletPreferences.getValues("visibilities",new String[]{})));
		} catch (ReadOnlyException ex) {
			System.out.println("Can't set values : " + ex);
		} 
		System.out.println("end try setValues");
		try {
			System.out.println("try store");
			portletPreferences.store();
			System.out.println("store done ");
		} catch (IOException ex) {
			System.out.println("Can't store preferences: " + ex);
		} catch (ValidatorException ex) {
			System.out.println("Can't validate : " + ex);
		} catch (UnsupportedOperationException ex) {
			System.out.println("Can't do portletPreferences.store() : " + ex);
		}
		System.out.println("end onToggle");

    }

}
</boolean></de.bitfabrik.portal.ef3service.ernst.flexireport.model.dataoffeneposten></dataoffeneposten></dataoffeneposten></boolean></dataoffeneposten>


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

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


					<f:facet name="header">
                                             Offeneposten-Report
					<p:commandbutton value="Export alles nach Excel" style="float:right" ajax="false" icon="fa fa-file-excel-o">
						<p:dataexporter type="xls" target="dataOffeneposten" fileName="Offeneposten" />
					</p:commandbutton>
						<p:commandbutton id="toggler" type="button" value="Spalten auswählen" style="float:left" icon="fa fa-gear" />
						<p:columntoggler datasource="dataOffeneposten" trigger="toggler">
							<p:ajax event="toggle" listener="#{dataOffenepostenBacking.doToggle}" />
						</p:columntoggler>
					<br>	
					</f:facet>


Thanks Oliver
thumbnail
Juan Gonzalez, modificado hace 7 años.

RE: JSF, Primefaces and PortletPreferences - UnsupportedOperationException

Liferay Legend Mensajes: 3089 Fecha de incorporación: 28/10/08 Mensajes recientes
Hi Oliver,

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

Thanks.
Oliver Eichhorn, modificado hace 7 años.

RE: JSF, Primefaces and PortletPreferences - UnsupportedOperationException

New Member Mensajes: 6 Fecha de incorporación: 25/02/15 Mensajes recientes
Added the snipped to the post
thumbnail
Juan Gonzalez, modificado hace 7 años.

RE: JSF, Primefaces and PortletPreferences - UnsupportedOperationException

Liferay Legend Mensajes: 3089 Fecha de incorporación: 28/10/08 Mensajes recientes
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, modificado hace 7 años.

RE: JSF, Primefaces and PortletPreferences - UnsupportedOperationException

New Member Mensajes: 6 Fecha de incorporación: 25/02/15 Mensajes recientes
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
thumbnail
Juan Gonzalez, modificado hace 7 años.

RE: JSF, Primefaces and PortletPreferences - UnsupportedOperationException

Liferay Legend Mensajes: 3089 Fecha de incorporación: 28/10/08 Mensajes recientes
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, modificado hace 7 años.

RE: JSF, Primefaces and PortletPreferences - UnsupportedOperationException

New Member Mensajes: 6 Fecha de incorporación: 25/02/15 Mensajes recientes
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
thumbnail
Juan Gonzalez, modificado hace 7 años.

RE: JSF, Primefaces and PortletPreferences - UnsupportedOperationException

Liferay Legend Mensajes: 3089 Fecha de incorporación: 28/10/08 Mensajes recientes
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, modificado hace 7 años.

RE: JSF, Primefaces and PortletPreferences - UnsupportedOperationException

New Member Mensajes: 6 Fecha de incorporación: 25/02/15 Mensajes recientes
Hello Juan,

i will try your suggestion today.

Thanks Oliver
thumbnail
Kyle Joseph Stiemann, modificado hace 7 años.

Thread Split

Liferay Master Mensajes: 760 Fecha de incorporación: 14/01/13 Mensajes recientes
Please do not resurrect dead threads.

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