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;
61 @Service("workflowScheduleService")
64 public class WorkflowScheduleServiceImpl implements WorkflowScheduleService{
66 private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(WorkflowScheduleServiceImpl.class);
69 private DataAccessService dataAccessService;
72 private WorkFlowScheduleRegistry workflowRegistry;
75 private ApplicationContext appContext;
78 public List<WorkflowSchedule> findAll() {
80 /* List<WorkflowSchedule> allworkflows = getDataAccessService().getList(WorkflowSchedule.class, null);
81 for (WorkflowSchedule ws : allworkflows) {
83 System.out.println("Key:"+ws.getWorkflowKey()+" "+"CronDetails:"+ws.getStartdatetimecron());
85 @SuppressWarnings("unchecked")
86 List<WorkflowSchedule> list = getDataAccessService().getList(WorkflowSchedule.class, null);
91 public void saveWorkflowSchedule(WorkflowSchedule ws){
93 getDataAccessService().saveDomainObject(ws, null);
94 logger.info(EELFLoggerDelegate.debugLogger, ("Workflow Scheduled " + ws.getId() + " " + ws.getEndDateTime()));
95 triggerWorkflowScheduling((SchedulerFactoryBean)appContext.getBean(SchedulerFactoryBean.class),ws);
99 public void triggerWorkflowScheduling(SchedulerFactoryBean schedulerBean, WorkflowSchedule ws) {
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()));
110 public List<Trigger> triggerWorkflowScheduling() {
112 Date date = new Date();
113 List<Trigger> triggers = new ArrayList<Trigger>();
115 DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
117 if (getDataAccessService() != null) {
118 @SuppressWarnings("unchecked")
119 List<WorkflowSchedule> allWorkflows = getDataAccessService()
120 .executeQuery("From WorkflowSchedule where endDateTime > '" + dateFormat.format(date) + "'", null);
122 for (WorkflowSchedule ws : allWorkflows) {
123 logger.info(EELFLoggerDelegate.debugLogger, ("Workflow Scheduled " + ws.getId() + "/ End Time: " + ws.getEndDateTime()));
127 final CronTriggerFactoryBean triggerBean = workflowRegistry.setUpTrigger(ws.getId(), ws.getServerUrl(), ws.getWorkflowKey(),ws.getArguments(), ws.getCronDetails(), ws.getStartDateTime(),ws.getEndDateTime());
129 triggers.add(triggerBean.getObject());
131 //schedulerBean.getScheduler().scheduleJob(trigger);
134 } catch (Exception e) {
135 logger.debug(EELFLoggerDelegate.debugLogger, ("Error scheduling work flow with Id" + ws.getId() + e.getMessage()));
146 public DataAccessService getDataAccessService() {
147 return dataAccessService;
151 public void setDataAccessService(DataAccessService dataAccessService) {
152 this.dataAccessService = dataAccessService;
156 public WorkflowSchedule getWorkflowScheduleByKey(Long key) {
157 return (WorkflowSchedule)(getDataAccessService().getDomainObject(WorkflowSchedule.class, key, null));