Wednesday, 21 December 2016

CRUD Operations in Liferay 7

Today we will see how to perform the CRUD operation in Liferay 7. I assume you know how to create mvc portlet module and servicebuilder module in Liferay.

Following are some simple steps 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 the above dependencies get resolved.

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

Step 6 Create render URL 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 building and deploying ProductRegistration MVC Module then adding a portlet on any page.

output

Final Project Structure

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

In case of any query please let me know.

Another related post 


New posts can be helpful.








For Spring boot, Spring microservices, Spring MVC, Spring Batch, Hibernate refer 

For Java - Java 8 refer Java Tutorial

Backlink To Festival Images

12 comments:

  1. i want to perform update operation also without search container. how can i do.

    ReplyDelete
  2. hi,
    what about table name and column name.

    ReplyDelete
  3. what is the name of the tool you are using to query?

    ReplyDelete
  4. can you public that code?

    ReplyDelete
  5. need update and delete operations with this example

    ReplyDelete
  6. can you please show operations for read, update and delete when we have two tables linked to each other though foreign key.

    ReplyDelete