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.DirectoryDataProvider;
27  import org.apache.syncope.client.console.commons.IdRepoConstants;
28  import org.apache.syncope.client.console.pages.BasePage;
29  import org.apache.syncope.client.console.panels.AjaxDataTablePanel;
30  import org.apache.syncope.client.console.panels.DirectoryPanel;
31  import org.apache.syncope.client.console.panels.MultilevelPanel;
32  import org.apache.syncope.client.console.panels.MultilevelPanel.SecondLevel;
33  import org.apache.syncope.client.console.rest.ExecutionRestClient;
34  import org.apache.syncope.client.console.tasks.ExecutionsDirectoryPanel.ExecProvider;
35  import org.apache.syncope.client.console.wicket.extensions.markup.html.repeater.data.table.DatePropertyColumn;
36  import org.apache.syncope.client.console.wicket.extensions.markup.html.repeater.data.table.KeyPropertyColumn;
37  import org.apache.syncope.client.console.wicket.markup.html.form.ActionLink;
38  import org.apache.syncope.client.console.wicket.markup.html.form.ActionsPanel;
39  import org.apache.syncope.client.ui.commons.Constants;
40  import org.apache.syncope.common.lib.SyncopeClientException;
41  import org.apache.syncope.common.lib.to.ExecTO;
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.extensions.markup.html.repeater.data.sort.SortOrder;
46  import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
47  import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
48  import org.apache.wicket.model.IModel;
49  import org.apache.wicket.model.StringResourceModel;
50  
51  public abstract class ExecutionsDirectoryPanel
52          extends DirectoryPanel<ExecTO, ExecTO, ExecProvider, ExecutionRestClient> {
53  
54      private static final long serialVersionUID = 2039393934721149162L;
55  
56      protected final MultilevelPanel multiLevelPanelRef;
57  
58      protected final String key;
59  
60      public ExecutionsDirectoryPanel(
61              final MultilevelPanel multiLevelPanelRef,
62              final String key,
63              final ExecutionRestClient executionRestClient,
64              final PageReference pageRef) {
65  
66          super(MultilevelPanel.FIRST_LEVEL_ID, executionRestClient, pageRef, false);
67  
68          this.multiLevelPanelRef = multiLevelPanelRef;
69          setOutputMarkupId(true);
70          this.key = key;
71          initResultTable();
72      }
73  
74      @Override
75      protected void resultTableCustomChanges(final AjaxDataTablePanel.Builder<ExecTO, String> resultTableBuilder) {
76          resultTableBuilder.setMultiLevelPanel(multiLevelPanelRef);
77      }
78  
79      protected abstract void next(String title, SecondLevel secondLevel, AjaxRequestTarget target);
80  
81      @Override
82      protected List<IColumn<ExecTO, String>> getColumns() {
83          List<IColumn<ExecTO, String>> columns = new ArrayList<>();
84  
85          columns.add(new KeyPropertyColumn<>(
86                  new StringResourceModel(Constants.KEY_FIELD_NAME, this),
87                  Constants.KEY_FIELD_NAME, Constants.KEY_FIELD_NAME));
88  
89          columns.add(new DatePropertyColumn<>(new StringResourceModel("start", this), "start", "start"));
90  
91          columns.add(new DatePropertyColumn<>(new StringResourceModel("end", this), "end", "end"));
92  
93          columns.add(new PropertyColumn<>(new StringResourceModel("status", this), "status", "status"));
94  
95          columns.add(new PropertyColumn<>(new StringResourceModel("executor", this), "executor", "executor"));
96  
97          return columns;
98      }
99  
100     @Override
101     public ActionsPanel<ExecTO> getActions(final IModel<ExecTO> model) {
102         ActionsPanel<ExecTO> panel = super.getActions(model);
103 
104         panel.add(new ActionLink<>() {
105 
106             private static final long serialVersionUID = -3722207913631435501L;
107 
108             @Override
109             public void onClick(final AjaxRequestTarget target, final ExecTO ignore) {
110                 ExecutionsDirectoryPanel.this.getTogglePanel().close(target);
111                 next(new StringResourceModel("execution.view", ExecutionsDirectoryPanel.this, model).
112                         getObject(), new ExecMessage(model.getObject().getMessage()), target);
113             }
114         }, ActionLink.ActionType.VIEW, IdRepoEntitlement.TASK_READ);
115 
116         panel.add(new ActionLink<>() {
117 
118             private static final long serialVersionUID = -3722207913631435501L;
119 
120             @Override
121             public void onClick(final AjaxRequestTarget target, final ExecTO ignore) {
122                 ExecutionsDirectoryPanel.this.getTogglePanel().close(target);
123                 try {
124                     restClient.deleteExecution(model.getObject().getKey());
125                     SyncopeConsoleSession.get().success(getString(Constants.OPERATION_SUCCEEDED));
126                     target.add(container);
127                 } catch (SyncopeClientException e) {
128                     SyncopeConsoleSession.get().onException(e);
129                 }
130                 ((BasePage) pageRef.getPage()).getNotificationPanel().refresh(target);
131             }
132         }, ActionLink.ActionType.DELETE, IdRepoEntitlement.TASK_DELETE, true);
133 
134         addFurtherActions(panel, model);
135 
136         return panel;
137     }
138 
139     protected void addFurtherActions(final ActionsPanel<ExecTO> panel, final IModel<ExecTO> model) {
140     }
141 
142     @Override
143     protected ExecProvider dataProvider() {
144         return new ExecProvider(key, rows);
145     }
146 
147     @Override
148     protected String paginatorRowsKey() {
149         return IdRepoConstants.PREF_TASK_EXECS_PAGINATOR_ROWS;
150     }
151 
152     @Override
153     protected Collection<ActionLink.ActionType> getBatches() {
154         List<ActionLink.ActionType> batches = new ArrayList<>();
155         batches.add(ActionLink.ActionType.DELETE);
156         return batches;
157     }
158 
159     protected class ExecProvider extends DirectoryDataProvider<ExecTO> {
160 
161         private static final long serialVersionUID = 8943636537120648961L;
162 
163         private final String taskKey;
164 
165         public ExecProvider(final String taskKey, final int paginatorRows) {
166             super(paginatorRows);
167 
168             this.taskKey = taskKey;
169             setSort("end", SortOrder.DESCENDING);
170         }
171 
172         @Override
173         public Iterator<ExecTO> iterator(final long first, final long count) {
174             int page = (int) first / paginatorRows;
175             return restClient.listExecutions(
176                     taskKey, (page < 0 ? 0 : page) + 1, paginatorRows, getSort()).
177                     iterator();
178         }
179 
180         @Override
181         public long size() {
182             return restClient.countExecutions(taskKey);
183         }
184 
185         @Override
186         public IModel<ExecTO> model(final ExecTO taskExecution) {
187 
188             return new IModel<>() {
189 
190                 private static final long serialVersionUID = 7485475149862342421L;
191 
192                 @Override
193                 public ExecTO getObject() {
194                     return taskExecution;
195                 }
196             };
197         }
198     }
199 }