alaindresse
alaindresse
Reputation Top 5%
Alain Dresse
3 Snippets  (217th place)
Published
0 Channels
Created
0 Channels
Following
121 points  (244th place)
Reputation
Junior Code Generator
Junior Publisher
Junior Team Manager
Junior Popular Coder
Junior Autobiographer
Serious Autobiographer
Senior Autobiographer
Junior Famous Coder
Junior Wise Coder

Recent Snippets See all snippets by alaindresse

public by alaindresse  3662  1  4  0

Liferay custom fields

Liferay custom fields: expando.jsp
<%@page import="com.liferay.portlet.expando.model.ExpandoColumnConstants"%>
<%@page import="com.liferay.portlet.expando.NoSuchTableException"%>
<%@page import="com.liferay.portlet.expando.service.ExpandoColumnLocalServiceUtil"%>
<%@page import="com.liferay.portlet.expando.model.ExpandoColumn"%>
<%@page import="com.liferay.portal.kernel.exception.PortalException"%>
<%@page import="com.liferay.portal.kernel.util.Validator"%>
<%@page import="com.liferay.portlet.expando.model.ExpandoTable"%>
<%@page import="com.liferay.portlet.expando.service.ExpandoTableLocalServiceUtil"%>
<%@page import="com.liferay.portlet.expando.model.ExpandoValue"%>
<%@page import="com.liferay.portlet.expando.model.ExpandoTableConstants"%>
<%@page import="com.liferay.portlet.expando.service.ExpandoValueLocalServiceUtil"%>
<%@page import="com.liferay.portlet.messageboards.model.MBMessage"%>
<%@page import="java.util.List"%>
<%@page import="com.liferay.portal.kernel.dao.orm.QueryUtil"%>
<%@page import="com.liferay.portal.kernel.workflow.WorkflowConstants"%>
<%@page import="com.liferay.portal.kernel.search.QueryConfig"%>
<%@page import="com.liferay.portal.kernel.dao.orm.QueryPos"%>
<%@page import="com.liferay.portal.kernel.util.Constants"%>
<%@page import="com.liferay.portlet.messageboards.model.MBMessageConstants"%>
<%@page import="com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="theme" %>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>

<portlet:defineObjects />
<theme:defineObjects />
<h1>Testing the access and setting of custom variables.</h1>

<%
// PREPARATION : Create Newsletter true / false custom field for messages

long companyId = themeDisplay.getCompanyId();
String className = MBMessage.class.getName();
String columnName = "Newsletter";


// Create table if it doesn't exist
ExpandoTable expandoTable;
try { 
	expandoTable = ExpandoTableLocalServiceUtil.getDefaultTable(companyId, className);
} catch (NoSuchTableException e) {
	expandoTable = ExpandoTableLocalServiceUtil.addDefaultTable(companyId, className);
}

// Create column if it doesn't exist
ExpandoColumn expandoColumn;
expandoColumn = ExpandoColumnLocalServiceUtil.getColumn(expandoTable.getTableId(), columnName);
if (Validator.isNull(expandoColumn)){
	expandoColumn = ExpandoColumnLocalServiceUtil.addColumn(
			expandoTable.getTableId(), 
			columnName, 
			ExpandoColumnConstants.BOOLEAN);
}
%>

<%
	List<MBMessage> messages = MBMessageLocalServiceUtil.getGroupMessages(
			themeDisplay.getScopeGroupId(), 
			WorkflowConstants.STATUS_ANY,  
			QueryUtil.ALL_POS, 
			QueryUtil.ALL_POS);
%>

<h2>List of custom variables for messages (newsletter indication)</h2>
<%
for (MBMessage message : messages){
%>
	<h3><%= message.getSubject() %></h3>
	<p>liferay-ui:custom-attribute-list</p>
	<liferay-ui:custom-attribute-list
		className="<%= MBMessage.class.getName() %>"
		classPK="<%= message.getMessageId() %>"
		label="true"
		editable="true"
	/>
	
	<p>liferay-ui:custom-attribute</p>
	<liferay-ui:custom-attribute
			className="<%= MBMessage.class.getName() %>"
			classPK="<%= message.getMessageId() %>"
			editable="true" 
			label="true"
			name="<%= columnName %>"
		/>
		
	<p>Direct access from expando bridge</p>
	<%=
	message.getExpandoBridge().getAttribute(columnName)
	%>
<%
}
%>
		
