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.panels.search;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  import java.util.Map;
24  import org.apache.commons.lang3.StringUtils;
25  import org.apache.wicket.core.util.lang.PropertyResolver;
26  import org.apache.wicket.model.PropertyModel;
27  
28  public class MapOfListModel<T> extends PropertyModel<List<T>> {
29  
30      private static final long serialVersionUID = -7647997536634092231L;
31  
32      private final String key;
33  
34      public MapOfListModel(final Object modelObject, final String expression, final String key) {
35          super(modelObject, expression);
36          this.key = key;
37      }
38  
39      @Override
40      @SuppressWarnings("unchecked")
41      public List<T> getObject() {
42          final String expression = propertyExpression();
43          final Object target = getInnermostModelOrObject();
44  
45          if (target == null || StringUtils.isBlank(expression) || expression.startsWith(".")) {
46              throw new IllegalArgumentException("Property expressions cannot start with a '.' character");
47          }
48  
49          Map<String, List<T>> map = (Map<String, List<T>>) PropertyResolver.getValue(expression, target);
50  
51          List<T> res;
52          if (map.containsKey(key)) {
53              res = map.get(key);
54          } else {
55              res = new ArrayList<>();
56              map.put(key, res);
57          }
58          return res;
59      }
60  
61      @Override
62      @SuppressWarnings("unchecked")
63      public void setObject(final List<T> object) {
64          final String expression = propertyExpression();
65          final Object target = getInnermostModelOrObject();
66          ((Map<String, List<T>>) PropertyResolver.getValue(expression, target)).put(key, object);
67      }
68  }