Skip to content

Commit 89694ad

Browse files
authored
Merge pull request #222 from SourceLabOrg/sp/moreDatatable
Convert more datatables
2 parents fd51b07 + 6f5b651 commit 89694ad

File tree

14 files changed

+347
-177
lines changed

14 files changed

+347
-177
lines changed

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

Lines changed: 16 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,7 @@
3333
import org.sourcelab.kafka.webview.ui.manager.ui.datatable.ConstraintOperator;
3434
import org.sourcelab.kafka.webview.ui.manager.ui.datatable.Datatable;
3535
import org.sourcelab.kafka.webview.ui.manager.ui.datatable.DatatableColumn;
36-
import org.sourcelab.kafka.webview.ui.manager.ui.datatable.DatatableFilter;
37-
import org.sourcelab.kafka.webview.ui.manager.ui.datatable.LinkTemplate;
38-
import org.sourcelab.kafka.webview.ui.manager.ui.datatable.YesNoBadgeTemplate;
3936
import org.sourcelab.kafka.webview.ui.manager.user.UserManager;
40-
import org.sourcelab.kafka.webview.ui.model.Cluster;
4137
import org.sourcelab.kafka.webview.ui.model.User;
4238
import org.sourcelab.kafka.webview.ui.model.UserRole;
4339
import org.sourcelab.kafka.webview.ui.repository.UserRepository;
@@ -79,27 +75,7 @@ public class UserController extends BaseController {
7975
* GET Displays main user index.
8076
*/
8177
@RequestMapping(path = "", method = RequestMethod.GET)
82-
public String index(final Model model, final RedirectAttributes redirectAttributes) {
83-
// Setup breadcrumbs
84-
setupBreadCrumbs(model, null, null);
85-
86-
// Check for LDAP auth method and restrict access.
87-
if (redirectIfUsingLdapAuthentication(redirectAttributes)) {
88-
return "redirect:/";
89-
}
90-
91-
// Retrieve all users
92-
final Iterable<User> usersList = userRepository.findAllByIsActiveOrderByEmailAsc(true);
93-
model.addAttribute("users", usersList);
94-
95-
return "configuration/user/index";
96-
}
97-
98-
/**
99-
* GET Displays main user index.
100-
*/
101-
@RequestMapping(path = "/datatable", method = RequestMethod.GET)
102-
public String datatable(
78+
public String index(
10379
final Model model,
10480
final Pageable pageable,
10581
@RequestParam Map<String,String> allParams,
@@ -113,19 +89,16 @@ public String datatable(
11389
return "redirect:/";
11490
}
11591

116-
// TODO verify add create link
117-
// TODO verify add action button and DRY
118-
// TODO fix enum filter for role
11992
final Datatable.Builder<User> builder = Datatable.newBuilder(User.class)
12093
.withRepository(userRepository)
12194
.withPageable(pageable)
12295
.withRequestParams(allParams)
123-
.withUrl("/configuration/user/datatable")
96+
.withUrl("/configuration/user")
12497
.withLabel("Users")
12598
// Only show active users.
12699
.withConstraint("isActive", true, ConstraintOperator.EQUALS)
127100
// With Create Link
128-
.withLink("/configuration/user/create", "Create user")
101+
.withCreateLink("/configuration/user/create")
129102
// Email Column
130103
.withColumn(DatatableColumn.newBuilder(User.class)
131104
.withFieldName("email")
@@ -161,39 +134,27 @@ public String datatable(
161134
.withLabel("Action")
162135
.withFieldName("id")
163136
.withIsSortable(false)
137+
.withHeaderAlignRight()
164138
.withRenderTemplate(ActionTemplate.newBuilder(User.class)
165139
// Edit Link
166-
.withLink(ActionTemplate.ActionLink.newBuilder(User.class)
167-
.withLabelFunction((record) -> "Edit")
168-
.withUrlFunction((record) -> "/configuration/user/edit/" + record.getId())
169-
.withIcon("fa-edit")
170-
.build())
140+
.withEditLink(User.class, (record) -> "/configuration/user/edit/" + record.getId())
171141
// Delete Link
172-
.withLink(ActionTemplate.ActionLink.newBuilder(User.class)
173-
.withLabelFunction((record) -> "Delete")
174-
.withUrlFunction((record) -> "/configuration/user/delete/" + record.getId())
175-
.withIcon("fa-remove")
176-
.withIsPost(true)
177-
.build())
142+
.withDeleteLink(User.class, (record) -> "/configuration/user/delete/" + record.getId())
178143
.build())
179144
.build())
180-
.withSearch("email")
181-
.withFilter(DatatableFilter.newBuilder()
182-
.withField("role")
183-
.withLabel("Role")
184-
.withOption(UserRole.ROLE_ADMIN.name(), "Admin")
185-
.withOption(UserRole.ROLE_USER.name(), "User")
186-
.build()
187-
);
145+
.withSearch("email", "displayName");
146+
// TODO fix filters with enums
147+
// .withFilter(DatatableFilter.newBuilder()
148+
// .withField("role")
149+
// .withLabel("Role")
150+
// .withOption(UserRole.ROLE_ADMIN.name(), "Admin")
151+
// .withOption(UserRole.ROLE_USER.name(), "User")
152+
// .build()
153+
// );
188154

189155
// Add datatable attribute
190156
model.addAttribute("datatable", builder.build());
191-
192-
// Retrieve all users
193-
final Iterable<User> usersList = userRepository.findAllByIsActiveOrderByEmailAsc(true);
194-
model.addAttribute("users", usersList);
195-
196-
return "configuration/user/datatable";
157+
return "configuration/user/index";
197158
}
198159

199160
/**

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

Lines changed: 110 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,38 @@
1+
/**
2+
* MIT License
3+
*
4+
* Copyright (c) 2017, 2018, 2019 SourceLab.org (https://github.com/SourceLabOrg/kafka-webview/)
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
125
package org.sourcelab.kafka.webview.ui.manager.ui.datatable;
226

327
import java.util.ArrayList;
428
import java.util.Collections;
529
import java.util.List;
30+
import java.util.Objects;
631
import java.util.function.Function;
732

833
/**
934
* Provides Action links template.
35+
* @param <T> Record type.
1036
*/
1137
public class ActionTemplate<T> extends RenderTemplate<T> {
1238
private final List<ActionLink<T>> links;
@@ -32,17 +58,39 @@ List<Object> getParameters(final T record) {
3258
return params;
3359
}
3460

61+
/**
62+
* Create new ActionLink Builder instance.
63+
* @param type Record type.
64+
* @param <T> Record type.
65+
* @return new Builder instance.
66+
*/
3567
public static <T> Builder<T> newBuilder(Class<T> type) {
3668
return new Builder<>();
3769
}
3870

71+
/**
72+
* Defines an Action Link.
73+
* @param <T> Record type.
74+
*/
3975
public static class ActionLink<T> {
4076
private final Function<T, String> urlFunction;
4177
private final Function<T, String> labelFunction;
4278
private final String icon;
4379
private final boolean isPost;
4480

45-
public ActionLink(final Function<T, String> urlFunction, final Function<T, String> labelFunction, final String icon, final boolean isPost) {
81+
/**
82+
* Constructor. See Builder instance.
83+
* @param urlFunction Function to generate URL.
84+
* @param labelFunction Function to generate label.
85+
* @param icon Icon to display.
86+
* @param isPost Should the link be a POST or get.
87+
*/
88+
public ActionLink(
89+
final Function<T, String> urlFunction,
90+
final Function<T, String> labelFunction,
91+
final String icon,
92+
final boolean isPost
93+
) {
4694
this.urlFunction = urlFunction;
4795
this.labelFunction = labelFunction;
4896
this.icon = icon;
@@ -65,6 +113,10 @@ public String getIcon() {
65113
return icon;
66114
}
67115

116+
/**
117+
* Does this action link have an icon.
118+
* @return True if yes, false if not.
119+
*/
68120
public boolean hasIcon() {
69121
if (icon == null || icon.trim().isEmpty()) {
70122
return false;
@@ -77,13 +129,23 @@ public boolean isPost() {
77129
}
78130
}
79131

132+
/**
133+
* ActionLink Builder instance.
134+
* @param <T> Record type.
135+
*/
80136
public static class Builder<T> {
81137
private List<ActionLink<T>> links = new ArrayList<>();
82138

83139
private Builder() {
84140
}
85141

142+
/**
143+
* Add multiple links.
144+
* @param links Links to add.
145+
* @return Builder instance.
146+
*/
86147
public Builder<T> withLinks(List<ActionLink<T>> links) {
148+
Objects.requireNonNull(links);
87149
this.links.clear();
88150
this.links.addAll(links);
89151
return this;
@@ -94,11 +156,58 @@ public Builder<T> withLink(final ActionLink<T> link) {
94156
return this;
95157
}
96158

159+
/**
160+
* Add a standard Edit link.
161+
* @param type Record type.
162+
* @param urlFunction The url to link to.
163+
* @return Builder instance.
164+
*/
165+
public Builder<T> withEditLink(
166+
final Class<T> type,
167+
final Function<T, String> urlFunction
168+
) {
169+
return withLink(
170+
ActionTemplate.ActionLink.newBuilder(type)
171+
.withLabelFunction((record) -> "Edit")
172+
.withUrlFunction(urlFunction)
173+
.withIcon("fa-edit")
174+
.build()
175+
);
176+
}
177+
178+
/**
179+
* Add a standard Delete link.
180+
* @param type Record type.
181+
* @param urlFunction The url to link to.
182+
* @return Builder instance.
183+
*/
184+
public Builder<T> withDeleteLink(
185+
final Class<T> type,
186+
final Function<T, String> urlFunction
187+
) {
188+
return withLink(
189+
ActionTemplate.ActionLink.newBuilder(type)
190+
.withLabelFunction((record) -> "Delete")
191+
.withUrlFunction(urlFunction)
192+
.withIcon("fa-remove")
193+
.withIsPost(true)
194+
.build()
195+
);
196+
}
197+
198+
/**
199+
* Create new ActionTemplate instance from Builder.
200+
* @return ActionType instance.
201+
*/
97202
public ActionTemplate<T> build() {
98203
return new ActionTemplate<T>(links);
99204
}
100205
}
101206

207+
/**
208+
* Link builder instance.
209+
* @param <T> Record type.
210+
*/
102211
public static class ActionLinkBuilder<T> {
103212
private Function<T, String> urlFunction;
104213
private Function<T, String> labelFunction;

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
/**
2+
* MIT License
3+
*
4+
* Copyright (c) 2017, 2018, 2019 SourceLab.org (https://github.com/SourceLabOrg/kafka-webview/)
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
125
package org.sourcelab.kafka.webview.ui.manager.ui.datatable;
226

327
/**

0 commit comments

Comments
 (0)