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.notifications;
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 java.util.stream.Collectors;
27  import org.apache.syncope.client.console.SyncopeConsoleSession;
28  import org.apache.syncope.client.console.commons.DirectoryDataProvider;
29  import org.apache.syncope.client.console.commons.IdRepoConstants;
30  import org.apache.syncope.client.console.commons.SortableDataProviderComparator;
31  import org.apache.syncope.client.console.notifications.NotificationDirectoryPanel.NotificationProvider;
32  import org.apache.syncope.client.console.pages.BasePage;
33  import org.apache.syncope.client.console.panels.DirectoryPanel;
34  import org.apache.syncope.client.console.rest.AnyTypeRestClient;
35  import org.apache.syncope.client.console.rest.AuditRestClient;
36  import org.apache.syncope.client.console.rest.ImplementationRestClient;
37  import org.apache.syncope.client.console.rest.NotificationRestClient;
38  import org.apache.syncope.client.console.rest.SchemaRestClient;
39  import org.apache.syncope.client.console.wicket.extensions.markup.html.repeater.data.table.BooleanPropertyColumn;
40  import org.apache.syncope.client.console.wicket.extensions.markup.html.repeater.data.table.CollectionPropertyColumn;
41  import org.apache.syncope.client.console.wicket.extensions.markup.html.repeater.data.table.KeyPropertyColumn;
42  import org.apache.syncope.client.console.wicket.markup.html.bootstrap.dialog.BaseModal;
43  import org.apache.syncope.client.console.wicket.markup.html.form.ActionLink;
44  import org.apache.syncope.client.console.wicket.markup.html.form.ActionsPanel;
45  import org.apache.syncope.client.ui.commons.Constants;
46  import org.apache.syncope.client.ui.commons.wizards.AjaxWizard;
47  import org.apache.syncope.common.lib.SyncopeClientException;
48  import org.apache.syncope.common.lib.audit.EventCategory;
49  import org.apache.syncope.common.lib.to.NotificationTO;
50  import org.apache.syncope.common.lib.types.AuditElements;
51  import org.apache.syncope.common.lib.types.IdRepoEntitlement;
52  import org.apache.wicket.PageReference;
53  import org.apache.wicket.ajax.AjaxRequestTarget;
54  import org.apache.wicket.authroles.authorization.strategies.role.metadata.MetaDataRoleAuthorizationStrategy;
55  import org.apache.wicket.event.Broadcast;
56  import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
57  import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
58  import org.apache.wicket.model.IModel;
59  import org.apache.wicket.model.LoadableDetachableModel;
60  import org.apache.wicket.model.Model;
61  import org.apache.wicket.model.StringResourceModel;
62  import org.apache.wicket.spring.injection.annot.SpringBean;
63  
64  public class NotificationDirectoryPanel
65          extends DirectoryPanel<NotificationTO, NotificationWrapper, NotificationProvider, NotificationRestClient> {
66  
67      private static final long serialVersionUID = -3789392431954221446L;
68  
69      @SpringBean
70      protected NotificationRestClient notificationRestClient;
71  
72      @SpringBean
73      protected AuditRestClient auditRestClient;
74  
75      @SpringBean
76      protected AnyTypeRestClient anyTypeRestClient;
77  
78      @SpringBean
79      protected ImplementationRestClient implementationRestClient;
80  
81      @SpringBean
82      protected SchemaRestClient schemaRestClient;
83  
84      protected final IModel<List<EventCategory>> eventCategories = new LoadableDetachableModel<List<EventCategory>>() {
85  
86          private static final long serialVersionUID = 4659376149825914247L;
87  
88          @Override
89          protected List<EventCategory> load() {
90              // cannot notify about WA events
91              return auditRestClient.listEvents().stream().
92                      filter(c -> c.getType() != AuditElements.EventCategoryType.WA).collect(Collectors.toList());
93          }
94      };
95  
96      protected final BaseModal<String> utilityModal = new BaseModal<>(Constants.OUTER);
97  
98      public NotificationDirectoryPanel(
99              final String id,
100             final NotificationRestClient restClient,
101             final PageReference pageRef) {
102 
103         super(id, restClient, pageRef, true);
104         disableCheckBoxes();
105 
106         addOuterObject(utilityModal);
107         setWindowClosedReloadCallback(utilityModal);
108         utilityModal.size(Modal.Size.Large);
109 
110         modal.size(Modal.Size.Large);
111         altDefaultModal.size(Modal.Size.Large);
112 
113         addNewItemPanelBuilder(new NotificationWizardBuilder(
114                 new NotificationTO(),
115                 notificationRestClient,
116                 anyTypeRestClient,
117                 implementationRestClient,
118                 schemaRestClient,
119                 eventCategories,
120                 pageRef), true);
121 
122         initResultTable();
123 
124         MetaDataRoleAuthorizationStrategy.authorize(addAjaxLink, RENDER, IdRepoEntitlement.NOTIFICATION_CREATE);
125     }
126 
127     @Override
128     protected List<IColumn<NotificationTO, String>> getColumns() {
129         List<IColumn<NotificationTO, String>> columns = new ArrayList<>();
130         columns.add(new KeyPropertyColumn<>(
131                 new StringResourceModel(Constants.KEY_FIELD_NAME, this), Constants.KEY_FIELD_NAME));
132         columns.add(new PropertyColumn<>(
133                 new StringResourceModel("sender", this), "sender", "sender"));
134         columns.add(new PropertyColumn<>(
135                 new StringResourceModel("subject", this), "subject", "subject"));
136         columns.add(new PropertyColumn<>(
137                 new StringResourceModel("template", this), "template", "template"));
138         columns.add(new CollectionPropertyColumn<>(
139                 new StringResourceModel("events", this), "events"));
140         columns.add(new BooleanPropertyColumn<>(
141                 new StringResourceModel("active", this), "active", "active"));
142         return columns;
143     }
144 
145     @Override
146     public ActionsPanel<NotificationTO> getActions(final IModel<NotificationTO> model) {
147         ActionsPanel<NotificationTO> actions = super.getActions(model);
148 
149         actions.add(new ActionLink<>() {
150 
151             private static final long serialVersionUID = -7978723352517770645L;
152 
153             @Override
154             public void onClick(final AjaxRequestTarget target, final NotificationTO ignore) {
155                 send(NotificationDirectoryPanel.this, Broadcast.EXACT,
156                         new AjaxWizard.EditItemActionEvent<>(
157                                 new NotificationWrapper(restClient.read(model.getObject().getKey())), target));
158             }
159         }, ActionLink.ActionType.EDIT, IdRepoEntitlement.NOTIFICATION_UPDATE);
160 
161         actions.add(new ActionLink<>() {
162 
163             private static final long serialVersionUID = -7978723352517770645L;
164 
165             @Override
166             public void onClick(final AjaxRequestTarget target, final NotificationTO ignore) {
167                 target.add(utilityModal.setContent(new NotificationTasks(model.getObject().getKey(), pageRef)));
168                 utilityModal.header(new StringResourceModel("notification.tasks", model));
169                 utilityModal.show(true);
170                 target.add(utilityModal);
171             }
172         }, ActionLink.ActionType.NOTIFICATION_TASKS, IdRepoEntitlement.TASK_LIST);
173 
174         actions.add(new ActionLink<>() {
175 
176             private static final long serialVersionUID = -3722207913631435501L;
177 
178             @Override
179             public void onClick(final AjaxRequestTarget target, final NotificationTO ignore) {
180                 try {
181                     restClient.delete(model.getObject().getKey());
182                     SyncopeConsoleSession.get().success(getString(Constants.OPERATION_SUCCEEDED));
183                     target.add(container);
184                 } catch (SyncopeClientException e) {
185                     LOG.error("While deleting object {}", model.getObject().getKey(), e);
186                     SyncopeConsoleSession.get().onException(e);
187                 }
188                 ((BasePage) pageRef.getPage()).getNotificationPanel().refresh(target);
189             }
190         }, ActionLink.ActionType.DELETE, IdRepoEntitlement.NOTIFICATION_DELETE, true);
191 
192         return actions;
193     }
194 
195     @Override
196     protected NotificationProvider dataProvider() {
197         return new NotificationProvider(rows);
198     }
199 
200     @Override
201     protected String paginatorRowsKey() {
202         return IdRepoConstants.PREF_NOTIFICATION_PAGINATOR_ROWS;
203     }
204 
205     @Override
206     protected Collection<ActionLink.ActionType> getBatches() {
207         return List.of();
208     }
209 
210     protected class NotificationProvider extends DirectoryDataProvider<NotificationTO> {
211 
212         private static final long serialVersionUID = -276043813563988590L;
213 
214         protected final SortableDataProviderComparator<NotificationTO> comparator;
215 
216         public NotificationProvider(final int paginatorRows) {
217             super(paginatorRows);
218             comparator = new SortableDataProviderComparator<>(this);
219         }
220 
221         @Override
222         public Iterator<NotificationTO> iterator(final long first, final long count) {
223             List<NotificationTO> notifications = restClient.list();
224             notifications.sort(comparator);
225             return notifications.subList((int) first, (int) first + (int) count).iterator();
226         }
227 
228         @Override
229         public long size() {
230             return restClient.list().size();
231         }
232 
233         @Override
234         public IModel<NotificationTO> model(final NotificationTO notification) {
235             return Model.of(notification);
236         }
237     }
238 }