NotificationJob.java

  1. /*
  2.  * Copyright (C) 2003-2013 eXo Platform SAS.
  3.  *
  4.  * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Affero General Public License
  6. * as published by the Free Software Foundation; either version 3
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, see<http://www.gnu.org/licenses/>.
  16.  */
  17. package org.exoplatform.commons.notification.job;

  18. import java.util.concurrent.Callable;

  19. import org.quartz.Job;
  20. import org.quartz.JobExecutionContext;
  21. import org.quartz.JobExecutionException;

  22. import org.exoplatform.commons.api.notification.model.ArgumentLiteral;
  23. import org.exoplatform.commons.api.notification.service.NotificationCompletionService;
  24. import org.exoplatform.commons.notification.NotificationUtils;
  25. import org.exoplatform.commons.notification.impl.NotificationSessionManager;
  26. import org.exoplatform.commons.utils.CommonsUtils;
  27. import org.exoplatform.container.ExoContainer;
  28. import org.exoplatform.container.ExoContainerContext;
  29. import org.exoplatform.container.PortalContainer;
  30. import org.exoplatform.services.log.ExoLogger;
  31. import org.exoplatform.services.log.Log;

  32. public abstract class NotificationJob implements Job {
  33.   /** Defines the Logger instance*/
  34.   protected static final Log LOG = ExoLogger.getLogger(NotificationJob.class);
  35.  
  36.   /** Define the argument parameter for DAILY job with Boolean type */  
  37.   public final static ArgumentLiteral<Boolean> JOB_DAILY = new ArgumentLiteral<Boolean>(Boolean.class, "jobDaily");
  38.  
  39.   /** Define the argument parameter for DAY OF JOB job with String type */  
  40.   public final static ArgumentLiteral<String> DAY_OF_JOB = new ArgumentLiteral<String>(String.class, "dayOfJob");
  41.  
  42.   /** Define the argument parameter for WEEKLY job with Boolean type */
  43.   public final static ArgumentLiteral<Boolean> JOB_WEEKLY = new ArgumentLiteral<Boolean>(Boolean.class, "jobWeekly");

  44.   private ExoContainer container;

  45.   public NotificationJob() {
  46.     this(PortalContainer.getInstance());
  47.   }

  48.   public NotificationJob(ExoContainer exoContainer) {
  49.     this.container = exoContainer;
  50.   }

  51.   @Override
  52.   public void execute(final JobExecutionContext context) throws JobExecutionException {
  53.     if (isValid() == false) {
  54.       return;
  55.     }
  56.     Callable<Boolean> task = new Callable<Boolean>() {
  57.       @Override
  58.       public Boolean call() throws Exception {
  59.         ExoContainer currentContainer = ExoContainerContext.getCurrentContainer();
  60.         ExoContainerContext.setCurrentContainer(container);
  61.         boolean created = NotificationSessionManager.createSystemProvider();
  62.         try {
  63.           processSendNotification(context);
  64.         } catch (Exception e) {
  65.           LOG.error("Failed to running NotificationJob", e);
  66.           return false;
  67.         } finally {
  68.           NotificationSessionManager.closeSessionProvider(created);
  69.           ExoContainerContext.setCurrentContainer(currentContainer);
  70.         }
  71.         return true;
  72.       }
  73.     };
  74.     //
  75.     CommonsUtils.getService(NotificationCompletionService.class).addTask(task);
  76.   }

  77.   protected boolean isValid() {
  78.     try {
  79.       return CommonsUtils.getRepository().getState() != 0 && CommonsUtils.isFeatureActive(NotificationUtils.FEATURE_NAME);
  80.     } catch (Exception e) {
  81.       LOG.error("Failed to get current repository", e);
  82.       return false;
  83.     }
  84.   }
  85.   /**
  86.    * Process the job to build the message and send to target.
  87.    *
  88.    * @throws Exception
  89.    */
  90.   protected void processSendNotification() throws Exception {}
  91.   protected void processSendNotification(JobExecutionContext context) throws Exception {
  92.     processSendNotification();
  93.   }
  94. }