Tuesday 29 August 2017

CRUD Operations in Liferay 7

CRUD Operations in Liferay 7


Following are some simple step to execute CRUD operation in LiferayDXP.

Step1 Create mvc portlet module with name ProductRegistration and Component class name ProductRegistrationPortlet and package name com.liferay.product.portlet.
For more information read my previous blog on creating mvc module portlet 



Step 2 Create Service Builder Module with name product-registration-service and package name com.liferay.product.service. and run build service gradle task. For more information on this visit my previous blog on creating service builder module

Step 3 Open build.gradle file of ProductRegistration module and add gradle dependencies of the api module generated after building service.

compileOnly project(":modules:product-registration-service:product-registration-service-api")

Step 4 Refresh your gradle project using ctrl+f5 so that above dependencies get resolved.

Step 5 Create addProduct.jsp in /META-INF/resources/ folder of your MVC portlet.

Step 6 Create renderURL in view.jsp as shown below.

view.jsp

<%@ include file="init.jsp" %>
<portlet:renderURL var="addProductURL">
<portlet:param name="mvcPath" value="/addProduct.jsp"/>
</portlet:renderURL>


<aui:button onClick="${addProductURL}" value="add-product"></aui:button>

Step 7 Create aui-form for user input in  addProduct.jsp as shown below

<%@ include file="init.jsp"%>
<portlet:actionURL name="addProduct" var="addProductURL">
</portlet:actionURL>

<aui:form action="${addProductURL}">
<aui:input name="productName" label="productName"></aui:input>
<aui:input name="productPrice" label="productPrice"></aui:input>
<aui:input name="" type="submit" value="add-product"></aui:input>
</aui:form>

Step 8 Create render method and process action method with name addProduct to scan user input in your controller as shown below.

package com.liferay.product.portlet;

import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.portlet.bridges.mvc.MVCPortlet;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.product.service.service.ProductLocalServiceUtil;

import java.io.IOException;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.Portlet;
import javax.portlet.PortletException;
import javax.portlet.ProcessAction;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.osgi.service.component.annotations.Component;

@Component(
immediate = true,
property = {
"com.liferay.portlet.display-category=category.sample",
"com.liferay.portlet.instanceable=true",
"javax.portlet.display-name=ProductRegistration Portlet",
"javax.portlet.init-param.template-path=/",
"javax.portlet.init-param.view-template=/view.jsp",
"javax.portlet.resource-bundle=content.Language",
"javax.portlet.security-role-ref=power-user,user"
},
service = Portlet.class
)
public class ProductRegistrationPortlet extends MVCPortlet {
private static final Log log=LogFactoryUtil.getLog(ProductRegistrationPortlet.class);
@Override
public void render(RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException {
// TODO Auto-generated method stub
super.render(renderRequest, renderResponse);
}
@ProcessAction(name="addProduct")
public void addProduct(ActionRequest actionRequest, ActionResponse actionResponse)
throws IOException, PortletException {

}
}

Step 9 Create a saveProduct method in ProductLocalServiceImpl located in service module inside package com.liferay.product.service.service.impl as shown below

package com.liferay.product.service.service.impl;
import aQute.bnd.annotation.ProviderType;
public class ProductLocalServiceImpl extends ProductLocalServiceBaseImpl {

public void saveProduct(String productName,long productPrice){

Product product=new ProductImpl();
product.setProductId(counterLocalService.increment());
product.setProductName(productName);
product.setProductPrice(productPrice);
updateProduct(product);

}
}

Step 10 Goto gradle task execute following task.

  • For product-registration-service-service and perform
               build service
               build 
               deploy
  • For product-registration-service-api
              build
              deploy

Then refresh your gradle project to resolve dependencies 

Step 10 Scan user input and saveProduct method in controller as shown below.

package com.liferay.product.portlet;

import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.portlet.bridges.mvc.MVCPortlet;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.product.service.service.ProductLocalServiceUtil;

import java.io.IOException;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.Portlet;
import javax.portlet.PortletException;
import javax.portlet.ProcessAction;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.osgi.service.component.annotations.Component;

@Component(
immediate = true,
property = {
"com.liferay.portlet.display-category=category.sample",
"com.liferay.portlet.instanceable=true",
"javax.portlet.display-name=ProductRegistration Portlet",
"javax.portlet.init-param.template-path=/",
"javax.portlet.init-param.view-template=/view.jsp",
"javax.portlet.resource-bundle=content.Language",
"javax.portlet.security-role-ref=power-user,user"
},
service = Portlet.class
)
public class ProductRegistrationPortlet extends MVCPortlet {
private static final Log log=LogFactoryUtil.getLog(ProductRegistrationPortlet.class);
@Override
public void render(RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException {
// TODO Auto-generated method stub
super.render(renderRequest, renderResponse);
}
@ProcessAction(name="addProduct")
public void addProduct(ActionRequest actionRequest, ActionResponse actionResponse)
throws IOException, PortletException {
String productName=ParamUtil.getString(actionRequest,"productName");
long productPrice=ParamUtil.getLong(actionRequest,"productPrice");
ProductLocalServiceUtil.saveProduct(productName, productPrice);
}
}

We are all done build and deploy ProductRegistration MVC Module then add portlet on any page .

MyOutput

Final Project Structure

Summary in this post we see how to add record in database using Liferay 7.
In my future post we will learn how to fetch all records from database and display them on jsp using SearchContainer in liferay 7.

In case of any query please let me know.

1 comment: