bf29f59a5a37d4d40ce7fb05fb13f1d2d35583b1
[portal/sdk.git] /
1 /*-
2  * ================================================================================
3  * eCOMP Portal SDK
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ================================================================================
19  */
20 package org.openecomp.portalsdk.workflow.scheduler;
21
22 import java.util.Calendar;
23 import java.util.Date;
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.UUID;
27
28 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
29 import org.quartz.impl.triggers.CronTriggerImpl;
30 import org.springframework.context.annotation.DependsOn;
31 import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
32 import org.springframework.scheduling.quartz.JobDetailFactoryBean;
33 import org.springframework.stereotype.Component;
34
35 @Component
36 @DependsOn({"systemProperties"})
37 public class WorkFlowScheduleRegistry{
38         
39
40         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(WorkFlowScheduleRegistry.class);
41
42
43         public WorkFlowScheduleRegistry() {
44
45         }
46
47         private static final String groupName = "AppGroup";
48         private static final String jobName = "WorkflowScheduleJob";
49         private static final String triggerName = "WorkflowScheduleTrigger";
50
51         // @Autowired
52         // private SystemProperties systemProperties;
53
54         // @Bean
55         public JobDetailFactoryBean jobDetailFactoryBean(Map<String, ?> contextInfoMap) {
56
57                 JobDetailFactoryBean jobDetailFactory = new JobDetailFactoryBean();
58                 jobDetailFactory.setJobClass(WorkFlowScheduleJob.class);
59                 jobDetailFactory.setJobDataAsMap(contextInfoMap);
60                 jobDetailFactory.setGroup(groupName);
61                 jobDetailFactory.setName(jobName + "_" + UUID.randomUUID());
62                 jobDetailFactory.afterPropertiesSet();
63                 return jobDetailFactory;
64         }
65
66         // @Bean
67         public CronTriggerFactoryBean cronTriggerFactoryBean(JobDetailFactoryBean jobDetailFactory, Long id,
68                         String cronExpression, Date startDateTime, Date enddatetime) throws Exception {
69                 CronTriggerFactoryBean cronTriggerFactory = new CronTriggerFactoryBean();
70                 cronTriggerFactory.setJobDetail(jobDetailFactory.getObject());
71                 cronTriggerFactory.setStartDelay(3000);
72                 cronTriggerFactory.setName(triggerName + "_" + id);
73                 cronTriggerFactory.setGroup(groupName);
74                 logger.debug(EELFLoggerDelegate.debugLogger, (triggerName + " Scheduled: " + cronExpression));
75                 cronTriggerFactory.setCronExpression( cronExpression);  //"0 * * * * ? *"
76                 cronTriggerFactory.afterPropertiesSet();
77
78                 final CronTriggerImpl cronTrigger = (CronTriggerImpl) cronTriggerFactory.getObject();
79                 cronTrigger.setStartTime(startDateTime == null ? Calendar.getInstance().getTime() : startDateTime);
80                 cronTrigger.setEndTime(enddatetime);
81                 Date fireAgainTime = cronTrigger.getFireTimeAfter(cronTrigger.getStartTime());
82                 if (fireAgainTime == null)
83                         throw new Exception("Cron not added as it may not fire again " + " Expr: " + cronExpression + " End Time: "
84                                         + cronTrigger.getEndTime());
85                 return cronTriggerFactory;
86
87         }
88
89         public CronTriggerFactoryBean setUpTrigger(Long wfId, String serverUrl, String workflowKey, String arguments,
90                         String startdatetimecron, Date startDateTime, Date enddatetime) throws Exception {
91
92                 Map<String, String> contextInfo = new HashMap<String, String>();
93                 contextInfo.put("serverUrl", serverUrl);
94                 contextInfo.put("workflowKey", workflowKey);
95                 contextInfo.put("arguments", arguments);
96                 JobDetailFactoryBean jobDetailFactory = jobDetailFactoryBean(contextInfo);
97
98                 CronTriggerFactoryBean cronTriggerFactory = cronTriggerFactoryBean(jobDetailFactory, wfId, startdatetimecron, startDateTime, enddatetime);
99                 
100                 logger.debug(EELFLoggerDelegate.debugLogger, (" Job to be Scheduled: " + contextInfo.get("workflowKey")));
101                 
102                 //cronTriggerFactory.
103                 
104                 return cronTriggerFactory;
105         }
106
107 }