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.events;
20  
21  import java.util.Collections;
22  import java.util.HashSet;
23  import java.util.List;
24  import java.util.Set;
25  import org.apache.commons.lang3.StringUtils;
26  import org.apache.syncope.client.ui.commons.ajax.form.IndicatorAjaxFormChoiceComponentUpdatingBehavior;
27  import org.apache.syncope.common.lib.audit.EventCategory;
28  import org.apache.syncope.common.lib.types.AuditElements;
29  import org.apache.syncope.common.lib.types.AuditLoggerName;
30  import org.apache.wicket.ajax.AjaxRequestTarget;
31  import org.apache.wicket.ajax.form.AjaxFormChoiceComponentUpdatingBehavior;
32  import org.apache.wicket.event.Broadcast;
33  import org.apache.wicket.event.IEvent;
34  import org.apache.wicket.markup.html.basic.Label;
35  import org.apache.wicket.markup.html.form.Check;
36  import org.apache.wicket.markup.html.form.CheckGroup;
37  import org.apache.wicket.markup.html.form.CheckGroupSelector;
38  import org.apache.wicket.markup.html.list.ListItem;
39  import org.apache.wicket.markup.html.list.ListView;
40  import org.apache.wicket.markup.html.panel.Panel;
41  import org.apache.wicket.model.IModel;
42  import org.apache.wicket.model.Model;
43  import org.apache.wicket.model.ResourceModel;
44  
45  public abstract class EventSelectionPanel extends Panel {
46  
47      private static final long serialVersionUID = 752233163798301002L;
48  
49      private final Set<String> selected = new HashSet<>();
50  
51      public EventSelectionPanel(final String id, final EventCategory eventCategory, final IModel<List<String>> model) {
52          super(id);
53          setOutputMarkupId(true);
54  
55          List<String> events = getEvents(eventCategory);
56  
57          // needed to avoid model reset: model have to be managed into SelectedEventsPanel
58          selected.addAll(model.getObject());
59  
60          CheckGroup<String> successGroup = new CheckGroup<>("successGroup", selected);
61          successGroup.add(new AjaxFormChoiceComponentUpdatingBehavior() {
62  
63              private static final long serialVersionUID = -151291731388673682L;
64  
65              @Override
66              protected void onUpdate(final AjaxRequestTarget target) {
67                  Set<AuditLoggerName> toBeRemoved = new HashSet<>();
68                  Set<AuditLoggerName> toBeAdded = new HashSet<>();
69  
70                  getEvents(eventCategory).forEach(event -> {
71                      AuditLoggerName auditLoggerName = new AuditLoggerName(
72                              eventCategory.getType(),
73                              eventCategory.getCategory(),
74                              eventCategory.getSubcategory(),
75                              event,
76                              AuditElements.Result.SUCCESS);
77  
78                      if (successGroup.getModelObject().contains(auditLoggerName.toString())) {
79                          toBeAdded.add(auditLoggerName);
80                      } else {
81                          toBeRemoved.add(auditLoggerName);
82                      }
83                  });
84  
85                  send(EventSelectionPanel.this.getPage(), Broadcast.BREADTH,
86                          new SelectedEventsPanel.EventSelectionChanged(target, toBeAdded, toBeRemoved));
87              }
88          });
89          successGroup.setVisible(!events.isEmpty());
90          add(successGroup);
91  
92          add(new Label("successLabel", new ResourceModel("Success", "Success"))).setVisible(!events.isEmpty());
93  
94          CheckGroupSelector successSelector = new CheckGroupSelector("successSelector", successGroup);
95          successSelector.setVisible(!events.isEmpty());
96          add(successSelector);
97  
98          ListView<String> categoryView = new ListView<>("categoryView", events) {
99  
100             private static final long serialVersionUID = 4949588177564901031L;
101 
102             @Override
103             protected void populateItem(final ListItem<String> item) {
104                 item.add(new Label("subcategory", Model.of(item.getModelObject())));
105             }
106         };
107         add(categoryView);
108 
109         ListView<String> successView = new ListView<>("successView", events) {
110 
111             private static final long serialVersionUID = 4949588177564901031L;
112 
113             @Override
114             protected void populateItem(final ListItem<String> item) {
115                 item.add(new Check<>("successCheck",
116                         new Model<>(AuditLoggerName.buildEvent(
117                                 eventCategory.getType(),
118                                 eventCategory.getCategory(),
119                                 eventCategory.getSubcategory(),
120                                 item.getModelObject(),
121                                 AuditElements.Result.SUCCESS)),
122                         successGroup));
123             }
124         };
125         successGroup.add(successView);
126 
127         CheckGroup<String> failureGroup = new CheckGroup<>("failureGroup", selected);
128         failureGroup.add(new IndicatorAjaxFormChoiceComponentUpdatingBehavior() {
129 
130             private static final long serialVersionUID = -151291731388673682L;
131 
132             @Override
133             protected void onUpdate(final AjaxRequestTarget target) {
134                 Set<AuditLoggerName> toBeRemoved = new HashSet<>();
135                 Set<AuditLoggerName> toBeAdded = new HashSet<>();
136 
137                 getEvents(eventCategory).forEach(event -> {
138                     AuditLoggerName auditLoggerName = new AuditLoggerName(
139                             eventCategory.getType(),
140                             eventCategory.getCategory(),
141                             eventCategory.getSubcategory(),
142                             event,
143                             AuditElements.Result.FAILURE);
144 
145                     if (failureGroup.getModelObject().contains(auditLoggerName.toString())) {
146                         toBeAdded.add(auditLoggerName);
147                     } else {
148                         toBeRemoved.add(auditLoggerName);
149                     }
150                 });
151 
152                 send(EventSelectionPanel.this.getPage(), Broadcast.BREADTH,
153                         new SelectedEventsPanel.EventSelectionChanged(target, toBeAdded, toBeRemoved));
154             }
155         });
156         failureGroup.setVisible(!events.isEmpty());
157         add(failureGroup);
158 
159         add(new Label("failureLabel", new ResourceModel("Failure", "Failure"))).setVisible(!events.isEmpty());
160 
161         CheckGroupSelector failureSelector = new CheckGroupSelector("failureSelector", failureGroup);
162         failureSelector.setVisible(!events.isEmpty());
163         add(failureSelector);
164 
165         ListView<String> failureView = new ListView<>("failureView", events) {
166 
167             private static final long serialVersionUID = 4949588177564901031L;
168 
169             @Override
170             protected void populateItem(final ListItem<String> item) {
171                 item.add(new Check<>("failureCheck",
172                         new Model<>(AuditLoggerName.buildEvent(
173                                 eventCategory.getType(),
174                                 eventCategory.getCategory(),
175                                 eventCategory.getSubcategory(),
176                                 item.getModelObject(),
177                                 AuditElements.Result.FAILURE)),
178                         failureGroup));
179             }
180         };
181         failureGroup.add(failureView);
182     }
183 
184     private static List<String> getEvents(final EventCategory eventCategoryTO) {
185         final List<String> res;
186 
187         res = eventCategoryTO.getEvents();
188 
189         if (res.isEmpty()) {
190             if ((AuditElements.EventCategoryType.PROPAGATION == eventCategoryTO.getType()
191                     || AuditElements.EventCategoryType.PULL == eventCategoryTO.getType()
192                     || AuditElements.EventCategoryType.PUSH == eventCategoryTO.getType())
193                     && StringUtils.isEmpty(eventCategoryTO.getCategory())) {
194                 res.add(eventCategoryTO.getType().toString());
195             } else if (AuditElements.EventCategoryType.TASK == eventCategoryTO.getType()
196                     && StringUtils.isNotEmpty(eventCategoryTO.getCategory())) {
197                 res.add(eventCategoryTO.getCategory());
198             } else if (AuditElements.EventCategoryType.REPORT == eventCategoryTO.getType()
199                     && StringUtils.isNotEmpty(eventCategoryTO.getCategory())) {
200                 res.add(eventCategoryTO.getCategory());
201             }
202         } else {
203             Collections.sort(res);
204         }
205 
206         return res;
207     }
208 
209     /**
210      * To be extended in order to add actions on events.
211      *
212      * @param event event.
213      */
214     protected abstract void onEventAction(IEvent<?> event);
215 
216     @Override
217     public void onEvent(final IEvent<?> event) {
218         onEventAction(event);
219     }
220 }