<commit_before>package edu.duke.cabig.c3pr.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.quartz.CronTrigger;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleTrigger;
import org.quartz.Trigger;
import org.quartz.TriggerUtils;

import edu.duke.cabig.c3pr.constants.NotificationFrequencyEnum;
import edu.duke.cabig.c3pr.dao.ScheduledNotificationDao;
import edu.duke.cabig.c3pr.domain.PlannedNotification;
import edu.duke.cabig.c3pr.domain.RecipientScheduledNotification;
import edu.duke.cabig.c3pr.domain.ScheduledNotification;
import edu.duke.cabig.c3pr.domain.scheduler.runtime.job.ScheduledNotificationJob;
import edu.duke.cabig.c3pr.service.SchedulerService;
import edu.duke.cabig.c3pr.utils.DateUtil;
import edu.duke.cabig.c3pr.utils.StudyTargetAccrualNotificationEmail;

/**
 * @author <NAME>
 */
public class SchedulerServiceImpl implements SchedulerService {
	
	public static final String WEEKLY = "0 35 10 ? * FRI";
	public static final String MONTHLY ="0 0 12 L * ?";
	public static final String ANNUAL ="0 0 12 L DEC ?";
	
	public static final Long REPEAT_INTERVAL_IN_MILLI_SECONDS = 10*60*1000L;
	public static final Integer REPEAT_COUNT = 3;
	
	private Logger log = Logger.getLogger(StudyTargetAccrualNotificationEmail.class);
	
	private Scheduler scheduler;
	private ScheduledNotificationJob scheduledNotificationJob;
	private ScheduledNotificationDao scheduledNotificationDao;
	
