View Javadoc
1   /**
2    * This file Copyright (c) 2018 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.databinding.converter;
35  
36  import info.magnolia.cms.util.PathUtil;
37  import info.magnolia.context.Context;
38  import info.magnolia.ui.datasource.jcr.SessionReattachingNodeWrapper;
39  import info.magnolia.ui.datasource.jcr.JcrDatasourceDefinition;
40  
41  import java.util.Optional;
42  
43  import javax.inject.Provider;
44  import javax.jcr.Node;
45  import javax.jcr.RepositoryException;
46  import javax.jcr.Session;
47  
48  import org.slf4j.Logger;
49  import org.slf4j.LoggerFactory;
50  
51  import com.vaadin.data.Converter;
52  import com.vaadin.data.Result;
53  import com.vaadin.data.ValueContext;
54  
55  import lombok.SneakyThrows;
56  
57  /**
58   * JCR-specific implementation of {@link Converter}. Turns a Node (presentation type) into its identifier (model type or a String) and vice versa.
59   * Converting to presentation will attempt to resolve a node either from its identifier or its path.
60   */
61  public class JcrItemToLinkConverter implements Converter<Node, String> {
62  
63      private static final Logger log = LoggerFactory.getLogger(JcrItemToLinkConverter.class);
64      private final JcrDatasourceDefinition definition;
65      private final Provider<Context> contextProvider;
66  
67      public JcrItemToLinkConverter(JcrDatasourceDefinition definition, Provider<Context> contextProvider) {
68          this.definition = definition;
69          this.contextProvider = contextProvider;
70      }
71  
72      @SneakyThrows
73      @Override
74      public Result<String> convertToModel(Node value, ValueContext context) {
75          if (value == null) {
76              return Result.ok(null);
77          }
78          return Result.ok(value.getIdentifier());
79      }
80  
81      @Override
82      public Node convertToPresentation(String value, ValueContext context) {
83          if (value == null) {
84              return null;
85          }
86          return resolveNodeByIdOrPath(value);
87      }
88  
89      private Node resolveNodeByIdOrPath(String value) {
90          try {
91              // if value isn't an id, it may be a node name used for setting a default value
92              return new SessionReattachingNodeWrapper(Optional.ofNullable(getNodeByIdSafely(value))
93                      .orElseGet(() -> getNodeByPath(value)));
94          } catch (RepositoryException e) {
95              log.warn("Failed to resolve node with provided value [{}]. The item may have been deleted or its name misspelled. Will return null", value);
96              return null;
97          }
98      }
99      
100     private Session getSession() throws RepositoryException {
101         return contextProvider.get().getJCRSession(definition.getWorkspace());
102     }
103 
104     private Node getNodeByIdSafely(String value) {
105         try {
106             return getSession().getNodeByIdentifier(value);
107         } catch (RepositoryException e) {
108             // here we ignore the exception as value may be a path
109             return null;
110         }
111     }
112 
113     @SneakyThrows(RepositoryException.class)
114     private Node getNodeByPath(String value) {
115         return getSession().getNode(PathUtil.createPath(definition.getRootPath(), value));
116     }
117 }