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.widgets;
20  
21  import de.agilecoders.wicket.core.markup.html.bootstrap.components.PopoverBehavior;
22  import de.agilecoders.wicket.core.markup.html.bootstrap.components.PopoverConfig;
23  import de.agilecoders.wicket.core.markup.html.bootstrap.components.TooltipConfig;
24  import java.io.Serializable;
25  import org.apache.commons.lang3.StringUtils;
26  import org.apache.syncope.client.console.SyncopeConsoleSession;
27  import org.apache.syncope.client.console.pages.BasePage;
28  import org.apache.syncope.client.console.rest.NotificationRestClient;
29  import org.apache.syncope.client.console.rest.ReportRestClient;
30  import org.apache.syncope.client.console.rest.TaskRestClient;
31  import org.apache.syncope.client.console.wicket.ajax.markup.html.IndicatorAjaxLink;
32  import org.apache.syncope.client.console.wizards.WizardMgtPanel;
33  import org.apache.syncope.client.ui.commons.Constants;
34  import org.apache.syncope.common.lib.to.JobTO;
35  import org.apache.syncope.common.lib.types.JobAction;
36  import org.apache.wicket.Component;
37  import org.apache.wicket.ajax.AjaxRequestTarget;
38  import org.apache.wicket.event.Broadcast;
39  import org.apache.wicket.markup.html.basic.Label;
40  import org.apache.wicket.markup.html.panel.Fragment;
41  import org.apache.wicket.model.Model;
42  import org.apache.wicket.spring.injection.annot.SpringBean;
43  import org.slf4j.Logger;
44  import org.slf4j.LoggerFactory;
45  
46  public class JobActionPanel extends WizardMgtPanel<Serializable> {
47  
48      private static final long serialVersionUID = 6645135178773151224L;
49  
50      protected static final Logger LOG = LoggerFactory.getLogger(JobActionPanel.class);
51  
52      @SpringBean
53      protected NotificationRestClient notificationRestClient;
54  
55      @SpringBean
56      protected ReportRestClient reportRestClient;
57  
58      @SpringBean
59      protected TaskRestClient taskRestClient;
60  
61      public JobActionPanel(
62              final String id,
63              final JobTO jobTO,
64              final boolean showNotRunning,
65              final Component container) {
66  
67          super(id, true);
68          setOutputMarkupId(true);
69  
70          Fragment controls;
71          if (jobTO.isRunning()) {
72              controls = new Fragment("controls", "runningFragment", this);
73              controls.add(new Label("status", Model.of()).add(new PopoverBehavior(
74                      Model.<String>of(),
75                      Model.of("<pre>" + (jobTO.getStatus() == null ? StringUtils.EMPTY : jobTO.getStatus()) + "</pre>"),
76                      new PopoverConfig().withAnimation(true).withHoverTrigger().withHtml(true).
77                              withPlacement(TooltipConfig.Placement.left))));
78              controls.add(new IndicatorAjaxLink<Void>("stop") {
79  
80                  private static final long serialVersionUID = -7978723352517770644L;
81  
82                  @Override
83                  public void onClick(final AjaxRequestTarget target) {
84                      try {
85                          switch (jobTO.getType()) {
86                              case NOTIFICATION:
87                                  notificationRestClient.actionJob(JobAction.STOP);
88                                  break;
89  
90                              case REPORT:
91                                  reportRestClient.actionJob(jobTO.getRefKey(), JobAction.STOP);
92                                  break;
93  
94                              case TASK:
95                                  taskRestClient.actionJob(jobTO.getRefKey(), JobAction.STOP);
96                                  break;
97  
98                              default:
99                          }
100                         SyncopeConsoleSession.get().success(getString(Constants.OPERATION_SUCCEEDED));
101                         send(container, Broadcast.EXACT, new JobActionPayload(target));
102                     } catch (Exception e) {
103                         LOG.error("While stopping {}", jobTO.getRefDesc(), e);
104                         SyncopeConsoleSession.get().onException(e);
105                     }
106                     ((BasePage) getPage()).getNotificationPanel().refresh(target);
107                 }
108             });
109         } else {
110             controls = new Fragment("controls", "notRunningFragment", this);
111             controls.add(new IndicatorAjaxLink<Void>("start") {
112 
113                 private static final long serialVersionUID = -7978723352517770644L;
114 
115                 @Override
116                 public void onClick(final AjaxRequestTarget target) {
117                     try {
118                         switch (jobTO.getType()) {
119                             case NOTIFICATION:
120                                 notificationRestClient.actionJob(JobAction.START);
121                                 break;
122 
123                             case REPORT:
124                                 reportRestClient.actionJob(jobTO.getRefKey(), JobAction.START);
125                                 break;
126 
127                             case TASK:
128                                 taskRestClient.actionJob(jobTO.getRefKey(), JobAction.START);
129                                 break;
130 
131                             default:
132                         }
133                         SyncopeConsoleSession.get().success(getString(Constants.OPERATION_SUCCEEDED));
134                         send(container, Broadcast.EXACT, new JobActionPayload(target));
135                     } catch (Exception e) {
136                         LOG.error("While starting {}", jobTO.getRefDesc(), e);
137                         SyncopeConsoleSession.get().onException(e);
138                     }
139                     ((BasePage) getPage()).getNotificationPanel().refresh(target);
140                 }
141             });
142             if (!showNotRunning) {
143                 controls.setOutputMarkupPlaceholderTag(true);
144                 controls.setVisible(false);
145             }
146         }
147         addInnerObject(controls);
148     }
149 
150     public static class JobActionPayload implements Serializable {
151 
152         private static final long serialVersionUID = -6798174303329212126L;
153 
154         private final AjaxRequestTarget target;
155 
156         public JobActionPayload(final AjaxRequestTarget target) {
157             this.target = target;
158         }
159 
160         public AjaxRequestTarget getTarget() {
161             return target;
162         }
163     }
164 }