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.io.Serializable;
22  import org.apache.syncope.client.console.SyncopeConsoleSession;
23  import org.apache.syncope.client.console.panels.AbstractModalPanel;
24  import org.apache.syncope.client.console.panels.BeanPanel;
25  import org.apache.syncope.client.console.rest.PolicyRestClient;
26  import org.apache.syncope.client.console.wicket.markup.html.bootstrap.dialog.BaseModal;
27  import org.apache.syncope.client.ui.commons.Constants;
28  import org.apache.syncope.client.ui.commons.ajax.form.IndicatorAjaxFormComponentUpdatingBehavior;
29  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxCheckBoxPanel;
30  import org.apache.syncope.client.ui.commons.pages.BaseWebPage;
31  import org.apache.syncope.common.lib.policy.DefaultTicketExpirationPolicyConf;
32  import org.apache.syncope.common.lib.policy.TicketExpirationPolicyTO;
33  import org.apache.syncope.common.lib.types.PolicyType;
34  import org.apache.wicket.PageReference;
35  import org.apache.wicket.ajax.AjaxRequestTarget;
36  import org.apache.wicket.model.IModel;
37  import org.apache.wicket.model.PropertyModel;
38  import org.apache.wicket.spring.injection.annot.SpringBean;
39  
40  public class TicketExpirationPolicyModalPanel extends AbstractModalPanel<TicketExpirationPolicyTO> {
41  
42      private static final long serialVersionUID = 2668291404983623500L;
43  
44      @SpringBean
45      protected PolicyRestClient policyRestClient;
46  
47      protected final IModel<TicketExpirationPolicyTO> model;
48  
49      public TicketExpirationPolicyModalPanel(
50              final BaseModal<TicketExpirationPolicyTO> modal,
51              final IModel<TicketExpirationPolicyTO> model,
52              final PageReference pageRef) {
53  
54          super(modal, pageRef);
55          this.model = model;
56  
57          conf("tgtConf", new DefaultTicketExpirationPolicyConf.TGTConf());
58          conf("stConf", new DefaultTicketExpirationPolicyConf.STConf());
59          conf("proxyTgtConf", new DefaultTicketExpirationPolicyConf.TGTConf());
60          conf("proxyStConf", new DefaultTicketExpirationPolicyConf.STConf());
61      }
62  
63      private <T extends Serializable> void conf(final String field, final T newInstance) {
64          PropertyModel<T> beanPanelModel = new PropertyModel<>(model.getObject(), "conf." + field);
65  
66          AjaxCheckBoxPanel enable = new AjaxCheckBoxPanel("enable." + field, "enable." + field, new IModel<Boolean>() {
67  
68              private static final long serialVersionUID = -7126718045816207110L;
69  
70              @Override
71              public Boolean getObject() {
72                  return beanPanelModel.getObject() != null;
73              }
74  
75              @Override
76              public void setObject(final Boolean object) {
77                  // nothing to do
78              }
79          });
80          enable.getField().add(new IndicatorAjaxFormComponentUpdatingBehavior(Constants.ON_CHANGE) {
81  
82              private static final long serialVersionUID = -1107858522700306810L;
83  
84              @Override
85              protected void onUpdate(final AjaxRequestTarget target) {
86                  if (beanPanelModel.getObject() == null) {
87                      beanPanelModel.setObject(newInstance);
88                  } else {
89                      beanPanelModel.setObject(null);
90                  }
91                  target.add(TicketExpirationPolicyModalPanel.this);
92              }
93          });
94          add(enable);
95  
96          add(new BeanPanel<>("bean." + field, beanPanelModel, pageRef).setRenderBodyOnly(true));
97      }
98  
99      @Override
100     public void onSubmit(final AjaxRequestTarget target) {
101         try {
102             policyRestClient.update(PolicyType.TICKET_EXPIRATION, model.getObject());
103 
104             SyncopeConsoleSession.get().success(getString(Constants.OPERATION_SUCCEEDED));
105             modal.close(target);
106         } catch (Exception e) {
107             LOG.error("While updating Ticket Expiration Policy {}", model.getObject().getKey(), e);
108             SyncopeConsoleSession.get().onException(e);
109         }
110         ((BaseWebPage) pageRef.getPage()).getNotificationPanel().refresh(target);
111     }
112 }