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.clientapps;
20  
21  import java.util.List;
22  import org.apache.syncope.client.console.SyncopeConsoleSession;
23  import org.apache.syncope.client.console.commons.AMConstants;
24  import org.apache.syncope.client.console.panels.AttrListDirectoryPanel;
25  import org.apache.syncope.client.console.rest.ClientAppRestClient;
26  import org.apache.syncope.client.console.wicket.markup.html.bootstrap.dialog.BaseModal;
27  import org.apache.syncope.client.console.wicket.markup.html.form.ActionLink;
28  import org.apache.syncope.client.console.wicket.markup.html.form.ActionsPanel;
29  import org.apache.syncope.client.ui.commons.Constants;
30  import org.apache.syncope.client.ui.commons.pages.BaseWebPage;
31  import org.apache.syncope.client.ui.commons.wizards.AjaxWizard;
32  import org.apache.syncope.client.ui.commons.wizards.AjaxWizard.EditItemActionEvent;
33  import org.apache.syncope.common.lib.Attr;
34  import org.apache.syncope.common.lib.to.ClientAppTO;
35  import org.apache.syncope.common.lib.types.AMEntitlement;
36  import org.apache.syncope.common.lib.types.ClientAppType;
37  import org.apache.wicket.PageReference;
38  import org.apache.wicket.ajax.AjaxRequestTarget;
39  import org.apache.wicket.event.Broadcast;
40  import org.apache.wicket.event.IEvent;
41  import org.apache.wicket.model.IModel;
42  
43  public class ClientAppPropertiesDirectoryPanel<T extends ClientAppTO> extends AttrListDirectoryPanel {
44  
45      private static final long serialVersionUID = 9072805604972532678L;
46  
47      protected final BaseModal<T> propertiesModal;
48  
49      protected final ClientAppType type;
50  
51      protected final IModel<T> model;
52  
53      public ClientAppPropertiesDirectoryPanel(
54              final String id,
55              final ClientAppRestClient restClient,
56              final BaseModal<T> propertiesModal,
57              final ClientAppType type,
58              final IModel<T> model,
59              final PageReference pageRef) {
60  
61          super(id, restClient, pageRef, false);
62  
63          this.propertiesModal = propertiesModal;
64          this.type = type;
65          this.model = model;
66  
67          setOutputMarkupId(true);
68  
69          enableUtilityButton();
70          setFooterVisibility(false);
71  
72          addNewItemPanelBuilder(new ClientAppPropertyWizardBuilder(
73                  type, model.getObject(), new Attr(), restClient, pageRef), true);
74  
75          initResultTable();
76      }
77  
78      @Override
79      public void onEvent(final IEvent<?> event) {
80          if (event.getPayload() instanceof ExitEvent) {
81              AjaxRequestTarget target = ExitEvent.class.cast(event.getPayload()).getTarget();
82              propertiesModal.close(target);
83          } else if (event.getPayload() instanceof EditItemActionEvent) {
84              @SuppressWarnings("unchecked")
85              EditItemActionEvent<T> payload = (EditItemActionEvent<T>) event.getPayload();
86              payload.getTarget().ifPresent(actionTogglePanel::close);
87          }
88          super.onEvent(event);
89      }
90  
91      @Override
92      protected AttrListProvider dataProvider() {
93          return new ClientAppPropertiesProvider(rows);
94      }
95  
96      @Override
97      protected String paginatorRowsKey() {
98          return AMConstants.PREF_CLIENTAPP_PROPERTIES_PAGINATOR_ROWS;
99      }
100 
101     @Override
102     protected ActionsPanel<Attr> getActions(final IModel<Attr> model) {
103         ActionsPanel<Attr> panel = super.getActions(model);
104 
105         panel.add(new ActionLink<>() {
106 
107             private static final long serialVersionUID = -3722207913631435501L;
108 
109             @Override
110             public void onClick(final AjaxRequestTarget target, final Attr ignore) {
111                 send(ClientAppPropertiesDirectoryPanel.this, Broadcast.EXACT,
112                         new AjaxWizard.EditItemActionEvent<>(model.getObject(), target));
113             }
114         }, ActionLink.ActionType.EDIT, AMEntitlement.CLIENTAPP_UPDATE);
115 
116         panel.add(new ActionLink<>() {
117 
118             private static final long serialVersionUID = -3722207913631435501L;
119 
120             @Override
121             public void onClick(final AjaxRequestTarget target, final Attr ignore) {
122                 try {
123                     ClientAppPropertiesDirectoryPanel.this.model.getObject().getProperties().remove(model.getObject());
124                     ((ClientAppRestClient) restClient).update(
125                             type, ClientAppPropertiesDirectoryPanel.this.model.getObject());
126 
127                     SyncopeConsoleSession.get().success(getString(Constants.OPERATION_SUCCEEDED));
128                     target.add(container);
129                 } catch (Exception e) {
130                     LOG.error("While deleting {}", model.getObject().getSchema(), e);
131                     SyncopeConsoleSession.get().onException(e);
132                 }
133                 ((BaseWebPage) pageRef.getPage()).getNotificationPanel().refresh(target);
134             }
135         }, ActionLink.ActionType.DELETE, AMEntitlement.CLIENTAPP_UPDATE, true);
136 
137         return panel;
138     }
139 
140     protected final class ClientAppPropertiesProvider extends AttrListProvider {
141 
142         private static final long serialVersionUID = -185944053385660794L;
143 
144         private ClientAppPropertiesProvider(final int paginatorRows) {
145             super(paginatorRows);
146         }
147 
148         @Override
149         protected List<Attr> list() {
150             return model.getObject().getProperties();
151         }
152     }
153 }