73da941cecc8cfa0ad07d8f0a27b638b6bf5ff2b
[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.services;
39
40 import java.text.DateFormat;
41 import java.text.SimpleDateFormat;
42 import java.util.ArrayList;
43 import java.util.Date;
44 import java.util.List;
45
46 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
47 import org.onap.portalsdk.core.service.DataAccessService;
48 import org.onap.portalsdk.workflow.domain.WorkflowSchedule;
49 import org.onap.portalsdk.workflow.scheduler.WorkFlowScheduleRegistry;
50 import org.quartz.JobDetail;
51 import org.quartz.Trigger;
52 import org.springframework.beans.factory.annotation.Autowired;
53 import org.springframework.context.ApplicationContext;
54 import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
55 import org.springframework.scheduling.quartz.SchedulerFactoryBean;
56 import org.springframework.stereotype.Service;
57 import org.springframework.transaction.annotation.Transactional;
58
59
60
61 @Service("workflowScheduleService")
62 @Transactional
63
64 public class WorkflowScheduleServiceImpl implements WorkflowScheduleService{
65         
66         private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(WorkflowScheduleServiceImpl.class);
67         
68         @Autowired
69         private DataAccessService  dataAccessService;
70         
71         @Autowired
72         private WorkFlowScheduleRegistry workflowRegistry;
73         
74         @Autowired
75         private ApplicationContext appContext;
76         
77
78         public List<WorkflowSchedule> findAll() {
79                 
80         /*      List<WorkflowSchedule> allworkflows = getDataAccessService().getList(WorkflowSchedule.class, null);
81                 for (WorkflowSchedule ws : allworkflows) {
82                         
83                         System.out.println("Key:"+ws.getWorkflowKey()+" "+"CronDetails:"+ws.getStartdatetimecron());
84                 } */
85                 @SuppressWarnings("unchecked")
86                 List<WorkflowSchedule> list = getDataAccessService().getList(WorkflowSchedule.class, null);
87                 return list;
88         }
89         
90         
91         public void saveWorkflowSchedule(WorkflowSchedule ws){
92                 
93                 getDataAccessService().saveDomainObject(ws, null);
94                 logger.info(EELFLoggerDelegate.debugLogger, ("Workflow Scheduled " + ws.getId() + " " + ws.getEndDateTime()));
95                 triggerWorkflowScheduling((SchedulerFactoryBean)appContext.getBean(SchedulerFactoryBean.class),ws);
96                 
97         }
98         
99         public void triggerWorkflowScheduling(SchedulerFactoryBean schedulerBean, WorkflowSchedule ws) {
100                 
101                 try {
102                         final CronTriggerFactoryBean triggerBean = workflowRegistry.setUpTrigger(ws.getId(), ws.getServerUrl(), ws.getWorkflowKey(),ws.getArguments(),ws.getCronDetails(), ws.getStartDateTime(),ws.getEndDateTime());
103                         schedulerBean.getScheduler().scheduleJob((JobDetail)triggerBean.getJobDataMap().get("jobDetail"),triggerBean.getObject());
104                 } catch (Exception e) {
105                         logger.debug(EELFLoggerDelegate.debugLogger, ("Error scheduling work flow with Id" + ws.getId() + e.getMessage()));
106                 }
107                 
108         }
109         
110         public List<Trigger> triggerWorkflowScheduling() {
111                 
112                  Date date = new Date();
113                  List<Trigger> triggers = new ArrayList<Trigger>();
114                  
115                  DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
116
117                 if (getDataAccessService() != null) {
118                         @SuppressWarnings("unchecked")
119                         List<WorkflowSchedule> allWorkflows = getDataAccessService()
120                                         .executeQuery("From  WorkflowSchedule where endDateTime > '" + dateFormat.format(date) + "'", null);
121
122                         for (WorkflowSchedule ws : allWorkflows) {
123                                 logger.info(EELFLoggerDelegate.debugLogger, ("Workflow Scheduled " + ws.getId() + "/ End Time: " + ws.getEndDateTime()));
124                 
125                                 try {
126                                         
127                                         final CronTriggerFactoryBean triggerBean = workflowRegistry.setUpTrigger(ws.getId(), ws.getServerUrl(), ws.getWorkflowKey(),ws.getArguments(), ws.getCronDetails(), ws.getStartDateTime(),ws.getEndDateTime());
128                                         
129                                         triggers.add(triggerBean.getObject());
130                                         
131                                         //schedulerBean.getScheduler().scheduleJob(trigger);
132                                 
133                                 
134                                 } catch (Exception e) {
135                                         logger.debug(EELFLoggerDelegate.debugLogger, ("Error scheduling work flow with Id" + ws.getId() + e.getMessage()));
136                                 }
137                         
138                         
139                         }
140                 }
141                 
142                 return triggers;
143         }
144         
145         
146         public DataAccessService getDataAccessService() {
147                 return dataAccessService;
148         }
149
150
151         public void setDataAccessService(DataAccessService dataAccessService) {
152                 this.dataAccessService = dataAccessService;
153         }
154
155         @Override
156         public WorkflowSchedule getWorkflowScheduleByKey(Long key) {
157                 return  (WorkflowSchedule)(getDataAccessService().getDomainObject(WorkflowSchedule.class, key, null));
158         }
159 }
160
161