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