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.layout;
20
21 import com.fasterxml.jackson.databind.json.JsonMapper;
22 import java.io.IOException;
23 import java.lang.reflect.InvocationTargetException;
24 import java.util.List;
25 import java.util.function.Function;
26 import java.util.stream.Collectors;
27 import java.util.stream.Stream;
28 import org.apache.commons.lang3.StringUtils;
29 import org.apache.syncope.client.console.SyncopeConsoleSession;
30 import org.apache.syncope.client.console.rest.AbstractAnyRestClient;
31 import org.apache.syncope.client.console.rest.AnyObjectRestClient;
32 import org.apache.syncope.client.console.rest.GroupRestClient;
33 import org.apache.syncope.client.console.rest.RoleRestClient;
34 import org.apache.syncope.client.console.rest.UserRestClient;
35 import org.apache.syncope.client.ui.commons.layout.AbstractAnyFormLayout;
36 import org.apache.syncope.client.ui.commons.wizards.any.AnyForm;
37 import org.apache.syncope.common.lib.to.AnyTO;
38 import org.apache.syncope.common.lib.to.GroupTO;
39 import org.apache.syncope.common.lib.to.UserTO;
40 import org.apache.syncope.common.lib.types.AnyTypeKind;
41 import org.apache.wicket.PageReference;
42
43 public final class AnyLayoutUtils {
44
45 private static final JsonMapper MAPPER = JsonMapper.builder().findAndAddModules().build();
46
47 private static void setUserIfEmpty(final AnyLayout anyLayout) {
48 if (anyLayout.getUser() == null) {
49 anyLayout.setUser(new UserFormLayoutInfo());
50 }
51 }
52
53 private static void setGroupIfEmpty(final AnyLayout anyLayout) {
54 if (anyLayout.getGroup() == null) {
55 anyLayout.setGroup(new GroupFormLayoutInfo());
56 }
57 }
58
59 private static void setAnyObjectsIfEmpty(final AnyLayout anyLayout, final List<String> anyTypes) {
60 if (anyLayout.getAnyObjects().isEmpty()) {
61 anyLayout.getAnyObjects().putAll(anyTypes.stream().filter(
62 anyType -> !anyType.equals(AnyTypeKind.USER.name()) && !anyType.equals(AnyTypeKind.GROUP.name())).
63 collect(Collectors.toMap(Function.identity(), anyType -> new AnyObjectFormLayoutInfo())));
64 }
65 }
66
67 private static AnyLayout empty(final List<String> anyTypes) {
68 AnyLayout anyLayout = new AnyLayout();
69 setUserIfEmpty(anyLayout);
70 setGroupIfEmpty(anyLayout);
71 setAnyObjectsIfEmpty(anyLayout, anyTypes);
72 return anyLayout;
73 }
74
75 public static AnyLayout fetch(final RoleRestClient roleRestClient, final List<String> anyTypes) {
76 List<String> ownedRoles = Stream.concat(
77 SyncopeConsoleSession.get().getSelfTO().getRoles().stream(),
78 SyncopeConsoleSession.get().getSelfTO().getDynRoles().stream()).
79 distinct().collect(Collectors.toList());
80 try {
81 AnyLayout anyLayout = null;
82 for (int i = 0; i < ownedRoles.size() && anyLayout == null; i++) {
83 String anyLayoutJSON = roleRestClient.readAnyLayout(ownedRoles.get(i));
84 if (StringUtils.isNotBlank(anyLayoutJSON)) {
85 anyLayout = MAPPER.readValue(anyLayoutJSON, AnyLayout.class);
86 }
87 }
88
89 if (anyLayout == null) {
90 anyLayout = empty(anyTypes);
91 }
92 setUserIfEmpty(anyLayout);
93 setGroupIfEmpty(anyLayout);
94 setAnyObjectsIfEmpty(anyLayout, anyTypes);
95
96 return anyLayout;
97 } catch (IOException e) {
98 throw new IllegalArgumentException("While parsing console layout for "
99 + SyncopeConsoleSession.get().getSelfTO().getUsername(), e);
100 }
101 }
102
103 public static String defaultIfEmpty(final String content, final List<String> anyTypes) {
104 String result;
105
106 if (StringUtils.isBlank(content)) {
107 AnyLayout anyLayout = empty(anyTypes);
108
109 try {
110 result = MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(anyLayout);
111 } catch (IOException e) {
112 throw new IllegalArgumentException("While generating default console layout for "
113 + SyncopeConsoleSession.get().getSelfTO().getUsername(), e);
114 }
115 } else {
116 try {
117 result = MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(MAPPER.readTree(content));
118 } catch (IOException e) {
119 result = content;
120 }
121 }
122
123 return result;
124 }
125
126 public static <A extends AnyTO, F extends AnyForm<A>, FL extends AbstractAnyFormLayout<A, F>> F newLayoutInfo(
127 final A anyTO,
128 final List<String> anyTypeClasses,
129 final FL anyFormLayout,
130 final AbstractAnyRestClient<?> anyRestClient,
131 final PageReference pageRef) {
132
133 try {
134 if (anyTO instanceof UserTO) {
135 return anyFormLayout.getFormClass().getConstructor(
136 anyTO.getClass(),
137 anyTO.getClass(),
138 List.class,
139 anyFormLayout.getClass(),
140 UserRestClient.class,
141 pageRef.getClass()).
142 newInstance(null, anyTO, anyTypeClasses, anyFormLayout, anyRestClient, pageRef);
143 }
144
145 if (anyTO instanceof GroupTO) {
146 return anyFormLayout.getFormClass().getConstructor(
147 anyTO.getClass(),
148 List.class,
149 anyFormLayout.getClass(),
150 GroupRestClient.class,
151 pageRef.getClass()).
152 newInstance(anyTO, anyTypeClasses, anyFormLayout, anyRestClient, pageRef);
153 }
154
155 return anyFormLayout.getFormClass().getConstructor(
156 anyTO.getClass(),
157 List.class,
158 anyFormLayout.getClass(),
159 AnyObjectRestClient.class,
160 pageRef.getClass()).
161 newInstance(anyTO, anyTypeClasses, anyFormLayout, anyRestClient, pageRef);
162 } catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException
163 | IllegalArgumentException | InvocationTargetException e) {
164 throw new IllegalArgumentException("Could not instantiate " + anyFormLayout.getFormClass().getName(), e);
165 }
166 }
167
168 private AnyLayoutUtils() {
169
170 }
171 }