KNOWLEDGEBASE

We pride ourselves on bringing a fresh perspective and effective insight knowledge of each technology.

  • Rest Extender and JAX-RS Restful Web Service in Liferay 7 DXP

    Rest Extender and JAX-RS Restful Web Service in Liferay 7 DXP


    Rest Extender and JAX-RS Restful Web Service in Liferay 7 Let's us to implement REST Web Service for any table in Liferay's database, Using which we can access various tables in liferay

    Environment
    Liferay IDE 3.1.2 GA3
    Liferay CE Portal Tomcat 7 GA4
    JDK 8
    MySql 5.7

    Step 1 : Create a New Liferay Module Project With Rest Template 




    Step 2 : Give Package name and Component Class Name



    Click on Finish.

    Module Project With following directory structure will be created.


    In my example i am going consume the REST api to retrieve the user tables data.

    com.liferay.portal.remote.cxf.common.configuration.CXFEndpointPublisherConfiguration-cxf.properties
    contextPath=/my-rest-service
    authVerifierProperties=auth.verifier.BasicAuthHeaderAuthVerifier.urls.includes=*
    

    com.liferay.portal.remote.rest.extender.configuration.RestExtenderConfiguration-rest.properties
    contextPaths=/my-rest-service
    jaxRsServiceFilterStrings=(component.name=com.liferaystack.application.MyRestServiceApplication)
    

    Note
    1. If you want to change the context path of your module, you can use the above properties files.
    2. Include compileOnly group: "com.liferay.portal", name: "com.liferay.portal.kernel", version: "2.0.0" in build.gradle to access User Table API from Liferay's Services.
    3. If You want to implement same REST Service for custom table (entity) please add the dependency in build.gradle

    MyRestServiceApplication.java
    This is the REST Controller Where we can write out REST API Code, I have removed the created template code and added the below code into this file
    package com.liferaystack.application;
    
    import com.liferay.portal.kernel.dao.orm.QueryUtil;
    import com.liferay.portal.kernel.exception.PortalException;
    import com.liferay.portal.kernel.log.Log;
    import com.liferay.portal.kernel.log.LogFactoryUtil;
    import com.liferay.portal.kernel.model.User;
    import com.liferay.portal.kernel.service.UserLocalServiceUtil;
    import com.liferay.portal.kernel.util.ContentTypes;
    import com.liferay.portal.kernel.util.Validator;
    
    import java.util.Collections;
    import java.util.List;
    import java.util.Set;
    
    import javax.ws.rs.ApplicationPath;
    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.PathParam;
    import javax.ws.rs.Produces;
    import javax.ws.rs.QueryParam;
    import javax.ws.rs.core.Application;
    
    import org.osgi.service.component.annotations.Component;
    
    /**
     * @author Syed Ali
     */
    
    @ApplicationPath("/users")
    @Component(immediate = true, service = Application.class)
    public class MyRestServiceApplication extends Application {
    
     public Set<Object> getSingletons() {
      return Collections.<Object>singleton(this);
     }
    
     @GET
     @Produces("text/plain")
     public String working() {
      return "Web Service Works!";
     }
    
     @GET
     @Path("/getalluser")
     @Produces(ContentTypes.TEXT_PLAIN)
     public String hello() {
      List<User> users = UserLocalServiceUtil.getUsers(QueryUtil.ALL_POS,QueryUtil.ALL_POS);
      return users.toString();
     }
    
     @GET
     @Path("/getuser/{userId}")
     @Produces(ContentTypes.TEXT_PLAIN)
     public String morning(
      @PathParam("userId") long userId,
      @QueryParam("param") String param) {
    
      User user = null;
      try {
       user = UserLocalServiceUtil.getUser(userId);
      } catch (PortalException e) {
       _log.error("PortalException : "+e.getMessage()); 
      }
    
      if(Validator.isNull(user)){
       return "[No User Found With User Id : "+userId+"]";
      }
      return user.toString();
     }
    
     private static Log _log = LogFactoryUtil.getLog(MyRestServiceApplication.class);
    }
    

    Deploy the module to your and Test the REST Web Service

    Testing the Web Service


    Accessing the Root Context
    Goto http://localhost:8080/o/my-rest-service/ it will show the list of services available

    Accessing Rest Controller
    Goto http://localhost:8080/o/my-rest-service/users it will show the list of services available

    Get All Users Using Rest Controller
    Goto http://localhost:8080/o/my-rest-service/users/getalluser you can fetch all users

    Get Users with userId Using Rest Controller
    Goto http://localhost:8080/o/my-rest-service/users/getuser/20156 you can fetch users with userId 20156

    Source Code


  • Layout In Liferay 7 DXP

    Layout In Liferay 7 DXP



    Layout in Liferay Lets us define the Grids into the Liferay Page, Which allows us to systematical arrange the portlet applications and contents into the page, To Create a layout in Liferay 7 DXP, I have already created a Liferay Gradle Workspace in my machine.

    Environment
    Liferay Developer Studio/ IDE 3.1.2 GA3
    Liferay 7 CE/DXP Portal Tomcat 7 GA4
    JDK 8
    MySql 5.7

    Step 1 : Create New Liferay Module Project

     


    Step 2 : Select layout-template and click next




    Step 3 : Give Component Class Name and package name and click finish

     


    A Layout Module will be created with following directory structure



    my-layout.tpl
    This is the main file in Liferay's Layout, I am going to customize this layout into my required layout as shown below.
    Lets See how we can create the Layout s shown above, below is the simple code snippet for my layout

    my-layout.tpl

    <div class="my-layout" id="main-content" role="main">
     
     <div class="portlet-layout row" style="background-color: red;">
     
      <div class="col-md-6 portlet-column portlet-column-first" id="column-1" style="background-color: pink;">
       $processor.processColumn("column-1", "portlet-column-content portlet-column-content-first")
      </div>
    
      <div class="col-md-6 portlet-column portlet-column-last" id="column-2" style="background-color: green;">
       $processor.processColumn("column-2", "portlet-column-content portlet-column-content-last")
      </div>
      
     </div>
     
     <div class="portlet-layout row" >
      
      <div class="col-md-6 portlet-column portlet-column-first" id="column-3" style="background-color: yellow;">
       $processor.processColumn("column-3", "portlet-column-content portlet-column-content-first")
      </div>
    
      <div class="col-md-6 portlet-column portlet-column-last" id="column-4" style="background-color:aqua;">
       $processor.processColumn("column-4", "portlet-column-content portlet-column-content-last")
      </div>
      
     </div>
     
    </div>
    

    Components and Classes In Layout


    portlet-layout row : This class indicates each row container in the Layout Grid System.

    $processor.processColumn : It have 2 arguments, id and Classes
    Id : It Must be an unique identifier for each column

    col-md-6 : We can use different Boostrap Grid Classes in our layout, in our case this class indicates 50% of the available parent DIV size.

    portlet-column : Its the container which holds the Portlet column.

    portlet-column-first : use this class if you have multiple columns in the layout, it indicates first column of the layout.

    portlet-column-last : use this class if you have multiple columns in the layout, it indicates Last column of the layout.

    portlet-column-only : use this class if you have only one column in the layout.

    portlet-column-content : this class is used to indicate the portlet content in the column.

    my-layout.png :  image for the layout, we can define this image indicating layout grid.

    liferay-layout-templates.xml 
    we can define the layout template file path, layout image file path, layout id, layout name and etc

    <?xml version="1.0"?>
    <!DOCTYPE layout-templates PUBLIC "-//Liferay//DTD Layout Templates 7.0.0//EN" "http://www.liferay.com/dtd/liferay-layout-templates_7_0_0.dtd">
    
    <layout-templates>
     <custom>
      <layout-template id="my-layout" name="my-layout">
       <template-path>/my-layout.tpl</template-path>
       <thumbnail-path>/my-layout.png</thumbnail-path>
      </layout-template>
     </custom>
    </layout-templates>
    

    After applying the my-layout to an empty page, The final layout outcome must look like this.

    Note : Every Column DIV must have $processor.processColumn, If you don't provide the $processor.processColumn then that DIV will not be considered as portlet container and the DIV cannot hold any portlet.

    Just to identify the rows and columns, i have used the different colors, make sure you remove them.


  • Roles In Liferay 7 DXP

    Roles In Liferay 7 DXP


    Roles In Liferay Determines what an user can do and what an user can access in the portal, let's have a closer look at the Roles System of Liferay.

    What is a Role In Liferay ?

    Roles of an User in Liferay defines
    1. What are the actions User can perform in the Portal (action permissions) in the given scopes
    2. What are the contents the User can access in the portal (resource permissions) in the given scopes

    Types of Roles In Liferay 


    Regular Role or Portal Role

    When these roles are assigned to the user, the role's permissions are available to the User across the Portal Instance, the sub hierarchies such as Sites, Organization and User Groups.

    Site Role

    When a Site Role is assigned to an Site User, The permissions of the Role to the User Will be applicable only inside the Site and will not applicable for the User in other sites.


    Organization Role

    When an Organization Role is assigned to an Organization User, The permissions of the Role to the User Will be applicable only inside the Organization and will not applicable for the User in other Organizations.

    Regular Roles Avaialble In Liferay 


    Guest

    Guest User will be having very minimal set of permissions in the portal and are non authenticated users


    Owner

    This is can be applied to the User who is the owner of certain entities in Liferay

    Administrator

    Administrator User can access almost anything in the portal and any action in the portal and can access Sites organization and any object without any constarints 


    User

    This is the basic Role Given to an User when the user is authenticated into the Portal. 


    Power User

    This Role have the same permissions as that of the User Role, we can use this Role Just to distinguish the normal user from this previllaged role and give more access.

    Site Roles Avaialble In Liferay 


    Site Administrator

    It enables the Site Administrator User the ability to manage different assets of the sites such Site Pages, Site Membership, Site Contents and Site Settings. 


    Site Member

    This Role grants permissions to visit the privates pages of the sites and its contents including public pages


    Site Owner

    This Role is same as that of the Site Administrator Role except that it provides the ability to manage the different aspects of the sites such as deleting the site membership or roles including Site Administrator


    Organization Roles Avaialble In Liferay 


    Organization Administrator

    It enables the Organization Administrator User the ability to manage different assets of the Organization such Site Pages, Organization Membership, Organization Site Contents and Organization Site Settings.


    Organization Owner

    This Role is same as that of the Organization Administrator Role except that it provides the ability to manage the different aspects of the Organization such as deleting the Organization membership or roles including Organization Administrator


    Organization User

    This Role grants user the ability to visit the Organization Site Pages when the user belongs to the Organization

    Apart from all the above roles, we can create our own Regular, Site and Organization Roles with our own permissions in the control panel.


  • Sites In Liferay 7 DXP

    Sites In Liferay 7 DXP


    Multi Tenancy of Liferay gives flexibility to host number of sites in a single instance of Liferay Server, lets explore the sites and their hierarchy in Liferay 7 DXP in Detail.

    Global Site

    When we setup a new Liferay instance, Liferay gives us a default Global Site, If you want another site you can create it from control panel, all the newly created sites will be the sub-sites of the parent global sites. All contents of the Global Sites are accessible to the sub-sites.


    What is a Site in Liferay ?

    Sites can have its own pages which in turn can publish the web contents or portlets in the pages. Users can be assigned to the sites.

    Types of Site Membership 


    Open 

    User can become member of Open site at any point of time, any user who is having an account to the portal can join Open Site.

    Restricted

    User having an account in the portal can join this site by sending a joining request to the site, Site Administrator can approve or deny the request of the User.

    Private

    Users cannot join this site or request for the site membership of a private site, rather Site admin have to manually add the users to the site.

    Optional Sites in Liferay 7 DXP


    Parent Site

    A site can have a parent site which is already being created and inherit the behaviors from the parent Site, a site can only have one parent site, A site can have any number of sub sites.

    Personal Site

    When an user is created in Liferay, Liferay  provides the user with his own personal site as an option which can be accessible only to that user.

    Organization Site

    An Organization in Liferay can have an optional site associated with it, with public and private pages and we can define the site template for them. User must be assigned to the Organization in order to access this site.

    Location Site

    An Organization can have its Locations which also can have a site, the user must be assigned to the Location and its parent organization in order to access this site.

    User Group Site

    Users can be grouped together into teams based on which allows us to create site for the User Groups, User must belongs to the User Group In Order to access this site.


    Its our responsibility to identify whether we have to create a Site or Organization based on the requirement