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.wizards.mapping;
20  
21  import java.io.Serializable;
22  import java.util.List;
23  import java.util.stream.Collectors;
24  import org.apache.commons.lang3.StringUtils;
25  import org.apache.syncope.client.console.panels.TogglePanel;
26  import org.apache.syncope.client.console.rest.ImplementationRestClient;
27  import org.apache.syncope.client.ui.commons.Constants;
28  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxPalettePanel;
29  import org.apache.syncope.common.lib.to.ImplementationTO;
30  import org.apache.syncope.common.lib.to.Item;
31  import org.apache.syncope.common.lib.types.IdRepoImplementationType;
32  import org.apache.wicket.PageReference;
33  import org.apache.wicket.ajax.AjaxRequestTarget;
34  import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
35  import org.apache.wicket.markup.html.WebMarkupContainer;
36  import org.apache.wicket.markup.html.form.Form;
37  import org.apache.wicket.markup.html.form.IChoiceRenderer;
38  import org.apache.wicket.model.IModel;
39  import org.apache.wicket.model.LoadableDetachableModel;
40  import org.apache.wicket.model.util.ListModel;
41  import org.apache.wicket.spring.injection.annot.SpringBean;
42  
43  public class ItemTransformersTogglePanel extends TogglePanel<Serializable> {
44  
45      private static final long serialVersionUID = -3195479265440591519L;
46  
47      @SpringBean
48      protected ImplementationRestClient implementationRestClient;
49  
50      protected Item item;
51  
52      public ItemTransformersTogglePanel(final WebMarkupContainer container, final PageReference pageRef) {
53          super(Constants.OUTER, "itemTransformersTogglePanel", pageRef);
54  
55          final LoadableDetachableModel<List<String>> model = new LoadableDetachableModel<>() {
56  
57              private static final long serialVersionUID = 5275935387613157437L;
58  
59              @Override
60              protected List<String> load() {
61                  // [!] this is required to disable changed with close button
62                  return item == null
63                          ? List.of()
64                          : item.getTransformers();
65              }
66          };
67  
68          Form<?> form = new Form<>("form");
69          addInnerObject(form);
70  
71          List<String> choices = implementationRestClient.list(IdRepoImplementationType.ITEM_TRANSFORMER).stream().
72                  map(ImplementationTO::getKey).sorted().collect(Collectors.toList());
73  
74          form.add(new AjaxPalettePanel.Builder<String>().setAllowOrder(true).setRenderer(new IChoiceRenderer<>() {
75  
76              private static final long serialVersionUID = 3464376099975468136L;
77  
78              private static final int MAX_LENGTH = 50;
79  
80              @Override
81              public Object getDisplayValue(final String object) {
82                  if (object.length() > MAX_LENGTH) {
83                      return "..." + object.substring(object.length() - MAX_LENGTH);
84                  } else {
85                      return object;
86                  }
87              }
88  
89              @Override
90              public String getIdValue(final String object, final int index) {
91                  return object;
92              }
93  
94              @Override
95              public String getObject(final String id, final IModel<? extends List<? extends String>> choices) {
96                  return id;
97              }
98          }).build(
99                  "classes",
100                 model,
101                 new ListModel<>(choices)).
102                 hideLabel().setEnabled(true).setOutputMarkupId(true));
103 
104         form.add(new AjaxSubmitLink("submit", form) {
105 
106             private static final long serialVersionUID = 5538299138211283825L;
107 
108             @Override
109             public void onSubmit(final AjaxRequestTarget target) {
110                 toggle(target, false);
111                 target.add(container);
112             }
113 
114         });
115     }
116 
117     public ItemTransformersTogglePanel setItem(final AjaxRequestTarget target, final Item item) {
118         this.item = item;
119         setHeader(target, StringUtils.EMPTY);
120         return this;
121     }
122 }