MobileAppJCRUtil.java
4.41 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.commons.jcr.JcrUtil
* javax.jcr.Node
* javax.jcr.NodeIterator
* javax.jcr.Property
* javax.jcr.PropertyIterator
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.nodetype.NodeDefinition
* javax.jcr.nodetype.NodeType
* org.apache.commons.lang.StringUtils
* org.apache.jackrabbit.commons.JcrUtils
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.mobile.platform.impl.utils;
import com.day.cq.commons.jcr.JcrUtil;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.nodetype.NodeDefinition;
import javax.jcr.nodetype.NodeType;
import org.apache.commons.lang.StringUtils;
import org.apache.jackrabbit.commons.JcrUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MobileAppJCRUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(MobileAppJCRUtil.class);
public static Node copy(Node src, Node dstParent, boolean bestEffort, boolean deep) throws RepositoryException {
String name = src.getName();
Node dst = null;
dst = dstParent.hasNode(name) ? dstParent.getNode(name) : dstParent.addNode(name, src.getPrimaryNodeType().getName());
for (NodeType mix : src.getMixinNodeTypes()) {
try {
dst.addMixin(mix.getName());
continue;
}
catch (RepositoryException e) {
MobileAppJCRUtil.assertBestEffort(bestEffort, e);
}
}
PropertyIterator iter = src.getProperties();
while (iter.hasNext()) {
try {
JcrUtil.copy((Property)iter.nextProperty(), (Node)dst, (String)null);
}
catch (RepositoryException e) {
MobileAppJCRUtil.assertBestEffort(bestEffort, e);
}
}
if (deep) {
if (dst.hasNode("jcr:content")) {
dst.getNode("jcr:content").remove();
}
iter = src.getNodes();
while (iter.hasNext()) {
Node n = iter.nextNode();
if (n.getDefinition().isProtected()) continue;
try {
MobileAppJCRUtil.copy(n, dst, bestEffort, deep);
}
catch (RepositoryException e) {
MobileAppJCRUtil.assertBestEffort(bestEffort, e);
}
}
}
return dst;
}
private static void assertBestEffort(boolean bestEffort, RepositoryException ex) throws RepositoryException {
if (!bestEffort) {
throw ex;
}
LOGGER.warn("Copy error ignored, best effort set to true", (Throwable)ex);
}
public static void setMobileAppProperty(Resource resource, String path, String[] value) throws RepositoryException {
String trimmedParamName = null;
int pos = path.lastIndexOf(47);
String childPath = "";
if (pos > 0) {
childPath = path.substring(0, pos);
trimmedParamName = path.substring(pos + 1);
} else if (pos == -1) {
childPath = ".";
trimmedParamName = path;
}
Node parentNode = JcrUtils.getOrCreateByPath((String)(resource.getPath() + "/" + childPath), (String)"nt:unstructured", (Session)((Session)resource.getResourceResolver().adaptTo(Session.class)));
boolean hasProperty = parentNode.hasProperty(trimmedParamName);
if (value == null || value.length == 1 && StringUtils.isEmpty((String)value[0])) {
if (hasProperty) {
Property property = parentNode.getProperty(trimmedParamName);
property.setValue((String)null);
}
} else {
if (hasProperty) {
Property property = parentNode.getProperty(trimmedParamName);
property.setValue((String)null);
}
if (value.length > 1) {
parentNode.setProperty(trimmedParamName, value);
} else {
parentNode.setProperty(trimmedParamName, value[0]);
}
}
}
}