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.client.console.rest;
20  
21  import java.io.IOException;
22  import java.util.ArrayList;
23  import java.util.Date;
24  import java.util.LinkedHashMap;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.Optional;
28  import javax.ws.rs.core.Response;
29  import org.apache.commons.lang3.StringUtils;
30  import org.apache.syncope.client.lib.batch.BatchRequest;
31  import org.apache.syncope.client.ui.commons.DateOps;
32  import org.apache.syncope.common.lib.SyncopeClientException;
33  import org.apache.syncope.common.lib.to.ExecTO;
34  import org.apache.syncope.common.lib.to.JobTO;
35  import org.apache.syncope.common.lib.to.ReportTO;
36  import org.apache.syncope.common.lib.types.JobAction;
37  import org.apache.syncope.common.rest.api.batch.BatchRequestItem;
38  import org.apache.syncope.common.rest.api.batch.BatchResponseItem;
39  import org.apache.syncope.common.rest.api.beans.ExecQuery;
40  import org.apache.syncope.common.rest.api.beans.ExecSpecs;
41  import org.apache.syncope.common.rest.api.service.ReportService;
42  import org.apache.wicket.extensions.markup.html.repeater.util.SortParam;
43  
44  public class ReportRestClient extends BaseRestClient implements ExecutionRestClient {
45  
46      private static final long serialVersionUID = 1644689667998953604L;
47  
48      public ReportTO read(final String reportKey) {
49          return getService(ReportService.class).read(reportKey);
50      }
51  
52      public List<ReportTO> list() {
53          return getService(ReportService.class).list();
54      }
55  
56      public JobTO getJob(final String key) {
57          return getService(ReportService.class).getJob(key);
58      }
59  
60      public List<JobTO> listJobs() {
61          return getService(ReportService.class).listJobs();
62      }
63  
64      public void actionJob(final String refKey, final JobAction jobAction) {
65          getService(ReportService.class).actionJob(refKey, jobAction);
66      }
67  
68      public void create(final ReportTO reportTO) {
69          getService(ReportService.class).create(reportTO);
70      }
71  
72      public void update(final ReportTO reportTO) {
73          getService(ReportService.class).update(reportTO);
74      }
75  
76      /**
77       * Delete specified report.
78       *
79       * @param reportKey report to delete
80       */
81      public void delete(final String reportKey) {
82          getService(ReportService.class).delete(reportKey);
83      }
84  
85      @Override
86      public void startExecution(final String reportKey, final Date startAt) {
87          getService(ReportService.class).execute(new ExecSpecs.Builder().key(reportKey).
88                  startAt(DateOps.toOffsetDateTime(startAt)).build());
89      }
90  
91      @Override
92      public void deleteExecution(final String reportExecKey) {
93          getService(ReportService.class).deleteExecution(reportExecKey);
94      }
95  
96      @Override
97      public List<ExecTO> listRecentExecutions(final int max) {
98          return getService(ReportService.class).listRecentExecutions(max);
99      }
100 
101     public Optional<Response> exportExecutionResult(final String executionKey) {
102         try {
103             return Optional.of(getService(ReportService.class).exportExecutionResult(executionKey));
104         } catch (SyncopeClientException e) {
105             LOG.error("While exporting execution {}", executionKey, e);
106             return Optional.empty();
107         }
108     }
109 
110     @Override
111     public List<ExecTO> listExecutions(
112             final String taskKey, final int page, final int size, final SortParam<String> sort) {
113 
114         return getService(ReportService.class).listExecutions(new ExecQuery.Builder().
115                 key(taskKey).page(page).size(size).orderBy(toOrderBy(sort)).build()).getResult();
116     }
117 
118     @Override
119     public int countExecutions(final String taskKey) {
120         return getService(ReportService.class).
121                 listExecutions(new ExecQuery.Builder().key(taskKey).page(1).size(0).build()).getTotalCount();
122     }
123 
124     @Override
125     public Map<String, String> batch(final BatchRequest batchRequest) {
126         List<BatchRequestItem> batchRequestItems = new ArrayList<>(batchRequest.getItems());
127 
128         Map<String, String> result = new LinkedHashMap<>();
129         try {
130             List<BatchResponseItem> batchResponseItems = batchRequest.commit().getItems();
131             for (int i = 0; i < batchResponseItems.size(); i++) {
132                 String status = getStatus(batchResponseItems.get(i).getStatus());
133 
134                 if (batchRequestItems.get(i).getRequestURI().contains("/execute")) {
135                     result.put(StringUtils.substringAfterLast(
136                             StringUtils.substringBefore(batchRequestItems.get(i).getRequestURI(), "/execute"), "/"),
137                             status);
138                 } else {
139                     result.put(StringUtils.substringAfterLast(
140                             batchRequestItems.get(i).getRequestURI(), "/"), status);
141                 }
142             }
143         } catch (IOException e) {
144             LOG.error("While processing Batch response", e);
145         }
146 
147         return result;
148     }
149 }