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.Collections;
25  import java.util.Iterator;
26  import java.util.List;
27  import org.apache.commons.lang3.StringUtils;
28  import org.apache.syncope.client.console.SyncopeConsoleSession;
29  import org.apache.syncope.client.console.commons.DirectoryDataProvider;
30  import org.apache.syncope.client.console.commons.IdRepoConstants;
31  import org.apache.syncope.client.console.commons.SortableDataProviderComparator;
32  import org.apache.syncope.client.console.pages.BasePage;
33  import org.apache.syncope.client.console.panels.DelegationDirectoryPanel.DelegationDataProvider;
34  import org.apache.syncope.client.console.rest.DelegationRestClient;
35  import org.apache.syncope.client.console.rest.UserRestClient;
36  import org.apache.syncope.client.console.wicket.extensions.markup.html.repeater.data.table.DatePropertyColumn;
37  import org.apache.syncope.client.console.wicket.extensions.markup.html.repeater.data.table.KeyPropertyColumn;
38  import org.apache.syncope.client.console.wicket.markup.html.form.ActionLink;
39  import org.apache.syncope.client.console.wicket.markup.html.form.ActionsPanel;
40  import org.apache.syncope.client.console.wizards.WizardMgtPanel;
41  import org.apache.syncope.client.ui.commons.Constants;
42  import org.apache.syncope.client.ui.commons.wizards.AjaxWizard;
43  import org.apache.syncope.common.lib.to.DelegationTO;
44  import org.apache.syncope.common.lib.types.IdRepoEntitlement;
45  import org.apache.wicket.PageReference;
46  import org.apache.wicket.ajax.AjaxRequestTarget;
47  import org.apache.wicket.event.Broadcast;
48  import org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator;
49  import org.apache.wicket.extensions.markup.html.repeater.data.table.AbstractColumn;
50  import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
51  import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
52  import org.apache.wicket.markup.html.basic.Label;
53  import org.apache.wicket.markup.repeater.Item;
54  import org.apache.wicket.model.CompoundPropertyModel;
55  import org.apache.wicket.model.IModel;
56  import org.apache.wicket.model.ResourceModel;
57  import org.apache.wicket.model.StringResourceModel;
58  import org.apache.wicket.spring.injection.annot.SpringBean;
59  
60  public class DelegationDirectoryPanel extends
61          DirectoryPanel<DelegationTO, DelegationTO, DelegationDataProvider, DelegationRestClient> {
62  
63      private static final long serialVersionUID = 28300423726398L;
64  
65      @SpringBean
66      protected UserRestClient userRestClient;
67  
68      protected DelegationDirectoryPanel(final String id, final Builder builder) {
69          super(id, builder);
70  
71          disableCheckBoxes();
72          setShowResultPanel(true);
73  
74          modal.size(Modal.Size.Large);
75          initResultTable();
76      }
77  
78      @Override
79      protected DelegationDataProvider dataProvider() {
80          return new DelegationDataProvider(rows);
81      }
82  
83      @Override
84      protected String paginatorRowsKey() {
85          return IdRepoConstants.PREF_DELEGATION_PAGINATOR_ROWS;
86      }
87  
88      @Override
89      protected Collection<ActionLink.ActionType> getBatches() {
90          return List.of();
91      }
92  
93      @Override
94      protected List<IColumn<DelegationTO, String>> getColumns() {
95          List<IColumn<DelegationTO, String>> columns = new ArrayList<>();
96  
97          columns.add(new KeyPropertyColumn<>(
98                  new ResourceModel(Constants.KEY_FIELD_NAME), Constants.KEY_FIELD_NAME, Constants.KEY_FIELD_NAME));
99  
100         columns.add(new AbstractColumn<>(new ResourceModel("delegating"), "delegating") {
101 
102             private static final long serialVersionUID = -7835464045129401360L;
103 
104             @Override
105             public void populateItem(
106                     final Item<ICellPopulator<DelegationTO>> cellItem,
107                     final String componentId,
108                     final IModel<DelegationTO> rowModel) {
109 
110                 String delegating = rowModel.getObject().getDelegating();
111                 if (SyncopeConsoleSession.get().owns(IdRepoEntitlement.USER_READ)) {
112                     delegating = userRestClient.read(delegating).getUsername();
113                 } else if (SyncopeConsoleSession.get().getSelfTO().getKey().equals(delegating)) {
114                     delegating = SyncopeConsoleSession.get().getSelfTO().getUsername();
115                 }
116 
117                 cellItem.add(new Label(componentId, delegating));
118             }
119         });
120 
121         columns.add(new AbstractColumn<>(new ResourceModel("delegated"), "delegated") {
122 
123             private static final long serialVersionUID = -7835464045129401360L;
124 
125             @Override
126             public void populateItem(
127                     final Item<ICellPopulator<DelegationTO>> cellItem,
128                     final String componentId,
129                     final IModel<DelegationTO> rowModel) {
130 
131                 String delegated = rowModel.getObject().getDelegated();
132                 if (SyncopeConsoleSession.get().owns(IdRepoEntitlement.USER_READ)) {
133                     delegated = userRestClient.read(delegated).getUsername();
134                 } else if (SyncopeConsoleSession.get().getSelfTO().getKey().equals(delegated)) {
135                     delegated = SyncopeConsoleSession.get().getSelfTO().getUsername();
136                 }
137 
138                 cellItem.add(new Label(componentId, delegated));
139             }
140         });
141 
142         columns.add(new DatePropertyColumn<>(new StringResourceModel("start", this), "start", "start"));
143 
144         columns.add(new DatePropertyColumn<>(new StringResourceModel("end", this), "end", "end"));
145 
146         columns.add(new PropertyColumn<>(new ResourceModel("roles"), null, "roles"));
147 
148         return columns;
149     }
150 
151     @Override
152     public ActionsPanel<DelegationTO> getActions(final IModel<DelegationTO> model) {
153         ActionsPanel<DelegationTO> panel = super.getActions(model);
154 
155         panel.add(new ActionLink<>() {
156 
157             private static final long serialVersionUID = -3722207913631435501L;
158 
159             @Override
160             public void onClick(final AjaxRequestTarget target, final DelegationTO ignore) {
161                 send(DelegationDirectoryPanel.this, Broadcast.EXACT,
162                         new AjaxWizard.EditItemActionEvent<>(model.getObject(), target));
163             }
164         }, ActionLink.ActionType.EDIT, StringUtils.EMPTY);
165         panel.add(new ActionLink<>() {
166 
167             private static final long serialVersionUID = -3722207913631435501L;
168 
169             @Override
170             public void onClick(final AjaxRequestTarget target, final DelegationTO ignore) {
171                 try {
172                     restClient.delete(model.getObject().getKey());
173 
174                     SyncopeConsoleSession.get().success(getString(Constants.OPERATION_SUCCEEDED));
175                     target.add(container);
176                 } catch (Exception e) {
177                     LOG.error("While deleting {}", model.getObject().getKey(), e);
178                     SyncopeConsoleSession.get().onException(e);
179                 }
180                 ((BasePage) pageRef.getPage()).getNotificationPanel().refresh(target);
181             }
182         }, ActionLink.ActionType.DELETE, StringUtils.EMPTY, true);
183 
184         return panel;
185     }
186 
187     protected class DelegationDataProvider extends DirectoryDataProvider<DelegationTO> {
188 
189         private static final long serialVersionUID = 28297380054779L;
190 
191         private final SortableDataProviderComparator<DelegationTO> comparator;
192 
193         public DelegationDataProvider(final int paginatorRows) {
194             super(paginatorRows);
195             this.comparator = new SortableDataProviderComparator<>(this);
196         }
197 
198         @Override
199         public Iterator<DelegationTO> iterator(final long first, final long count) {
200             List<DelegationTO> result = restClient.list();
201             Collections.sort(result, comparator);
202             return result.subList((int) first, (int) first + (int) count).iterator();
203         }
204 
205         @Override
206         public long size() {
207             return restClient.list().size();
208         }
209 
210         @Override
211         public IModel<DelegationTO> model(final DelegationTO object) {
212             return new CompoundPropertyModel<>(object);
213         }
214     }
215 
216     public abstract static class Builder
217             extends DirectoryPanel.Builder<DelegationTO, DelegationTO, DelegationRestClient> {
218 
219         private static final long serialVersionUID = 5530948153889495221L;
220 
221         public Builder(final DelegationRestClient restClient, final PageReference pageRef) {
222             super(restClient, pageRef);
223         }
224 
225         @Override
226         protected WizardMgtPanel<DelegationTO> newInstance(final String id, final boolean wizardInModal) {
227             return new DelegationDirectoryPanel(id, this);
228         }
229     }
230 }