Finding messages for newsletter
<%
List<ExpandoValue> newsletterMessages = ExpandoValueLocalServiceUtil.getColumnValues(
	themeDisplay.getCompanyId(), 
	MBMessage.class.getName(),
	ExpandoTableConstants.DEFAULT_TABLE_NAME, 
	columnName, 
	String.valueOf(true), // data, 
	QueryUtil.ALL_POS, QueryUtil.ALL_POS);
%>
<ul>
	<%	
	for (ExpandoValue expandoValue : newsletterMessages){
		MBMessage message = MBMessageLocalServiceUtil.getMessage(expandoValue.getClassPK());
		%>
		<li><%= message.getSubject() %></li>	
	<%	
	}
	%>
</ul>


;

public by alaindresse  19142  117  5  0

Understanding aui:input type="checkbox"

Understanding aui:input type="checkbox": aui_checkbox.jsp auiCheckbox.java info.txt
<portlet:actionURL name="testAction" var="testActionURL" />
<aui:form action="<%=testActionURL%>">
	<aui:input id="novalue1" type="checkbox" name="novalue"
		label="no value first" />
	<aui:input id="novalue2" type="checkbox" name="novalue"
		label="no value second" />
	<aui:input id="value1" type="checkbox" name="withvalue" label="value first"
		value="first" />
	<aui:input id="value2" type="checkbox" name="withvalue" label="value second"
		value="second" />
	<aui:button type="button" value="setValue1"
		onClick='<%=renderResponse.getNamespace() +
					"testJSCheckbox(\'value1\', true)"%>' />
	<aui:button type="button" value="unsetValue1"
		onClick='<%=renderResponse.getNamespace() +
					"testJSCheckbox(\'value1\', false)"%>' />
	<aui:button type="submit" />
</aui:form>

<aui:script>
Liferay.provide(
	window, 
	"<portlet:namespace />testJSCheckbox",
	function(id,value){
		var A = AUI();
		var checkbox = A.one("#<portlet:namespace />"+id+"Checkbox");
		if (checkbox){ 
			checkbox.set('checked', value);
			Liferay.Util.updateCheckboxValue(checkbox);
		}
	},
	['aui-base']
);
</aui:script>

public class TestPortlet extends MVCPortlet {

	public void testAction(ActionRequest actionRequest,
			ActionResponse actionResponse) throws IOException, PortletException {
		Map<String, String[]> parameters = actionRequest.getParameterMap();
		for (String key : parameters.keySet()){
			_log.info(key + ":[" + StringUtil.merge(parameters.get(key)) + "]");
		}
	}
	
	private static Log _log = LogFactoryUtil.getLog("TestPortlet"); 
}


Each `<aui:input type=”checkbox” name="xyz" … />` entry translates into two input items:

> <input type="hidden" name="portlet:namespace/>xyz" ...
> <input type="checkbox" name="<portlet:namespace/>xyzCheckbox"

If the <aui:input field has __NO value assinged__, when checkboxes are clicked, the corresponding hidden item's value is set to true or false. 

If the <aui:input field has __A value assinged__ (other than "true"), the value of the hidden item does not depend on the state of the checkbox. In this case, you need to rely on the entries under the name 'xyzCheckbox' from the request parameter map in your processAction method to capture the checkbox state.

Note also that if you set the value of the checkbox programmatically (in javascript), and you use the xyz value in the request parameter map in your processAction method, then you also need to adapt the value of the hidden item by calling
> Liferay.Util.updateCheckboxValue(checkbox)

;

public by alaindresse  370858  3  6  0

Gist to demonstrate ajax request on checkbox

Gist to demonstrate ajax request on checkbox: ajax-checkbox.tag ajaxCheckbox.java main.js view.jsp
// Tagfile to be placed in WEB-INF/tags/ui

<%@tag import="com.liferay.portal.kernel.util.HtmlUtil"%>
<%@tag import="com.liferay.portal.kernel.language.LanguageUtil"%>
<%@tag import="com.liferay.portal.kernel.util.StringPool"%>
<%@tag import="com.liferay.portal.util.PortalUtil"%>
<%@ attribute 
	name="resourceUrl"
	description="URL to process the ajax call. ServeResource parameter: newValue (0 or 1 for unchecked, checked). responseData is not used. An alert is open on failure."
	type="String" 
	required="true" %>
