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              implementation.setEngine(null);
78              updateResultTable(target);
79              modal.show(false);
80          });
81          setFooterVisibility(true);
82  
83          initResultTable();
84  
85          ImplementationEngineTogglePanel engineTogglePanel =
86                  new ImplementationEngineTogglePanel("engineTogglePanel", implementation, pageRef) {
87  
88              private static final long serialVersionUID = -112426445257072782L;
89  
90              @Override
91              protected void onSubmit(final ImplementationEngine engine, final AjaxRequestTarget target) {
92                  implementation.setKey(null);
93                  implementation.setBody(null);
94  
95                  target.add(ImplementationDirectoryPanel.this.modal.setContent(new ImplementationModalPanel(
96                          ImplementationDirectoryPanel.this.modal, implementation, pageRef)));
97                  ImplementationDirectoryPanel.this.modal.header(
98                          new StringResourceModel("any.new", Model.of(implementation)));
99                  ImplementationDirectoryPanel.this.modal.show(true);
100             }
101         };
102         addInnerObject(engineTogglePanel);
103 
104         AjaxLink<Void> replaceAddLink = new AjaxLink<>("add") {
105 
106             private static final long serialVersionUID = -7978723352517770644L;
107 
108             @Override
109             public void onClick(final AjaxRequestTarget target) {
110                 send(ImplementationDirectoryPanel.this, Broadcast.BREADTH,
111                         new ActionLinksTogglePanel.ActionLinkToggleCloseEventPayload(target));
112                 engineTogglePanel.setHeaderLabel(target);
113                 engineTogglePanel.toggle(target, true);
114             }
115         };
116         ((WebMarkupContainer) get("container:content")).addOrReplace(replaceAddLink);
117 
118         MetaDataRoleAuthorizationStrategy.authorize(addAjaxLink, RENDER, IdRepoEntitlement.IMPLEMENTATION_CREATE);
119     }
120 
121     @Override
122     protected List<IColumn<ImplementationTO, String>> getColumns() {
123         List<IColumn<ImplementationTO, String>> columns = new ArrayList<>();
124 
125         columns.add(new PropertyColumn<>(
126                 new StringResourceModel(Constants.KEY_FIELD_NAME, this),
127                 Constants.KEY_FIELD_NAME, Constants.KEY_FIELD_NAME));
128         columns.add(new PropertyColumn<>(new StringResourceModel("engine", this), "engine", "engine"));
129 
130         return columns;
131     }
132 
133     @Override
134     protected ActionsPanel<ImplementationTO> getActions(final IModel<ImplementationTO> model) {
135         final ActionsPanel<ImplementationTO> panel = super.getActions(model);
136 
137         panel.add(new ActionLink<>() {
138 
139             private static final long serialVersionUID = -7978723352517770645L;
140 
141             @Override
142             public void onClick(final AjaxRequestTarget target, final ImplementationTO ignore) {
143                 target.add(modal.setContent(
144                         new ImplementationModalPanel(modal, model.getObject(), pageRef)));
145                 modal.header(new StringResourceModel("any.edit", Model.of(model.getObject())));
146                 modal.show(true);
147             }
148         }, ActionLink.ActionType.EDIT, IdRepoEntitlement.IMPLEMENTATION_UPDATE);
149 
150         panel.add(new ActionLink<>() {
151 
152             private static final long serialVersionUID = -3722207913631435501L;
153 
154             @Override
155             public void onClick(final AjaxRequestTarget target, final ImplementationTO ignore) {
156                 try {
157                     restClient.delete(model.getObject().getType(), model.getObject().getKey());
158 
159                     SyncopeConsoleSession.get().success(getString(Constants.OPERATION_SUCCEEDED));
160                     target.add(container);
161                 } catch (SyncopeClientException e) {
162                     LOG.error("While deleting object {}", model.getObject().getKey(), e);
163                     SyncopeConsoleSession.get().onException(e);
164                 }
165                 ((BasePage) pageRef.getPage()).getNotificationPanel().refresh(target);
166             }
167         }, ActionLink.ActionType.DELETE, IdRepoEntitlement.IMPLEMENTATION_DELETE, true);
168 
169         return panel;
170     }
171 
172     @Override
173     protected ImplementationProvider dataProvider() {
174         return new ImplementationProvider(rows);
175     }
176 
177     @Override
178     protected String paginatorRowsKey() {
179         return IdRepoConstants.PREF_IMPLEMENTATION_PAGINATOR_ROWS;
180     }
181 
182     @Override
183     protected Collection<ActionLink.ActionType> getBatches() {
184         return List.of();
185     }
186 
187     protected class ImplementationProvider extends DirectoryDataProvider<ImplementationTO> {
188 
189         private static final long serialVersionUID = 8594921866993979224L;
190 
191         private final SortableDataProviderComparator<ImplementationTO> comparator;
192 
193         public ImplementationProvider(final int paginatorRows) {
194             super(paginatorRows);
195             comparator = new SortableDataProviderComparator<>(this);
196         }
197 
198         @Override
199         public Iterator<ImplementationTO> iterator(final long first, final long count) {
200             List<ImplementationTO> implementations = restClient.list(type);
201             implementations.sort(comparator);
202             return implementations.subList((int) first, (int) first + (int) count).iterator();
203         }
204 
205         @Override
206         public long size() {
207             return restClient.list(type).size();
208         }
209 
210         @Override
211         public IModel<ImplementationTO> model(final ImplementationTO implementation) {
212             return new IModel<>() {
213 
214                 private static final long serialVersionUID = 999513782683391483L;
215 
216                 @Override
217                 public ImplementationTO getObject() {
218                     return implementation;
219                 }
220             };
221         }
222     }
223 }