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.io.Serializable;
23  import java.util.ArrayList;
24  import java.util.Collection;
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.audit.AuditHistoryModal;
30  import org.apache.syncope.client.console.commons.AMConstants;
31  import org.apache.syncope.client.console.commons.DirectoryDataProvider;
32  import org.apache.syncope.client.console.commons.SortableDataProviderComparator;
33  import org.apache.syncope.client.console.pages.BasePage;
34  import org.apache.syncope.client.console.panels.AttrRepoDirectoryPanel.AttrRepoProvider;
35  import org.apache.syncope.client.console.rest.AttrRepoRestClient;
36  import org.apache.syncope.client.console.rest.AuditRestClient;
37  import org.apache.syncope.client.console.wicket.markup.html.bootstrap.dialog.BaseModal;
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.AttrRepoWizardBuilder;
41  import org.apache.syncope.client.ui.commons.Constants;
42  import org.apache.syncope.client.ui.commons.pages.BaseWebPage;
43  import org.apache.syncope.client.ui.commons.wizards.AjaxWizard;
44  import org.apache.syncope.common.lib.to.AttrRepoTO;
45  import org.apache.syncope.common.lib.types.AMEntitlement;
46  import org.apache.syncope.common.lib.types.AuditElements;
47  import org.apache.syncope.common.lib.types.IdRepoEntitlement;
48  import org.apache.wicket.PageReference;
49  import org.apache.wicket.ajax.AjaxRequestTarget;
50  import org.apache.wicket.authroles.authorization.strategies.role.metadata.MetaDataRoleAuthorizationStrategy;
51  import org.apache.wicket.event.Broadcast;
52  import org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator;
53  import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
54  import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
55  import org.apache.wicket.markup.html.basic.Label;
56  import org.apache.wicket.markup.repeater.Item;
57  import org.apache.wicket.model.CompoundPropertyModel;
58  import org.apache.wicket.model.IModel;
59  import org.apache.wicket.model.Model;
60  import org.apache.wicket.model.ResourceModel;
61  import org.apache.wicket.model.StringResourceModel;
62  import org.apache.wicket.spring.injection.annot.SpringBean;
63  
64  public class AttrRepoDirectoryPanel
65          extends DirectoryPanel<AttrRepoTO, AttrRepoTO, AttrRepoProvider, AttrRepoRestClient> {
66  
67      private static final long serialVersionUID = 1005345990563741296L;
68  
69      @SpringBean
70      protected AuditRestClient auditRestClient;
71  
72      protected final BaseModal<Serializable> historyModal;
73  
74      public AttrRepoDirectoryPanel(final String id, final AttrRepoRestClient restClient, final PageReference pageRef) {
75          super(id, restClient, pageRef);
76  
77          disableCheckBoxes();
78  
79          this.addNewItemPanelBuilder(new AttrRepoWizardBuilder(new AttrRepoTO(), restClient, pageRef), true);
80  
81          MetaDataRoleAuthorizationStrategy.authorize(addAjaxLink, RENDER, AMEntitlement.ATTR_REPO_CREATE);
82  
83          modal.size(Modal.Size.Extra_large);
84          initResultTable();
85  
86          historyModal = new BaseModal<>(Constants.OUTER);
87          historyModal.size(Modal.Size.Large);
88          addOuterObject(historyModal);
89      }
90  
91      @Override
92      protected AttrRepoProvider dataProvider() {
93          return new AttrRepoProvider(rows);
94      }
95  
96      @Override
97      protected String paginatorRowsKey() {
98          return AMConstants.PREF_AUTHMODULE_PAGINATOR_ROWS;
99      }
100 
101     @Override
102     protected Collection<ActionLink.ActionType> getBatches() {
103         return List.of();
104     }
105 
106     @Override
107     protected List<IColumn<AttrRepoTO, String>> getColumns() {
108         List<IColumn<AttrRepoTO, String>> columns = new ArrayList<>();
109         columns.add(new PropertyColumn<>(
110                 new StringResourceModel(Constants.KEY_FIELD_NAME, this),
111                 Constants.KEY_FIELD_NAME, Constants.KEY_FIELD_NAME));
112         columns.add(new PropertyColumn<>(new ResourceModel(Constants.DESCRIPTION_FIELD_NAME),
113                 Constants.DESCRIPTION_FIELD_NAME, Constants.DESCRIPTION_FIELD_NAME));
114         columns.add(new PropertyColumn<>(new ResourceModel("type"), "conf") {
115 
116             private static final long serialVersionUID = -1822504503325964706L;
117 
118             @Override
119             public void populateItem(
120                     final Item<ICellPopulator<AttrRepoTO>> item,
121                     final String componentId,
122                     final IModel<AttrRepoTO> rowModel) {
123 
124                 item.add(new Label(componentId, rowModel.getObject().getConf() == null
125                         ? StringUtils.EMPTY
126                         : StringUtils.substringBefore(
127                                 rowModel.getObject().getConf().getClass().getSimpleName(), "AttrRepoConf")));
128             }
129         });
130         columns.add(new PropertyColumn<>(new ResourceModel("state"), "state", "state"));
131         columns.add(new PropertyColumn<>(new ResourceModel("order"), "order", "order"));
132         return columns;
133     }
134 
135     @Override
136     public ActionsPanel<AttrRepoTO> getActions(final IModel<AttrRepoTO> model) {
137         ActionsPanel<AttrRepoTO> panel = super.getActions(model);
138 
139         panel.add(new ActionLink<>() {
140 
141             private static final long serialVersionUID = -3722207913631435501L;
142 
143             @Override
144             public void onClick(final AjaxRequestTarget target, final AttrRepoTO ignore) {
145                 send(AttrRepoDirectoryPanel.this, Broadcast.EXACT, new AjaxWizard.EditItemActionEvent<>(
146                         restClient.read(model.getObject().getKey()), target));
147             }
148         }, ActionLink.ActionType.EDIT, AMEntitlement.ATTR_REPO_UPDATE);
149 
150         panel.add(new ActionLink<>() {
151 
152             private static final long serialVersionUID = -5432034353017728756L;
153 
154             @Override
155             public void onClick(final AjaxRequestTarget target, final AttrRepoTO ignore) {
156                 model.setObject(restClient.read(model.getObject().getKey()));
157 
158                 target.add(historyModal.setContent(new AuditHistoryModal<>(
159                         AuditElements.EventCategoryType.LOGIC,
160                         "AttrRepoLogic",
161                         model.getObject(),
162                         AMEntitlement.ATTR_REPO_UPDATE,
163                         auditRestClient) {
164 
165                     private static final long serialVersionUID = -3712506022627033822L;
166 
167                     @Override
168                     protected void restore(final String json, final AjaxRequestTarget target) {
169                         try {
170                             AttrRepoTO updated = MAPPER.readValue(json, AttrRepoTO.class);
171                             restClient.update(updated);
172 
173                             SyncopeConsoleSession.get().success(getString(Constants.OPERATION_SUCCEEDED));
174                         } catch (Exception e) {
175                             LOG.error("While restoring AttrRepo {}", ((AttrRepoTO) model.getObject()).getKey(), e);
176                             SyncopeConsoleSession.get().onException(e);
177                         }
178                         ((BasePage) pageRef.getPage()).getNotificationPanel().refresh(target);
179                     }
180                 }));
181 
182                 historyModal.header(new Model<>(getString("auditHistory.title", new Model<>(model.getObject()))));
183 
184                 historyModal.show(true);
185             }
186         }, ActionLink.ActionType.VIEW_AUDIT_HISTORY, String.format("%s,%s", AMEntitlement.ATTR_REPO_READ,
187                 IdRepoEntitlement.AUDIT_LIST));
188 
189         panel.add(new ActionLink<>() {
190 
191             private static final long serialVersionUID = -3722207913631435501L;
192 
193             @Override
194             public void onClick(final AjaxRequestTarget target, final AttrRepoTO ignore) {
195                 try {
196                     restClient.delete(model.getObject().getKey());
197 
198                     SyncopeConsoleSession.get().success(getString(Constants.OPERATION_SUCCEEDED));
199                     target.add(container);
200                 } catch (Exception e) {
201                     LOG.error("While deleting {}", model.getObject().getKey(), e);
202                     SyncopeConsoleSession.get().onException(e);
203                 }
204                 ((BaseWebPage) pageRef.getPage()).getNotificationPanel().refresh(target);
205             }
206         }, ActionLink.ActionType.DELETE, AMEntitlement.ATTR_REPO_DELETE, true);
207 
208         return panel;
209     }
210 
211     protected final class AttrRepoProvider extends DirectoryDataProvider<AttrRepoTO> {
212 
213         private static final long serialVersionUID = -185944053385660794L;
214 
215         private final SortableDataProviderComparator<AttrRepoTO> comparator;
216 
217         private AttrRepoProvider(final int paginatorRows) {
218             super(paginatorRows);
219             comparator = new SortableDataProviderComparator<>(this);
220         }
221 
222         @Override
223         public Iterator<AttrRepoTO> iterator(final long first, final long count) {
224             List<AttrRepoTO> attrRepos = restClient.list();
225             attrRepos.sort(comparator);
226             return attrRepos.subList((int) first, (int) first + (int) count).iterator();
227         }
228 
229         @Override
230         public long size() {
231             return restClient.list().size();
232         }
233 
234         @Override
235         public IModel<AttrRepoTO> model(final AttrRepoTO object) {
236             return new CompoundPropertyModel<>(object);
237         }
238     }
239 }