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.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.console.SyncopeConsoleSession;
28  import org.apache.syncope.client.console.wicket.markup.html.form.MultiFieldPanel;
29  import org.apache.syncope.client.ui.commons.ajax.markup.html.LabelInfo;
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.AjaxWizard;
34  import org.apache.syncope.client.ui.commons.wizards.any.AnyWrapper;
35  import org.apache.syncope.client.ui.commons.wizards.any.UserWrapper;
36  import org.apache.syncope.common.lib.Attr;
37  import org.apache.syncope.common.lib.EntityTOUtils;
38  import org.apache.syncope.common.lib.to.AnyTO;
39  import org.apache.syncope.common.lib.to.GroupableRelatableTO;
40  import org.apache.syncope.common.lib.to.MembershipTO;
41  import org.apache.syncope.common.lib.to.VirSchemaTO;
42  import org.apache.syncope.common.lib.types.SchemaType;
43  import org.apache.wicket.extensions.markup.html.tabs.AbstractTab;
44  import org.apache.wicket.markup.html.WebMarkupContainer;
45  import org.apache.wicket.markup.html.list.ListItem;
46  import org.apache.wicket.markup.html.list.ListView;
47  import org.apache.wicket.model.IModel;
48  import org.apache.wicket.model.Model;
49  import org.apache.wicket.model.PropertyModel;
50  import org.apache.wicket.model.ResourceModel;
51  import org.apache.wicket.model.StringResourceModel;
52  import org.apache.wicket.model.util.ListModel;
53  
54  public class VirAttrs extends AbstractAttrs<VirSchemaTO> {
55  
56      private static final long serialVersionUID = -7982691107029848579L;
57  
58      private final AnyWrapper<?> modelObject;
59  
60      public <T extends AnyTO> VirAttrs(
61              final AnyWrapper<T> modelObject,
62              final AjaxWizard.Mode mode,
63              final List<String> anyTypeClasses,
64              final List<String> whichVirAttrs) {
65  
66          super(modelObject, mode, anyTypeClasses, whichVirAttrs);
67          this.modelObject = modelObject;
68  
69          setTitleModel(new ResourceModel("attributes.virtual"));
70  
71          add(new Accordion("virSchemas", List.of(new AbstractTab(
72                  new ResourceModel("attributes.accordion", "Virtual Attributes")) {
73  
74              private static final long serialVersionUID = 1037272333056449378L;
75  
76              @Override
77              public WebMarkupContainer getPanel(final String panelId) {
78                  return new VirAttrs.VirSchemas(panelId, schemas, attrs);
79              }
80          }), Model.of(0)).setOutputMarkupId(true));
81  
82          add(new ListView<>("membershipsVirSchemas", memberships) {
83  
84              private static final long serialVersionUID = 9101744072914090143L;
85  
86              @Override
87              protected void populateItem(final ListItem<MembershipTO> item) {
88                  MembershipTO membTO = item.getModelObject();
89                  item.add(new Accordion("membershipVirSchemas", List.of(new AbstractTab(
90                      new StringResourceModel("attributes.membership.accordion", VirAttrs.this, Model.of(membTO))) {
91  
92                      private static final long serialVersionUID = 1037272333056449378L;
93  
94                      @Override
95                      public WebMarkupContainer getPanel(final String panelId) {
96                          return new VirAttrs.VirSchemas(
97                              panelId,
98                              membershipSchemas.get(membTO.getGroupKey()),
99                              new ListModel<>(getAttrsFromTO(membTO)));
100                     }
101                 }), Model.of(-1)).setOutputMarkupId(true));
102             }
103         });
104     }
105 
106     @Override
107     protected SchemaType getSchemaType() {
108         return SchemaType.VIRTUAL;
109     }
110 
111     @Override
112     protected List<Attr> getAttrsFromTO() {
113         return anyTO.getVirAttrs().stream().sorted(attrComparator).collect(Collectors.toList());
114     }
115 
116     @Override
117     protected List<Attr> getAttrsFromTO(final MembershipTO membershipTO) {
118         return membershipTO.getVirAttrs().stream().sorted(attrComparator).collect(Collectors.toList());
119     }
120 
121     @Override
122     protected void setAttrs() {
123         List<Attr> virAttrs = new ArrayList<>();
124 
125         Map<String, Attr> attrMap = EntityTOUtils.buildAttrMap(anyTO.getVirAttrs());
126 
127         virAttrs.addAll(schemas.values().stream().map(schema -> {
128             Attr attrTO = new Attr();
129             attrTO.setSchema(schema.getKey());
130             if (attrMap.containsKey(schema.getKey())) {
131                 attrTO.getValues().addAll(attrMap.get(schema.getKey()).getValues());
132             } else {
133                 attrTO.getValues().add(StringUtils.EMPTY);
134             }
135             return attrTO;
136         }).collect(Collectors.toList()));
137 
138         anyTO.getVirAttrs().clear();
139         anyTO.getVirAttrs().addAll(virAttrs);
140     }
141 
142     @Override
143     protected void setAttrs(final MembershipTO membershipTO) {
144         Map<String, Attr> attrMap = GroupableRelatableTO.class.cast(anyTO).getMembership(membershipTO.getGroupKey()).
145                 map(gr -> EntityTOUtils.buildAttrMap(gr.getVirAttrs())).
146                 orElseGet(() -> new HashMap<>());
147 
148         List<Attr> virAttrs = membershipSchemas.get(membershipTO.getGroupKey()).values().stream().map(schema -> {
149             Attr attr = new Attr();
150             attr.setSchema(schema.getKey());
151             if (attrMap.get(schema.getKey()) == null || attrMap.get(schema.getKey()).getValues().isEmpty()) {
152                 attr.getValues().add(StringUtils.EMPTY);
153             } else {
154                 attr.getValues().addAll(attrMap.get(schema.getKey()).getValues());
155             }
156             return attr;
157         }).collect(Collectors.toList());
158 
159         membershipTO.getVirAttrs().clear();
160         membershipTO.getVirAttrs().addAll(virAttrs);
161     }
162 
163     public class VirSchemas extends Schemas {
164 
165         private static final long serialVersionUID = -4730563859116024676L;
166 
167         public VirSchemas(
168                 final String id,
169                 final Map<String, VirSchemaTO> schemas,
170                 final IModel<List<Attr>> attrTOs) {
171             super(id);
172 
173             add(new ListView<>("schemas", attrTOs) {
174 
175                 private static final long serialVersionUID = 9101744072914090143L;
176 
177                 @Override
178                 @SuppressWarnings("unchecked")
179                 protected void populateItem(final ListItem<Attr> item) {
180                     Attr attrTO = item.getModelObject();
181                     VirSchemaTO virSchemaTO = schemas.get(attrTO.getSchema());
182 
183                     AbstractFieldPanel<?> panel = new AjaxTextFieldPanel(
184                         "panel",
185                         virSchemaTO.getLabel(SyncopeConsoleSession.get().getLocale()),
186                         new Model<>(),
187                         false);
188 
189                     if (mode == AjaxWizard.Mode.TEMPLATE) {
190                         AjaxTextFieldPanel.class.cast(panel).enableJexlHelp().setEnabled(!virSchemaTO.isReadonly());
191                     } else {
192                         panel = new MultiFieldPanel.Builder<>(
193                             new PropertyModel<List<String>>(attrTO, "values")).build(
194                             "panel",
195                             virSchemaTO.getLabel(SyncopeConsoleSession.get().getLocale()),
196                             AjaxTextFieldPanel.class.cast(panel));
197                         panel.setEnabled(!virSchemaTO.isReadonly());
198                     }
199 
200                     item.add(panel);
201 
202                     if (!attrTO.getValues().isEmpty()
203                         && VirAttrs.this.modelObject instanceof UserWrapper
204                         && UserWrapper.class.cast(VirAttrs.this.modelObject).getPreviousUserTO() != null) {
205 
206                         panel.showExternAction(new LabelInfo("externalAction", StringUtils.EMPTY));
207                     }
208                 }
209             });
210         }
211     }
212 }