View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.core.provisioning.java.data;
20  
21  import java.util.stream.Collectors;
22  import org.apache.commons.lang3.StringUtils;
23  import org.apache.syncope.common.lib.SyncopeClientException;
24  import org.apache.syncope.common.lib.to.ExecTO;
25  import org.apache.syncope.common.lib.to.ReportTO;
26  import org.apache.syncope.common.lib.types.ClientExceptionType;
27  import org.apache.syncope.common.lib.types.IdRepoImplementationType;
28  import org.apache.syncope.common.lib.types.JobType;
29  import org.apache.syncope.core.persistence.api.dao.ImplementationDAO;
30  import org.apache.syncope.core.persistence.api.dao.ReportExecDAO;
31  import org.apache.syncope.core.persistence.api.entity.Implementation;
32  import org.apache.syncope.core.persistence.api.entity.Report;
33  import org.apache.syncope.core.persistence.api.entity.ReportExec;
34  import org.apache.syncope.core.provisioning.api.data.ReportDataBinder;
35  import org.apache.syncope.core.provisioning.api.job.JobNamer;
36  import org.quartz.Scheduler;
37  import org.quartz.SchedulerException;
38  import org.quartz.Trigger;
39  import org.quartz.TriggerKey;
40  import org.slf4j.Logger;
41  import org.slf4j.LoggerFactory;
42  import org.springframework.scheduling.quartz.SchedulerFactoryBean;
43  
44  public class ReportDataBinderImpl extends AbstractExecutableDatabinder implements ReportDataBinder {
45  
46      protected static final Logger LOG = LoggerFactory.getLogger(ReportDataBinder.class);
47  
48      protected final ReportExecDAO reportExecDAO;
49  
50      protected final ImplementationDAO implementationDAO;
51  
52      protected final SchedulerFactoryBean scheduler;
53  
54      public ReportDataBinderImpl(
55              final ReportExecDAO reportExecDAO,
56              final ImplementationDAO implementationDAO,
57              final SchedulerFactoryBean scheduler) {
58  
59          this.reportExecDAO = reportExecDAO;
60          this.implementationDAO = implementationDAO;
61          this.scheduler = scheduler;
62      }
63  
64      @Override
65      public void getReport(final Report report, final ReportTO reportTO) {
66          report.setName(reportTO.getName());
67          report.setMimeType(reportTO.getMimeType());
68          report.setFileExt(reportTO.getFileExt());
69          report.setCronExpression(reportTO.getCronExpression());
70          report.setActive(reportTO.isActive());
71  
72          Implementation jobDelegate = implementationDAO.find(reportTO.getJobDelegate());
73          if (jobDelegate == null || !IdRepoImplementationType.REPORT_DELEGATE.equals(jobDelegate.getType())) {
74              SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.InvalidImplementation);
75              sce.getElements().add("Null or invalid JobDelegate, expected " + IdRepoImplementationType.REPORT_DELEGATE);
76              throw sce;
77          }
78          report.setJobDelegate(jobDelegate);
79      }
80  
81      @Override
82      public ReportTO getReportTO(final Report report) {
83          ReportTO reportTO = new ReportTO();
84          reportTO.setKey(report.getKey());
85          reportTO.setName(report.getName());
86          reportTO.setMimeType(report.getMimeType());
87          reportTO.setFileExt(report.getFileExt());
88          reportTO.setCronExpression(report.getCronExpression());
89          reportTO.setJobDelegate(report.getJobDelegate().getKey());
90          reportTO.setActive(report.isActive());
91  
92          ReportExec latestExec = reportExecDAO.findLatestStarted(report);
93          if (latestExec == null) {
94              reportTO.setLatestExecStatus(StringUtils.EMPTY);
95          } else {
96              reportTO.setLatestExecStatus(latestExec.getStatus());
97              reportTO.setStart(latestExec.getStart());
98              reportTO.setEnd(latestExec.getEnd());
99  
100             reportTO.setLastExecutor(latestExec.getExecutor());
101             reportTO.setLastExec(reportTO.getStart());
102         }
103 
104         reportTO.getExecutions().addAll(report.getExecs().stream().
105                 map(this::getExecTO).collect(Collectors.toList()));
106 
107         String triggerName = JobNamer.getTriggerName(JobNamer.getJobKey(report).getName());
108         try {
109             Trigger trigger = scheduler.getScheduler().getTrigger(new TriggerKey(triggerName, Scheduler.DEFAULT_GROUP));
110             if (trigger != null) {
111                 reportTO.setLastExec(toOffsetDateTime(trigger.getPreviousFireTime()));
112                 reportTO.setNextExec(toOffsetDateTime(trigger.getNextFireTime()));
113             }
114         } catch (SchedulerException e) {
115             LOG.warn("While trying to get to " + triggerName, e);
116         }
117 
118         return reportTO;
119     }
120 
121     @Override
122     public String buildRefDesc(final Report report) {
123         return "Report "
124                 + report.getKey() + ' '
125                 + report.getName();
126     }
127 
128     @Override
129     public ExecTO getExecTO(final ReportExec execution) {
130         ExecTO execTO = new ExecTO();
131         execTO.setKey(execution.getKey());
132         execTO.setJobType(JobType.REPORT);
133         execTO.setRefKey(execution.getReport().getKey());
134         execTO.setRefDesc(buildRefDesc(execution.getReport()));
135         execTO.setStatus(execution.getStatus());
136         execTO.setMessage(execution.getMessage());
137         execTO.setStart(execution.getStart());
138         execTO.setEnd(execution.getEnd());
139 
140         execTO.setExecutor(execution.getExecutor());
141         return execTO;
142     }
143 }