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          setWindowClosedReloadCallback(modal, true);
76          setFooterVisibility(true);
77  
78          this.addNewItemPanelBuilder(
79                  new AbstractModalPanelBuilder<SecurityQuestionTO>(new SecurityQuestionTO(), pageRef) {
80  
81              private static final long serialVersionUID = -6388405037134399367L;
82  
83              @Override
84              public WizardModalPanel<SecurityQuestionTO> build(
85                      final String id, final int index, final AjaxWizard.Mode mode) {
86  
87                  return new SecurityQuestionsModalPanel(modal, newModelObject(), pageRef);
88              }
89          }, true);
90  
91          initResultTable();
92  
93          MetaDataRoleAuthorizationStrategy.authorize(addAjaxLink, RENDER, IdRepoEntitlement.SECURITY_QUESTION_CREATE);
94      }
95  
96      @Override
97      protected SecurityQuestionsProvider dataProvider() {
98          return new SecurityQuestionsProvider(rows);
99      }
100 
101     @Override
102     protected String paginatorRowsKey() {
103         return IdRepoConstants.PREF_SECURITY_QUESTIONS_PAGINATOR_ROWS;
104     }
105 
106     @Override
107     protected Collection<ActionLink.ActionType> getBatches() {
108         return List.of();
109     }
110 
111     @Override
112     protected List<IColumn<SecurityQuestionTO, String>> getColumns() {
113         List<IColumn<SecurityQuestionTO, String>> columns = new ArrayList<>();
114 
115         columns.add(new KeyPropertyColumn<>(
116                 new StringResourceModel(Constants.KEY_FIELD_NAME, this), Constants.KEY_FIELD_NAME));
117         columns.add(new PropertyColumn<>(
118                 new StringResourceModel("content", this), "content", "content"));
119 
120         return columns;
121     }
122 
123     @Override
124     public ActionsPanel<SecurityQuestionTO> getActions(final IModel<SecurityQuestionTO> model) {
125         ActionsPanel<SecurityQuestionTO> panel = super.getActions(model);
126 
127         panel.add(new ActionLink<>() {
128 
129             private static final long serialVersionUID = -3722207913631435501L;
130 
131             @Override
132             public void onClick(final AjaxRequestTarget target, final SecurityQuestionTO ignore) {
133                 send(SecurityQuestionsPanel.this, Broadcast.EXACT,
134                         new AjaxWizard.EditItemActionEvent<>(model.getObject(), target));
135             }
136         }, ActionLink.ActionType.EDIT, IdRepoEntitlement.SECURITY_QUESTION_UPDATE);
137         panel.add(new ActionLink<>() {
138 
139             private static final long serialVersionUID = -3722207913631435501L;
140 
141             @Override
142             public void onClick(final AjaxRequestTarget target, final SecurityQuestionTO ignore) {
143                 try {
144                     restClient.delete(model.getObject().getKey());
145 
146                     SyncopeConsoleSession.get().success(getString(Constants.OPERATION_SUCCEEDED));
147                     target.add(container);
148                 } catch (Exception e) {
149                     LOG.error("While deleting {}", model.getObject(), e);
150                     SyncopeConsoleSession.get().onException(e);
151                 }
152                 ((BasePage) pageRef.getPage()).getNotificationPanel().refresh(target);
153             }
154         }, ActionLink.ActionType.DELETE, IdRepoEntitlement.SECURITY_QUESTION_DELETE, true);
155 
156         return panel;
157     }
158 
159     protected final class SecurityQuestionsProvider extends DirectoryDataProvider<SecurityQuestionTO> {
160 
161         private static final long serialVersionUID = -185944053385660794L;
162 
163         private final SortableDataProviderComparator<SecurityQuestionTO> comparator;
164 
165         private SecurityQuestionsProvider(final int paginatorRows) {
166             super(paginatorRows);
167             comparator = new SortableDataProviderComparator<>(this);
168         }
169 
170         @Override
171         public Iterator<SecurityQuestionTO> iterator(final long first, final long count) {
172             List<SecurityQuestionTO> list = restClient.list();
173             list.sort(comparator);
174             return list.subList((int) first, (int) first + (int) count).iterator();
175         }
176 
177         @Override
178         public long size() {
179             return restClient.list().size();
180         }
181 
182         @Override
183         public IModel<SecurityQuestionTO> model(final SecurityQuestionTO object) {
184             return new CompoundPropertyModel<>(object);
185         }
186     }
187 }