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.commons.lang3.SerializationUtils;
27  import org.apache.syncope.client.console.SyncopeConsoleSession;
28  import org.apache.syncope.client.console.commons.AMConstants;
29  import org.apache.syncope.client.console.commons.DirectoryDataProvider;
30  import org.apache.syncope.client.console.commons.SortableDataProviderComparator;
31  import org.apache.syncope.client.console.panels.SRARouteDirectoryPanel.SRARouteProvider;
32  import org.apache.syncope.client.console.rest.SRARouteRestClient;
33  import org.apache.syncope.client.console.wicket.extensions.markup.html.repeater.data.table.BooleanPropertyColumn;
34  import org.apache.syncope.client.console.wicket.extensions.markup.html.repeater.data.table.KeyPropertyColumn;
35  import org.apache.syncope.client.console.wicket.markup.html.form.ActionLink;
36  import org.apache.syncope.client.console.wicket.markup.html.form.ActionsPanel;
37  import org.apache.syncope.client.ui.commons.Constants;
38  import org.apache.syncope.client.ui.commons.pages.BaseWebPage;
39  import org.apache.syncope.client.ui.commons.wizards.AjaxWizard;
40  import org.apache.syncope.common.lib.SyncopeClientException;
41  import org.apache.syncope.common.lib.to.SRARouteTO;
42  import org.apache.syncope.common.lib.types.AMEntitlement;
43  import org.apache.wicket.PageReference;
44  import org.apache.wicket.ajax.AjaxRequestTarget;
45  import org.apache.wicket.event.Broadcast;
46  import org.apache.wicket.extensions.markup.html.repeater.data.sort.SortOrder;
47  import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
48  import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
49  import org.apache.wicket.model.CompoundPropertyModel;
50  import org.apache.wicket.model.IModel;
51  import org.apache.wicket.model.StringResourceModel;
52  
53  public class SRARouteDirectoryPanel
54          extends DirectoryPanel<SRARouteTO, SRARouteTO, SRARouteProvider, SRARouteRestClient> {
55  
56      private static final long serialVersionUID = -2334397933375604015L;
57  
58      public SRARouteDirectoryPanel(final String id, final SRARouteRestClient restClient, final PageReference pageRef) {
59          super(id, restClient, pageRef);
60          disableCheckBoxes();
61  
62          modal.size(Modal.Size.Large);
63          modal.addSubmitButton();
64  
65          modal.setWindowClosedCallback(target -> {
66              updateResultTable(target);
67              modal.show(false);
68          });
69  
70          addNewItemPanelBuilder(new SRARouteWizardBuilder(new SRARouteTO(), restClient, pageRef), true);
71          initResultTable();
72      }
73  
74      @Override
75      protected List<IColumn<SRARouteTO, String>> getColumns() {
76          List<IColumn<SRARouteTO, String>> columns = new ArrayList<>();
77  
78          columns.add(new KeyPropertyColumn<>(
79                  new StringResourceModel(Constants.KEY_FIELD_NAME, this), Constants.KEY_FIELD_NAME));
80          columns.add(new PropertyColumn<>(
81                  new StringResourceModel(Constants.NAME_FIELD_NAME, this),
82                  Constants.NAME_FIELD_NAME, Constants.NAME_FIELD_NAME));
83          columns.add(new PropertyColumn<>(new StringResourceModel("target", this), "target", "target"));
84          columns.add(new PropertyColumn<>(new StringResourceModel("type", this), "type", "type"));
85          columns.add(new BooleanPropertyColumn<>(new StringResourceModel("logout", this), "logout", "logout"));
86          columns.add(new BooleanPropertyColumn<>(new StringResourceModel("csrf", this), "csrf", "csrf"));
87          columns.add(new PropertyColumn<>(new StringResourceModel("order", this), "order", "order"));
88  
89          return columns;
90      }
91  
92      @Override
93      protected ActionsPanel<SRARouteTO> getActions(final IModel<SRARouteTO> model) {
94          ActionsPanel<SRARouteTO> panel = super.getActions(model);
95  
96          panel.add(new ActionLink<>() {
97  
98              private static final long serialVersionUID = -4608353559809323466L;
99  
100             @Override
101             public void onClick(final AjaxRequestTarget target, final SRARouteTO ignore) {
102                 send(SRARouteDirectoryPanel.this, Broadcast.EXACT,
103                         new AjaxWizard.EditItemActionEvent<>(
104                                 restClient.read(model.getObject().getKey()), target));
105             }
106         }, ActionLink.ActionType.EDIT, AMEntitlement.SRA_ROUTE_UPDATE);
107 
108         panel.add(new ActionLink<>() {
109 
110             private static final long serialVersionUID = -4608353559809323466L;
111 
112             @Override
113             public void onClick(final AjaxRequestTarget target, final SRARouteTO ignore) {
114                 SRARouteTO clone = SerializationUtils.clone(model.getObject());
115                 clone.setKey(null);
116                 send(SRARouteDirectoryPanel.this, Broadcast.EXACT,
117                         new AjaxWizard.EditItemActionEvent<>(clone, target));
118             }
119         }, ActionLink.ActionType.CLONE, AMEntitlement.SRA_ROUTE_CREATE);
120 
121         panel.add(new ActionLink<>() {
122 
123             private static final long serialVersionUID = -4608353559809323466L;
124 
125             @Override
126             public void onClick(final AjaxRequestTarget target, final SRARouteTO ignore) {
127                 SRARouteTO route = model.getObject();
128                 try {
129                     restClient.delete(route.getKey());
130 
131                     SyncopeConsoleSession.get().success(getString(Constants.OPERATION_SUCCEEDED));
132                     target.add(container);
133                 } catch (SyncopeClientException e) {
134                     LOG.error("While deleting {}", route.getKey(), e);
135                     SyncopeConsoleSession.get().onException(e);
136                 }
137                 ((BaseWebPage) pageRef.getPage()).getNotificationPanel().refresh(target);
138             }
139         }, ActionLink.ActionType.DELETE, AMEntitlement.SRA_ROUTE_DELETE, true);
140 
141         return panel;
142     }
143 
144     @Override
145     protected Collection<ActionLink.ActionType> getBatches() {
146         return List.of();
147     }
148 
149     @Override
150     protected SRARouteProvider dataProvider() {
151         return new SRARouteProvider(rows);
152     }
153 
154     @Override
155     protected String paginatorRowsKey() {
156         return AMConstants.PREF_GATEWAYROUTE_PAGINATOR_ROWS;
157     }
158 
159     protected final class SRARouteProvider extends DirectoryDataProvider<SRARouteTO> {
160 
161         private static final long serialVersionUID = 5282134321828253058L;
162 
163         private final SortableDataProviderComparator<SRARouteTO> comparator;
164 
165         public SRARouteProvider(final int paginatorRows) {
166             super(paginatorRows);
167             setSort(Constants.NAME_FIELD_NAME, SortOrder.ASCENDING);
168             comparator = new SortableDataProviderComparator<>(this);
169         }
170 
171         @Override
172         public Iterator<? extends SRARouteTO> iterator(final long first, final long count) {
173             List<SRARouteTO> list = restClient.list();
174             list.sort(comparator);
175             return list.subList((int) first, (int) first + (int) count).iterator();
176         }
177 
178         @Override
179         public long size() {
180             return restClient.list().size();
181         }
182 
183         @Override
184         public IModel<SRARouteTO> model(final SRARouteTO route) {
185             return new CompoundPropertyModel<>(route);
186         }
187     }
188 }