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.pages.BasePage;
30  import org.apache.syncope.client.console.panels.NetworkServiceDirectoryPanel.NetworkServiceProvider;
31  import org.apache.syncope.client.console.rest.SyncopeRestClient;
32  import org.apache.syncope.client.console.wicket.markup.html.form.ActionLink;
33  import org.apache.syncope.client.console.wicket.markup.html.form.ActionsPanel;
34  import org.apache.syncope.common.keymaster.client.api.ServiceOps;
35  import org.apache.syncope.common.keymaster.client.api.model.NetworkService;
36  import org.apache.syncope.common.lib.types.IdRepoEntitlement;
37  import org.apache.wicket.PageReference;
38  import org.apache.wicket.ajax.AjaxRequestTarget;
39  import org.apache.wicket.extensions.markup.html.repeater.data.sort.SortOrder;
40  import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
41  import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
42  import org.apache.wicket.model.IModel;
43  import org.apache.wicket.model.StringResourceModel;
44  import org.apache.wicket.spring.injection.annot.SpringBean;
45  
46  public class NetworkServiceDirectoryPanel extends DirectoryPanel<
47          NetworkService, NetworkService, NetworkServiceProvider, SyncopeRestClient> {
48  
49      private static final long serialVersionUID = 1868839768348072635L;
50  
51      @SpringBean
52      protected ServiceOps serviceOps;
53  
54      private final NetworkService.Type type;
55  
56      public NetworkServiceDirectoryPanel(
57              final String id,
58              final NetworkService.Type type,
59              final SyncopeRestClient syncopeRestClient,
60              final PageReference pageRef) {
61          super(id, syncopeRestClient, pageRef, true);
62          this.type = type;
63  
64          NetworkService service = new NetworkService();
65          service.setType(type);
66  
67          disableCheckBoxes();
68  
69          modal.size(Modal.Size.Large);
70          modal.addSubmitButton();
71          modal.setWindowClosedCallback(target -> {
72              updateResultTable(target);
73              modal.show(false);
74          });
75          setFooterVisibility(true);
76  
77          initResultTable();
78      }
79  
80      @Override
81      protected List<IColumn<NetworkService, String>> getColumns() {
82          List<IColumn<NetworkService, String>> columns = new ArrayList<>();
83  
84          columns.add(new PropertyColumn<>(new StringResourceModel("address", this), "address", "address"));
85  
86          return columns;
87      }
88  
89      @Override
90      protected ActionsPanel<NetworkService> getActions(final IModel<NetworkService> model) {
91          ActionsPanel<NetworkService> panel = super.getActions(model);
92  
93          panel.add(new ActionLink<>() {
94  
95              private static final long serialVersionUID = -3722207913631435501L;
96  
97              @Override
98              public void onClick(final AjaxRequestTarget target, final NetworkService ignore) {
99                  try {
100                     serviceOps.unregister(model.getObject());
101                     target.add(container);
102                 } catch (Exception e) {
103                     LOG.error("While deleting {}", model.getObject().getAddress(), e);
104                     SyncopeConsoleSession.get().onException(e);
105                 }
106                 ((BasePage) pageRef.getPage()).getNotificationPanel().refresh(target);
107             }
108         }, ActionLink.ActionType.DELETE, IdRepoEntitlement.KEYMASTER, true);
109 
110         return panel;
111     }
112 
113     @Override
114     protected NetworkServiceProvider dataProvider() {
115         return new NetworkServiceProvider(rows);
116     }
117 
118     @Override
119     protected String paginatorRowsKey() {
120         return IdRepoConstants.PREF_NETWORK_SERVICE_PAGINATOR_ROWS;
121     }
122 
123     @Override
124     protected Collection<ActionLink.ActionType> getBatches() {
125         return List.of();
126     }
127 
128     protected class NetworkServiceProvider extends DirectoryDataProvider<NetworkService> {
129 
130         private static final long serialVersionUID = 8594921866993979224L;
131 
132         public NetworkServiceProvider(final int paginatorRows) {
133             super(paginatorRows);
134 
135             setSort("address", SortOrder.ASCENDING);
136         }
137 
138         @Override
139         public Iterator<NetworkService> iterator(final long first, final long count) {
140             List<NetworkService> list = serviceOps.list(type);
141             return list.subList((int) first, (int) first + (int) count).iterator();
142         }
143 
144         @Override
145         public long size() {
146             return serviceOps.list(type).size();
147         }
148 
149         @Override
150         public IModel<NetworkService> model(final NetworkService service) {
151             return new IModel<>() {
152 
153                 private static final long serialVersionUID = 999513782683391483L;
154 
155                 @Override
156                 public NetworkService getObject() {
157                     return service;
158                 }
159             };
160         }
161     }
162 }