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