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.enduser.panels.any;
20  
21  import java.util.ArrayList;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.stream.Collectors;
26  import org.apache.commons.lang3.StringUtils;
27  import org.apache.syncope.client.enduser.SyncopeEnduserSession;
28  import org.apache.syncope.client.enduser.layout.CustomizationOption;
29  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxTextFieldPanel;
30  import org.apache.syncope.client.ui.commons.wicket.markup.html.bootstrap.tabs.Accordion;
31  import org.apache.syncope.client.ui.commons.wizards.any.AnyWrapper;
32  import org.apache.syncope.common.lib.Attr;
33  import org.apache.syncope.common.lib.EntityTOUtils;
34  import org.apache.syncope.common.lib.to.DerSchemaTO;
35  import org.apache.syncope.common.lib.to.GroupableRelatableTO;
36  import org.apache.syncope.common.lib.to.MembershipTO;
37  import org.apache.syncope.common.lib.to.UserTO;
38  import org.apache.syncope.common.lib.types.SchemaType;
39  import org.apache.wicket.extensions.markup.html.tabs.AbstractTab;
40  import org.apache.wicket.markup.ComponentTag;
41  import org.apache.wicket.markup.MarkupStream;
42  import org.apache.wicket.markup.html.WebMarkupContainer;
43  import org.apache.wicket.markup.html.list.ListItem;
44  import org.apache.wicket.markup.html.list.ListView;
45  import org.apache.wicket.model.IModel;
46  import org.apache.wicket.model.Model;
47  import org.apache.wicket.model.ResourceModel;
48  import org.apache.wicket.model.StringResourceModel;
49  import org.apache.wicket.model.util.ListModel;
50  
51  public class DerAttrs extends AbstractAttrs<DerSchemaTO> {
52  
53      private static final long serialVersionUID = -5387344116983102292L;
54  
55      public DerAttrs(
56              final String id,
57              final AnyWrapper<UserTO> modelObject,
58              final List<String> anyTypeClasses,
59              final Map<String, CustomizationOption> whichDerAttrs) {
60  
61          super(id, modelObject, anyTypeClasses, whichDerAttrs);
62  
63          add(new DerAttrs.DerSchemas("derSchemas", schemas, attrs).setOutputMarkupId(true));
64          add(new ListView<>("membershipsDerSchemas", membershipTOs) {
65  
66              private static final long serialVersionUID = 6741044372185745296L;
67  
68              @Override
69              protected void populateItem(final ListItem<MembershipTO> item) {
70                  MembershipTO membershipTO = item.getModelObject();
71                  item.add(new Accordion("membershipDerSchemas", List.of(new AbstractTab(
72                          new StringResourceModel(
73                                  "attributes.membership.accordion",
74                                  DerAttrs.this,
75                                  Model.of(membershipTO))) {
76  
77                      private static final long serialVersionUID = 1037272333056449378L;
78  
79                      @Override
80                      public WebMarkupContainer getPanel(final String panelId) {
81                          return new DerAttrs.DerSchemas(
82                                  panelId,
83                                  membershipSchemas.get(membershipTO.getGroupKey()),
84                                  new ListModel<>(getAttrsFromTO(membershipTO)));
85                      }
86                  }), Model.of(-1)).setOutputMarkupId(true));
87              }
88          });
89      }
90  
91      @Override
92      protected SchemaType getSchemaType() {
93          return SchemaType.DERIVED;
94      }
95  
96      @Override
97      protected List<Attr> getAttrsFromTO() {
98          return userTO.getDerAttrs().stream().sorted(attrComparator).collect(Collectors.toList());
99      }
100 
101     @Override
102     protected List<Attr> getAttrsFromTO(final MembershipTO membershipTO) {
103         return membershipTO.getDerAttrs().stream().sorted(attrComparator).collect(Collectors.toList());
104     }
105 
106     @Override
107     protected void setAttrs() {
108         List<Attr> derAttrs = new ArrayList<>();
109 
110         Map<String, Attr> attrMap = EntityTOUtils.buildAttrMap(userTO.getDerAttrs());
111 
112         schemas.values().forEach(schema -> {
113             Attr attrTO = new Attr();
114             attrTO.setSchema(schema.getKey());
115             if (attrMap.containsKey(schema.getKey())) {
116                 attrTO.getValues().addAll(attrMap.get(schema.getKey()).getValues());
117             }
118 
119             derAttrs.add(attrTO);
120         });
121 
122         userTO.getDerAttrs().clear();
123         userTO.getDerAttrs().addAll(derAttrs);
124     }
125 
126     @Override
127     protected void setAttrs(final MembershipTO membershipTO) {
128         Map<String, Attr> attrMap = GroupableRelatableTO.class.cast(userTO).getMembership(membershipTO.getGroupKey()).
129                 map(gr -> EntityTOUtils.buildAttrMap(gr.getDerAttrs())).
130                 orElseGet(() -> new HashMap<>());
131 
132         List<Attr> derAttrs = membershipSchemas.get(membershipTO.getGroupKey()).values().stream().map(schema -> {
133             Attr attr = new Attr();
134             attr.setSchema(schema.getKey());
135             if (attrMap.containsKey(schema.getKey())) {
136                 attr.getValues().addAll(attrMap.get(schema.getKey()).getValues());
137             }
138 
139             return attr;
140         }).collect(Collectors.toList());
141 
142         membershipTO.getDerAttrs().clear();
143         membershipTO.getDerAttrs().addAll(derAttrs);
144     }
145 
146     public static class DerSchemas extends Schemas {
147 
148         private static final long serialVersionUID = -4730563859116024676L;
149 
150         public DerSchemas(
151                 final String id,
152                 final Map<String, DerSchemaTO> schemas,
153                 final IModel<List<Attr>> attrTOs) {
154             super(id);
155 
156             add(new ListView<>("schemas", attrTOs) {
157 
158                 private static final long serialVersionUID = 9101744072914090143L;
159 
160                 @Override
161                 public void onComponentTagBody(final MarkupStream markupStream, final ComponentTag openTag) {
162                     super.onComponentTagBody(markupStream, openTag);
163                     openTag.put("class", "empty");
164                 }
165 
166                 @Override
167                 protected void populateItem(final ListItem<Attr> item) {
168                     Attr attrTO = item.getModelObject();
169 
170                     IModel<String> model;
171                     List<String> values = attrTO.getValues();
172                     if (values == null || values.isEmpty()) {
173                         model = new ResourceModel("derived.emptyvalue.message", StringUtils.EMPTY);
174                     } else {
175                         model = new Model<>(values.get(0));
176                     }
177 
178                     AjaxTextFieldPanel panel = new AjaxTextFieldPanel(
179                             "panel",
180                             schemas.get(attrTO.getSchema()).getLabel(SyncopeEnduserSession.get().getLocale()),
181                             model,
182                             false);
183                     panel.setEnabled(false);
184                     panel.setRequired(true);
185                     panel.setOutputMarkupId(true);
186                     item.add(panel);
187                 }
188             });
189         }
190     }
191 }