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