[SYNCOPE-1262] Last matrix parameter still there
[syncope.git] / client / console / src / main / java / org / apache / syncope / client / console / panels / ImplementationDirectoryPanel.java
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.Constants;
30 import org.apache.syncope.client.console.commons.DirectoryDataProvider;
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.ImplementationDirectoryPanel.ImplementationProvider;
34 import org.apache.syncope.client.console.rest.ImplementationRestClient;
35 import org.apache.syncope.client.console.wicket.markup.html.form.ActionLink;
36 import org.apache.syncope.client.console.wicket.markup.html.form.ActionLinksTogglePanel;
37 import org.apache.syncope.client.console.wicket.markup.html.form.ActionsPanel;
38 import org.apache.syncope.common.lib.SyncopeClientException;
39 import org.apache.syncope.common.lib.to.ImplementationTO;
40 import org.apache.syncope.common.lib.types.ImplementationEngine;
41 import org.apache.syncope.common.lib.types.ImplementationType;
42 import org.apache.syncope.common.lib.types.StandardEntitlement;
43 import org.apache.wicket.PageReference;
44 import org.apache.wicket.ajax.AjaxRequestTarget;
45 import org.apache.wicket.ajax.markup.html.AjaxLink;
46 import org.apache.wicket.authroles.authorization.strategies.role.metadata.MetaDataRoleAuthorizationStrategy;
47 import org.apache.wicket.event.Broadcast;
48 import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
49 import org.apache.wicket.extensions.markup.html.repeater.data.sort.SortOrder;
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.WebMarkupContainer;
53 import org.apache.wicket.model.AbstractReadOnlyModel;
54 import org.apache.wicket.model.IModel;
55 import org.apache.wicket.model.Model;
56 import org.apache.wicket.model.StringResourceModel;
57
58 public class ImplementationDirectoryPanel extends DirectoryPanel<
59 ImplementationTO, ImplementationTO, ImplementationProvider, ImplementationRestClient> {
60
61 private static final long serialVersionUID = 1868839768348072635L;
62
63 private final ImplementationType type;
64
65 public ImplementationDirectoryPanel(final String id, final ImplementationType type, final PageReference pageRef) {
66 super(id, pageRef, true);
67 this.type = type;
68
69 ImplementationTO implementation = new ImplementationTO();
70 implementation.setType(type);
71
72 disableCheckBoxes();
73
74 modal.size(Modal.Size.Large);
75 modal.addSubmitButton();
76 modal.setWindowClosedCallback(new ModalWindow.WindowClosedCallback() {
77
78 private static final long serialVersionUID = 8804221891699487139L;
79
80 @Override
81 public void onClose(final AjaxRequestTarget target) {
82 implementation.setEngine(null);
83 updateResultTable(target);
84 modal.show(false);
85 }
86 });
87 setFooterVisibility(true);
88
89 restClient = new ImplementationRestClient();
90
91 initResultTable();
92
93 ImplementationEngineTogglePanel engineTogglePanel =
94 new ImplementationEngineTogglePanel("engineTogglePanel", implementation, pageRef) {
95
96 private static final long serialVersionUID = -112426445257072782L;
97
98 @Override
99 protected void onSubmit(final ImplementationEngine engine, final AjaxRequestTarget target) {
100 implementation.setKey(null);
101 implementation.setBody(null);
102
103 target.add(ImplementationDirectoryPanel.this.modal.setContent(new ImplementationModalPanel(
104 ImplementationDirectoryPanel.this.modal, implementation, pageRef)));
105 ImplementationDirectoryPanel.this.modal.header(
106 new StringResourceModel("any.new", Model.of(implementation)));
107 ImplementationDirectoryPanel.this.modal.show(true);
108 }
109 };
110 addInnerObject(engineTogglePanel);
111
112 AjaxLink<Void> replaceAddLink = new AjaxLink<Void>("add") {
113
114 private static final long serialVersionUID = -7978723352517770644L;
115
116 @Override
117 public void onClick(final AjaxRequestTarget target) {
118 send(ImplementationDirectoryPanel.this, Broadcast.BREADTH,
119 new ActionLinksTogglePanel.ActionLinkToggleCloseEventPayload(target));
120 engineTogglePanel.setHeaderLabel(target);
121 engineTogglePanel.toggle(target, true);
122 }
123 };
124 ((WebMarkupContainer) get("container:content")).addOrReplace(replaceAddLink);
125
126 MetaDataRoleAuthorizationStrategy.authorize(addAjaxLink, RENDER, StandardEntitlement.IMPLEMENTATION_CREATE);
127 }
128
129 @Override
130 protected List<IColumn<ImplementationTO, String>> getColumns() {
131 List<IColumn<ImplementationTO, String>> columns = new ArrayList<>();
132
133 columns.add(new PropertyColumn<>(new StringResourceModel("key", this), "key", "key"));
134 columns.add(new PropertyColumn<>(new StringResourceModel("engine", this), "engine", "engine"));
135
136 return columns;
137 }
138
139 @Override
140 protected ActionsPanel<ImplementationTO> getActions(final IModel<ImplementationTO> model) {
141 final ActionsPanel<ImplementationTO> panel = super.getActions(model);
142
143 panel.add(new ActionLink<ImplementationTO>() {
144
145 private static final long serialVersionUID = -7978723352517770645L;
146
147 @Override
148 public void onClick(final AjaxRequestTarget target, final ImplementationTO ignore) {
149 target.add(modal.setContent(
150 new ImplementationModalPanel(modal, model.getObject(), pageRef)));
151 modal.header(new StringResourceModel("any.edit", Model.of(model.getObject())));
152 modal.show(true);
153 }
154 }, ActionLink.ActionType.EDIT, StandardEntitlement.IMPLEMENTATION_UPDATE);
155
156 panel.add(new ActionLink<ImplementationTO>() {
157
158 private static final long serialVersionUID = -3722207913631435501L;
159
160 @Override
161 public void onClick(final AjaxRequestTarget target, final ImplementationTO ignore) {
162 try {
163 restClient.delete(model.getObject().getType(), model.getObject().getKey());
164 SyncopeConsoleSession.get().info(getString(Constants.OPERATION_SUCCEEDED));
165 target.add(container);
166 } catch (SyncopeClientException e) {
167 LOG.error("While deleting object {}", model.getObject().getKey(), e);
168 SyncopeConsoleSession.get().error(
169 StringUtils.isBlank(e.getMessage()) ? e.getClass().getName() : e.getMessage());
170 }
171 ((BasePage) pageRef.getPage()).getNotificationPanel().refresh(target);
172 }
173 }, ActionLink.ActionType.DELETE, StandardEntitlement.IMPLEMENTATION_DELETE, true);
174
175 return panel;
176 }
177
178 @Override
179 protected ImplementationProvider dataProvider() {
180 return new ImplementationProvider(rows);
181 }
182
183 @Override
184 protected String paginatorRowsKey() {
185 return Constants.PREF_IMPLEMENTATION_PAGINATOR_ROWS;
186 }
187
188 @Override
189 protected Collection<ActionLink.ActionType> getBulkActions() {
190 return Collections.<ActionLink.ActionType>emptyList();
191 }
192
193 protected class ImplementationProvider extends DirectoryDataProvider<ImplementationTO> {
194
195 private static final long serialVersionUID = 8594921866993979224L;
196
197 private final SortableDataProviderComparator<ImplementationTO> comparator;
198
199 public ImplementationProvider(final int paginatorRows) {
200 super(paginatorRows);
201
202 setSort("key", SortOrder.ASCENDING);
203 comparator = new SortableDataProviderComparator<>(this);
204 }
205
206 @Override
207 public Iterator<ImplementationTO> iterator(final long first, final long count) {
208 List<ImplementationTO> list = restClient.list(type);
209 Collections.sort(list, comparator);
210 return list.subList((int) first, (int) first + (int) count).iterator();
211 }
212
213 @Override
214 public long size() {
215 return restClient.list(type).size();
216 }
217
218 @Override
219 public IModel<ImplementationTO> model(final ImplementationTO implementation) {
220 return new AbstractReadOnlyModel<ImplementationTO>() {
221
222 private static final long serialVersionUID = 999513782683391483L;
223
224 @Override
225 public ImplementationTO getObject() {
226 return implementation;
227 }
228 };
229 }
230 }
231
232 }