Hook for Remote Live Publishing History of web content

General Blogs May 11, 2018 By Vikash Kumar

 
portal.properties
-----------------------
journal.article.form.default.values=content,abstract,categorization,display-page,related-assets,permissions,custom-fields,Publish History
journal.article.form.update=content,abstract,categorization,schedule,display-page,related-assets,custom-fields,Publish History
 
 
create Publish History.jsp inside html/portlet/journal/article and put below code
<%--
/**
 * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */
--%>
 
<%@ include file="/html/portlet/journal/init.jsp" %>
 
<%@ page import="com.liferay.portal.model.BackgroundTask" %>
<%@ page import="com.liferay.portal.service.BackgroundTaskLocalServiceUtil" %>
<%@ page import="com.liferay.portal.kernel.dao.orm.DynamicQuery" %>
<%@ page import="com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil" %>
<%@ page import="com.liferay.portal.kernel.util.PortalClassLoaderUtil" %>
<%@ page import="com.liferay.portal.kernel.dao.orm.RestrictionsFactoryUtil"%>
<%@ page import="com.liferay.portal.kernel.dao.orm.Criterion"%>
<%@ page import="com.liferay.portal.kernel.dao.orm.OrderFactoryUtil" %>
<%@ page import="com.liferay.portal.kernel.backgroundtask.BackgroundTaskConstants" %>
 
<div><h4>Remote Live Publishing History</h4></div>
<%
JournalArticle article = (JournalArticle)request.getAttribute(WebKeys.JOURNAL_ARTICLE);
String articleId = article.getArticleId();
try{
DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
BackgroundTask.class, PortalClassLoaderUtil.getClassLoader());
Criterion criterion1=RestrictionsFactoryUtil.like("taskContext","%##########% "+articleId+",%##########%");
        Criterion criterion2 =RestrictionsFactoryUtil.eq("completed", true);
        
        dynamicQuery.add(RestrictionsFactoryUtil.and(criterion1, criterion2));
        dynamicQuery.addOrder(OrderFactoryUtil.desc("createDate"));
List<Object> list = BackgroundTaskLocalServiceUtil.dynamicQuery(dynamicQuery);
if(list!=null && list.size()>0){%>
<div style="max-height:400px; overflow-x:auto">
<table class="table table-bordered table-striped">
<thead class="table-columns">
<tr>
<th>#</th>
<th>Published On</th>
<th>Published By</th>
<th>Status</th>
<!-- <th>BackgroundTaskId</th> -->
</tr>
</thead>
<tbody class="table-data">
<%for(int j=0;j<list.size();j++){%>
<tr>
<td class="table-cell"><%=j+1%></td>
<td class="table-cell"><%=((BackgroundTask)list.get(j)).getCreateDate()%></td>
<td class="table-cell"><%=((BackgroundTask)list.get(j)).getUserName()%></td>
<td class="table-cell"><%=BackgroundTaskConstants.getStatusLabel(((BackgroundTask)list.get(j)).getStatus())%></td>
<%-- <td class="table-cell"><%=((BackgroundTask)list.get(j)).getBackgroundTaskId()%></td> --%>
</tr>
<% }%>
</tbody>
</table>
</div>
<%
}
else{%>
<div>Either this has not been published or no history available</div>
<%}
}
catch(Exception ex){
ex.printStackTrace();
}
 
 
%>
 

Upload web document/file in to liferay from external java application

General Blogs May 5, 2018 By Vikash Kumar

Refer https://web.liferay.com/web/antonio.junior/blog/-/blogs/12168124 and create web service client for Portlet_DL_DLAppService.

 

package com.lr.ws.soap.fileupload;
 
import java.net.MalformedURLException;
import java.net.URL;
 
import com.liferay.portal.service.ServiceContext;
import com.liferay.portlet.documentlibrary.service.http.DLAppServiceSoap;
import com.liferay.portlet.documentlibrary.service.http.DLAppServiceSoapServiceLocator;
 
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
 
