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.DynRealmDirectoryPanel.DynRealmDataProvider;
32  import org.apache.syncope.client.console.rest.DynRealmRestClient;
33  import org.apache.syncope.client.console.wicket.markup.html.form.ActionLink;
34  import org.apache.syncope.client.console.wicket.markup.html.form.ActionsPanel;
35  import org.apache.syncope.client.console.wizards.DynRealmWrapper;
36  import org.apache.syncope.client.console.wizards.WizardMgtPanel;
37  import org.apache.syncope.client.ui.commons.Constants;
38  import org.apache.syncope.common.lib.SyncopeClientException;
39  import org.apache.syncope.common.lib.to.DynRealmTO;
40  import org.apache.syncope.common.lib.types.IdRepoEntitlement;
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.extensions.markup.html.repeater.data.table.IColumn;
46  import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
47  import org.apache.wicket.markup.html.WebMarkupContainer;
48  import org.apache.wicket.model.CompoundPropertyModel;
49  import org.apache.wicket.model.IModel;
50  import org.apache.wicket.model.ResourceModel;
51  import org.apache.wicket.model.StringResourceModel;
52  
53  public class DynRealmDirectoryPanel extends
54          DirectoryPanel<DynRealmTO, DynRealmWrapper, DynRealmDataProvider, DynRealmRestClient> {
55  
56      private static final long serialVersionUID = -5491515010207202168L;
57  
58      protected DynRealmDirectoryPanel(final String id, final Builder builder) {
59          super(id, builder);
60          MetaDataRoleAuthorizationStrategy.authorize(addAjaxLink, RENDER, IdRepoEntitlement.DYNREALM_CREATE);
61          setReadOnly(!SyncopeConsoleSession.get().owns(IdRepoEntitlement.DYNREALM_UPDATE));
62  
63          disableCheckBoxes();
64          setShowResultPanel(true);
65  
66          modal.size(Modal.Size.Large);
67          modal.addSubmitButton();
68          modal.setWindowClosedCallback(target -> {
69              updateResultTable(target);
70              modal.show(false);
71          });
72          setFooterVisibility(true);
73  
74          AjaxLink<Void> newDynRealmlLink = new AjaxLink<>("add") {
75  
76              private static final long serialVersionUID = -7978723352517770644L;
77  
78              @Override
79              public void onClick(final AjaxRequestTarget target) {
80                  modal.header(new StringResourceModel("any.new"));
81                  modal.setContent(new DynRealmModalPanel(new DynRealmWrapper(new DynRealmTO()), true, modal, pageRef));
82                  modal.show(true);
83                  target.add(modal);
84              }
85          };
86          ((WebMarkupContainer) get("container:content")).addOrReplace(newDynRealmlLink);
87          MetaDataRoleAuthorizationStrategy.authorize(newDynRealmlLink, RENDER, IdRepoEntitlement.DYNREALM_CREATE);
88  
89          initResultTable();
90      }
91  
92      @Override
93      protected DynRealmDataProvider dataProvider() {
94          return new DynRealmDataProvider(rows);
95      }
96  
97      @Override
98      protected String paginatorRowsKey() {
99          return IdRepoConstants.PREF_DYNREALM_PAGINATOR_ROWS;
100     }
101 
102     @Override
103     protected List<IColumn<DynRealmTO, String>> getColumns() {
104         final List<IColumn<DynRealmTO, String>> columns = new ArrayList<>();
105 
106         columns.add(new PropertyColumn<>(
107                 new ResourceModel(Constants.KEY_FIELD_NAME), Constants.KEY_FIELD_NAME, Constants.KEY_FIELD_NAME));
108 
109         return columns;
110     }
111 
112     @Override
113     public ActionsPanel<DynRealmTO> getActions(final IModel<DynRealmTO> model) {
114         final ActionsPanel<DynRealmTO> panel = super.getActions(model);
115 
116         panel.add(new ActionLink<>() {
117 
118             private static final long serialVersionUID = -7978723352517770644L;
119 
120             @Override
121             public void onClick(final AjaxRequestTarget target, final DynRealmTO ignore) {
122                 modal.header(new StringResourceModel("any.edit", model));
123                 modal.setContent(new DynRealmModalPanel(new DynRealmWrapper(model.getObject()), false, modal, pageRef));
124                 modal.show(true);
125                 target.add(modal);
126             }
127         }, ActionLink.ActionType.EDIT, IdRepoEntitlement.DYNREALM_UPDATE);
128 
129         panel.add(new ActionLink<>() {
130 
131             private static final long serialVersionUID = 3766262567901552032L;
132 
133             @Override
134             public void onClick(final AjaxRequestTarget target, final DynRealmTO ignore) {
135                 try {
136                     restClient.delete(model.getObject().getKey());
137 
138                     SyncopeConsoleSession.get().success(getString(Constants.OPERATION_SUCCEEDED));
139                     target.add(container);
140                 } catch (SyncopeClientException e) {
141                     LOG.error("While deleting dynamic realm {}", model.getObject().getKey(), e);
142                     SyncopeConsoleSession.get().onException(e);
143                 }
144                 ((BasePage) pageRef.getPage()).getNotificationPanel().refresh(target);
145             }
146         }, ActionLink.ActionType.DELETE, IdRepoEntitlement.DYNREALM_DELETE, true);
147 
148         return panel;
149     }
150 
151     @Override
152     protected Collection<ActionLink.ActionType> getBatches() {
153         return List.of();
154     }
155 
156     protected class DynRealmDataProvider extends DirectoryDataProvider<DynRealmTO> {
157 
158         private static final long serialVersionUID = 3124431855954382273L;
159 
160         private final SortableDataProviderComparator<DynRealmTO> comparator;
161 
162         public DynRealmDataProvider(final int paginatorRows) {
163             super(paginatorRows);
164             this.comparator = new SortableDataProviderComparator<>(this);
165         }
166 
167         @Override
168         public Iterator<DynRealmTO> iterator(final long first, final long count) {
169             List<DynRealmTO> result = restClient.list();
170             result.sort(comparator);
171             return result.subList((int) first, (int) first + (int) count).iterator();
172         }
173 
174         @Override
175         public long size() {
176             return restClient.list().size();
177         }
178 
179         @Override
180         public IModel<DynRealmTO> model(final DynRealmTO object) {
181             return new CompoundPropertyModel<>(object);
182         }
183     }
184 
185     public abstract static class Builder
186             extends DirectoryPanel.Builder<DynRealmTO, DynRealmWrapper, DynRealmRestClient> {
187 
188         private static final long serialVersionUID = 5530948153889495221L;
189 
190         public Builder(final DynRealmRestClient restClient, final PageReference pageRef) {
191             super(restClient, pageRef);
192         }
193 
194         @Override
195         protected WizardMgtPanel<DynRealmWrapper> newInstance(final String id, final boolean wizardInModal) {
196             return new DynRealmDirectoryPanel(id, this);
197         }
198     }
199 }