2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
26 * https://creativecommons.org/licenses/by/4.0/
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.
34 * ============LICENSE_END============================================
36 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
38 package org.onap.portalsdk.workflow.services;
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;
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;
59 @Service("workflowScheduleService")
62 public class WorkflowScheduleServiceImpl implements WorkflowScheduleService {
64 private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(WorkflowScheduleServiceImpl.class);
67 private DataAccessService dataAccessService;
70 private WorkFlowScheduleRegistry workflowRegistry;
73 private ApplicationContext appContext;
76 public List<WorkflowSchedule> findAll() {
77 @SuppressWarnings("unchecked")
78 List<WorkflowSchedule> list = getDataAccessService().getList(WorkflowSchedule.class, null);
83 public void saveWorkflowSchedule(WorkflowSchedule ws) {
84 getDataAccessService().saveDomainObject(ws, null);
85 logger.info(EELFLoggerDelegate.debugLogger, "Workflow Scheduled " + ws.getId() + " " + ws.getEndDateTime());
86 triggerWorkflowScheduling((SchedulerFactoryBean) appContext.getBean(SchedulerFactoryBean.class), ws);
90 public void triggerWorkflowScheduling(SchedulerFactoryBean schedulerBean, WorkflowSchedule ws) {
92 final CronTriggerFactoryBean triggerBean = workflowRegistry.setUpTrigger(ws.getId(), ws.getServerUrl(),
93 ws.getWorkflowKey(), ws.getArguments(), ws.getCronDetails(), ws.getStartDateTime(),
95 schedulerBean.getScheduler().scheduleJob((JobDetail) triggerBean.getJobDataMap().get("jobDetail"),
96 triggerBean.getObject());
97 } catch (Exception e) {
98 logger.error(EELFLoggerDelegate.errorLogger, "Error scheduling work flow with Id" + ws.getId(), e);
103 public List<Trigger> triggerWorkflowScheduling() {
105 Date date = new Date();
106 List<Trigger> triggers = new ArrayList<>();
108 DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
110 if (getDataAccessService() != null) {
111 @SuppressWarnings("unchecked")
112 List<WorkflowSchedule> allWorkflows = getDataAccessService()
113 .executeQuery("From WorkflowSchedule where endDateTime > '" + dateFormat.format(date) + "'", null);
115 for (WorkflowSchedule ws : allWorkflows) {
116 logger.info(EELFLoggerDelegate.debugLogger,
117 "Workflow Scheduled " + ws.getId() + "/ End Time: " + ws.getEndDateTime());
119 final CronTriggerFactoryBean triggerBean = workflowRegistry.setUpTrigger(ws.getId(),
120 ws.getServerUrl(), ws.getWorkflowKey(), ws.getArguments(), ws.getCronDetails(),
121 ws.getStartDateTime(), ws.getEndDateTime());
122 triggers.add(triggerBean.getObject());
123 } catch (Exception e) {
124 logger.error(EELFLoggerDelegate.errorLogger, "Error scheduling work flow with Id" + ws.getId(), e);
132 public DataAccessService getDataAccessService() {
133 return dataAccessService;
136 public void setDataAccessService(DataAccessService dataAccessService) {
137 this.dataAccessService = dataAccessService;
141 public WorkflowSchedule getWorkflowScheduleByKey(Long key) {
142 return (WorkflowSchedule) (getDataAccessService().getDomainObject(WorkflowSchedule.class, key, null));