public class DLFileUpload {
public static void main(String[] args) {
        try {
        DLAppServiceSoapServiceLocator dlAppLocator = new DLAppServiceSoapServiceLocator();
            DLAppServiceSoap dlAppService = dlAppLocator.getPortlet_DL_DLAppService(getURL("Portlet_DL_DLAppService"));
        ServiceContext serviceContext = new ServiceContext();
       
        Path path = Paths.get("path of file");
        byte[] bytes = Files.readAllBytes(path);
       
         dlAppService.addFileEntry(repositoryId, folderId, sourceFileName, mimeType, title, description, changeLog, bytes, serviceContext);
            System.out.println("-------------------FILE UPLOADED-------------------------------");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    private static URL getURL(String serviceName) throws MalformedURLException {
        String url = "http://localhost:8080";
        String screenName = "userName";
        String password = "Password";
 
        int pos = url.indexOf("://");
        String protocol = url.substring(0, pos + 3);
        String host = url.substring(pos + 3, url.length());
 
        StringBuilder sb = new StringBuilder();
        sb.append(protocol);
        sb.append(screenName);
        sb.append(":");
        sb.append(password);
        sb.append("@");
        sb.append(host);
        //sb.append("/api/secure/axis/");
        sb.append("/api/axis/");
        sb.append(serviceName);
        System.out.println("URL "+sb.toString());
        return new URL(sb.toString());
    }
}
 

 

Show live or logged in users

General Blogs January 5, 2018 By Vikash Kumar

This blog is for creating an interface which will show number of live users and some other details.

First of all create a portlet which will provide interface and then create a hook within the portlet.Below link willl help to create hook in portlet.

http://www.learnandpost.com/2012/05/create-hook-inside-portlet-liferay.html

view.jsp

Add below line of code in portlet view.jsp

<%@page import="com.liferay.portal.service.UserLocalServiceUtil"%>
<%@page import="com.liferay.portal.model.User"%>
<%@page import="java.util.Iterator"%>
<%@page import="com.services.loggedinuser.LoggedInUsers"%>
<%@page import="java.util.Set"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />
 
<style>
table {
    font-family: arial, sans-serif;
    border-collapse: collapse;
    width: 100%;
}
 
td, th {
    border: 1px solid #dddddd;
    text-align: left;
    padding: 8px;
}
th {
background-color: #e1e1e1;
}
tr:nth-child(even) {
    background-color: #eee;
}
</style>
 
<div style="border:1px solid #ccc;padding:5pt;">
<b>Total number of Live Users : </b>
 
<%
Set<Long> users_set = LoggedInUsers.loggedInUsers();
%>
<b><%=users_set.size() %></b><br/>
<table>
<thead>
<tr><th>User Id</th><th>Screen Name</th><th>Full Name</th><th>IP Address</th><th>Login Date</th></tr>
</thead>
<tbody>
<%
Iterator<Long> it = users_set.iterator();
while(it.hasNext()){
long user_id = it.next();
User user = UserLocalServiceUtil.getUser(user_id);
String fullName = user.getFullName();
String ipAddress = user.getLastLoginIP();
String screenname = user.getScreenName();
String loginDate = user.getLoginDate().toString();
%>
<tr>
<td><%=user_id %></td>
<td><%=screenname %></td>
<td><%= fullName%></td>
<td><%=ipAddress %></td>
<td><%=loginDate %></td>
</tr>
<%
}
%>
</tbody>
</table>
</div>
 
Now it s time to work on hook which we have create and which will tell about live users.
liferay-hook.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hook PUBLIC "-//Liferay//DTD Hook 6.2.0//EN" "http://www.liferay.com/dtd/liferay-hook_6_2_0.dtd">
 
<hook>
<portal-properties>portal.properties</portal-properties>
</hook>
portal.properties
login.events.post=com.services.loggedinuser.LoginPostAction 
servlet.session.destroy.events=com.services.loggedinuser.SessionDestroyAction
 
Create LoginPostAction and SessionDestroyAction class.
LoginPostAction
package com.services.loggedinuser;
import com.liferay.portal.kernel.events.Action;
import com.liferay.portal.util.PortalUtil;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
public class LoginPostAction extends Action {
@Override
public void run(HttpServletRequest request, HttpServletResponse response) {
long userId = PortalUtil.getUserId(request);
LoggedInUsers.addUser(userId);
System.out.println("Logged in user :"+userId);
}
}
 
SessionDestroyAction
package com.services.loggedinuser;
import com.liferay.portal.kernel.events.SessionAction;
import com.liferay.portal.kernel.util.WebKeys;
 
import javax.servlet.http.HttpSession;
 
public class SessionDestroyAction extends SessionAction{
@Override
public void run(HttpSession session) {
Long userId = (Long) session.getAttribute(WebKeys.USER_ID);
LoggedInUsers.removeUser(userId);
System.out.println("Log out user :"+userId);
}
}
Also create one model object for live users.
 
LoggedInUsers 
package com.services.loggedinuser;
 
import java.util.HashSet;
import java.util.Set;
 
public class LoggedInUsers {
private static Set<Long> users = new HashSet<Long>();
 
public static void addUser(long userId) {
users.add(userId);
}
 
public static void clearUsers() {
users.clear();
}
 
public static void removeUser(long userId) {
users.remove(userId);
}
 
public static int countUsers() {
return users.size();
}
 
public static boolean isLogged(long userId) {
return users.contains(userId);
}
 
public static Set<Long> loggedInUsers(){
return users;
}
}
 

How to get themedisplay object and portlet id in wcd velocity template

General Blogs July 27, 2017 By Vikash Kumar

#set ($serviceContext = $portal.getClass().forName("com.liferay.portal.service.ServiceContextThreadLocal").getServiceContext())

    #set ($httpServletRequest = $serviceContext.getRequest())

    #set ($objThemeDisplay = $httpServletRequest.getAttribute("LIFERAY_SHARED_THEME_DISPLAY"))

 

$objThemeDisplay.getPortletDisplay().getId()

Disable browser back button

General Blogs April 18, 2017 By Vikash Kumar

<script type = "text/javascript" >
(function (global) {
 
if(typeof (global) === "undefined")
{
throw new Error("window is undefined");
}
 
    var _hash = "#";
    var noBackPlease = function () {
        global.location.href += "#";
 
// making sure we have the fruit available for juice....
// 50 milliseconds for just once do not cost much (^__^)
        global.setTimeout(function () {
            global.location.href += "#";
        }, 50);
    };
 
// Earlier we had setInerval here....
    global.onhashchange = function () {
        if (global.location.hash !== _hash) {
            global.location.hash = _hash;
        }
    };
 
    global.onload = function () {
        
noBackPlease();
 
// disables backspace on page except on input fields and textarea..
document.body.onkeydown = function (e) {
            var elm = e.target.nodeName.toLowerCase();
            if (e.which === 8 && (elm !== 'input' && elm  !== 'textarea')) {
                e.preventDefault();
            }
            // stopping event bubbling up the DOM tree..
            e.stopPropagation();
        };
 
    };
 
})(window);
</script>

Create a copy document from document and media into portlet local temp folder

General Blogs November 16, 2016 By Vikash Kumar

OutputStream outputStream = null;
InputStream inputStream = null;
try {
DLFileEntry fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(targetGroupId, folderId , filename);
inputStream = DLFileEntryLocalServiceUtil.getFileAsStream(themeDisplay.getUserId(),fileEntry .getFileEntryId(), fileEntry .getVersion());
String tempFolderPath = resourceRequest.getPortletSession().getPortletContext().getRealPath(File.separator) + "temp"+File.separator;
 
File tempFolder = new File(tempFolderPath);
if (!tempFolder.exists()) {
   try{
    tempFolder.mkdir();
   }catch(Exception se){
       se.printStackTrace();
   } 
}else{
String[] files;
       if (tempFolder.isDirectory()) {
        files = tempFolder.list();
           for (int i = 0; i < files.length; i++) {
               File file = new File(tempFolder, files[i]);
               if (!file.isDirectory()) {
                file.delete();
               }
           }
       }
}
 
File file = new File(tempFolderPath+File.separator+spreadSheetName);
outputStream = new FileOutputStream(file);
int read = 0;
byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
outputStream.close();
 
spreadSheetPath = file.getAbsolutePath();
} catch (Exception e) {
e.printStackTrace();
} finally{
try{
if(outputStream != null)
outputStream.close();
if(inputStream != null)
inputStream.close();
}catch(Exception e){
e.printStackTrace();
}
}

Carousel with Structure and Temple in WCD

General Blogs October 7, 2015 By Vikash Kumar

Struture

 

 
<root available-locales="en_US" default-locale="en_US">
<dynamic-element dataType="string" indexType="keyword" localizable="true" name="title" readOnly="false" repeatable="true" required="false" showLabel="true" type="text" width="small">
<dynamic-element dataType="image" fieldNamespace="wcm" indexType="keyword" localizable="true" name="image" readOnly="false" repeatable="false" required="false" showLabel="true" type="wcm-image" width="">
<meta-data locale="en_US">
<entry name="label">
<![CDATA[Image]]>
</entry>
<entry name="predefinedValue">
<![CDATA[]]>
</entry>
<entry name="tip">
<![CDATA[]]>
</entry>
</meta-data>
</dynamic-element>
<meta-data locale="en_US">
<entry name="label">
<![CDATA[Title]]>
</entry>
<entry name="predefinedValue">
<![CDATA[]]>
</entry>
<entry name="tip">
<![CDATA[]]>
</entry>
</meta-data>
</dynamic-element>
<dynamic-element dataType="number" fieldNamespace="ddm" indexType="keyword" localizable="true" name="intervaltime" readOnly="false" repeatable="false" required="false" showLabel="true" type="ddm-number" width="small">
<meta-data locale="en_US">
<entry name="label">
<![CDATA[Interval Time]]>
</entry>
<entry name="predefinedValue">
<![CDATA[]]>
</entry>
<entry name="tip">
<![CDATA[Interval Time should be in millisecond]]>
</entry>
</meta-data>
</dynamic-element>
</root>

Template(ftl)

<script src="//code.jquery.com/jquery-1.10.2.js"></script>

<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/flexslider/2.5.0/jquery.flexslider.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/flexslider/2.5.0/jquery.flexslider.min.js"></script>
<script>
   $(document).ready(function() {
 $('.flexslider').flexslider({
   animation: "slide",
   slideshowSpeed: ${intervaltime.getData()},      
animationDuration: 0,
pauseOnHover: true
 });
});
</script>
 
<style>
.flex-direction-nav{
display:none;
}
 
.flex-control-paging{
display:none
}
 
.flexslider {
    text-align:center;
    width:100%;
}
 
.title{
    text-decoration:underline;
    font-size: 15px;
    font-weight: bold;
    font-style: italic;
    text-align:center;
    color:#000;
}
 
</style>
 
<#if title.getSiblings()?has_content>
    <div class="flexslider">
        <ul class="slides">   
        <#list title.getSiblings() as cur_title>
           <li>
              <div>
                   <p class="title">${cur_title.getData()}</p>
                   <img src="${cur_title.image.getData()}" alt"">
               </div>
           </li>
        </#list>
        </ul>
    </div>
</#if>

 

Maximize and Minimize portlet option for signed in user

General Blogs September 29, 2015 By Vikash Kumar

Liferay 6.x portal default portlet configuration does not display minimize and maximize icons on portlet headers. These buttons are only available to admin role users. For one of our projects, we had to display the minimize and maximize button for all of our logged in users. We tried to do a lot of research and realized that there is no easy way of doing it in liferay 6.x portal. This post is about a simple workaround we have found which will start showing minimize and maximize icons on all liferay portlets including custom and out of the box portlets.

This workaround requires us to modify a liferay core JSP file. You need to carefully follow below steps to start displaying the minimize and maximize icons on all portlets. We are using a tomcat bundle for describing below steps, in case you are using a different bundle you may need to look for specific directories in respective bundles.
 

portletDisplay.recycle();
if (themeDisplay.isSignedIn()) 
{         
showMaxIcon = true;         
showMinIcon = true; 
} 
  1. Go to the directory liferay-portal-6.x/tomcat-xxx/webapps/ROOT/html/portal
  2. Open the file render_portlet.jsp
  3. Find below text line in the render_portlet.jsp file
  4. Add below code snippet just above this line


The if condition "themeDisplay.isSignedIn()" is going to verify if the user is logged in and display the minimize and maximize icons for all logged in users on all portlets. In case you want to show the minimize and maximize icons to all users including guests you can remove this if condition and replace it with below snippet
 

showMaxIcon = true;
showMinIcon = true;


We spent a lot of time finding out this workaround, hope this helps others. Let us know if you can think of a better approach to do the same thing.

Set timer on AUI and send request on ActionURL over AJAX

General Blogs May 7, 2015 By Vikash Kumar

setInterval(function() {
                    var url = '<portlet:actionURL><portlet:param name="javax.portlet.action" value="getXXXMethod" /><portlet:param name="userid" value="<%=String.valueOf(userid)%>" /><portlet:param name="isGuest" value="<%=String.valueOf(isGuest)%>" /></portlet:actionURL>';  
                    AUI().use('aui-base','aui-io-request', function(A){
                    A.io.request(url,{dataType: 'json',method: 'GET',
                        data: {param:"timezone"},                     
                        on: {  
                          failure: function() { },  
                          success: function(data) {
                              var data = this.get('responseData');                        
                              A.Array.each(data, function(obj, idx){
                                  var location = obj.area;
                                  var timeValue =obj.timeZone;
                              });
                          }
                        }  
                    });
                });
            },60*1000);

Liferay LDAP integration and configuration Manual

Technical Blogs April 23, 2015 By Vikash Kumar

Liferay LDAP Configuration Manual

  1. Goto LDAP settings from Control Panel->Portal Settings. Under Configuration, click Authentication->LDAP.

  2. To enable LDAP, check "Enable", simple enough.

  3. Under Import/Export, check "Import Enabled". When checked, Liferay will periodically synchronize with LDAP. The interval Liferay does this can be changed in your portal-ext.properties.

  4. "Import on Startup Enabled" should be self-explanatory. Check it.

  5. We'll leave "Export Enabled" unchecked. As we are not going to export our data to Directory Server as of now.

  6. Now then, click "Add" to add your LDAP Server.

Type in the name of your LDAP Server at the top. We will notice that there is a radio button list of default choices (Optional).

  1. Now create Connection with LDAP by filling up following values

A) Base Provider URL: The LDAP Base Provider URL format is ldap: //host: port.

For example ldap://10.0.0.116:389

B) Base DN: The Base DN specifies the initial search context for users and is

