RSS
Showing posts with label Liferay SDK. Show all posts
Showing posts with label Liferay SDK. Show all posts

How to Visualize a SDK Liferay portlet in panel control with other roles than Administrator

Version Liferay-5.2.3
For a SDK portlet to be visualizer in panelControl we just need add two lines in lifeay-portlet.xml :

<control-panel-entry-category>content</control-panel-entry-category>
<control-panel-entry-weight>12.0</control-panel-entry-weight>

The first line indicates that this portlet will be available in the Control Panel under that category, and the second indicate the position of the portlet in that category.
But this portlet only will be visualized by administrators users, to define other roles wee need to create a class that extends BaseControlPanelEntry and there control the roles, wee must indicate this class in liferay-portlet.xml:

<control-panel-entry-class>
com.portlet.struts.TagsControlPanelEntry
</control-panel-entry-class>  

Then create the roles in liferay-portlet.xml:

<role-mapper>
 <role-name>ROL_EDITOR</role-name>
 <role-link>ROL_EDITOR</role-link>
</role-mapper>

The class that control the roles:


package com.portlet.struts;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.liferay.portal.model.Organization;
import com.liferay.portal.model.Portlet;
import com.liferay.portal.model.Role;
import com.liferay.portal.security.permission.PermissionChecker;
import com.liferay.portal.service.OrganizationLocalServiceUtil;
import com.liferay.portal.service.RoleLocalServiceUtil;
import com.liferay.portlet.BaseControlPanelEntry;

public class TagsControlPanelEntry extends BaseControlPanelEntry {
 private static Log log = LogFactory.getLog(TagsControlPanelEntry.class);
 
 public boolean isVisible(PermissionChecker permissionChecker,
   Portlet portlet) throws Exception {

  long groupId ;
  
  try {
   List org = OrganizationLocalServiceUtil.getUserOrganizations(permissionChecker.getUserId());
   groupId = org.get(0).getGroup().getGroupId();
  } catch (Exception e1) {
   log.error("Caught:" + e1);
  }
  
  String roles[] = new String[2];
  try {
   roles = portlet.getRolesArray();
  } catch (Exception e) {
   log.error("Caught:" + e);
  }

  List userRoles = null;
  try {
   userRoles = RoleLocalServiceUtil.getUserGroupRoles(
     permissionChecker.getUserId(), groupId);
  } catch (Exception e) {
   log.error("Caught:" + e);
  }

  try {
   for (int i = 0; i < roles.length; i++) {

    for (Role userRole : userRoles) {
     if (userRole.getName().equals(roles[i])) {
      return true;
     }
    }
   }
  } catch (Exception e) {
   log.error("Caught:" + e);
  }

  return false;
 }

}

This class just gets the roles defined in portlet and verify if the user have some role defined in the portlet, this class search for organization roles in the user.
And now all the users with role "ROL_EDITOR" have the permission to see the portlet.

How to create scheduler in portlet with Quartz

Version Liferay-5.2.3
Using Liferay SDK, create a scheduler job in a portlet with Quartz

First create a class that contains actual task that needs to be executed:

package com.portlet.struts;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class SabiasQueJob implements org.quartz.Job{
   private static Log log = LogFactory.getLog(SabiasQueJob.class);
   
   public SabiasQueJob() {}

   public void execute(org.quartz.JobExecutionContext arg0) throws org.quartz.JobExecutionException {
    try {
   System.out.println("THIS IS THE ACTUAL TASK!");
   
  } catch (Exception e) {
   log.error("Portlet, Caught:" + e);
  }
    }

}


Second write a class that implements Scheduler interface.

package com.portlet.struts;

import com.liferay.portal.kernel.job.Scheduler;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;

public class SabiasQueSchedulerInterface implements Scheduler {
 private static Log _log = LogFactory
   .getLog(SabiasQueSchedulerInterface.class);

 public void NewSchedule() throws Exception {
  SchedulerFactory sf = new StdSchedulerFactory();
  org.quartz.Scheduler sched = sf.getScheduler();

  JobDetail jd2 = new JobDetail("myjob",
    org.quartz.Scheduler.DEFAULT_GROUP, SabiasQueJob.class);
  CronTrigger trigger = new CronTrigger("mytrigger",
    org.quartz.Scheduler.DEFAULT_GROUP, "myjob",
    org.quartz.Scheduler.DEFAULT_GROUP, "0 0 0 * * ?");

  sched.scheduleJob(jd2, trigger);

  sched.start();

 }

 public void schedule() {
  if (_log.isInfoEnabled()) {
   _log.info("Schedule");
  }

  try {
   NewSchedule();
  } catch (Exception ex) {
   _log.error(ex);
  }
 }

 public void unschedule() {
  if (_log.isInfoEnabled()) {
   _log.info("Unschedule");
  }
 }

}

This job is executed every day at 00h00m.For more detail and examples of Quartz see: Quartz

Last just register your scheduler in liferay-portlet.xml

<liferay-portlet-app>
    <portlet>
        <portlet-name>eventosSearch-portlet</portlet-name>
        <scheduler-class>com.portlet.struts.SabiasQueSchedulerInterface</scheduler-class>
    </portlet>
    <role-mapper>
        <role-name>administrator</role-name>
        <role-link>Administrator</role-link>
    </role-mapper>
</liferay-portlet-app>

How to create scheduler in portlet

Version Liferay-5.2.3
Using Liferay SDK, create a scheduler job in a portlet.

First create a class that contains actual task that needs to be executed:

package com.portlet;

import com.liferay.portal.kernel.job.IntervalJob;
import com.liferay.portal.kernel.job.JobExecutionContext;
import com.liferay.portal.kernel.job.JobExecutionException;
import com.liferay.portal.kernel.util.Time;


public class SyncDataJob implements IntervalJob{
 public SyncDataJob() {
  _interval = Time.DAY;
 }
 public long getInterval() {
  return _interval;
 }
 public void execute(JobExecutionContext context)
  throws JobExecutionException {
  try {
   System.out.println("THIS IS THE ACTUAL TASK!");
  }
  catch (Exception e) {
  }
 }
 private long _interval;
}

Second write a class that implements Scheduler interface.

package com.portlet.struts;

import com.portlet.SyncDataJob;
import com.liferay.portal.kernel.job.JobSchedulerUtil;
import com.liferay.portal.kernel.job.Scheduler;

public class EventHistoricoAction implements Scheduler {

 public void schedule() {

  JobSchedulerUtil.schedule(_testIntervalJob);
 }

 public void unschedule() {

  JobSchedulerUtil.unschedule(_testIntervalJob);
 }


 private SyncDataJob _testIntervalJob = new SyncDataJob();

}

Last just register your scheduler in liferay-portlet.xml

<liferay-portlet-app>
    <portlet>
        <portlet-name>eventosSearch-portlet</portlet-name>
        <scheduler-class>com.portlet.struts.EventHistoricoAction</scheduler-class>
    </portlet>
    <role-mapper>
        <role-name>administrator</role-name>
        <role-link>Administrator</role-link>
    </role-mapper>
</liferay-portlet-app>