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.commons;
20  
21  import java.util.ArrayList;
22  import java.util.LinkedHashMap;
23  import java.util.Map;
24  import java.util.stream.Collectors;
25  import org.apache.syncope.client.console.rest.PolicyRestClient;
26  import org.apache.syncope.client.console.wicket.markup.html.form.PolicyRenderer;
27  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxDropDownChoicePanel;
28  import org.apache.syncope.common.lib.policy.PolicyTO;
29  import org.apache.syncope.common.lib.to.RealmTO;
30  import org.apache.syncope.common.lib.types.PolicyType;
31  import org.apache.wicket.markup.html.form.AbstractSingleSelectChoice;
32  import org.apache.wicket.markup.repeater.RepeatingView;
33  import org.apache.wicket.model.PropertyModel;
34  import org.apache.wicket.model.ResourceModel;
35  
36  public class IdRepoRealmPolicyProvider implements RealmPolicyProvider {
37  
38      private static final long serialVersionUID = 2933826125264961282L;
39  
40      protected final PolicyRestClient policyRestClient;
41  
42      public IdRepoRealmPolicyProvider(final PolicyRestClient policyRestClient) {
43          this.policyRestClient = policyRestClient;
44      }
45  
46      @Override
47      public void add(final RealmTO realmTO, final RepeatingView view) {
48          Map<String, String> accountPolicies = policyRestClient.list(PolicyType.ACCOUNT).stream().
49                  collect(Collectors.toMap(PolicyTO::getKey, PolicyTO::getName, (v1, v2) -> v1, LinkedHashMap::new));
50          AjaxDropDownChoicePanel<String> accountPolicy = new AjaxDropDownChoicePanel<>(
51                  view.newChildId(),
52                  new ResourceModel("accountPolicy", "accountPolicy").getObject(),
53                  new PropertyModel<>(realmTO, "accountPolicy"),
54                  false);
55          accountPolicy.setChoiceRenderer(new PolicyRenderer(accountPolicies));
56          accountPolicy.setChoices(new ArrayList<>(accountPolicies.keySet()));
57          ((AbstractSingleSelectChoice<?>) accountPolicy.getField()).setNullValid(true);
58          view.add(accountPolicy);
59  
60          Map<String, String> passwordPolicies = policyRestClient.list(PolicyType.PASSWORD).stream().
61                  collect(Collectors.toMap(PolicyTO::getKey, PolicyTO::getName, (v1, v2) -> v1, LinkedHashMap::new));
62          AjaxDropDownChoicePanel<String> passwordPolicy = new AjaxDropDownChoicePanel<>(
63                  view.newChildId(),
64                  new ResourceModel("passwordPolicy", "passwordPolicy").getObject(),
65                  new PropertyModel<>(realmTO, "passwordPolicy"),
66                  false);
67          passwordPolicy.setChoiceRenderer(new PolicyRenderer(passwordPolicies));
68          passwordPolicy.setChoices(new ArrayList<>(passwordPolicies.keySet()));
69          ((AbstractSingleSelectChoice<?>) passwordPolicy.getField()).setNullValid(true);
70          view.add(passwordPolicy);
71      }
72  }