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.wicket.extensions.markup.html.repeater.data.table;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  import org.apache.syncope.common.lib.Attributable;
24  import org.apache.syncope.common.lib.types.SchemaType;
25  import org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator;
26  import org.apache.wicket.extensions.markup.html.repeater.data.table.AbstractColumn;
27  import org.apache.wicket.markup.html.basic.Label;
28  import org.apache.wicket.markup.repeater.Item;
29  import org.apache.wicket.model.IModel;
30  import org.apache.wicket.model.ResourceModel;
31  
32  public class AttrColumn<T extends Attributable> extends AbstractColumn<T, String> {
33  
34      private static final long serialVersionUID = 2624734332447371372L;
35  
36      private final String name;
37  
38      private final SchemaType schemaType;
39  
40      public AttrColumn(final String name, final String label, final SchemaType schemaType) {
41          // set sortProperty to schematype#name (e.g. derivedSchema#cn, 
42          // for use with SortableUserProviderComparator.AttrModel#getObject)
43          super(new ResourceModel(name, label), schemaType.name() + '#' + name);
44          this.name = name;
45          this.schemaType = schemaType;
46      }
47  
48      @Override
49      public void populateItem(
50              final Item<ICellPopulator<T>> cellItem, final String componentId, final IModel<T> rowModel) {
51  
52          List<String> values = new ArrayList<>();
53  
54          switch (schemaType) {
55              case PLAIN:
56                  rowModel.getObject().getPlainAttr(name).ifPresent(attr -> values.addAll(attr.getValues()));
57                  break;
58  
59              case DERIVED:
60                  rowModel.getObject().getDerAttr(name).ifPresent(attr -> values.addAll(attr.getValues()));
61                  break;
62  
63              case VIRTUAL:
64                  rowModel.getObject().getVirAttr(name).ifPresent(attr -> values.addAll(attr.getValues()));
65                  break;
66  
67              default:
68          }
69  
70          if (values.isEmpty()) {
71              cellItem.add(new Label(componentId, ""));
72          } else if (values.size() == 1) {
73              cellItem.add(new Label(componentId, values.get(0)));
74          } else {
75              cellItem.add(new Label(componentId, values.toString()));
76          }
77      }
78  }