Featured post

Docker setup for Liferay 7 with MySQL

Wednesday 26 October 2016

jQuery UI with Liferay 7

If you want to integrate liferay 7 theme/portlet/application with jQuery UI, you need to make subtle change in jQuery UI library itself.

By default library starts with -

(function( factory ) {
 if ( typeof define === "function" && define.amd ) {

  // AMD. Register as an anonymous module.
  define([ "jquery" ], factory );
 } else {

  // Browser globals
  factory( jQuery );
 }
}(function( $ ) {


If you add library directly in your liferay application, you will be getting an error - Mismatched anonymous define() module:

To overcome this issue, you need to change the library like



(function( factory ) {
 
  factory( jQuery );
 
}(function( $ ) {

What you are doing is removing the reference of define call, which causes the issue, as jQuery is already loaded by default.
Same thing you need to perform for other JS libraries which contains the call.


You are just done, Try & Enjoy the function.............:)

Monday 24 October 2016

3 ways to use serve resource in portal

JSR-286 introduced resource serving and the same method can perform AJAX requests as well.
It's a  long time but I am just describing here with code snippets all you can do with resource method.

This example works with Liferay 7.

There are three ways you can use resource method from your portlet.
We will be looking at these with code snippets
  • You can download a file
  • You can fetch piece of data
  • You can return a jsp/ftl...

1) Download File

Create a hyperlink and create resource url(Same step needs to be done for all calls)

<portlet:resourceURL var="downloadURL" />

<a href="<%=downloadURL%>">Download Users</a>

Method in your portlet class

public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse)
   throws IOException, PortletException {
   resourceResponse.setContentType("text/csv");
   resourceResponse.addProperty(HttpHeaders.CONTENT_DISPOSITION,
      "attachment;filename=sample.csv");
   OutputStream out = resourceResponse.getPortletOutputStream();
   String names = "Vipin , Bardia";
   
   InputStream in = new ByteArrayInputStream(names.getBytes());
   IOUtils.copy(in, out);
   
   out.flush();
}



2) Fetch dynamic data

Javascript call

jQuery(".htmlButton").click(function() {
      jQuery.ajax({
          url:$("#resource_url").val(),
          success: function(data)
          {
              jQuery('#result').html(data);
          }
  });
 });

Method in your portlet class


public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse)
   throws IOException, PortletException {
        resourceResponse.getWriter().write("I am sending my name : Vipin Bardia");
}

3) Return the jsp in AJAX call

Javascript call

Same javascript call as we used in example 2 to fetch dynamic data.


Method in your portlet class

public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse)
   throws IOException, PortletException {
        include("/MYAJAX.jsp", resourceRequest, resourceResponse);
}

You are just done, Try & Enjoy the function.............:)

Thursday 25 August 2016

Portal events in Liferay 7


Portal events or sometimes referred as action in Liferay are of different types.
They executes on different time and a developer can choose relevant action to perform.
You can find these entries in portal.properties-

1) Servlet service event
The pre-service events process before Struts processes the request.
The post-service events process after Struts processes the request.

servlet.service.events.pre
servlet.service.events.post

2) Login Events process after successful login only.

login.events.pre
login.events.post

3) Logout events called after successful logout.

logout.events.pre
logout.events.post

In this post we will be setting landing page in Liferay 7 OSGI way with post login action.
But at the end you will explore that all of the actions can be implemented by a single change of property.

I will be adding class only in the post, you can choose build type from - Gradle/Maven

To add events you need to -
1) Implement com.liferay.portal.kernel.events.LifecycleAction
2) Add annotation
@Component(
immediate = true, property = {"key=login.events.pre"},
service = LifecycleAction.class
)

that's all, you need to do :)

Below is the code you can refer -


package com.sample;

import javax.servlet.http.HttpSession;

import org.osgi.service.component.annotations.Component;

import com.liferay.portal.kernel.events.ActionException;
import com.liferay.portal.kernel.events.LifecycleAction;
import com.liferay.portal.kernel.events.LifecycleEvent;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.struts.LastPath;
import com.liferay.portal.kernel.util.StringPool;

@Component(
  immediate = true, property = {"key=login.events.post"},
  service = LifecycleAction.class
 )
public class LandingRedirectAction implements LifecycleAction {
 public static final Log LOGGER = LogFactoryUtil.getLog(LandingRedirectAction.class);
 @Override
 public void processLifecycleEvent(LifecycleEvent lifecycleEvent) throws ActionException {
  LOGGER.info("PostLogin called");
        HttpSession session = lifecycleEvent.getRequest().getSession();
        session.setAttribute("LAST_PATH", new LastPath(StringPool.BLANK, "/web/guest/landMeOnMoon"));
 }
}


Now, in here if you want to change this action/event to be called on each page just change the property "key" to "servlet.service.events.pre" and add your code accordingly.

You are just done, Try & Enjoy the function.............:)

Monday 25 July 2016

Osgi Console and commands

Liferay is now fully supporting osgi, So here are some steps to connect with osgi consoles and some basic commands

OSGI console is the way to deploy and manage state of modules/components.



Connect gogo shell


 Enable telnet in windows
Go to command prompt
 command - telnet localhost 11311

OR

use putty with localhost and port 11311



Install osgi web console

Go to gogo shell

command - install 

http://mirror.switch.ch/mirror/apache/dist/felix/org.apache.felix.webconsole-4.2.16-all.jar

OR

command -  install file:///C:/org.apache.felix.webconsole-4.2.16-all.jar

After installing bundle, use the bundle id to start the bundle. By default bundle is in installed state.

command - start [bundle id]

Access - http://localhost:8080/o/system/console/bundles- (admin/admin)

[bundle id] - is an integer which is identifier for particular bundle.



Gogo shell commands


install file:///C:/liferay-blade-samples/maven/hook/target/hook-1.0.0.jar - Install the bundle
diag [bundle id] - Check for issues
update [bundle id] - Pick up from the last location
lb - List all the bundles
 ss - List all the bundles(Different View)
lb [bundle name or part of the string] - List all the bundles with matching string
uninstall [bundle id] - Uninstall the bundle
start [bundle id] - Start the bundle
stop [bundle id] - Stop the bundle
 b [bundle id] - Provides bundle details
refresh [bundle id] - Refresh bundles
disconnect [bundle id] - Disconnect from Gogo shell
inspect  ('capability' | 'requirement') <namespace>  [bundle id] - Inspect the bundle


Osgi Status

INSTALLED - Bundle is available in container
ACTIVE - Bundle is available to use or can be activated lazily
RESOLVED - All prerequisite are available to activate the bundle
STARTING
STOPPING

You are just done, Try & Enjoy the function.............:)