Monday, September 26, 2016

Use of Structure in Custom Portlet

Today we will discuss how to use Structure in Custom Portlet. The benefit of this approach is we easily create structure by just drag and drop and simply use this structure in our form. Before reading this blog you must know how to create Structure in Liferay.So lets start this step by step:-


Step 1:-Create Structure 
First you Create Structure and get the structureId . Here i am creating a structure that contain 3 fields Name(String),Age(Double) and Doc for uploading documents like images:-



And Click on save. Here my StructureId is 11821




Step 2:-Create Portlet
Create your portlet and Change the view.jsp as:-

view.jsp
<%@page import="com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil"%>
<%@page import="com.liferay.portlet.dynamicdatamapping.model.DDMStructure"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@page import="com.liferay.portal.service.ServiceContextFactory"%>
<%@page import="com.liferay.portal.service.ServiceContext"%>
<%@page import="com.liferay.portlet.dynamicdatamapping.util.DDMUtil"%>
<%@page import="com.liferay.portlet.dynamicdatamapping.storage.Fields"%>
<%@ taglib uri="http://liferay.com/tld/ddm" prefix="liferay-ddm"%>
<%@page import="com.liferay.portal.util.PortalUtil"%>
<portlet:defineObjects />
<%
long structureId= 11821;// structureId can be taken from control panel
ServiceContext serviceContext = ServiceContextFactory.getInstance(renderRequest);
DDMStructure ddmStructure = DDMStructureLocalServiceUtil.getDDMStructure(structureId);
Fields newFields = DDMUtil.getFields(ddmStructure.getStructureId(), serviceContext);
%>
<portlet:actionURL name="submitForm" var="submitFormURL"/>
<form action="<%=submitFormURL%>" name="emplyeeForm" method="POST">
<liferay-ddm:html classNameId="<%= PortalUtil.getClassNameId(DDMStructure.class)%>" classPK="<%= ddmStructure.getStructureId() %>" fields="<%= newFields %>" />
<input type="submit" value="SUBMIT">
</form>
view raw view.jsp hosted with ❤ by GitHub


Explanation:-
Here we are using structureId to get the fields and than by using <liferay-ddm:html> tag we get the structure. And finally on click of submit button we submit the form.




Now Change the Controller as:-

Demo.java
package com.test;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.ProcessAction;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.security.auth.CompanyThreadLocal;
import com.liferay.portal.service.ServiceContext;
import com.liferay.portal.service.ServiceContextFactory;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portlet.documentlibrary.model.DLFileEntry;
import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
import com.liferay.portlet.dynamicdatamapping.storage.Fields;
import com.liferay.portlet.dynamicdatamapping.util.DDMUtil;
import com.liferay.portlet.journal.model.JournalArticle;
import com.liferay.util.bridges.mvc.MVCPortlet;
public class Demo extends MVCPortlet {
@ProcessAction(name="submitForm")
public void submitForm(ActionRequest request,ActionResponse response) throws Exception {
long structureId= 11821;
ServiceContext serviceContext = ServiceContextFactory.getInstance(JournalArticle.class.getName(), request);
DDMStructure ddmStructure = DDMStructureLocalServiceUtil.getDDMStructure(structureId);
Fields fields = DDMUtil.getFields(ddmStructure.getStructureId(), serviceContext);
String name = fields.get("Name").getValue().toString();
String age = fields.get("Age").getValue().toString();
String[] document = fields.get("Doc").getValue().toString().split(",");
String uuid = document[1].split(":")[1].replace("\"", "");
DLFileEntry uploadedFile =DLFileEntryLocalServiceUtil.getDLFileEntryByUuidAndCompanyId(uuid,CompanyThreadLocal.getCompanyId());
ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
String url = themeDisplay.getPortalURL() + themeDisplay.getPathContext() + "/documents/" + themeDisplay.getScopeGroupId() + "/" + uploadedFile.getFolderId() + "/" + uploadedFile.getTitle();
System.out.println("URL=>"+url);
System.out.println("Name = >"+name);
System.out.println("Age = > "+age);
}
}
view raw Demo.java hosted with ❤ by GitHub


Explanation:-
In Demo.java we first get the DDMStructure by structureId and then get the fields and finally display on console.






Step 3:-Check the Output
Deploy your portlet and check the output:-








Hope this will Help....

You can Download Source code from  Structure in Custom Portlet. 

Related Post:-

No comments:

Post a Comment

Total Pageviews

1041377

Number Of Unique Visitor

Free counters!