KNOWLEDGEBASE

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

  • Portlet Modes In Liferay 7 DXP

    Portlet Modes In Liferay 7 DXP


    Portlet Mode of a Portlet Indicates the function that a portlet performs and generate different content based on the function they perform. Some Specific features are provided in each portlet mode to enhance the accessibility, Each Portlet Mode have its own Portlet Controller Class and Views defined in each specific Portlet mode.

    Standard Portlet Modes of JSR-286 Portlet Specification


    View Mode

    Portlet renders markup text fragment (outcome of view) in view mode, its the most commonly used mode.

    Edit Mode

    Used to change per-user settings to customize rendering in the Portlet, We can use this mode when we want to let the User to perform actions in edit Mode

    Help Mode

    Used to display helpful information to the end User, Usually we can use this mode to give Help information to the user. 

    Additional Liferay portlet modes


    About

    About Mode Used to display the information about the Application (portlet) such as Application owner, Author etc.

    Config

    We can use this mode to Configure the Portlet Application for User Personalization and We can Store the User preferences in Portlet Preferences for giving User Specific Application behavior.

    Edit default

    This mode is used when we want to let User to Edit the default configuration and preferences of the Portlet Application.

    Edit guest

    Using This Mode we can let the Guest User to Perform edit operation (function) in the Portlet Application.

    Print

    This Mode is Used When we want to let user to print the Content of the Portlet Application Using the Printer, User can be given some additional features related to printing the content using this mode.

    Preview Mode

    We can Use This mode when we want the Ability or the User to Preview the Content or document in the Portlet Application.

    Note : Each Mode Must have the Portlet Controller Class and Views Defined for the mode.  

    Please refer the Portlet specifications for more details.


    JSR 286: Portlet Specification 2.0
    Understanding the Java Portlet Specification 2.0 (JSR 286)
    Java Portlet Specification

    I Will be covering the implementation of different portlet modes in Liferay 7 DXP in the future posts.


  • Overriding Module Apps JSP In Liferay 7 DXP

    Overriding Module Apps JSP In Liferay 7 DXP


     
    Overriding Module JSP Apps JSPs In Liferay 7 DXP, Since Liferay 7 is completely restructured its OOB Portlets into Modularized OSGi Bunles of Modules, OOB Portlets are referred as Apps or Independent Modules. In my example i am going to Override the JSP of The Famous Login Portlet.

    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 a New Liferay Module Project Fragment


    Click On the New Wizard Button of the Liferay In The Liferay Developer Studio as in the above image.



    Give a Project Name, Build Type and Select Liferay Run time Environment and Click Next

    Step 2 : Select The OSGi Host Bundle To Override


    I am Overriding Login Portlet Which is available in the com.liferay.login.web Bundle, so Select the Module and Click OK

    Step 3 : Add Files From OSGi Bundle To Override



    There is an "Add Files From OSGi Bundle" Button in the right side of the window as shown above, Click on that button that List you all the JSPs in that Bundle to override as below.



    Select META-INF/resources/login.jsp as shown above, as we are overriding the login.jsp, If you want override any other jsp you can select the jsp from the list.


    Click Finish Button, This Will Create a Liferay Fragment Module Project For Login Portlet as shown in the below directory structure.


    Step 4 : Modify The JSP Of The OSGi Bundle

    Lets add some text into the login.jsp, i have added the text LIFERAY STACK LOGIN FRAGMENT at the top of the jsp page, lets deploy the login fragment module and test it. When you deploy the fragment bundle we can clearly see the com.liferay.login.web bundle is getting restarted


    Reload the Portal and Click on the Sign In Button of the Portal to See the Changes


    Awesome, Now you can modify the JSP of any App Module of Liferay DXP.
    Source Code


  • Adding Portlet To Product Menu In Liferay 7 DXP

    Adding Portlet To Product Menu In Liferay 7 DXP

    Adding Portlet To Product Menu In Liferay 7 DXP

    Adding Portlet To Product Menu In Liferay 7 DXP, In Earlier versions of liferay Before Liferay 7 we used to mention the control panel category in liferay-portlet.xml, In Liferay 7 DXP Portlet can be added to Product menu by extending  PanelApp Service

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

    In Liferay 7 DXP The Layout of the Portal Have Been Changed, The New Layout of the Liferay 7 DXP Portal is Shown Below







    Step 1 : Create A Portlet

    Create A Portlet Which needs to added to the Product Menu
    In My Case I have Created  a Portlet Named "Product Admin Portlet", make the category of this portlet to hidden using "com.liferay.portlet.display-category=category.hidden"
    The Complete Portlet Class of the portlet is shown below.
    package com.liferaystack.admin.panel.app.portlet;
    
    import com.liferaystack.admin.panel.app.constants.ProductAdminPortletKeys;
    import com.liferay.portal.kernel.portlet.bridges.mvc.MVCPortlet;
    import javax.portlet.Portlet;
    import org.osgi.service.component.annotations.Component;
    
    /**
     * @author Syed Ali
     */
    
    @Component(
     immediate = true,
     property = {
      "com.liferay.portlet.display-category=category.hidden",
      "com.liferay.portlet.instanceable=true",
      "javax.portlet.display-name=Product Admin Portlet",
      "javax.portlet.init-param.template-path=/",
      "javax.portlet.init-param.view-template=/view.jsp",
      "javax.portlet.name=" + ProductAdminPortletKeys.ProductAdmin,
      "javax.portlet.resource-bundle=content.Language",
      "javax.portlet.security-role-ref=power-user,user"
     },
     service = Portlet.class
    )
    public class ProductAdminPortlet extends MVCPortlet {
    }
    


    Step 2 : Add Dependency

    Add the below Gradle Dependency to the Portlet, This Dependency is mandatory.
    compileOnly group: "com.liferay", name: "com.liferay.application.list.api", version: "2.0.0"
    to your build.gradle


    Step 3 : Create ProductAdminPanelApp Component Class

    Create a Class named ProductAdminPanelApp, this class must extend the BasePanelApp Class, implement the methods of super class as shown below and declare it as a service using service = PanelApp.class

    @Override
    public String getPortletId() {
     return ProductAdminPortletKeys.ProductAdmin;
    }
    
        @Override
        @Reference(target = "(javax.portlet.name=" + ProductAdminPortletKeys.ProductAdmin + ")",unbind = "-")
        public void setPortlet(Portlet portlet) {
            super.setPortlet(portlet);
        }
    

    The Complete ProductAdminPanelApp Will be

    package com.liferaystack.admin.panel.app.portlet;
    
    import com.liferay.application.list.BasePanelApp;
    import com.liferay.application.list.PanelApp;
    import com.liferay.application.list.constants.PanelCategoryKeys;
    import com.liferay.portal.kernel.model.Portlet;
    import com.liferaystack.admin.panel.app.constants.ProductAdminPortletKeys;
    
    import org.osgi.service.component.annotations.Component;
    import org.osgi.service.component.annotations.Reference;
    
    /**
     * @author Syed Ali
     */
    
    @Component(
         immediate = true,
         property = {
             "panel.app.order:Integer=100",
             "panel.category.key=" + PanelCategoryKeys.CONTROL_PANEL_USERS
         },
        service = PanelApp.class
    )
    public class ProductAdminPanelApp extends BasePanelApp {
    
        @Override
        public String getPortletId() {
     return ProductAdminPortletKeys.ProductAdmin;
        }
    
        @Override
        @Reference(target = "(javax.portlet.name=" + ProductAdminPortletKeys.ProductAdmin + ")",unbind = "-")
        public void setPortlet(Portlet portlet) {
            super.setPortlet(portlet);
        }
    }
    

    You are done just deploy the portlet. After Deploying the created portlet you can see your portlet under Users Category Of Control Panel Section of the Product Menu. as shown below.



    Source Code


  • OSGI In Liferay 7 DXP

    OSGI In Liferay 7 DXP

    OSGI In Liferay 7 DXP


    OSGi In Liferay 7 DXP is a new concept introduced In Liferay 7, The main intention behind OSGi is to split the Liferay 7 Portal  Modules independent of each other. I am gonna introduce you to some of the basic concepts of OSGI such OSGi Modules, OSGi Components and OSGi Services.

    What is Modularity?

    A Software application is Consists of different parts, they are typically called as software components. These components interact with each other using the API's. API's are nothing but the Classes, Interfaces and Methods defined in a Module.

    What is a Module?

    A module can be defined a part of Software application composed of different Components and Services, Service can be used to perform any action. In OSGI a Module is collection of classes and interfaces as a Bundle, Finally Its a JAR file with some meta data. In older versions of Liferay we were using the concept of plugins, now in Liferay 7 DXP any new project we create is called as module.

    What is OSGi?

    OSGI Is  service oriented platform on top of unique class loading mechanism, its enables features such as versioning of modules, encapsulation of API's (Classes) and run-time dynamics(starting stopping updating modules at run-time)

    Problems with Non Modular Applications

    Blurring the responsibility
    Increases complexity
    maintenance becomes harder over time

    Benefits of OSGi

    Instead of gigantic big application, using OSGI we can separate them as independent modules.
    Easy maintenance
    Each Module with its own clear responsibility
    Restrict and Expose the visibility of classes properly

    OSGi Component

    If you want the lifecycle of your object to be managed by the OSGi container,  you can declare it as a component. Using Declarative Service annotations,  you can make a POJO Class a OSGi component by annotating it with @Component With this,  you'll get the ability to start, stop and configure the components.

    OSGi Service

    OSGi components classes can be made as OSGi services just by annotating it with @Service annotation. Services should implement atleast an interface. When you mark a component as service, you can use (call) this service from other osgi components.
    Any class defined as a component can be used only inside the module, where as when you declare class as service, it can be accessible outside the module, you can export them, the other modules just have to import them in bnd.bnd files in order to use the service. 

    What is Dependency In OSGI?

    When a module uses the services of another module we can say the Module 1 is Dependent on Module 2.

    What is OSGI Container?

    OSGI Container manages the Lifecycle of  Components, Services and Bundle or Modules

    What is Bundle?

    OSGI Modules (Projects) are called as bundles. 


    Bundle Lifecycle

    The life cycle of a bundle is shown in the below picture, once the module is published(deployed), it will go through the Life cycle as shown below.


    Publish Find Bind Model

    OSGi Works on the model called publish-find-bind If a module wants to consume the service of the other module, the other module have to be published, the consumer module have to find the published module and bind it to consume its services


    OSGI Framework Architecture


    Bundles – Bundles are the OSGi components that we create in module project.

    Services – Services connects bundles dynamically using a publish-find-bind model for POJO Objects (Services and components).

    Life Cycle – An Interfce to install, start, stop, update, and uninstall bundles.

    Modules – It's a layer that defines how a bundle can import and export packges,classes codes.

    Security – A layer to the handles the security features.

    Execution Environment – Tells Which methods and Classes are available in the specific platorm.


    I know most of us will not understand OSGi when we read it for the first time, i will give more information on OSGi with examples in detail soon.