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.status;
20  
21  import java.io.Serializable;
22  import java.util.Collection;
23  import java.util.List;
24  import java.util.Objects;
25  import java.util.Optional;
26  import java.util.stream.Collectors;
27  import org.apache.commons.lang3.tuple.Pair;
28  import org.apache.syncope.client.console.rest.ReconciliationRestClient;
29  import org.apache.syncope.client.ui.commons.ConnIdSpecialName;
30  import org.apache.syncope.common.lib.to.ReconStatus;
31  import org.apache.syncope.common.rest.api.beans.ReconQuery;
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  
35  public class ReconStatusUtils implements Serializable {
36  
37      private static final long serialVersionUID = -5411720003057109354L;
38  
39      private static final Logger LOG = LoggerFactory.getLogger(ReconStatusUtils.class);
40  
41      protected final ReconciliationRestClient reconciliationRestClient;
42  
43      public ReconStatusUtils(final ReconciliationRestClient reconciliationRestClient) {
44          this.reconciliationRestClient = reconciliationRestClient;
45      }
46  
47      public Optional<ReconStatus> getReconStatus(
48              final String anyTypeKey, final String connObjectKeyValue, final String resource) {
49  
50          ReconStatus result = null;
51          try {
52              result = reconciliationRestClient.status(new ReconQuery.Builder(anyTypeKey, resource).
53                      fiql(ConnIdSpecialName.UID + "==" + connObjectKeyValue).build());
54          } catch (Exception e) {
55              LOG.warn("Unexpected error for {} {} on {}", anyTypeKey, connObjectKeyValue, resource, e);
56          }
57          return Optional.ofNullable(result);
58      }
59  
60      public List<Pair<String, ReconStatus>> getReconStatuses(
61              final String anyTypeKey, final String anyKey, final Collection<String> resources) {
62  
63          return resources.stream().map(resource -> {
64              try {
65                  return Pair.of(resource, reconciliationRestClient.status(
66                          new ReconQuery.Builder(anyTypeKey, resource).anyKey(anyKey).build()));
67              } catch (Exception e) {
68                  LOG.warn("Unexpected error for {} {} on {}", anyTypeKey, anyKey, resource, e);
69                  return null;
70              }
71          }).filter(Objects::nonNull).collect(Collectors.toList());
72      }
73  }