optional. For example dc=ktree,dc=org where dc is domain component of the

organization.

C) Principal: It is User Principal Name. These are generally in the format of

<sAMAccountName>@<UPN Suffix>. Example cn=Manager,dc=ktree,dc=org

d) Credentials: Fill up the correct credential for LDAP Server to which Liferay is

going to connect with.

NOTE: CHECK THE CONNECTION If all are correct Liferay will prompt a message "Liferay has successfully connected to the LDAP server.

  1. Now fill up the USER Fields

  2. Authentication Search Filter : Enter the search filter that will be used to test the validity of a user. The tokens @company_id@, @email_address@, @screen_name@, and @user_id@ are replaced at runtime with the correct values.

  1.  

For example : (mail=@screen_name@).

  1. Import Search Filter : Used for checking filter while importing data on LDAP.

If we don't want to apply import search filter when we import user then simple fill this it is as (objectClass=person).

Now fill up the following attribute in order to import data on LDAP

  1. Screen Name : cn (common name or canonical name)

  2. Password : usePassword (users password)

  3. Email Address : mail

  4. Full Name : cn (ldap-full-name-attribute-help)

  5. First Name : cn (ldap-full-name-attribute-help)

  6. Middle Name : (leave it)

  7. Last Name : sn (sir name)

  8. Job Title : (leave it)

  9. Portrait : (leave it)

  10. Group : ou (organization unit)

  11. UUID : uidNumber / uid (universal id)

