Skip to content

Commit bc0e3be

Browse files
committed
wrap up configuration/user datatable
1 parent 0eee16a commit bc0e3be

File tree

5 files changed

+46
-129
lines changed

5 files changed

+46
-129
lines changed

kafka-webview-ui/src/main/java/org/sourcelab/kafka/webview/ui/controller/configuration/user/UserController.java

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -75,27 +75,7 @@ public class UserController extends BaseController {
7575
* GET Displays main user index.
7676
*/
7777
@RequestMapping(path = "", method = RequestMethod.GET)
78-
public String index(final Model model, final RedirectAttributes redirectAttributes) {
79-
// Setup breadcrumbs
80-
setupBreadCrumbs(model, null, null);
81-
82-
// Check for LDAP auth method and restrict access.
83-
if (redirectIfUsingLdapAuthentication(redirectAttributes)) {
84-
return "redirect:/";
85-
}
86-
87-
// Retrieve all users
88-
final Iterable<User> usersList = userRepository.findAllByIsActiveOrderByEmailAsc(true);
89-
model.addAttribute("users", usersList);
90-
91-
return "configuration/user/index";
92-
}
93-
94-
/**
95-
* GET Displays main user index.
96-
*/
97-
@RequestMapping(path = "/datatable", method = RequestMethod.GET)
98-
public String datatable(
78+
public String index(
9979
final Model model,
10080
final Pageable pageable,
10181
@RequestParam Map<String,String> allParams,
@@ -109,12 +89,11 @@ public String datatable(
10989
return "redirect:/";
11090
}
11191

112-
// TODO fix enum filter for role
11392
final Datatable.Builder<User> builder = Datatable.newBuilder(User.class)
11493
.withRepository(userRepository)
11594
.withPageable(pageable)
11695
.withRequestParams(allParams)
117-
.withUrl("/configuration/user/datatable")
96+
.withUrl("/configuration/user")
11897
.withLabel("Users")
11998
// Only show active users.
12099
.withConstraint("isActive", true, ConstraintOperator.EQUALS)
@@ -163,7 +142,8 @@ public String datatable(
163142
.withDeleteLink(User.class, (record) -> "/configuration/user/delete/" + record.getId())
164143
.build())
165144
.build())
166-
.withSearch("email");
145+
.withSearch("email", "displayName");
146+
// TODO fix filters with enums
167147
// .withFilter(DatatableFilter.newBuilder()
168148
// .withField("role")
169149
// .withLabel("Role")
@@ -174,12 +154,7 @@ public String datatable(
174154

175155
// Add datatable attribute
176156
model.addAttribute("datatable", builder.build());
177-
178-
// Retrieve all users
179-
final Iterable<User> usersList = userRepository.findAllByIsActiveOrderByEmailAsc(true);
180-
model.addAttribute("users", usersList);
181-
182-
return "configuration/user/datatable";
157+
return "configuration/user/index";
183158
}
184159

185160
/**

kafka-webview-ui/src/main/java/org/sourcelab/kafka/webview/ui/manager/ui/datatable/Datatable.java

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@
3131
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
3232

3333
import javax.persistence.criteria.Path;
34+
import javax.persistence.criteria.Predicate;
3435
import java.io.UnsupportedEncodingException;
3536
import java.net.URLEncoder;
3637
import java.nio.charset.StandardCharsets;
3738
import java.util.ArrayList;
39+
import java.util.Arrays;
3840
import java.util.Collections;
3941
import java.util.HashMap;
4042
import java.util.HashSet;
@@ -330,18 +332,19 @@ private Page<T> getPage() {
330332
searchValue = null;
331333
}
332334
}
333-
Specification<T> specification;
334-
if (searchValue == null) {
335-
specification = Specification.where(null);
336-
} else {
337-
specification = Specification.where((root, query, builder) ->
338-
builder.like(
339-
builder.lower(root.get(getSearch().getField())), "%" + getSearch().getCurrentSearchTerm().toLowerCase() + "%"
340-
)
341-
);
335+
Specification<T> specification = Specification.where(null);
336+
if (searchValue != null) {
337+
// OR all of the search fields
338+
for (final String field : getSearch().getFields()) {
339+
specification = specification.or((root, query, builder) ->
340+
builder.like(
341+
builder.lower(root.get(field)), "%" + getSearch().getCurrentSearchTerm().toLowerCase() + "%"
342+
)
343+
);
344+
}
342345
}
343346

344-
// Add filter criterias
347+
// Add filter criteria
345348
for (final DatatableFilter filter : getFilters()) {
346349
// Skip non-provided filters.
347350
if (getCurrentFilterValueFor(filter).isEmpty()) {
@@ -528,16 +531,16 @@ public Builder<T> withSearch(DatatableSearch datatableSearch) {
528531
return this;
529532
}
530533

531-
public Builder<T> withSearch(final String name) {
532-
return withSearch("Search...", name, null);
534+
public Builder<T> withSearch(final String ... fields) {
535+
return withSearch("Search...", Arrays.asList(fields));
533536
}
534537

535-
public Builder<T> withSearch(final String search, final String name) {
536-
return withSearch(search, name, null);
538+
public Builder<T> withSearch(final String label, final List<String> fields) {
539+
return withSearch(label, fields, null);
537540
}
538541

539-
public Builder<T> withSearch(final String search, final String name, final String currentSearchTerm) {
540-
return withSearch(new DatatableSearch(search, name, currentSearchTerm));
542+
public Builder<T> withSearch(final String search, final List<String> fields, final String currentSearchTerm) {
543+
return withSearch(new DatatableSearch(search, fields, currentSearchTerm));
541544
}
542545

543546
public Builder<T> withCreateLink(final String url) {
@@ -573,11 +576,11 @@ public Builder<T> withConstraint(final DatatableConstraint constraint) {
573576
*/
574577
public Datatable<T> build() {
575578
// Inject current search term from request parameters if available and not already set.
576-
if (datatableSearch != null && datatableSearch.getField() != null && datatableSearch.getCurrentSearchTerm() == null) {
579+
if (datatableSearch != null && datatableSearch.getCurrentSearchTerm() == null) {
577580
if (requestParams.containsKey("search")) {
578581
datatableSearch = new DatatableSearch(
579582
datatableSearch.getLabel(),
580-
datatableSearch.getField(),
583+
datatableSearch.getFields(),
581584
requestParams.get("search")
582585
);
583586
}

kafka-webview-ui/src/main/java/org/sourcelab/kafka/webview/ui/manager/ui/datatable/DatatableSearch.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,17 @@
2424

2525
package org.sourcelab.kafka.webview.ui.manager.ui.datatable;
2626

27+
import java.util.ArrayList;
28+
import java.util.Collections;
29+
import java.util.List;
30+
import java.util.Objects;
31+
2732
/**
2833
* Represents a searchable field on a datatable.
2934
*/
3035
public class DatatableSearch {
3136
private final String label;
32-
private final String field;
37+
private final List<String> fields;
3338
private final String currentSearchTerm;
3439

3540
/**
@@ -39,17 +44,23 @@ public class DatatableSearch {
3944
* @param currentSearchTerm The current search term if defined in a request.
4045
*/
4146
public DatatableSearch(final String label, final String field, final String currentSearchTerm) {
42-
this.label = label;
43-
this.field = field;
47+
this(label, Collections.singletonList(field), currentSearchTerm);
48+
}
49+
50+
public DatatableSearch(final String label, final List<String> fields, final String currentSearchTerm) {
51+
Objects.requireNonNull(fields);
52+
53+
this.label = Objects.requireNonNull(label);
54+
this.fields = Collections.unmodifiableList(new ArrayList<>(fields));
4455
this.currentSearchTerm = currentSearchTerm;
4556
}
4657

4758
public String getLabel() {
4859
return label;
4960
}
5061

51-
public String getField() {
52-
return field;
62+
public List<String> getFields() {
63+
return fields;
5364
}
5465

5566
public String getCurrentSearchTerm() {
@@ -60,7 +71,8 @@ public String getCurrentSearchTerm() {
6071
public String toString() {
6172
return "DatatableSearch{"
6273
+ "label='" + label + '\''
63-
+ ", field='" + field + '\''
74+
+ ", fields=" + fields
75+
+ ", currentSearchTerm='" + currentSearchTerm + '\''
6476
+ '}';
6577
}
6678
}

kafka-webview-ui/src/main/resources/templates/configuration/user/datatable.html

Lines changed: 0 additions & 20 deletions
This file was deleted.

kafka-webview-ui/src/main/resources/templates/configuration/user/index.html

Lines changed: 2 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -11,61 +11,8 @@
1111
<body>
1212
<section layout:fragment="content">
1313
<div class="container">
14-
<div class="row">
15-
<div class="col-lg-12">
16-
<div class="card">
17-
<div class="card-header">
18-
<i class="fa fa-align-justify"></i>
19-
Users
20-
<div class="btn-group float-right" role="group" aria-label="Button group">
21-
<a class="btn" th:href="@{/configuration/user/create}" style="padding-bottom: 0;">
22-
<i class="icon-settings"></i>
23-
&nbsp;Create new
24-
</a>
25-
</div>
26-
</div>
27-
<div class="card-body">
28-
<table class="table table-bordered table-striped table-sm">
29-
<thead>
30-
<tr>
31-
<th>Email</th>
32-
<th>Name</th>
33-
<th>Role</th>
34-
<th class="text-right">Action</th>
35-
</tr>
36-
</thead>
37-
<tbody>
38-
<tr th:each="user : ${users}">
39-
<td th:text="${user.email}"></td>
40-
<td th:text="${user.displayName}"></td>
41-
<td th:text="${user.role}"></td>
42-
<td class="text-right">
43-
<div class="dropdown">
44-
<button class="btn btn-secondary btn-sm dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
45-
Actions
46-
</button>
47-
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
48-
<a class="dropdown-item" th:href="@{/configuration/user/edit/{id}(id=${user.id})}">
49-
<i class="fa fa-edit"></i>
50-
Edit
51-
</a>
52-
<form th:action="@{/configuration/user/delete/{id}(id=${user.id})}" method="post">
53-
<button class="dropdown-item" onclick="return confirm('Are you sure?');" type="submit">
54-
<i class="fa fa-remove"></i>
55-
Delete
56-
</button>
57-
</form>
58-
</div>
59-
</div>
60-
</td>
61-
</tr>
62-
</tbody>
63-
</table>
64-
</div>
65-
</div>
66-
</div>
67-
<!--/.col-->
68-
</div>
14+
<!-- Render datatable -->
15+
<div th:replace="fragments/datatable/Datatable :: display(${datatable})"></div>
6916
</div>
7017
</section>
7118

0 commit comments

Comments
 (0)