1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
60
61
62
63
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
107
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
191
192
193 protected String getUniqueNewItemName(Item referenceItem, Node destination) throws RepositoryException {
194 return Path.getUniqueLabel(destination.getSession(), destination.getPath(), referenceItem.getName());
195 }
196 }