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.wicket.extensions.markup.html.repeater.data.table;
20  
21  import java.lang.reflect.InvocationTargetException;
22  import java.util.Map;
23  import java.util.Objects;
24  import org.apache.commons.lang3.ArrayUtils;
25  import org.apache.syncope.common.lib.types.ExecStatus;
26  import org.apache.wicket.Component;
27  import org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator;
28  import org.apache.wicket.extensions.markup.html.repeater.data.table.AbstractColumn;
29  import org.apache.wicket.markup.html.basic.Label;
30  import org.apache.wicket.markup.repeater.Item;
31  import org.apache.wicket.model.IModel;
32  import org.apache.wicket.model.Model;
33  import org.apache.wicket.model.StringResourceModel;
34  import org.slf4j.Logger;
35  import org.slf4j.LoggerFactory;
36  import org.springframework.beans.BeanUtils;
37  import org.springframework.beans.BeansException;
38  
39  public class BatchResponseColumn<T, S> extends AbstractColumn<T, S> {
40  
41      private static final long serialVersionUID = 7955560320949560716L;
42  
43      private static final Logger LOG = LoggerFactory.getLogger(BatchResponseColumn.class);
44  
45      private final Map<String, String> results;
46  
47      private final String keyFieldName;
48  
49      public BatchResponseColumn(final Map<String, String> results, final String keyFieldName) {
50          super(new Model<>());
51          this.results = results;
52          this.keyFieldName = keyFieldName;
53      }
54  
55      @Override
56      public Component getHeader(final String componentId) {
57          Label label = new Label(componentId, new Model<>());
58          label.setDefaultModel(new StringResourceModel("batch.result.header", label, new Model<>("Result")));
59          return label;
60      }
61  
62      @Override
63      public void populateItem(final Item<ICellPopulator<T>> item, final String componentId, final IModel<T> rowModel) {
64          try {
65              Object key = Objects.requireNonNull(
66                  BeanUtils.getPropertyDescriptor(rowModel.getObject().getClass(), keyFieldName))
67                  .getReadMethod().invoke(rowModel.getObject(), ArrayUtils.EMPTY_OBJECT_ARRAY);
68              String status = results.containsKey(key.toString())
69                      ? results.get(key.toString())
70                      : ExecStatus.NOT_ATTEMPTED.name();
71  
72              item.add(new Label(componentId, new StringResourceModel(status, item, new Model<>(status))));
73          } catch (BeansException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
74              LOG.error("Error retrieving target key value", e);
75          }
76      }
77  }