Thursday 31 August 2017

Open Office Integration


Liferay has a capability to convert document in different format like pdf, doc...etc. We can convert web content as well as document media content in different format. 

But this feature is not enabled by default. To enable this feature we need to perform some extra steps. 



Consider the below web content and you want to have a link in the content to export as PDF. 

Sample Web Content

To achieve this we can integrate open office in your portal. Once open office is configured we can easily convert any document as well as liferay web content in different formats. 


Below are the few steps to integrate open office in Liferay.

Step 1: Download open office from the site  https://www.openoffice.org 

Step 2: Install open office. 

Step 3: Run the open office service. 

Step 4: Configure Liferay to use open office service. 

Step 5: Enable export option for web content. 

                                              

I have downloaded Apache_OpenOffice_4.1.1_Win_x86_install_en-US.exe. You can download open office according to your machine. (Step 1 is completed)


Install open office in your machine in any drive. I have installed in C:\Program Files (x86)\OpenOffice 4. You can install according to your interest. (Step 2 is completed)


In the Step 3 you have to start the open office service. Navigate to C:\Program Files (x86)\OpenOffice 4\program (In your case it may be different). Run the below command (its a windows command)

soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard

(Step 3 is completed)


In step 4 configure Liferay to use this service. To integrate go to your control panel and navigate as shown in below screen shot. 

External Services

Then enable open office as below 

Enable Open Office

(Step 4 is completed)

In Step 5 go to your web content. Click the setting icon or Configuration icon. 

Click the settings icon

                         Check the PDF and Save. 

Enable PDF
Refresh your page. PDF icon will appear in your web content as in the below image. (Step 5 is completed)

PDF icon appeared


Similarly you can convert your documents which are uploaded through document and media portlet. Just open any document from your document and media portlet. You will see different formats to convert a document.

Hide Sign In Portlet


Have you ever thought of hiding Sign In Portlet? Once I got a requirement to do this for my project. Here is a quick tips. 

Sign In Portlet


Log in Liferay as Admin and go to look and feel as below 


Go to look and feel


In the advance styling add below code and save the changes.

.signed-in .portlet-login {
    display: none;
}

Advance Styling



Refresh your browser. Sign In Portlet will be invisible. 




Careful: 
Suppose you want to revert back. How will you do that? Because its invisible.  By firebug just inspect the page as below 

Inspect

The same thing can be in VM file of the theme also. But this is the most light wight option to achieve this. 

Wednesday 30 August 2017

How to Access Liferay Services in Velocity(VM) files using Service Locator?



Some time we need to access Liferay Services in our theme or Web content Templates. So today we will discuss how to access Liferay Services in our theme and templates. 

For Theme
If you want to access liferay services like user services in your theme you can use this code in portal_normal.vm:-

#set($userLocalService = $serviceLocator.findService('com.liferay.portal.service.UserLocalService'))
=>$userLocalService.getUsersCount()
view rawportal_normal.vm hosted with ❤ by GitHub


Explanation:-
Here we first get the UserLocalService object and then callgetUsersCount method.
For Web Content Templates
You can use the above code in your templates files but you need to do one extra step. Just enter :-

velocity.engine.restricted.variables=

in your portal-ext.properties and restart server

Set 'X-Frame-Options' In Liferay DXP To Allow Iframe Access


You might have seen below error message in your browser console while including any of the Liferay page url in iframe.

Refused to display 'liferay-url' in a frame because it set 'X-Frame-Options' to 'sameorigin'.

Liferay does not allow access to any of its url using iframe if the request is coming from different domain. If the domain is same you will be able to access Liferay url using iframe.

To resolve this you can either disable check by keeping below property in system-ext.properties.
(Previous to Liferay DXP this property was part of portal.properties but from DXP it has been moved in system.properties).

 #
    # Set this to true for the portal to send the "X-Frame-Options: DENY" HTTP
    # header to protect against clickjacking.
    #
    # Custom HTTP header values instead of "DENY" can be specified per URL via
    # the properties "http.header.secure.x.frame.options.*".
    #
    http.header.secure.x.frame.options=false

If you disable this property all your Liferay URL will be accessible through iframe and it cause any security issue. This is not a recommended approach to fix this issue.

