View Javadoc
1   /**
2    * This file Copyright (c) 2021 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.ui.field;
35  
36  
37  import info.magnolia.objectfactory.ComponentProvider;
38  import info.magnolia.ui.contentapp.FilteringMode;
39  import info.magnolia.ui.datasource.ItemResolver;
40  
41  import java.util.Optional;
42  
43  import javax.inject.Inject;
44  
45  import com.vaadin.data.Converter;
46  import com.vaadin.data.HasValue;
47  import com.vaadin.data.Result;
48  import com.vaadin.data.ValueContext;
49  
50  /**
51   * Link field specific binder. Uses different converter based on the inner field.
52   * Either {@link info.magnolia.ui.datasource.ItemResolver} in case of {@link com.vaadin.ui.TextField} or {@link SelectFieldSupport#defaultConverter()} in case of {@link com.vaadin.ui.ComboBox}.
53   *
54   * @param <T> type of the bound item.
55   */
56  public class LinkFieldBinder<T> extends FieldBinder.Default<T> {
57  
58      private final SelectFieldSupport<T> selectFieldSupport;
59      private final ItemResolver<T> itemResolver;
60  
61      @Inject
62      LinkFieldBinder(ComponentProvider componentProvider, SelectFieldSupport<T> selectFieldSupport, ItemResolver<T> itemResolver) {
63          super(componentProvider);
64          this.selectFieldSupport = selectFieldSupport;
65          this.itemResolver = itemResolver;
66      }
67  
68      @Override
69      protected <PT> Optional<Converter<PT, ?>> createConfiguredConverter(FieldDefinition<PT> definition, HasValue<?> field) {
70          Converter configuredConverter = super.createConfiguredConverter(definition, field).orElse(null);
71          if (configuredConverter == null) {
72              final Converter defaultConverter = selectFieldSupport.defaultConverter();
73              configuredConverter = field instanceof TextLinkField ? new PlainTextLinkConverter() : defaultConverter;
74          }
75          return Optional.of(configuredConverter);
76      }
77  
78      private class PlainTextLinkConverter implements Converter<String, String> {
79  
80          @Override
81          public Result<String> convertToModel(String value, ValueContext context) {
82              final T item = itemResolver.getItemById(value).orElse(null);
83              return item == null ? Result.ok(value) : selectFieldSupport.defaultConverter().convertToModel(item, context);
84          }
85  
86          @Override
87          public String convertToPresentation(String value, ValueContext context) {
88              if (value == null) {
89                  return null;
90              }
91              try {
92                  final T item = selectFieldSupport.defaultConverter().convertToPresentation(value, context);
93                  return item == null ? value : itemResolver.getId(item);
94              } catch (RuntimeException e) { //e.g. invalid UUID or external link such as https://jira.magnolia-cms.com/browse/MGNLUI-6530
95                  return value;
96              }
97          }
98      }
99  
100 
101 }