View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.client.console.wizards.any;
20  
21  import java.util.ArrayList;
22  import java.util.LinkedHashMap;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.stream.Collectors;
26  import org.apache.commons.collections4.CollectionUtils;
27  import org.apache.syncope.client.console.rest.GroupRestClient;
28  import org.apache.syncope.client.ui.commons.wizards.AjaxWizard;
29  import org.apache.syncope.client.ui.commons.wizards.any.AnyWrapper;
30  import org.apache.syncope.common.lib.Attr;
31  import org.apache.syncope.common.lib.to.AnyTypeClassTO;
32  import org.apache.syncope.common.lib.to.GroupTO;
33  import org.apache.syncope.common.lib.to.MembershipTO;
34  import org.apache.syncope.common.lib.to.SchemaTO;
35  import org.apache.syncope.common.lib.to.TypeExtensionTO;
36  import org.apache.wicket.WicketRuntimeException;
37  import org.apache.wicket.core.util.lang.PropertyResolver;
38  import org.apache.wicket.markup.head.IHeaderResponse;
39  import org.apache.wicket.markup.head.OnDomReadyHeaderItem;
40  import org.apache.wicket.model.IModel;
41  import org.apache.wicket.model.util.ListModel;
42  import org.apache.wicket.spring.injection.annot.SpringBean;
43  
44  public abstract class AbstractAttrs<S extends SchemaTO> extends AbstractAttrsWizardStep<S> {
45  
46      private static final long serialVersionUID = -5387344116983102292L;
47  
48      @SpringBean
49      protected GroupRestClient groupRestClient;
50  
51      protected final IModel<List<MembershipTO>> memberships;
52  
53      protected final Map<String, Map<String, S>> membershipSchemas = new LinkedHashMap<>();
54  
55      public AbstractAttrs(
56              final AnyWrapper<?> modelObject,
57              final AjaxWizard.Mode mode,
58              final List<String> anyTypeClasses,
59              final List<String> whichAttrs) {
60  
61          super(modelObject.getInnerObject(), mode, anyTypeClasses, whichAttrs);
62  
63          this.memberships = new ListModel<>(List.of());
64  
65          this.setOutputMarkupId(true);
66      }
67  
68      @SuppressWarnings("unchecked")
69      private List<MembershipTO> loadMemberships() {
70          membershipSchemas.clear();
71  
72          List<MembershipTO> membs = new ArrayList<>();
73          try {
74              ((List<MembershipTO>) PropertyResolver.getPropertyField("memberships", anyTO).get(anyTO)).forEach(memb -> {
75                  setSchemas(memb.getGroupKey(),
76                          anyTypeClassRestClient.list(getMembershipAuxClasses(memb, anyTO.getType())).
77                                  stream().map(AnyTypeClassTO::getKey).collect(Collectors.toList()));
78                  setAttrs(memb);
79  
80                  if (this instanceof PlainAttrs && !memb.getPlainAttrs().isEmpty()) {
81                      membs.add(memb);
82                  } else if (this instanceof DerAttrs && !memb.getDerAttrs().isEmpty()) {
83                      membs.add(memb);
84                  } else if (this instanceof VirAttrs && !memb.getVirAttrs().isEmpty()) {
85                      membs.add(memb);
86                  }
87              });
88          } catch (WicketRuntimeException | IllegalArgumentException | IllegalAccessException ex) {
89              // ignore
90          }
91  
92          return membs;
93      }
94  
95      private void setSchemas(final String membership, final List<String> anyTypeClasses) {
96          final Map<String, S> mscs;
97  
98          if (membershipSchemas.containsKey(membership)) {
99              mscs = membershipSchemas.get(membership);
100         } else {
101             mscs = new LinkedHashMap<>();
102             membershipSchemas.put(membership, mscs);
103         }
104         setSchemas(anyTypeClasses, mscs);
105     }
106 
107     protected List<String> getMembershipAuxClasses(final MembershipTO membershipTO, final String anyType) {
108         try {
109             GroupTO groupTO = groupRestClient.read(membershipTO.getGroupKey());
110             return groupTO.getTypeExtension(anyType).
111                     map(TypeExtensionTO::getAuxClasses).
112                     orElse(List.of());
113         } catch (Exception e) {
114             return List.of();
115         }
116     }
117 
118     protected abstract void setAttrs(MembershipTO membershipTO);
119 
120     protected abstract List<Attr> getAttrsFromTO(MembershipTO membershipTO);
121 
122     @Override
123     public void renderHead(final IHeaderResponse response) {
124         super.renderHead(response);
125         if (CollectionUtils.isEmpty(attrs.getObject()) && CollectionUtils.isEmpty(memberships.getObject())) {
126             response.render(OnDomReadyHeaderItem.forScript(
127                     String.format("$('#emptyPlaceholder').append(\"%s\"); $('#attributes').hide();",
128                             getString("attribute.empty.list"))));
129         }
130     }
131 
132     @Override
133     public boolean evaluate() {
134         this.attrs.setObject(loadAttrs());
135         this.memberships.setObject(loadMemberships());
136         return !attrs.getObject().isEmpty() || !memberships.getObject().isEmpty();
137     }
138 }