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;
20  
21  import de.agilecoders.wicket.core.markup.html.bootstrap.dialog.Modal;
22  import java.util.ArrayList;
23  import java.util.Collection;
24  import java.util.Iterator;
25  import java.util.List;
26  import org.apache.syncope.client.console.SyncopeConsoleSession;
27  import org.apache.syncope.client.console.commons.DirectoryDataProvider;
28  import org.apache.syncope.client.console.commons.IdRepoConstants;
29  import org.apache.syncope.client.console.commons.SortableDataProviderComparator;
30  import org.apache.syncope.client.console.pages.BasePage;
31  import org.apache.syncope.client.console.panels.ImplementationDirectoryPanel.ImplementationProvider;
32  import org.apache.syncope.client.console.rest.ImplementationRestClient;
33  import org.apache.syncope.client.console.wicket.markup.html.form.ActionLink;
34  import org.apache.syncope.client.console.wicket.markup.html.form.ActionLinksTogglePanel;
35  import org.apache.syncope.client.console.wicket.markup.html.form.ActionsPanel;
36  import org.apache.syncope.client.ui.commons.Constants;
37  import org.apache.syncope.common.lib.SyncopeClientException;
38  import org.apache.syncope.common.lib.to.ImplementationTO;
39  import org.apache.syncope.common.lib.types.IdRepoEntitlement;
40  import org.apache.syncope.common.lib.types.ImplementationEngine;
41  import org.apache.wicket.PageReference;
42  import org.apache.wicket.ajax.AjaxRequestTarget;
43  import org.apache.wicket.ajax.markup.html.AjaxLink;
44  import org.apache.wicket.authroles.authorization.strategies.role.metadata.MetaDataRoleAuthorizationStrategy;
45  import org.apache.wicket.event.Broadcast;
46  import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
47  import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
48  import org.apache.wicket.markup.html.WebMarkupContainer;
49  import org.apache.wicket.model.IModel;
50  import org.apache.wicket.model.Model;
51  import org.apache.wicket.model.StringResourceModel;
52  
53  public class ImplementationDirectoryPanel extends DirectoryPanel<
54          ImplementationTO, ImplementationTO, ImplementationProvider, ImplementationRestClient> {
55  
56      private static final long serialVersionUID = 1868839768348072635L;
57  
58      private final String type;
59  
60      public ImplementationDirectoryPanel(
61              final String id,
62              final String type,
63              final ImplementationRestClient restClient,
64              final PageReference pageRef) {
65  
66          super(id, restClient, pageRef, true);
67          this.type = type;
68  
69          ImplementationTO implementation = new ImplementationTO();
70          implementation.setType(type);
71  
72          disableCheckBoxes();
73  
74          modal.size(Modal.Size.Large);
75          modal.addSubmitButton();
76          modal.setWindowClosedCallback(target -> {
77              if (actionTogglePanel.isVisibleInHierarchy()) {
78                  actionTogglePanel.toggle(target, false);
79              }
80  
81              implementation.setEngine(null);
82              updateResultTable(target);
83              modal.show(false);
84          });
85          setFooterVisibility(true);
86  
87          initResultTable();
88  
89          ImplementationEngineTogglePanel engineTogglePanel =
90                  new ImplementationEngineTogglePanel("engineTogglePanel", implementation, pageRef) {
91  
92              private static final long serialVersionUID = -112426445257072782L;
93  
94              @Override
95              protected void onSubmit(final ImplementationEngine engine, final AjaxRequestTarget target) {
96                  implementation.setKey(null);
97                  implementation.setBody(null);
98  
99                  target.add(ImplementationDirectoryPanel.this.modal.setContent(new ImplementationModalPanel(
100                         ImplementationDirectoryPanel.this.modal, implementation, pageRef)));
101                 ImplementationDirectoryPanel.this.modal.header(
102                         new StringResourceModel("any.new", Model.of(implementation)));
103                 ImplementationDirectoryPanel.this.modal.show(true);
104             }
105         };
106         addInnerObject(engineTogglePanel);
107 
108         AjaxLink<Void> replaceAddLink = new AjaxLink<>("add") {
109 
110             private static final long serialVersionUID = -7978723352517770644L;
111 
112             @Override
113             public void onClick(final AjaxRequestTarget target) {
114                 send(ImplementationDirectoryPanel.this, Broadcast.BREADTH,
115                         new ActionLinksTogglePanel.ActionLinkToggleCloseEventPayload(target));
116                 engineTogglePanel.setHeaderLabel(target);
117                 engineTogglePanel.toggle(target, true);
118             }
119         };
120         ((WebMarkupContainer) get("container:content")).addOrReplace(replaceAddLink);
121 
122         MetaDataRoleAuthorizationStrategy.authorize(addAjaxLink, RENDER, IdRepoEntitlement.IMPLEMENTATION_CREATE);
123     }
124 
125     @Override
126     protected List<IColumn<ImplementationTO, String>> getColumns() {
127         List<IColumn<ImplementationTO, String>> columns = new ArrayList<>();
128 
129         columns.add(new PropertyColumn<>(
130                 new StringResourceModel(Constants.KEY_FIELD_NAME, this),
131                 Constants.KEY_FIELD_NAME, Constants.KEY_FIELD_NAME));
132         columns.add(new PropertyColumn<>(new StringResourceModel("engine", this), "engine", "engine"));
133 
134         return columns;
135     }
136 
137     @Override
138     protected ActionsPanel<ImplementationTO> getActions(final IModel<ImplementationTO> model) {
139         final ActionsPanel<ImplementationTO> panel = super.getActions(model);
140 
141         panel.add(new ActionLink<>() {
142 
143             private static final long serialVersionUID = -7978723352517770645L;
144 
145             @Override
146             public void onClick(final AjaxRequestTarget target, final ImplementationTO ignore) {
147                 target.add(modal.setContent(
148                         new ImplementationModalPanel(modal, model.getObject(), pageRef)));
149                 modal.header(new StringResourceModel("any.edit", Model.of(model.getObject())));
150                 modal.show(true);
151             }
152         }, ActionLink.ActionType.EDIT, IdRepoEntitlement.IMPLEMENTATION_UPDATE);
153 
154         panel.add(new ActionLink<>() {
155 
156             private static final long serialVersionUID = -3722207913631435501L;
157 
158             @Override
159             public void onClick(final AjaxRequestTarget target, final ImplementationTO ignore) {
160                 try {
161                     restClient.delete(model.getObject().getType(), model.getObject().getKey());
162 
163                     SyncopeConsoleSession.get().success(getString(Constants.OPERATION_SUCCEEDED));
164                     target.add(container);
165                 } catch (SyncopeClientException e) {
166                     LOG.error("While deleting object {}", model.getObject().getKey(), e);
167                     SyncopeConsoleSession.get().onException(e);
168                 }
169                 ((BasePage) pageRef.getPage()).getNotificationPanel().refresh(target);
170             }
171         }, ActionLink.ActionType.DELETE, IdRepoEntitlement.IMPLEMENTATION_DELETE, true);
172 
173         return panel;
174     }
175 
176     @Override
177     protected ImplementationProvider dataProvider() {
178         return new ImplementationProvider(rows);
179     }
180 
181     @Override
182     protected String paginatorRowsKey() {
183         return IdRepoConstants.PREF_IMPLEMENTATION_PAGINATOR_ROWS;
184     }
185 
186     @Override
187     protected Collection<ActionLink.ActionType> getBatches() {
188         return List.of();
189     }
190 
191     protected class ImplementationProvider extends DirectoryDataProvider<ImplementationTO> {
192 
193         private static final long serialVersionUID = 8594921866993979224L;
194 
195         private final SortableDataProviderComparator<ImplementationTO> comparator;
196 
197         public ImplementationProvider(final int paginatorRows) {
198             super(paginatorRows);
199             comparator = new SortableDataProviderComparator<>(this);
200         }
201 
202         @Override
203         public Iterator<ImplementationTO> iterator(final long first, final long count) {
204             List<ImplementationTO> implementations = restClient.list(type);
205             implementations.sort(comparator);
206             return implementations.subList((int) first, (int) first + (int) count).iterator();
207         }
208 
209         @Override
210         public long size() {
211             return restClient.list(type).size();
212         }
213 
214         @Override
215         public IModel<ImplementationTO> model(final ImplementationTO implementation) {
216             return new IModel<>() {
217 
218                 private static final long serialVersionUID = 999513782683391483L;
219 
220                 @Override
221                 public ImplementationTO getObject() {
222                     return implementation;
223                 }
224             };
225         }
226     }
227 }