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.SecurityQuestionsPanel.SecurityQuestionsProvider;
32  import org.apache.syncope.client.console.rest.SecurityQuestionRestClient;
33  import org.apache.syncope.client.console.wicket.extensions.markup.html.repeater.data.table.KeyPropertyColumn;
34  import org.apache.syncope.client.console.wicket.markup.html.form.ActionLink;
35  import org.apache.syncope.client.console.wicket.markup.html.form.ActionsPanel;
36  import org.apache.syncope.client.console.wizards.WizardMgtPanel;
37  import org.apache.syncope.client.ui.commons.Constants;
38  import org.apache.syncope.client.ui.commons.panels.WizardModalPanel;
39  import org.apache.syncope.client.ui.commons.wizards.AbstractModalPanelBuilder;
40  import org.apache.syncope.client.ui.commons.wizards.AjaxWizard;
41  import org.apache.syncope.common.lib.to.SecurityQuestionTO;
42  import org.apache.syncope.common.lib.types.IdRepoEntitlement;
43  import org.apache.wicket.PageReference;
44  import org.apache.wicket.ajax.AjaxRequestTarget;
45  import org.apache.wicket.authroles.authorization.strategies.role.metadata.MetaDataRoleAuthorizationStrategy;
46  import org.apache.wicket.event.Broadcast;
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 SecurityQuestionsPanel extends DirectoryPanel<
54          SecurityQuestionTO, SecurityQuestionTO, SecurityQuestionsProvider, SecurityQuestionRestClient> {
55  
56      private static final long serialVersionUID = 3323019773236588850L;
57  
58      public SecurityQuestionsPanel(
59              final String id,
60              final SecurityQuestionRestClient restClient,
61              final PageReference pageRef) {
62  
63          super(id, new Builder<SecurityQuestionTO, SecurityQuestionTO, SecurityQuestionRestClient>(restClient, pageRef) {
64  
65              private static final long serialVersionUID = 8769126634538601689L;
66  
67              @Override
68              protected WizardMgtPanel<SecurityQuestionTO> newInstance(final String id, final boolean wizardInModal) {
69                  throw new UnsupportedOperationException();
70              }
71          }.disableCheckBoxes());
72  
73          modal.addSubmitButton();
74          modal.size(Modal.Size.Large);
75          modal.setWindowClosedCallback(target -> {
76              modal.show(false);
77              target.add(container);
78          });
79          setFooterVisibility(true);
80  
81          this.addNewItemPanelBuilder(
82                  new AbstractModalPanelBuilder<SecurityQuestionTO>(new SecurityQuestionTO(), pageRef) {
83  
84              private static final long serialVersionUID = -6388405037134399367L;
85  
86              @Override
87              public WizardModalPanel<SecurityQuestionTO> build(
88                      final String id, final int index, final AjaxWizard.Mode mode) {
89  
90                  return new SecurityQuestionsModalPanel(modal, newModelObject(), pageRef);
91              }
92          }, true);
93  
94          initResultTable();
95  
96          MetaDataRoleAuthorizationStrategy.authorize(addAjaxLink, RENDER, IdRepoEntitlement.SECURITY_QUESTION_CREATE);
97      }
98  
99      @Override
100     protected SecurityQuestionsProvider dataProvider() {
101         return new SecurityQuestionsProvider(rows);
102     }
103 
104     @Override
105     protected String paginatorRowsKey() {
106         return IdRepoConstants.PREF_SECURITY_QUESTIONS_PAGINATOR_ROWS;
107     }
108 
109     @Override
110     protected Collection<ActionLink.ActionType> getBatches() {
111         return List.of();
112     }
113 
114     @Override
115     protected List<IColumn<SecurityQuestionTO, String>> getColumns() {
116         List<IColumn<SecurityQuestionTO, String>> columns = new ArrayList<>();
117 
118         columns.add(new KeyPropertyColumn<>(
119                 new StringResourceModel(Constants.KEY_FIELD_NAME, this), Constants.KEY_FIELD_NAME));
120         columns.add(new PropertyColumn<>(
121                 new StringResourceModel("content", this), "content", "content"));
122 
123         return columns;
124     }
125 
126     @Override
127     public ActionsPanel<SecurityQuestionTO> getActions(final IModel<SecurityQuestionTO> model) {
128         ActionsPanel<SecurityQuestionTO> panel = super.getActions(model);
129 
130         panel.add(new ActionLink<>() {
131 
132             private static final long serialVersionUID = -3722207913631435501L;
133 
134             @Override
135             public void onClick(final AjaxRequestTarget target, final SecurityQuestionTO ignore) {
136                 send(SecurityQuestionsPanel.this, Broadcast.EXACT,
137                         new AjaxWizard.EditItemActionEvent<>(model.getObject(), target));
138             }
139         }, ActionLink.ActionType.EDIT, IdRepoEntitlement.SECURITY_QUESTION_UPDATE);
140         panel.add(new ActionLink<>() {
141 
142             private static final long serialVersionUID = -3722207913631435501L;
143 
144             @Override
145             public void onClick(final AjaxRequestTarget target, final SecurityQuestionTO ignore) {
146                 try {
147                     restClient.delete(model.getObject().getKey());
148 
149                     SyncopeConsoleSession.get().success(getString(Constants.OPERATION_SUCCEEDED));
150                     target.add(container);
151                 } catch (Exception e) {
152                     LOG.error("While deleting {}", model.getObject(), e);
153                     SyncopeConsoleSession.get().onException(e);
154                 }
155                 ((BasePage) pageRef.getPage()).getNotificationPanel().refresh(target);
156             }
157         }, ActionLink.ActionType.DELETE, IdRepoEntitlement.SECURITY_QUESTION_DELETE, true);
158 
159         return panel;
160     }
161 
162     protected final class SecurityQuestionsProvider extends DirectoryDataProvider<SecurityQuestionTO> {
163 
164         private static final long serialVersionUID = -185944053385660794L;
165 
166         private final SortableDataProviderComparator<SecurityQuestionTO> comparator;
167 
168         private SecurityQuestionsProvider(final int paginatorRows) {
169             super(paginatorRows);
170             comparator = new SortableDataProviderComparator<>(this);
171         }
172 
173         @Override
174         public Iterator<SecurityQuestionTO> iterator(final long first, final long count) {
175             List<SecurityQuestionTO> list = restClient.list();
176             list.sort(comparator);
177             return list.subList((int) first, (int) first + (int) count).iterator();
178         }
179 
180         @Override
181         public long size() {
182             return restClient.list().size();
183         }
184 
185         @Override
186         public IModel<SecurityQuestionTO> model(final SecurityQuestionTO object) {
187             return new CompoundPropertyModel<>(object);
188         }
189     }
190 }