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.commons;
20  
21  import java.io.Serializable;
22  import java.util.Collections;
23  import java.util.Comparator;
24  import java.util.Iterator;
25  import java.util.List;
26  import java.util.stream.Collectors;
27  import org.apache.commons.lang3.StringUtils;
28  import org.apache.syncope.client.console.SyncopeConsoleSession;
29  import org.apache.syncope.client.console.pages.BasePage;
30  import org.apache.syncope.client.console.rest.ConnectorRestClient;
31  import org.apache.syncope.common.lib.to.ConnInstanceTO;
32  import org.apache.wicket.PageReference;
33  import org.apache.wicket.ajax.AjaxRequestTarget;
34  import org.apache.wicket.extensions.markup.html.repeater.data.sort.SortOrder;
35  import org.apache.wicket.extensions.markup.html.repeater.util.SortParam;
36  import org.apache.wicket.model.CompoundPropertyModel;
37  import org.apache.wicket.model.IModel;
38  import org.apache.wicket.request.cycle.RequestCycle;
39  import org.slf4j.Logger;
40  import org.slf4j.LoggerFactory;
41  
42  public class ConnectorDataProvider extends DirectoryDataProvider<Serializable> {
43  
44      private static final long serialVersionUID = 3122389673525690470L;
45  
46      protected static final Logger LOG = LoggerFactory.getLogger(ConnectorDataProvider.class);
47  
48      protected final ConnectorRestClient connectorRestClient;
49  
50      protected final PageReference pageRef;
51  
52      protected int currentPage;
53  
54      protected final String keyword;
55  
56      public ConnectorDataProvider(
57              final ConnectorRestClient connectorRestClient,
58              final int paginatorRows,
59              final PageReference pageRef,
60              final String keyword) {
61  
62          super(paginatorRows);
63  
64          setSort("displayNameSortParam", SortOrder.ASCENDING);
65          this.connectorRestClient = connectorRestClient;
66          this.pageRef = pageRef;
67          this.keyword = keyword;
68      }
69  
70      @Override
71      public Iterator<ConnInstanceTO> iterator(final long first, final long count) {
72          List<ConnInstanceTO> result = Collections.emptyList();
73  
74          try {
75              currentPage = ((int) first / paginatorRows);
76              if (currentPage < 0) {
77                  currentPage = 0;
78              }
79              if (StringUtils.isBlank(keyword)) {
80                  result = connectorRestClient.getAllConnectors();
81              } else {
82                  result = connectorRestClient.getAllConnectors().stream().
83                          filter(conn -> conn.getDisplayName().toLowerCase().contains(keyword)).
84                          collect(Collectors.toList());
85              }
86          } catch (Exception e) {
87              LOG.error("While searching", e);
88              SyncopeConsoleSession.get().onException(e);
89  
90              RequestCycle.get().find(AjaxRequestTarget.class).
91                      ifPresent(t -> ((BasePage) pageRef.getPage()).getNotificationPanel().refresh(t));
92          }
93  
94          SortParam<String> sortParam = getSort();
95          if (sortParam != null) {
96              result.sort(getComparator(sortParam));
97          }
98  
99          return result.subList((int) first, (int) first + (int) count).iterator();
100     }
101 
102     private Comparator<ConnInstanceTO> getComparator(final SortParam<String> sortParam) {
103         Comparator<ConnInstanceTO> comparator;
104 
105         switch (sortParam.getProperty()) {
106             case "displayNameSortParam":
107                 comparator = Comparator.nullsFirst(Comparator.comparing(
108                         item -> item.getDisplayName().toLowerCase()));
109                 break;
110             case "connectorNameSortParam":
111                 comparator = Comparator.nullsFirst(Comparator.comparing(
112                         item -> item.getConnectorName().toLowerCase()));
113                 break;
114             default:
115                 throw new IllegalStateException("The sort param " + sortParam.getProperty() + " is not correct");
116         }
117 
118         if (!sortParam.isAscending()) {
119             comparator = comparator.reversed();
120         }
121 
122         return comparator;
123     }
124 
125     @Override
126     public long size() {
127         long result = 0;
128 
129         try {
130             if (StringUtils.isBlank(keyword)) {
131                 result = connectorRestClient.getAllConnectors().size();
132             } else {
133                 result = connectorRestClient.getAllConnectors().stream().filter(conn
134                         -> conn.getDisplayName().toLowerCase().contains(keyword)).count();
135             }
136         } catch (Exception e) {
137             LOG.error("While requesting for size()", e);
138             SyncopeConsoleSession.get().onException(e);
139 
140             RequestCycle.get().find(AjaxRequestTarget.class).
141                     ifPresent(target -> ((BasePage) pageRef.getPage()).getNotificationPanel().refresh(target));
142         }
143 
144         return result;
145     }
146 
147     @Override
148     public IModel<Serializable> model(final Serializable object) {
149         return new CompoundPropertyModel<>(object);
150     }
151 
152     public int getCurrentPage() {
153         return currentPage;
154     }
155 }