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.util.ArrayList;
22  import java.util.Iterator;
23  import java.util.List;
24  import java.util.Optional;
25  import org.apache.syncope.client.console.SyncopeConsoleSession;
26  import org.apache.syncope.client.console.pages.BasePage;
27  import org.apache.syncope.client.console.rest.AbstractAnyRestClient;
28  import org.apache.syncope.client.ui.commons.Constants;
29  import org.apache.syncope.common.lib.to.AnyTO;
30  import org.apache.wicket.PageReference;
31  import org.apache.wicket.ajax.AjaxRequestTarget;
32  import org.apache.wicket.extensions.markup.html.repeater.data.sort.SortOrder;
33  import org.apache.wicket.model.CompoundPropertyModel;
34  import org.apache.wicket.model.IModel;
35  import org.apache.wicket.request.cycle.RequestCycle;
36  import org.slf4j.Logger;
37  import org.slf4j.LoggerFactory;
38  
39  public class AnyDataProvider<A extends AnyTO> extends DirectoryDataProvider<A> {
40  
41      private static final long serialVersionUID = 6267494272884913376L;
42  
43      protected static final Logger LOG = LoggerFactory.getLogger(AnyDataProvider.class);
44  
45      protected final SortableAnyProviderComparator<A> comparator;
46  
47      protected final AbstractAnyRestClient<A> restClient;
48  
49      protected String fiql;
50  
51      protected final boolean filtered;
52  
53      protected final String realm;
54  
55      protected final String type;
56  
57      protected final PageReference pageRef;
58  
59      protected int currentPage;
60  
61      public AnyDataProvider(
62              final AbstractAnyRestClient<A> restClient,
63              final int paginatorRows,
64              final boolean filtered,
65              final String realm,
66              final String type,
67              final PageReference pageRef) {
68  
69          super(paginatorRows);
70  
71          this.restClient = restClient;
72  
73          this.filtered = filtered;
74  
75          // default sorting
76          switch (type) {
77              case "USER":
78                  setSort(Constants.USERNAME_FIELD_NAME, SortOrder.ASCENDING);
79                  break;
80  
81              case "GROUP":
82                  setSort(Constants.NAME_FIELD_NAME, SortOrder.ASCENDING);
83                  break;
84  
85              default:
86          }
87  
88          this.comparator = new SortableAnyProviderComparator<>(this);
89  
90          this.realm = realm;
91          this.type = type;
92          this.pageRef = pageRef;
93      }
94  
95      @Override
96      public Iterator<A> iterator(final long first, final long count) {
97          List<A> result = new ArrayList<>();
98  
99          try {
100             currentPage = ((int) first / paginatorRows);
101             if (currentPage < 0) {
102                 currentPage = 0;
103             }
104 
105             if (filtered) {
106                 result = Optional.ofNullable(fiql).
107                         map(s -> restClient.search(realm, s, currentPage + 1, paginatorRows, getSort(), type)).
108                         orElseGet(() -> List.of());
109             } else {
110                 result = restClient.search(realm, null, currentPage + 1, paginatorRows, getSort(), type);
111             }
112         } catch (Exception e) {
113             LOG.error("While searching with FIQL {}", fiql, e);
114             SyncopeConsoleSession.get().onException(e);
115 
116             RequestCycle.get().find(AjaxRequestTarget.class).
117                     ifPresent(target -> ((BasePage) pageRef.getPage()).getNotificationPanel().refresh(target));
118         }
119 
120         return result.iterator();
121     }
122 
123     @Override
124     public long size() {
125         long result = 0;
126 
127         try {
128             if (filtered) {
129                 result = Optional.ofNullable(fiql).map(s -> restClient.count(realm, s, type)).orElse(0);
130             } else {
131                 result = restClient.count(realm, null, type);
132             }
133         } catch (Exception e) {
134             LOG.error("While requesting for size() with FIQL {}", fiql, e);
135             SyncopeConsoleSession.get().onException(e);
136 
137             RequestCycle.get().find(AjaxRequestTarget.class).
138                     ifPresent(target -> ((BasePage) pageRef.getPage()).getNotificationPanel().refresh(target));
139         }
140 
141         return result;
142     }
143 
144     public AnyDataProvider<A> setFIQL(final String fiql) {
145         this.fiql = fiql;
146         return this;
147     }
148 
149     @Override
150     public IModel<A> model(final A object) {
151         return new CompoundPropertyModel<>(object);
152     }
153 
154     public int getCurrentPage() {
155         return currentPage;
156     }
157 }