View Javadoc
1   /**
2    * This file Copyright (c) 2013-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.vaadin.integration.contentconnector;
35  
36  import info.magnolia.cms.core.Path;
37  import info.magnolia.cms.core.version.VersionManager;
38  import info.magnolia.objectfactory.ComponentProvider;
39  import info.magnolia.ui.vaadin.integration.jcr.JcrItemAdapter;
40  import info.magnolia.ui.vaadin.integration.jcr.JcrItemId;
41  import info.magnolia.ui.vaadin.integration.jcr.JcrItemUtil;
42  import info.magnolia.ui.vaadin.integration.jcr.JcrNewNodeAdapter;
43  import info.magnolia.ui.vaadin.integration.jcr.JcrNewNodeItemId;
44  import info.magnolia.ui.vaadin.integration.jcr.JcrNodeAdapter;
45  import info.magnolia.ui.vaadin.integration.jcr.JcrPropertyAdapter;
46  import info.magnolia.ui.vaadin.integration.jcr.JcrPropertyItemId;
47  
48  import javax.inject.Inject;
49  import javax.jcr.Node;
50  import javax.jcr.Property;
51  import javax.jcr.RepositoryException;
52  import javax.jcr.version.Version;
53  
54  import org.apache.commons.lang3.StringUtils;
55  import org.slf4j.Logger;
56  import org.slf4j.LoggerFactory;
57  
58  import com.vaadin.v7.data.Item;
59  
60  /**
61   * JCR-based implementation of {@link info.magnolia.ui.vaadin.integration.contentconnector.ContentConnector}.
62   */
63  public class JcrContentConnector extends AbstractContentConnector implements SupportsVersions, SupportsCreation {
64  
65      private Logger log = LoggerFactory.getLogger(getClass());
66  
67      private VersionManager versionManager;
68  
69      @Inject
70      public JcrContentConnector(final VersionManager versionManager, JcrContentConnectorDefinition definition) {
71          super(definition);
72          this.versionManager = versionManager;
73      }
74  
75      /**
76       * @deprecated since 5.4.4 - use {@link #JcrContentConnector(VersionManager, JcrContentConnectorDefinition)}.
77       * instead.
78       */
79      @Deprecated
80      public JcrContentConnector(final VersionManager versionManager, JcrContentConnectorDefinition definition, ComponentProvider componentProvider) {
81          this(versionManager, definition);
82      }
83  
84      @Override
85      public String getItemUrlFragment(Object itemId) {
86          try {
87              if (itemId instanceof JcrItemId) {
88                  JcrItemId../../../../info/magnolia/ui/vaadin/integration/jcr/JcrItemId.html#JcrItemId">JcrItemId jcrItemId = (JcrItemId) itemId;
89                  javax.jcr.Item selected = JcrItemUtil.getJcrItem(jcrItemId);
90                  String selectedPath = JcrItemUtil.getItemPath(selected);
91                  String rootPath = getRootPath();
92                  String urlFragment = StringUtils.removeStart(selectedPath, "/".equals(rootPath) ? "" : rootPath);
93                  if (itemId instanceof JcrNewNodeItemId) {
94                      if (!urlFragment.endsWith("/")) {
95                          urlFragment += "/";
96                      }
97                      urlFragment += ((JcrNewNodeItemId)jcrItemId).getName();
98                  }
99                  return urlFragment;
100             }
101         } catch (RepositoryException e) {
102             log.error("Failed to convert item id to URL fragment: " + e.getMessage(), e);
103         }
104         return null;
105     }
106 
107 
108     @Override
109     public JcrItemId getItemIdByUrlFragment(String urlFragment) {
110         try {
111             String fullFragment = ("/".equals(getRootPath()) ? "" : getRootPath()) + urlFragment;
112             String nodePath = JcrItemUtil.parseNodeIdentifier(fullFragment);
113             nodePath = !StringUtils.isBlank(nodePath) ? nodePath : getRootPath();
114             JcrItemId nodeItemId = JcrItemUtil.getItemId(getWorkspace(), nodePath);
115             if (nodeItemId == null || !JcrItemUtil.isPropertyItemId(fullFragment)) {
116                 return nodeItemId;
117             } else {
118                 return new JcrPropertyItemId(nodeItemId, parsePropertyName(fullFragment));
119             }
120         } catch (RepositoryException e) {
121             log.error("Failed to obtain JCR id for fragment: " + e.getMessage(), e);
122             return null;
123         }
124     }
125 
126 
127     @Override
128     public JcrItemAdapter getItem(Object itemId) {
129         if (!(itemId instanceof JcrItemId)) {
130             return null;
131         }
132 
133         javax.jcr.Item jcrItem;
134         try {
135             jcrItem = JcrItemUtil.getJcrItem((JcrItemId) itemId);
136             if (jcrItem == null) {
137                 return null;
138             }
139             JcrItemAdapter itemAdapter;
140             if (jcrItem.isNode()) {
141                 if (itemId instanceof JcrNewNodeItemId) {
142                     itemAdapter = new JcrNewNodeAdapter((Node) jcrItem, ((JcrNewNodeItemIdui/vaadin/integration/jcr/JcrNewNodeItemId.html#JcrNewNodeItemId">JcrNewNodeItemId)itemId).getPrimaryNodeType(), ((JcrNewNodeItemId) itemId).getName());
143                 } else {
144                     itemAdapter = new JcrNodeAdapter((Node) jcrItem);
145                 }
146             } else {
147                 itemAdapter = new JcrPropertyAdapter((Property) jcrItem);
148             }
149             return itemAdapter;
150         } catch (RepositoryException e) {
151             log.error("Failed to find item for id: " + itemId, e.getMessage());
152             return null;
153         } catch (Exception e) {
154             log.error("Unknown error for: " + itemId, e.getMessage());
155             return null;
156         }
157     }
158 
159     @Override
160     public Object getItemId(Item item) {
161         if (item instanceof JcrItemAdapter) {
162             return ((JcrItemAdapter)item).getItemId();
163         }
164         return null;
165     }
166 
167     @Override
168     public Object getDefaultItemId() {
169         try {
170             return  JcrItemUtil.getItemId(getWorkspace(), getRootPath());
171         } catch (RepositoryException e) {
172             log.error("Failed to retrieve default id: " + e.getMessage() + ", returning null.", e);
173             return null;
174         }
175     }
176 
177     @Override
178     public boolean canHandleItem(Object itemId) {
179         if (itemId instanceof JcrItemId) {
180             JcrItemId/../../../../../info/magnolia/ui/vaadin/integration/jcr/JcrItemId.html#JcrItemId">JcrItemId jcrId = (JcrItemId) itemId;
181             return jcrId.getWorkspace().equalsIgnoreCase(getWorkspace());
182         }
183         return false;
184     }
185 
186     @Override
187     public Object getItemVersion(Object itemId, String versionName) {
188         try {
189             Node node = (Node) JcrItemUtil.getJcrItem((JcrItemId) itemId);
190             Version version = versionManager.getVersion(node, versionName);
191             return JcrItemUtil.getItemId(version);
192         } catch (RepositoryException e) {
193             log.error("Failed to find item version for id: " + itemId, e.getMessage());
194         }
195         return null;
196     }
197 
198     private static final String NEW_NODE_NAME = "untitled";
199 
200     @Override
201     public Object getNewItemId(Object parentId, Object typeDefinition) {
202 
203         String primaryNodeType = String.valueOf(typeDefinition);
204         try {
205             Node parent = (Node)JcrItemUtil.getJcrItem((JcrItemId)parentId);
206             JcrNewNodeItemIdrNewNodeItemId.html#JcrNewNodeItemId">JcrNewNodeItemId jcrNewNodeItemId = new JcrNewNodeItemId(parent.getIdentifier(), getWorkspace(), primaryNodeType);
207             jcrNewNodeItemId.setName(Path.getUniqueLabel(parent, NEW_NODE_NAME));
208             return jcrNewNodeItemId;
209         } catch (RepositoryException e) {
210             log.error("Failed to create new jcr node item id: " + e.getMessage(), e);
211         }
212 
213         return null;
214     }
215 
216     protected String getRootPath() {
217         return getContentConnectorDefinition().getRootPath();
218     }
219 
220     protected String getWorkspace() {
221         return getContentConnectorDefinition().getWorkspace();
222     }
223 
224     @Override
225     public JcrContentConnectorDefinition getContentConnectorDefinition() {
226         return (JcrContentConnectorDefinition) super.getContentConnectorDefinition();
227     }
228 
229     /**
230      * @deprecated since 5.4.8 - use {@link JcrItemUtil#parseNodeIdentifier(String)} instead.
231      */
232     @Deprecated
233     protected final String parseNodePath(final String urlFragment) {
234         return JcrItemUtil.parseNodeIdentifier(urlFragment);
235     }
236 
237     /**
238      * @deprecated since 5.4.8 - use {@link JcrItemUtil#parsePropertyName(String)} instead.
239      */
240     @Deprecated
241     protected final String parsePropertyName(final String urlFragment) {
242         return JcrItemUtil.parsePropertyName(urlFragment);
243     }
244 
245     /**
246      * @deprecated since 5.4.8 - use {@link JcrItemUtil#isPropertyItemId(String)} instead.
247      */
248     @Deprecated
249     protected final boolean isPropertyItemId(final String urlFragment) {
250         return JcrItemUtil.isPropertyItemId(urlFragment);
251     }
252 }