As a alternative approach you can specify url you want include in iframe using below property in system-ext.properties. For each url you need to create separate property.

 #
    # If the property "http.header.secure.x.frame.options" is set to true, then
    # the portal will iterate through the properties
    # "http.header.secure.x.frame.options.*" where the wildcard "*"
    # is replaced with an int number starting from 0 and ending with 255 to find
    # a pipe (|) delimited URL and HTTP header value pair (e.g. "/|SAMEORIGIN").
    #
    # Upon finding a matched a URL, the portal will use the custom HTTP header
    # value instead of the "DENY" value.
    #
    # By default, URLs that start with "/" will use a custom HTTP header value
    # of "SAMEORIGIN". Specify another URL and HTTP header value with a number
    # smaller than 255 to intercept this default behavior.
http.header.secure.x.frame.options.1=/web/guest/home
http.header.secure.x.frame.options.4=/web/test/home
http.header.secure.x.frame.options.5=/servlet-url/servlet-content.

Tuesday 29 August 2017

Liferay 7: How to create a new portlet?

\

There were many interesting features added or improved in Liferay 7. And Liferay Workspace is one of them, it is a new way to organizing our source code and recommended by Liferay. In this post, I will create a portlet by Liferay 7 default style ( it means using Liferay Workspace).

Environment installation

Liferay 7's tools

Firstly, let's download Liferay 7 Portal and install the development environment. And take a look the different between Liferay 7 and previous. Here are the tools and its links:
  • Liferay Portal 7.0.2 GA3: Click here to download.
  • Liferay IDE 3.1.0 M1: Let's download the suit version (Windows x64Linux x64 or other versions at here). Mote: you can download only Liferay IDE and then plug in your Eclipse IDE. But, using the Liferay IDE Eclipse which is provided by Liferay is the best.

Configurations

Unzip two downloaded files and change some configurations.

Database configuration

The database configuration is similar to Liferay 6.x's one (there is the best tutorial). You have to create a file named portal-ext.properties in {liferay-portal-home}/tomcat-8.0.32/webapps/ROOT/WEB-INF/classes/ folder. And you must provide some configuration like this:
jdbc.default.driverClassName=org.postgresql.Driver
jdbc.default.url=jdbc:postgresql://localhost:5432/lportal7
jdbc.default.username=postgres
jdbc.default.password=123456
jdbc.default.maxPoolSize=100
jdbc.default.minPoolSize=10
jdbc.default.numHelperThreads=10

I am using PostgreSQL database in my example, I created a database name lportal7 in the local machine. If your database is different with mine, let's see the tutorial in this link. Note, Liferay 7 CE version in only supported the open source database like MySQL, PostgreSQL, ... (click here to reach the details).

Eclipse and Liferay portal configuration.

Create new Liferay Server: Open Eclipse IDE which has been unzipping. On the menu, click on New > Server > Server link below figure:

Click on Next button and select Liferay, Inc. >  Liferay 7.x,  see the next figure:


