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.tasks;
20  
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.Iterator;
24  import java.util.List;
25  import org.apache.syncope.client.console.SyncopeConsoleSession;
26  import org.apache.syncope.client.console.commons.IdRepoConstants;
27  import org.apache.syncope.client.console.commons.TaskDataProvider;
28  import org.apache.syncope.client.console.pages.BasePage;
29  import org.apache.syncope.client.console.panels.MultilevelPanel;
30  import org.apache.syncope.client.console.rest.TaskRestClient;
31  import org.apache.syncope.client.console.wicket.extensions.markup.html.repeater.data.table.CollectionPropertyColumn;
32  import org.apache.syncope.client.console.wicket.extensions.markup.html.repeater.data.table.DatePropertyColumn;
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.ActionLink.ActionType;
36  import org.apache.syncope.client.console.wicket.markup.html.form.ActionsPanel;
37  import org.apache.syncope.client.ui.commons.Constants;
38  import org.apache.syncope.client.ui.commons.panels.ModalPanel;
39  import org.apache.syncope.common.lib.SyncopeClientException;
40  import org.apache.syncope.common.lib.to.NotificationTaskTO;
41  import org.apache.syncope.common.lib.types.AnyTypeKind;
42  import org.apache.syncope.common.lib.types.IdRepoEntitlement;
43  import org.apache.syncope.common.lib.types.MailTemplateFormat;
44  import org.apache.syncope.common.lib.types.TaskType;
45  import org.apache.wicket.PageReference;
46  import org.apache.wicket.ajax.AjaxRequestTarget;
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.IModel;
50  import org.apache.wicket.model.StringResourceModel;
51  
52  public abstract class NotificationTaskDirectoryPanel
53          extends TaskDirectoryPanel<NotificationTaskTO> implements ModalPanel {
54  
55      private static final long serialVersionUID = 4984337552918213290L;
56  
57      private final String notification;
58  
59      private final AnyTypeKind anyTypeKind;
60  
61      private final String entityKey;
62  
63      protected NotificationTaskDirectoryPanel(
64              final TaskRestClient restClient,
65              final String notification,
66              final AnyTypeKind anyTypeKind,
67              final String entityKey,
68              final MultilevelPanel multiLevelPanelRef,
69              final PageReference pageRef) {
70  
71          super(restClient, null, multiLevelPanelRef, pageRef, false);
72          this.notification = notification;
73          this.anyTypeKind = anyTypeKind;
74          this.entityKey = entityKey;
75          initResultTable();
76      }
77  
78      protected abstract void viewMailBody(MailTemplateFormat format, String content, AjaxRequestTarget target);
79  
80      @Override
81      protected List<IColumn<NotificationTaskTO, String>> getColumns() {
82          List<IColumn<NotificationTaskTO, String>> columns = new ArrayList<>();
83  
84          columns.add(new KeyPropertyColumn<>(
85                  new StringResourceModel(Constants.KEY_FIELD_NAME, this), Constants.KEY_FIELD_NAME));
86  
87          columns.add(new PropertyColumn<>(
88                  new StringResourceModel("sender", this), "sender", "sender"));
89  
90          columns.add(new PropertyColumn<>(
91                  new StringResourceModel("subject", this), "subject", "subject"));
92  
93          columns.add(new CollectionPropertyColumn<>(
94                  new StringResourceModel("recipients", this), "recipients"));
95  
96          columns.add(new DatePropertyColumn<>(
97                  new StringResourceModel("start", this), "start", "start"));
98  
99          columns.add(new DatePropertyColumn<>(
100                 new StringResourceModel("end", this), "end", "end"));
101 
102         columns.add(new PropertyColumn<>(
103                 new StringResourceModel("latestExecStatus", this), "latestExecStatus", "latestExecStatus"));
104 
105         return columns;
106     }
107 
108     @Override
109     public ActionsPanel<NotificationTaskTO> getActions(final IModel<NotificationTaskTO> model) {
110         final ActionsPanel<NotificationTaskTO> panel = super.getActions(model);
111         final NotificationTaskTO taskTO = model.getObject();
112 
113         panel.add(new ActionLink<>() {
114 
115             private static final long serialVersionUID = -3722207913631435501L;
116 
117             @Override
118             public void onClick(final AjaxRequestTarget target, final NotificationTaskTO modelObject) {
119                 viewTaskExecs(taskTO, target);
120             }
121         }, ActionLink.ActionType.VIEW, IdRepoEntitlement.TASK_READ);
122         panel.add(new ActionLink<>() {
123 
124             private static final long serialVersionUID = -3722207913631435501L;
125 
126             @Override
127             public void onClick(final AjaxRequestTarget target, final NotificationTaskTO modelObject) {
128                 viewMailBody(MailTemplateFormat.TEXT, taskTO.getTextBody(), target);
129             }
130         }, ActionLink.ActionType.TEXT, IdRepoEntitlement.TASK_READ);
131         panel.add(new ActionLink<>() {
132 
133             private static final long serialVersionUID = -3722207913631435501L;
134 
135             @Override
136             public void onClick(final AjaxRequestTarget target, final NotificationTaskTO modelObject) {
137                 viewMailBody(MailTemplateFormat.HTML, taskTO.getHtmlBody(), target);
138             }
139         }, ActionLink.ActionType.HTML, IdRepoEntitlement.TASK_READ);
140         panel.add(new ActionLink<>() {
141 
142             private static final long serialVersionUID = -3722207913631435501L;
143 
144             @Override
145             public void onClick(final AjaxRequestTarget target, final NotificationTaskTO modelObject) {
146                 try {
147                     restClient.startExecution(taskTO.getKey(), null);
148                     SyncopeConsoleSession.get().success(getString(Constants.OPERATION_SUCCEEDED));
149                     target.add(container);
150                 } catch (SyncopeClientException e) {
151                     LOG.error("While running {}", taskTO.getKey(), e);
152                     SyncopeConsoleSession.get().onException(e);
153                 }
154                 ((BasePage) pageRef.getPage()).getNotificationPanel().refresh(target);
155             }
156         }, ActionLink.ActionType.EXECUTE, IdRepoEntitlement.TASK_EXECUTE);
157         panel.add(new ActionLink<>() {
158 
159             private static final long serialVersionUID = -3722207913631435501L;
160 
161             @Override
162             public void onClick(final AjaxRequestTarget target, final NotificationTaskTO modelObject) {
163                 try {
164                     restClient.delete(TaskType.NOTIFICATION, taskTO.getKey());
165                     updateResultTable(target);
166 
167                     SyncopeConsoleSession.get().success(getString(Constants.OPERATION_SUCCEEDED));
168                     target.add(container);
169                 } catch (SyncopeClientException e) {
170                     LOG.error("While deleting {}", taskTO.getKey(), e);
171                     SyncopeConsoleSession.get().onException(e);
172                 }
173                 ((BasePage) pageRef.getPage()).getNotificationPanel().refresh(target);
174             }
175         }, ActionLink.ActionType.DELETE, IdRepoEntitlement.TASK_DELETE, true);
176 
177         return panel;
178     }
179 
180     @Override
181     protected Collection<ActionType> getBatches() {
182         List<ActionType> batches = new ArrayList<>();
183         batches.add(ActionType.DELETE);
184         batches.add(ActionType.EXECUTE);
185         return batches;
186     }
187 
188     @Override
189     protected NotificationTasksProvider dataProvider() {
190         return new NotificationTasksProvider(notification, anyTypeKind, entityKey, rows);
191     }
192 
193     @Override
194     protected String paginatorRowsKey() {
195         return IdRepoConstants.PREF_NOTIFICATION_TASKS_PAGINATOR_ROWS;
196     }
197 
198     protected class NotificationTasksProvider extends TaskDataProvider<NotificationTaskTO> {
199 
200         private static final long serialVersionUID = 4725679400450513556L;
201 
202         private final String notification;
203 
204         private final AnyTypeKind anyTypeKind;
205 
206         private final String entityKey;
207 
208         public NotificationTasksProvider(
209                 final String notification,
210                 final AnyTypeKind anyTypeKind,
211                 final String entityKey,
212                 final int paginatorRows) {
213 
214             super(paginatorRows, TaskType.NOTIFICATION);
215             this.notification = notification;
216             this.anyTypeKind = anyTypeKind;
217             this.entityKey = entityKey;
218         }
219 
220         @Override
221         public long size() {
222             return restClient.count(anyTypeKind, entityKey, notification);
223         }
224 
225         @Override
226         public Iterator<NotificationTaskTO> iterator(final long first, final long count) {
227             int page = ((int) first / paginatorRows);
228             return restClient.listNotificationTasks(
229                     notification, anyTypeKind, entityKey, (page < 0 ? 0 : page) + 1, paginatorRows, getSort()).
230                     iterator();
231         }
232     }
233 }