View Javadoc
1   /**
2    * This file Copyright (c) 2016-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.contentapp.browser;
35  
36  import info.magnolia.cms.core.Path;
37  import info.magnolia.jcr.util.NodeTypes;
38  import info.magnolia.jcr.util.NodeUtil;
39  import info.magnolia.ui.api.ioc.SubAppScoped;
40  import info.magnolia.ui.framework.ContentClipboard;
41  import info.magnolia.ui.framework.ContentClipboardException;
42  import info.magnolia.ui.vaadin.integration.jcr.JcrItemId;
43  import info.magnolia.ui.vaadin.integration.jcr.JcrItemUtil;
44  
45  import java.text.MessageFormat;
46  import java.util.ArrayList;
47  import java.util.List;
48  
49  import javax.jcr.Item;
50  import javax.jcr.Node;
51  import javax.jcr.Property;
52  import javax.jcr.RepositoryException;
53  import javax.jcr.nodetype.NodeType;
54  
55  import org.slf4j.Logger;
56  import org.slf4j.LoggerFactory;
57  
58  /**
59   * Default JCR implementation of {@link info.magnolia.ui.framework.ContentClipboard}.
60   *
61   * @deprecated since 6.2 - use {@link info.magnolia.ui.contentapp.action.clipboard.JcrClipboard} instead.
62   *
63   * @see <a href="https://documentation.magnolia-cms.com/display/DOCS62/Upgrading+to+Magnolia+6.2.x">Upgrading to Magnolia 6.2.x</a>
64   */
65  @Deprecated
66  @SubAppScoped
67  public class JcrContentClipboard implements ContentClipboard<JcrItemId> {
68  
69      private static final Logger log = LoggerFactory.getLogger(JcrContentClipboard.class);
70  
71      private List<JcrItemId> items = new ArrayList<>();
72  
73      @Override
74      public void copy(final List<JcrItemId> source) {
75          if (canCopy(source)) {
76              this.items = source;
77          }
78      }
79  
80      @Override
81      public List<JcrItemId> paste(final JcrItemId destination) {
82          List<JcrItemId> pastedItems = new ArrayList<>();
83          try {
84              if (canPasteInto(destination)) {
85                  for (JcrItemId sourceItem : items) {
86                      pastedItems.add(pasteSingleItem(sourceItem, destination));
87                  }
88              }
89          } catch (RepositoryException e) {
90              throw new ContentClipboardException(MessageFormat.format("Failed while pasting items under JCR item with UUID [{0}] from [{1}] workspace.", destination.getUuid(), destination.getWorkspace()), e);
91          }
92          return pastedItems;
93      }
94  
95      protected JcrItemIdration/jcr/JcrItemId.html#JcrItemId">JcrItemIdcr/JcrItemId.html#JcrItemId">JcrItemId pasteSingleItem(final JcrItemIdration/jcr/JcrItemId.html#JcrItemId">JcrItemId source, final JcrItemId destination) throws RepositoryException {
96          Item sourceItem = JcrItemUtil.getJcrItem(source);
97          Node destinationNode = (Node) JcrItemUtil.getJcrItem(destination);
98          if (sourceItem.isNode()) {
99              return pasteSingleNode((Node) sourceItem, destinationNode);
100         } else {
101             return pasteSingleProperty((Property) sourceItem, destinationNode);
102         }
103     }
104 
105     protected JcrItemId pasteSingleNode(final Node sourceNode, final Node destinationNode) throws RepositoryException {
106         // we assume copying only inside one workspace.
107         // if we support different workspaces (and different repositories), we need to manually duplicate instead of using #copy().
108 
109         String newName = getUniqueNewItemName(sourceNode, destinationNode);
110         String newPath = Path.getAbsolutePath(destinationNode.getPath(), newName);
111 
112         sourceNode.getSession().getWorkspace().copy(sourceNode.getPath(), newPath);
113         sourceNode.getSession().save();
114         Node newNode = sourceNode.getSession().getNode(newPath);
115         return JcrItemUtil.getItemId(newNode);
116     }
117 
118     protected JcrItemId pasteSingleProperty(final Property property, final Node destinationNode) throws RepositoryException {
119         String newName = getUniqueNewItemName(property, destinationNode);
120         if (property.isMultiple()) {
121             destinationNode.setProperty(newName, property.getValues());
122         } else {
123             destinationNode.setProperty(newName, property.getValue());
124         }
125         destinationNode.getSession().save();
126         return JcrItemUtil.getItemId(destinationNode.getProperty(newName));
127     }
128 
129     @Override
130     public boolean canCopy(final List<JcrItemId> source) {
131         if (source.isEmpty()) {
132             return false;
133         } else {
134             for (JcrItemId itemId : source) {
135                 try {
136                     Item item = JcrItemUtil.getJcrItem(itemId);
137                     if (item.isNode() && (NodeUtil.hasMixin((Node) item, NodeTypes.Deleted.NAME))) {
138                         return false;
139                     }
140                 } catch (RepositoryException e) {
141                     log.error("Failed to obtain JCR item with UUID [{}] from workspace [{}] due to: {}. Returning false...", itemId.getUuid(), itemId.getWorkspace(), e.getMessage());
142                     return false;
143                 }
144             }
145             return true;
146         }
147     }
148 
149     @Override
150     public boolean canPasteInto(final JcrItemId destination) {
151         try {
152             if (!JcrItemUtil.getJcrItem(destination).isNode()) {
153                 return false;
154             }
155         } catch (RepositoryException e) {
156             log.warn("Failed to check whether the clipboard content can be pasted to the JCR item with UUID [{}] from workspace [{}] due to: {}. Returning false...", destination.getUuid(), destination.getWorkspace(), e.getMessage());
157             return false;
158         }
159 
160         return items.stream().filter(item -> canPasteInto(item, destination)).findFirst().isPresent();
161     }
162 
163     protected boolean canPasteInto(final JcrItemIdration/jcr/JcrItemId.html#JcrItemId">JcrItemId source, final JcrItemId destination) {
164         boolean result = false;
165         try {
166             Item sourceItem = JcrItemUtil.getJcrItem(source);
167             Item destinationItem = JcrItemUtil.getJcrItem(destination);
168             if (sourceItem != null && destinationItem.isNode() && sourceItem.getSession().getWorkspace().getName().equals(destinationItem.getSession().getWorkspace().getName())) {
169                 final Node destinationNode = (Node) destinationItem;
170                 final NodeType destinationNodeType = destinationNode.getPrimaryNodeType();
171                 if (sourceItem.isNode()) {
172                     result = destinationNodeType.canAddChildNode(sourceItem.getName(), ((Node) sourceItem).getPrimaryNodeType().getName());
173                 } else {
174                     Property property = (Property) sourceItem;
175                     if (property.isMultiple()) {
176                         result = destinationNodeType.canSetProperty(property.getName(), property.getValues());
177                     } else {
178                         result = destinationNodeType.canSetProperty(property.getName(), property.getValue());
179                     }
180                 }
181             }
182         } catch (RepositoryException e) {
183             log.error("Failed to check whether the item with UUID [{}] from workspace [{}] into the item with UUID [{}] from workspace [{}] due to: {}", source.getUuid(), source.getWorkspace(), destination.getUuid(), destination.getWorkspace(), e.getMessage(), e);
184         }
185 
186         return result;
187     }
188 
189     /**
190      * @param referenceItem the JCR item whose name must be unique at the given destination
191      * @param destination the destination targeted for uniqueness check
192      */
193     protected String getUniqueNewItemName(Item referenceItem, Node destination) throws RepositoryException {
194         return Path.getUniqueLabel(destination.getSession(), destination.getPath(), referenceItem.getName());
195     }
196 }