Click on Next button to move the next step:
At this step, enter the name of the server in name text box and browse to the Liferay Portal Bundle folder (which have been unzipping). Next, you must select the suit JRE runtime version (JRE 1.8 version is used in this example). Then, click on Finish button to get the end of configuration.
Last, you have to turn the created server on. After starting up, let's open a browser and go the http://localhost:8080 page. Amazing, your portal is here, you got it!

    Create new portlet

    After the setting up, as a Liferay developer, I will create a new project by Liferay 7 style. So, how do I create it?
    On the menu of Eclipse IDE, click on File > New > Liferay Module Project. A new popup window appears and you have to fill some information about your project like the below figure:
    You have to enter the project name, select a build tool and project template.There were two build tools (gradle and maven). You must select a fit project template (there were many project templates, I will introduce about them in the future posts.). For the purpose of introduction, I have choose mv-portlet template and click on Next button to reach the next step.

    In step, you have to enter the main class and package's name. And, lest click on Finish button to finish this creation. Now, you created a new project and the below figure is its structure.
    The structure of project which creates by gradle or maven tool is same. Java code and the resources separated. It is easy to code management
    Let's take a look, you do not see some familiar file like portlet.xml, liferay-portlet.xml và liferay-display.xml. Where are they?
    Next, we will review the content of TestPortlet class,
    package com.chingovan.liferay7.test.portlet;
    import com.liferay.portal.kernel.portlet.bridges.mvc.MVCPortlet;
    import javax.portlet.Portlet;
    import org.osgi.service.component.annotations.Component;
    /**
    * @author ChiNV
    */
    @Component(
    immediate = true,
    property = {
    "com.liferay.portlet.display-category=category.sample",
    "com.liferay.portlet.instanceable=true",
    "javax.portlet.display-name=test-portlet 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 TestPortlet extends MVCPortlet {
    }

    As you can see, this class contains a new part (line 12th to 24th). Take a closer look, they are the pairs of key and value which were defined in some configuration file like portlet.xml, liferay-portlet.xml và liferay-display.xml. So, Liferay 7 instead of writing them on configuration files, write into Java code.

    Portlet deployment

    Next step is the deployment created project into the server. It is the easy task. You only right click on Liferay 7x server and chose "Add or Remove...." in float menu. Let's choose the portlet which will be deployed and click on OK button. The deployment will be complete automatically.
    Open your browser and drag your portlet on your page. Let's click on "+" button on the top right browser:
    Opening the "Sample" category and drag your portlet into any position. And then, your portlet will be displayed.

    Access Gogo Shell in Liferay 7

    Access Gogo Shell in Liferay 7

    To access Gogo shell your server must be running...

    Their are two ways to access Gogo shell 

    1st Option

    We can open Gogo Shell using Liferay IDE or Eclipse simply by right clicking on server and clicking Open Gogo Shell




    This will open Gogo shell terminal in your IDE 


    Option 2 

    Open command prompt and hit the command

    telnet localhost 11311

    Important Gogo shell command

    1) List Bundle Command i.e lb command

    Type lb and hit Enter this will list all modules in liferay with its Id State and Name as shown bellow




    2)Using grep command along with lb command:

    lb command list all module available in liferay
    Using grep along with liferay will narrow list of module to what we want to see

    Example


    Open Gogo Shell and hit following command


    lb | grep Journal




    This will list down only modules related to Journal

    You can replace Journal with any other module name you wish to see...

    3)start and stop command


    You can start and stop module using simple start and stop command along with id of module


    Example start 420

                   stop 420

    Liferay 7 Installation

    Liferay 7 Installation


    Step 1 Download Liferay and Java 8 and Eclipse

    Step 2 Server Configuration
    • Open your Eclipse IDE GoTo Windows--Show View--click on server 
    • Click available link to create new server 
    • Go to Liferay  and configure Liferay 7.x as shown below

    • Click Next and set path of your local directory where you have extracted Liferay portal tomcat. 
    • Note Liferay 7 requires complete jdk8 not just jre so click on Installed JRE and configure your jdk as shown below.

    • Click on Add
    • Select Standard VM and click on Next
    • Then give path of your jdk and click finish 
    • Now select available jdk option and click on Finish 

    Step 3 Creating Liferay Workspace
    • Goto File ---New--click on liferay workspace project.
    • Enter name of workspace and download liferay bundle must be unchecked since we have already download  liferay bundle.
    • Click on Finish.
    • This will create your liferay workspace as shown below.

    Step 4 Database Configration
    • Create portal-ext.properties file and add database properties as shown bellow
    • Note here liferayportal is my database name in mysql
    • Change username,password and liferay home path according to your configration and installation directory
    • Place portal-ext.properties parallel to your tomcat server directory
    jdbc.default.driverClassName=com.mysql.jdbc.Driver

    jdbc.default.password=root

    jdbc.default.username=root

    jdbc.default.url=jdbc:mysql://localhost/liferayportal?characterEncoding=UTF-8&dontTrackOpenResources=true&holdResultsOpenOverStatementClose=true&useFastDateParsing=false&useUnicode=true

    liferay.home=D:/LiferayDXP/liferay-dxp-digital-enterprise-tomcat-7.0-ga1-20160617092557801/liferay-dxp-digital-enterprise-7.0-ga1

    Step 5 Start tomcat server
    • Create database  liferayportal  in your mysql.
    • Finally we have completed all our configration 
    • Start your server.