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.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.stream.Collectors;
25  import org.apache.commons.lang3.StringUtils;
26  import org.apache.syncope.client.ui.commons.wicket.markup.html.bootstrap.tabs.Accordion;
27  import org.apache.syncope.client.ui.commons.wizards.AjaxWizard;
28  import org.apache.syncope.client.ui.commons.wizards.any.AnyWrapper;
29  import org.apache.syncope.client.ui.commons.wizards.any.UserWrapper;
30  import org.apache.syncope.common.lib.Attr;
31  import org.apache.syncope.common.lib.Attributable;
32  import org.apache.syncope.common.lib.EntityTOUtils;
33  import org.apache.syncope.common.lib.to.AnyObjectTO;
34  import org.apache.syncope.common.lib.to.AnyTO;
35  import org.apache.syncope.common.lib.to.GroupTO;
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.PlainSchemaTO;
39  import org.apache.syncope.common.lib.to.UserTO;
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.LoadableDetachableModel;
47  import org.apache.wicket.model.Model;
48  import org.apache.wicket.model.ResourceModel;
49  import org.apache.wicket.model.StringResourceModel;
50  import org.apache.wicket.model.util.ListModel;
51  
52  public class PlainAttrs extends AbstractAttrs<PlainSchemaTO> {
53  
54      private static final long serialVersionUID = 552437609667518888L;
55  
56      public <T extends AnyTO> PlainAttrs(
57              final AnyWrapper<T> modelObject,
58              final AjaxWizard.Mode mode,
59              final List<String> anyTypeClasses,
60              final List<String> whichPlainAttrs) throws IllegalArgumentException {
61  
62          super(modelObject, mode, anyTypeClasses, whichPlainAttrs);
63  
64          if (modelObject.getInnerObject() instanceof UserTO) {
65              fileKey = UserTO.class.cast(modelObject.getInnerObject()).getUsername();
66          } else if (modelObject.getInnerObject() instanceof GroupTO) {
67              fileKey = GroupTO.class.cast(modelObject.getInnerObject()).getName();
68          } else if (modelObject.getInnerObject() instanceof AnyObjectTO) {
69              fileKey = AnyObjectTO.class.cast(modelObject.getInnerObject()).getName();
70          }
71  
72          if (modelObject instanceof UserWrapper) {
73              previousObject = UserWrapper.class.cast(modelObject).getPreviousUserTO();
74          } else {
75              previousObject = null;
76          }
77  
78          setTitleModel(new ResourceModel("attributes.plain"));
79  
80          add(new Accordion("plainSchemas", List.of(new AbstractTab(
81                  new ResourceModel("attributes.accordion", "Plain Attributes")) {
82  
83              private static final long serialVersionUID = 1037272333056449378L;
84  
85              @Override
86              public WebMarkupContainer getPanel(final String panelId) {
87                  return new PlainSchemasOwn(panelId, schemas, attrs);
88              }
89          }), Model.of(0)).setOutputMarkupId(true));
90  
91          add(new ListView<>("membershipsPlainSchemas", memberships) {
92  
93              private static final long serialVersionUID = 6741044372185745296L;
94  
95              @Override
96              protected void populateItem(final ListItem<MembershipTO> item) {
97                  final MembershipTO membershipTO = item.getModelObject();
98                  item.add(new Accordion("membershipPlainSchemas", List.of(new AbstractTab(
99                          new StringResourceModel(
100                                 "attributes.membership.accordion",
101                                 PlainAttrs.this,
102                                 Model.of(membershipTO))) {
103 
104                     private static final long serialVersionUID = 1037272333056449378L;
105 
106                     @Override
107                     public WebMarkupContainer getPanel(final String panelId) {
108                         return new PlainSchemasMemberships(
109                                 panelId,
110                                 membershipSchemas.get(membershipTO.getGroupKey()),
111                                 new LoadableDetachableModel<>() { // SYNCOPE-1439
112 
113                             private static final long serialVersionUID = 526768546610546553L;
114 
115                             @Override
116                             protected Attributable load() {
117                                 return membershipTO;
118                             }
119                         });
120                     }
121                 }), Model.of(-1)).setOutputMarkupId(true));
122             }
123         });
124     }
125 
126     @Override
127     protected SchemaType getSchemaType() {
128         return SchemaType.PLAIN;
129     }
130 
131     @Override
132     protected boolean reoderSchemas() {
133         return super.reoderSchemas() && mode != AjaxWizard.Mode.TEMPLATE;
134     }
135 
136     @Override
137     protected List<Attr> getAttrsFromTO() {
138         return anyTO.getPlainAttrs().stream().sorted(attrComparator).collect(Collectors.toList());
139     }
140 
141     @Override
142     protected List<Attr> getAttrsFromTO(final MembershipTO membershipTO) {
143         return membershipTO.getPlainAttrs().stream().sorted(attrComparator).collect(Collectors.toList());
144     }
145 
146     @Override
147     protected void setAttrs() {
148         Map<String, Attr> attrMap = EntityTOUtils.buildAttrMap(anyTO.getPlainAttrs());
149 
150         List<Attr> plainAttrs = schemas.values().stream().map(schema -> {
151             Attr attr = new Attr();
152             attr.setSchema(schema.getKey());
153             if (attrMap.get(schema.getKey()) == null || attrMap.get(schema.getKey()).getValues().isEmpty()) {
154                 attr.getValues().add("");
155             } else {
156                 attr = attrMap.get(schema.getKey());
157             }
158             return attr;
159         }).collect(Collectors.toList());
160 
161         anyTO.getPlainAttrs().clear();
162         anyTO.getPlainAttrs().addAll(plainAttrs);
163     }
164 
165     @Override
166     protected void setAttrs(final MembershipTO membershipTO) {
167         Map<String, Attr> attrMap = GroupableRelatableTO.class.cast(anyTO).getMembership(membershipTO.getGroupKey()).
168                 map(gr -> EntityTOUtils.buildAttrMap(gr.getPlainAttrs())).
169                 orElseGet(() -> new HashMap<>());
170 
171         List<Attr> plainAttrs = membershipSchemas.get(membershipTO.getGroupKey()).values().stream().map(schema -> {
172             Attr attr = new Attr();
173             attr.setSchema(schema.getKey());
174             if (attrMap.get(schema.getKey()) == null || attrMap.get(schema.getKey()).getValues().isEmpty()) {
175                 attr.getValues().add(StringUtils.EMPTY);
176             } else {
177                 attr.getValues().addAll(attrMap.get(schema.getKey()).getValues());
178             }
179             return attr;
180         }).collect(Collectors.toList());
181 
182         membershipTO.getPlainAttrs().clear();
183         membershipTO.getPlainAttrs().addAll(plainAttrs);
184     }
185 
186     protected class PlainSchemasOwn extends PlainSchemas<List<Attr>> {
187 
188         private static final long serialVersionUID = -4730563859116024676L;
189 
190         public PlainSchemasOwn(
191                 final String id,
192                 final Map<String, PlainSchemaTO> schemas,
193                 final IModel<List<Attr>> attrTOs) {
194 
195             super(id);
196 
197             add(new ListView<>("schemas", attrTOs) {
198 
199                 private static final long serialVersionUID = 9101744072914090143L;
200 
201                 @Override
202                 protected void populateItem(final ListItem<Attr> item) {
203                     PlainSchemaTO schema = schemas.get(item.getModelObject().getSchema());
204                     setPanel(schemas, item, schema == null ? false : schema.isReadonly());
205                 }
206             });
207         }
208     }
209 
210     protected class PlainSchemasMemberships extends PlainSchemas<Attributable> {
211 
212         private static final long serialVersionUID = 456754923340249215L;
213 
214         public PlainSchemasMemberships(
215                 final String id,
216                 final Map<String, PlainSchemaTO> schemas,
217                 final IModel<Attributable> attributableTO) {
218 
219             super(id);
220 
221             add(new ListView<>("schemas", new ListModel<>(attributableTO.getObject().
222                     getPlainAttrs().stream().sorted(attrComparator).collect(Collectors.toList()))) {
223 
224                 private static final long serialVersionUID = 5306618783986001008L;
225 
226                 @Override
227                 protected void populateItem(final ListItem<Attr> item) {
228                     PlainSchemaTO schema = schemas.get(item.getModelObject().getSchema());
229                     setPanel(schemas, item, schema == null ? false : schema.isReadonly());
230                 }
231             });
232         }
233     }
234 }