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.policies;
20  
21  import java.util.List;
22  import java.util.concurrent.TimeUnit;
23  import java.util.stream.Collectors;
24  import org.apache.syncope.client.console.SyncopeConsoleSession;
25  import org.apache.syncope.client.console.panels.AbstractModalPanel;
26  import org.apache.syncope.client.console.rest.AttrRepoRestClient;
27  import org.apache.syncope.client.console.rest.PolicyRestClient;
28  import org.apache.syncope.client.console.wicket.markup.html.bootstrap.dialog.BaseModal;
29  import org.apache.syncope.client.console.wicket.markup.html.form.MultiFieldPanel;
30  import org.apache.syncope.client.ui.commons.Constants;
31  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxCheckBoxPanel;
32  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxDropDownChoicePanel;
33  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxGridFieldPanel;
34  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxPalettePanel;
35  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxSpinnerFieldPanel;
36  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxTextFieldPanel;
37  import org.apache.syncope.client.ui.commons.pages.BaseWebPage;
38  import org.apache.syncope.common.lib.policy.AttrReleasePolicyTO;
39  import org.apache.syncope.common.lib.policy.DefaultAttrReleasePolicyConf.PrincipalAttrRepoMergingStrategy;
40  import org.apache.syncope.common.lib.to.AttrRepoTO;
41  import org.apache.syncope.common.lib.types.PolicyType;
42  import org.apache.wicket.PageReference;
43  import org.apache.wicket.ajax.AjaxRequestTarget;
44  import org.apache.wicket.model.IModel;
45  import org.apache.wicket.model.LoadableDetachableModel;
46  import org.apache.wicket.model.Model;
47  import org.apache.wicket.model.PropertyModel;
48  import org.apache.wicket.spring.injection.annot.SpringBean;
49  
50  public class AttrReleasePolicyModalPanel extends AbstractModalPanel<AttrReleasePolicyTO> {
51  
52      private static final long serialVersionUID = 2668291404983623500L;
53  
54      @SpringBean
55      protected PolicyRestClient policyRestClient;
56  
57      @SpringBean
58      protected AttrRepoRestClient attrRepoRestClient;
59  
60      protected final IModel<List<String>> allAttrRepos = new LoadableDetachableModel<>() {
61  
62          private static final long serialVersionUID = -2012833443695917883L;
63  
64          @Override
65          protected List<String> load() {
66              return attrRepoRestClient.list().stream().map(AttrRepoTO::getKey).sorted().collect(Collectors.toList());
67          }
68      };
69  
70      protected final IModel<AttrReleasePolicyTO> model;
71  
72      public AttrReleasePolicyModalPanel(
73              final BaseModal<AttrReleasePolicyTO> modal,
74              final IModel<AttrReleasePolicyTO> model,
75              final PageReference pageRef) {
76  
77          super(modal, pageRef);
78          this.model = model;
79  
80          add(new AjaxGridFieldPanel<>(
81                  "releaseAttrs", "releaseAttrs",
82                  new PropertyModel<>(model.getObject().getConf(), "releaseAttrs")));
83  
84          AjaxTextFieldPanel allowedAttr = new AjaxTextFieldPanel("panel", "allowedAttrs", new Model<>());
85          add(new MultiFieldPanel.Builder<String>(
86                  new PropertyModel<>(model.getObject().getConf(), "allowedAttrs")).build(
87                  "allowedAttrs",
88                  "allowedAttrs",
89                  allowedAttr));
90  
91          AjaxTextFieldPanel excludedAttr = new AjaxTextFieldPanel("panel", "excludedAttrs", new Model<>());
92          add(new MultiFieldPanel.Builder<String>(
93                  new PropertyModel<>(model.getObject().getConf(), "excludedAttrs")).build(
94                  "excludedAttrs",
95                  "excludedAttrs",
96                  excludedAttr));
97  
98          AjaxTextFieldPanel includeOnlyAttr = new AjaxTextFieldPanel("panel", "includeOnlyAttrs", new Model<>());
99          add(new MultiFieldPanel.Builder<String>(
100                 new PropertyModel<>(model.getObject().getConf(), "includeOnlyAttrs")).build(
101                 "includeOnlyAttrs",
102                 "includeOnlyAttrs",
103                 includeOnlyAttr));
104 
105         add(new AjaxTextFieldPanel(
106                 "principalIdAttr", "principalIdAttr",
107                 new PropertyModel<>(model.getObject().getConf(), "principalIdAttr")));
108 
109         AjaxDropDownChoicePanel<PrincipalAttrRepoMergingStrategy> mergingStrategy = new AjaxDropDownChoicePanel<>(
110                 "mergingStrategy", "mergingStrategy",
111                 new PropertyModel<>(model.getObject().getConf(), "principalAttrRepoConf.mergingStrategy"));
112         mergingStrategy.setChoices(List.of(PrincipalAttrRepoMergingStrategy.values()));
113         mergingStrategy.addRequiredLabel();
114         mergingStrategy.setNullValid(false);
115         add(mergingStrategy);
116 
117         add(new AjaxCheckBoxPanel(
118                 "ignoreResolvedAttributes",
119                 "ignoreResolvedAttributes",
120                 new PropertyModel<>(model.getObject().getConf(), "principalAttrRepoConf.ignoreResolvedAttributes"),
121                 false));
122 
123         add(new AjaxSpinnerFieldPanel.Builder<Long>().build(
124                 "expiration",
125                 "expiration",
126                 Long.class,
127                 new PropertyModel<>(model.getObject().getConf(), "principalAttrRepoConf.expiration")));
128 
129         AjaxDropDownChoicePanel<TimeUnit> timeUnit = new AjaxDropDownChoicePanel<>(
130                 "timeUnit", "timeUnit",
131                 new PropertyModel<>(model.getObject().getConf(), "principalAttrRepoConf.timeUnit"));
132         timeUnit.setChoices(List.of(TimeUnit.values()));
133         timeUnit.addRequiredLabel();
134         timeUnit.setNullValid(false);
135         add(timeUnit);
136 
137         add(new AjaxPalettePanel.Builder<String>().setName("attrRepos").build(
138                 "attrRepos",
139                 new PropertyModel<>(model.getObject().getConf(), "principalAttrRepoConf.attrRepos"),
140                 allAttrRepos));
141     }
142 
143     @Override
144     public void onSubmit(final AjaxRequestTarget target) {
145         try {
146             policyRestClient.update(PolicyType.ATTR_RELEASE, model.getObject());
147 
148             SyncopeConsoleSession.get().success(getString(Constants.OPERATION_SUCCEEDED));
149             modal.close(target);
150         } catch (Exception e) {
151             LOG.error("While updating Attribute Release Policy {}", model.getObject().getKey(), e);
152             SyncopeConsoleSession.get().onException(e);
153         }
154         ((BaseWebPage) pageRef.getPage()).getNotificationPanel().refresh(target);
155     }
156 }