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.enduser.markup.html.form.MultiFieldPanel;
30  import org.apache.syncope.client.ui.commons.markup.html.form.AbstractFieldPanel;
31  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxTextFieldPanel;
32  import org.apache.syncope.client.ui.commons.wicket.markup.html.bootstrap.tabs.Accordion;
33  import org.apache.syncope.client.ui.commons.wizards.any.AnyWrapper;
34  import org.apache.syncope.common.lib.Attr;
35  import org.apache.syncope.common.lib.EntityTOUtils;
36  import org.apache.syncope.common.lib.to.GroupableRelatableTO;
37  import org.apache.syncope.common.lib.to.MembershipTO;
38  import org.apache.syncope.common.lib.to.UserTO;
39  import org.apache.syncope.common.lib.to.VirSchemaTO;
40  import org.apache.syncope.common.lib.types.SchemaType;
41  import org.apache.wicket.extensions.markup.html.tabs.AbstractTab;
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.PropertyModel;
48  import org.apache.wicket.model.StringResourceModel;
49  import org.apache.wicket.model.util.ListModel;
50  
51  public class VirAttrs extends AbstractAttrs<VirSchemaTO> {
52  
53      private static final long serialVersionUID = -7982691107029848579L;
54  
55      public VirAttrs(
56              final String id,
57              final AnyWrapper<UserTO> modelObject,
58              final List<String> anyTypeClasses,
59              final Map<String, CustomizationOption> whichVirAttrs) {
60  
61          super(id, modelObject, anyTypeClasses, whichVirAttrs);
62  
63          add(new VirAttrs.VirSchemas("virSchemas", null, schemas, attrs).setOutputMarkupId(true));
64          add(new ListView<>("membershipsVirSchemas", membershipTOs) {
65  
66              private static final long serialVersionUID = 9101744072914090143L;
67  
68              @Override
69              protected void populateItem(final ListItem<MembershipTO> item) {
70                  MembershipTO membershipTO = item.getModelObject();
71                  item.add(new Accordion("membershipVirSchemas", List.of(
72                          new AbstractTab(new StringResourceModel(
73                                  "attributes.membership.accordion", VirAttrs.this, Model.of(membershipTO))) {
74  
75                      private static final long serialVersionUID = 1037272333056449378L;
76  
77                      @Override
78                      public WebMarkupContainer getPanel(final String panelId) {
79                          return new VirAttrs.VirSchemas(
80                                  panelId,
81                                  membershipTO.getGroupName(),
82                                  membershipSchemas.get(membershipTO.getGroupKey()),
83                                  new ListModel<>(getAttrsFromTO(membershipTO)));
84                      }
85                  }), Model.of(-1)).setOutputMarkupId(true));
86              }
87          }).setOutputMarkupId(true);
88      }
89  
90      @Override
91      protected SchemaType getSchemaType() {
92          return SchemaType.VIRTUAL;
93      }
94  
95      @Override
96      protected List<Attr> getAttrsFromTO() {
97          return userTO.getVirAttrs().stream().sorted(attrComparator).collect(Collectors.toList());
98      }
99  
100     @Override
101     protected List<Attr> getAttrsFromTO(final MembershipTO membershipTO) {
102         return membershipTO.getVirAttrs().stream().sorted(attrComparator).collect(Collectors.toList());
103     }
104 
105     @Override
106     protected void setAttrs() {
107         List<Attr> virAttrs = new ArrayList<>();
108 
109         Map<String, Attr> attrMap = EntityTOUtils.buildAttrMap(userTO.getVirAttrs());
110 
111         virAttrs.addAll(schemas.values().stream().map(schema -> {
112             Attr attrTO = new Attr();
113             attrTO.setSchema(schema.getKey());
114             if (attrMap.containsKey(schema.getKey())) {
115                 attrTO.getValues().addAll(attrMap.get(schema.getKey()).getValues());
116             } else {
117                 attrTO.getValues().add(StringUtils.EMPTY);
118             }
119             return attrTO;
120         }).collect(Collectors.toList()));
121 
122         userTO.getVirAttrs().clear();
123         userTO.getVirAttrs().addAll(virAttrs);
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.getVirAttrs())).
130                 orElseGet(() -> new HashMap<>());
131 
132         List<Attr> virAttrs = membershipSchemas.get(membershipTO.getGroupKey()).values().stream().map(schema -> {
133             Attr attr = new Attr();
134             attr.setSchema(schema.getKey());
135             if (attrMap.get(schema.getKey()) == null || attrMap.get(schema.getKey()).getValues().isEmpty()) {
136                 attr.getValues().add(StringUtils.EMPTY);
137             } else {
138                 attr.getValues().addAll(attrMap.get(schema.getKey()).getValues());
139             }
140             return attr;
141         }).collect(Collectors.toList());
142 
143         membershipTO.getVirAttrs().clear();
144         membershipTO.getVirAttrs().addAll(virAttrs);
145     }
146 
147     public class VirSchemas extends Schemas {
148 
149         private static final long serialVersionUID = -4730563859116024676L;
150 
151         public VirSchemas(
152                 final String id,
153                 final String groupName,
154                 final Map<String, VirSchemaTO> schemas,
155                 final IModel<List<Attr>> attrTOs) {
156 
157             super(id);
158 
159             add(new ListView<>("schemas", attrTOs) {
160 
161                 private static final long serialVersionUID = 9101744072914090143L;
162 
163                 @Override
164                 @SuppressWarnings("unchecked")
165                 protected void populateItem(final ListItem<Attr> item) {
166                     Attr attrTO = item.getModelObject();
167 
168                     // set default values, if any
169                     if (attrTO.getValues().stream().filter(StringUtils::isNotBlank)
170                             .collect(Collectors.toList()).isEmpty()) {
171                         attrTO.getValues().clear();
172                         attrTO.getValues().addAll(getDefaultValues(attrTO.getSchema(), groupName));
173                     }
174 
175                     VirSchemaTO virSchemaTO = schemas.get(attrTO.getSchema());
176 
177                     AbstractFieldPanel<?> panel = new AjaxTextFieldPanel(
178                             "panel",
179                             virSchemaTO.getLabel(SyncopeEnduserSession.get().getLocale()),
180                             new Model<>(),
181                             false);
182 
183                     panel = new MultiFieldPanel.Builder<>(
184                             new PropertyModel<List<String>>(attrTO, "values")).build(
185                             "panel",
186                             virSchemaTO.getLabel(SyncopeEnduserSession.get().getLocale()),
187                             AjaxTextFieldPanel.class.cast(panel));
188                     panel.setEnabled(!virSchemaTO.isReadonly() && !renderAsReadonly(attrTO.getSchema(), groupName));
189 
190                     item.add(panel);
191                 }
192             });
193         }
194     }
195 }