<%@ attribute
	name="name"
	description="Checkbox name. A random name is set if this is not provided."
	type="String"%>
<%@ attribute
	name="label"
	description="Checkbox label"
	type="String"
	required="true" %>
<%@ attribute
	name="initialValue"
	description="Initial value of checkbox"
	type="Boolean"
	required="true" %>
<%@ attribute
	name="successCallback"
	description="ajax callback on success. Parameters are: checkbox node, response data."
	type="String" %>
<%@ attribute
	name="failureCallback"
	description="Callback on failure. Parameter is checkbox name."
	type="String" %>
	
<%@ taglib uri="http://liferay.com/tld/theme" prefix="theme" %>
<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>
<%@ taglib uri="http://alloy.liferay.com/tld/aui" prefix="aui" %>
<portlet:defineObjects />
<theme:defineObjects />

<%
if (name == null){
	name = PortalUtil.generateRandomKey(request, "ajaxToggleKey");
}
String onclick = "bb_ajaxCheckbox('" + resourceUrl  + "', this, " + successCallback + "," + failureCallback + ")";
%>
<aui:input type="checkbox" onClick="<%=onclick%>" value="<%=initialValue%>" name="<%=name%>" label="<%=label%>" /> 


	@Override
	public void serveResource(ResourceRequest resourceRequest,
			ResourceResponse resourceResponse) throws IOException,
			PortletException {
		
		_log.info("processing resource");
		Map<String, String[]> parameters = resourceRequest.getParameterMap();
		for (String key : parameters.keySet()){
			_log.info(key + ":[" + StringUtil.merge(parameters.get(key)) + "]");
		}


		String resourceId = resourceRequest.getResourceID();
		if (Validator.isNotNull(resourceId)) {
			String cmd = resourceRequest.getResourceID();
			if (cmd.equals("testToggle")) {
				try {
					boolean newValue = ParamUtil.getBoolean(resourceRequest,"newValue");
					_log.info(String.valueOf(newValue));
					if (!newValue){
						throw new IOException("simulate failure");
					} else {
						resourceResponse.setContentType("text/plain");
						resourceResponse.getWriter().write("this is the response data");
					}
				} catch(Exception e){
					throw new IOException(e);
				}
			}
		}
		super.serveResource(resourceRequest, resourceResponse);
	}


Liferay.provide(
	window, 
	"bb_ajaxCheckbox",
	function(resourceUrl, checkbox, success, failure){
		var A = AUI();
		var checked = false;
		var checkbox = A.one(checkbox);
		var label = null;
		if (checkbox) {
			checked = checkbox.attr('checked');
			var labelSelector = 'label[for="' + checkbox.attr("name") + '"]';
			if (labelSelector) label = A.one(labelSelector);
		}
		var ajaxCallbacks = {
			success: function(){
				if (label) label.removeClass("ajax-loading");
				if (success) success(checkbox, this.get('responseData'));
			},
			failure: function(){
				if (failure) failure(checkbox);
			}
		};
		if (label) label.addClass("ajax-loading");
		A.io.request(resourceUrl, {
			method: 'GET',
			data: {newValue: checked,},
			on : ajaxCallbacks,
		});
	}, [ 'aui-io-request' ]
);

<%@ taglib tagdir="/WEB-INF/tags/ui" prefix="bamboost-ui"%>
<%@ include file="/html/init.jsp"%>

<portlet:resourceURL id="testToggle" var="testToggleURl"/>
<bamboost-ui:ajax-checkbox
	name="myname" 
	resourceUrl="<%=testToggleURl %>" 
	initialValue="true"
	label="no callback"/>
<bamboost-ui:ajax-checkbox
	resourceUrl="<%=testToggleURl %>" 
	initialValue="true"
	label="testSuccess"
	successCallback="testSuccess"/>
<bamboost-ui:ajax-checkbox
	resourceUrl="<%=testToggleURl %>" 
	initialValue="true"
	label="testFailure"
	failureCallback="testFailure"/>
	
	<script>
	function testSuccess(checkbox, responseData) {
		alert(checkbox.attr("name") + "\n" + responseData);
	};
	function testFailure() {alert("failure");};
	</script>

;