	/* Called from the rules xml for the immediate frequency event based notifications.
	 */
	public void scheduleStudyNotification(PlannedNotification plannedNotification, Integer scheduledNotificationId){
		
        assert scheduledNotificationId != null: "scheduledNotificationId cannot be null";

       	log.debug("Entering ScheduleNotification of the SchedulerServiceImpl: " + scheduledNotificationId);

        try {
            	// for each rcptSchldNotification create job detail, and associate with the scheduler
        		ScheduledNotification scheduledNotification = scheduledNotificationDao.getById(scheduledNotificationId);
        		List<RecipientScheduledNotification> rsnList = scheduledNotification.getRecipientScheduledNotification();
        		
        		for(RecipientScheduledNotification rsn: rsnList){
                    // create a trigger
                    Trigger trigger = makeTrigger(plannedNotification, rsn.getId());
                    if(trigger == null){
                    	log.error("Trigger cannot be null");
                    	return;
                    }
                    
                    // create job detail and set the map values
                    String jobName = "J:recipientScheduledNotificationId:" + rsn.getId().toString();
                    String jobGroupName = "JG:recipientScheduledNotificationId:" + rsn.getId().toString();
                    JobDetail jobDetail = new JobDetail(jobName, jobGroupName, scheduledNotificationJob.getClass());
                                    
                    JobDataMap jobDataMap = jobDetail.getJobDataMap();
                    jobDataMap.put("plannedNotificationId", plannedNotification.getId());
                    jobDataMap.put("recipientScheduledNotificationId", rsn.getId());

                    // schedule the jobs
                    log.info("Scheduling the job (jobFullName : " + jobDetail.getFullName() + ")");
                    scheduler.scheduleJob(jobDetail, trigger);
        		}
        	
        } catch (SchedulerException e) {
            log.error("Exception while scheduling ", e);
        }
	}
	
	
	/* Cron-Expressions are used to configure instances of CronTrigger. Cron-Expressions are strings that are actually made up of seven sub-expressions, 
	 * that describe individual details of the schedule. These sub-expression are separated with white-space, and represent:
	   1. Seconds
	   2. Minutes
	   3. Hours
	   4. Day-of-Month
	   5. Month
	   6. Day-of-Week
	   7. Year (optional field)
	   An example of a complete cron-expression is the string "0 0 12 ? * WED" - which means "every Wednesday at 12:00 pm".
	   Refer: http://wiki.opensymphony.com/display/QRTZ1/TutorialLesson6
	*/
	private Trigger makeTrigger(PlannedNotification plannedNotification, Integer recipientScheduledNotificationId) {
        Trigger t = null;
        
		NotificationFrequencyEnum frequency = plannedNotification.getFrequency();
		try{
			if(frequency.equals(NotificationFrequencyEnum.WEEKLY)){
	        	//every Friday at 12:00pm
	        	t = new CronTrigger("TW:scheduledNotificationId:" + recipientScheduledNotificationId, "TGW:PlannedNotificationId:" + recipientScheduledNotificationId, WEEKLY);
			}
			if(frequency.equals(NotificationFrequencyEnum.MONTHLY)){
		        	//every last day of month at 12:00pm
		        	t = new CronTrigger("TM:scheduledNotificationId:" + recipientScheduledNotificationId, "TGM:PlannedNotificationId:" + recipientScheduledNotificationId , MONTHLY);
			}
			if(frequency.equals(NotificationFrequencyEnum.ANNUAL)){
		        	//every last day December at 12:00pm
		        	t = new CronTrigger("TA:scheduledNotificationId:" + recipientScheduledNotificationId , "TGA:PlannedNotificationId:" + recipientScheduledNotificationId , ANNUAL);
			}
			if(frequency.equals(NotificationFrequencyEnum.END_OF_THE_DAY)){
				Date endOfTheDay = DateUtil.getNextDayDate(new Date());
				t = new SimpleTrigger("TG:scheduledNotificationId:" + recipientScheduledNotificationId, "TG:scheduledNotificationId:" + recipientScheduledNotificationId, endOfTheDay);
			}
			//This is the only one that will be used from here...the cron triggers will be created in createNotificationController
			//at the time of creation of Planned Notifications.
			if(frequency.equals(NotificationFrequencyEnum.IMMEDIATE) || t == null){
		        t = TriggerUtils.makeImmediateTrigger("T:recipientScheduledNotificationId:" + recipientScheduledNotificationId,  REPEAT_COUNT, REPEAT_INTERVAL_IN_MILLI_SECONDS);<commit_msg>cpr-1861 <commit_after>package edu.duke.cabig.c3pr.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.quartz.CronTrigger;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleTrigger;
import org.quartz.Trigger;
import org.quartz.TriggerUtils;

import edu.duke.cabig.c3pr.constants.NotificationFrequencyEnum;
import edu.duke.cabig.c3pr.dao.ScheduledNotificationDao;
import edu.duke.cabig.c3pr.domain.PlannedNotification;
import edu.duke.cabig.c3pr.domain.RecipientScheduledNotification;
import edu.duke.cabig.c3pr.domain.ScheduledNotification;
import edu.duke.cabig.c3pr.domain.scheduler.runtime.job.ScheduledNotificationJob;
import edu.duke.cabig.c3pr.service.SchedulerService;
import edu.duke.cabig.c3pr.utils.DateUtil;
import edu.duke.cabig.c3pr.utils.StudyTargetAccrualNotificationEmail;

/**
 * @author Gangoli, Vinay
 */
public class SchedulerServiceImpl implements SchedulerService {
	
	public static final String WEEKLY = "0 35 10 ? * FRI";
	public static final String MONTHLY ="0 0 12 L * ?";
	public static final String ANNUAL ="0 0 12 L DEC ?";
	
	public static final Long REPEAT_INTERVAL_IN_MILLI_SECONDS = 10*60*1000L;
	public static final Integer REPEAT_COUNT = 3;
	
	private Logger log = Logger.getLogger(StudyTargetAccrualNotificationEmail.class);
	
	private Scheduler scheduler;
	private ScheduledNotificationJob scheduledNotificationJob;
	private ScheduledNotificationDao scheduledNotificationDao;
	
	/* Called from the rules xml for the immediate frequency event based notifications.
	 */
	public void scheduleStudyNotification(PlannedNotification plannedNotification, Integer scheduledNotificationId){
		
        assert scheduledNotificationId != null: "scheduledNotificationId cannot be null";

       	log.debug("Entering ScheduleNotification of the SchedulerServiceImpl: " + scheduledNotificationId);

        try {
            	// for each rcptSchldNotification create job detail, and associate with the scheduler
        		ScheduledNotification scheduledNotification = scheduledNotificationDao.getById(scheduledNotificationId);
        		List<RecipientScheduledNotification> rsnList = scheduledNotification.getRecipientScheduledNotification();
        		
        		for(RecipientScheduledNotification rsn: rsnList){
                    // create a trigger
                    Trigger trigger = makeTrigger(plannedNotification, rsn.getId());
                    if(trigger == null){
                    	log.error("Trigger cannot be null");
                    	return;
                    }
                    
                    // create job detail and set the map values
                    String jobName = "J:recipientScheduledNotificationId:" + rsn.getId().toString();
                    String jobGroupName = "JG:recipientScheduledNotificationId:" + rsn.getId().toString();
                    JobDetail jobDetail = new JobDetail(jobName, jobGroupName, scheduledNotificationJob.getClass());
                                    
                    JobDataMap jobDataMap = jobDetail.getJobDataMap();
                    jobDataMap.put("plannedNotificationId", plannedNotification.getId());
                    jobDataMap.put("recipientScheduledNotificationId", rsn.getId());

                    // schedule the jobs
                    log.info("Scheduling the job (jobFullName : " + jobDetail.getFullName() + ")");
                    scheduler.scheduleJob(jobDetail, trigger);
        		}
        	
        } catch (SchedulerException e) {
            log.error("Exception while scheduling ", e);
        }
	}
	
	
	/* Cron-Expressions are used to configure instances of CronTrigger. Cron-Expressions are strings that are actually made up of seven sub-expressions, 
	 * that describe individual details of the schedule. These sub-expression are separated with white-space, and represent:
	   1. Seconds
	   2. Minutes
	   3. Hours
	   4. Day-of-Month
	   5. Month
	   6. Day-of-Week
	   7. Year (optional field)
	   An example of a complete cron-expression is the string "0 0 12 ? * WED" - which means "every Wednesday at 12:00 pm".
	   Refer: http://wiki.opensymphony.com/display/QRTZ1/TutorialLesson6
	*/
	private Trigger makeTrigger(PlannedNotification plannedNotification, Integer recipientScheduledNotificationId) {
        Trigger t = null;
        
		NotificationFrequencyEnum frequency = plannedNotification.getFrequency();
		try{
			if(frequency.equals(NotificationFrequencyEnum.WEEKLY)){
	        	//every Friday at 12:00pm
	        	t = new CronTrigger("TW:scheduledNotificationId:" + recipientScheduledNotificationId, "TGW:PlannedNotificationId:" + recipientScheduledNotificationId, WEEKLY);
			}
			if(frequency.equals(NotificationFrequencyEnum.MONTHLY)){
		        	//every last day of month at 12:00pm
		        	t = new CronTrigger("TM:scheduledNotificationId:" + recipientScheduledNotificationId, "TGM:PlannedNotificationId:" + recipientScheduledNotificationId , MONTHLY);
			}
			if(frequency.equals(NotificationFrequencyEnum.ANNUAL)){
		        	//every last day December at 12:00pm
		        	t = new CronTrigger("TA:scheduledNotificationId:" + recipientScheduledNotificationId , "TGA:PlannedNotificationId:" + recipientScheduledNotificationId , ANNUAL);
			}
			if(frequency.equals(NotificationFrequencyEnum.END_OF_THE_DAY)){
				
				Date endOfDayTime = DateUtil.getMidNightTime();
				t = new SimpleTrigger("TG:scheduledNotificationId:" + recipientScheduledNotificationId, "TG:scheduledNotificationId:" + recipientScheduledNotificationId, endOfDayTime);
			}
			//This is the only one that will be used from here...the cron triggers will be created in createNotificationController
			//at the time of creation of Planned Notifications.
			if(frequency.equals(NotificationFrequencyEnum.IMMEDIATE) || t == null){
		        t = TriggerUtils.makeImmediateTrigger("T:recipientScheduledNotificationId:" + recipientScheduledNotificationId,  REPEAT_COUNT, REPEAT_INTERVAL_IN_MILLI_SECONDS);
{'fraction_non_alphanumeric': 0.0834291345048448, 'fraction_numerical': 0.010510757102972574, 'mean_word_length': 3.480868285504047, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2774588', 'n_tokens_mistral': 3602, 'n_tokens_neox': 3256, 'n_words': 776}