Note: Click Check LDAP Users button. If everything is fine it will pop a list of users

Leave the other fields as it is and click save button in order to use this LDAP configuration.


 

Manual to support preview of different file format

Technical Blogs April 23, 2015 By Vikash Kumar

Manual to support preview of different file format


 

  1. Install OpenOffice in local system to work with LifeRay in order to display the preview of different types of file. LifeRay will interact with OpenOffice to convert

Current file format into pdf format as LifeRay internally supports PDFBox.


 

  1. Run the following command to start OpenOffice:

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


 

  1. Open portal-ext.properties file and add following line at the end:

openoffice.server.enabled=true

openoffice.server.host=127.0.0.1

openoffice.server.port=8100

openoffice.cache.enabled=true


 

openoffice.conversion.source.extensions[drawing]=odg

openoffice.conversion.source.extensions[presentation]=odp,ppt,pptx,sxi

openoffice.conversion.source.extensions[spreadsheet]=csv,ods,sxc,tsv,xls,xlsx

openoffice.conversion.source.extensions[text]=doc,docx,html,odt,rtf,sxw,txt,wpd


 

openoffice.conversion.target.extensions[drawing]=pdf,svg,swf

openoffice.conversion.target.extensions[presentation]=odp,pdf,ppt,swf,sxi

openoffice.conversion.target.extensions[spreadsheet]=csv,ods,pdf,sxc,tsv,xls

openoffice.conversion.target.extensions[text]=doc,odt,pdf,rtf,sxw,txt


 

  1. Run LifeRay server through eclipse IDE and goto Control Panel of LifeRay.

Server -> Server Administration ->Open External Service Tab -> Enable open office integration -> Tick on Enables check in button -> save

Showing 12 results.
Items 20
of 1