package com.schedule.demo.service;

import java.util.List;

import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.schedule.demo.jobs.SimpleJob;
import com.schedule.demo.vo.ScheduleJob;

@Component
public class ScheduleJobService {

	@Autowired
	private QuartzManager quartzManager;

	public List<ScheduleJob> getJobs() {

		return null;
	}

	public ScheduleJob getScheduleJob(Long id) {
		ScheduleJob ScheduleJobs = null;

		return ScheduleJobs;
	}

	public boolean addScheduleJob(ScheduleJob job) {
		if (job == null) {
			return false;
		}
		job.setJobClass(SimpleJob.class);
		long current = System.currentTimeMillis();
		// job.setJobId(current);
		job.setTriggerName("testTriggerName-" + current);
		job.setTriggerGroupName("testTriggerGroupName");
		job.setJobGroupName("testJobGroupName-" + current);
		boolean isSuccessful = false;
		// save job information to db

		// submit to ScheduleService
		try {
			quartzManager.addJob(job);
			isSuccessful = true;
		} catch (SchedulerException e) {
			isSuccessful = false;
		}
		return isSuccessful;
	}

	public boolean updateScheduleJob(ScheduleJob job) {
		if (job == null) {
			return false;
		}
		boolean successful = false;
		// get job details by id

		// populate job updated details

		// update latest in db

		// update to ScheduleService
		try {
			quartzManager.updateJobCron(job);
			successful = true;
		} catch (SchedulerException e) {
			successful = false;
		}
		return successful;
	}

	public boolean changeStatus(Long id, String status) {
		boolean successful = false;
		// get job details by id

		// populate job updated details

		// update latest in db

		// update to ScheduleService
		try {
			ScheduleJob job = new ScheduleJob();
			if ("NORMAL".equals(status)) {
				quartzManager.resumeJob(job);
			} else if ("PAUSE".equals(status)) {
				quartzManager.pauseJob(job);
			}
			successful = true;
		} catch (SchedulerException e) {
			successful = false;
		}
		return successful;
	}

	public boolean deleteStatus(Long id) {
		boolean successful = false;
		// get job details by id

		// delete job in db

		// update to ScheduleService
		try {
			ScheduleJob job = new ScheduleJob();
			quartzManager.deleteJob(job);
			successful = true;
		} catch (SchedulerException e) {
			successful = false;
		}
		return successful;
	}

}

@@ -200,24 +200,69 @@
 Component;%0A%0A
+import com.schedule.demo.dao.ScheduleJobDao;%0A
 import com.s
@@ -427,16 +427,68 @@
 Manager;
+%0A%09@Autowired%0A%09private ScheduleJobDao scheduleJobDao;
 %0A%0A%09publi
@@ -519,17 +519,16 @@
 obs() %7B%0A
-%0A
 %09%09return
@@ -528,20 +528,50 @@
 %09return 
-null
+scheduleJobDao.queryScheduleJobs()
 ;%0A%09%7D%0A%0A%09p
@@ -1008,36 +1008,30 @@
 me(%22
-testJobGroupName-%22 + current
+G-%22 + job.getJobName()
 );%0A%09
@@ -1089,24 +1089,89 @@
 ation to db%0A
+%09%09Long jobId = scheduleJobDao.insert(job);%0A%09%09job.setJobId(jobId);
 %0A%09%09// submit

add db operation