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