CopyParagraphLCPostProcessor.java
3.7 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.wcm.msm.api.LiveRelationship
* com.day.cq.wcm.msm.api.LiveRelationshipManager
* com.day.cq.wcm.msm.api.LiveStatus
* com.day.text.Text
* javax.jcr.Node
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.resource.NonExistingResource
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.servlets.post.Modification
* org.apache.sling.servlets.post.ModificationType
* org.apache.sling.servlets.post.SlingPostProcessor
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.wcm.msm.impl.postprocessing;
import com.day.cq.wcm.msm.api.LiveRelationship;
import com.day.cq.wcm.msm.api.LiveRelationshipManager;
import com.day.cq.wcm.msm.api.LiveStatus;
import com.day.cq.wcm.msm.impl.Utils;
import com.day.text.Text;
import java.util.List;
import javax.jcr.Node;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.NonExistingResource;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.servlets.post.Modification;
import org.apache.sling.servlets.post.ModificationType;
import org.apache.sling.servlets.post.SlingPostProcessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=0)
@Service
public class CopyParagraphLCPostProcessor
implements SlingPostProcessor {
private static final Logger log = LoggerFactory.getLogger(CopyParagraphLCPostProcessor.class);
private static final String PROCESSOR_ROOT = "/content";
@Reference
private LiveRelationshipManager liveRelationshipManager = null;
public void process(SlingHttpServletRequest request, List<Modification> changes) throws Exception {
ResourceResolver resourceResolver = request.getResourceResolver();
for (Modification change : changes) {
if (!CopyParagraphLCPostProcessor.filter(change)) continue;
String sourcePath = change.getSource();
String destPath = change.getDestination();
NonExistingResource srcResource = new NonExistingResource(resourceResolver, sourcePath);
Resource paragraphCopy = resourceResolver.getResource(destPath);
LiveRelationship relationship = this.liveRelationshipManager.getLiveRelationship((Resource)srcResource, false);
if (relationship == null || !relationship.getStatus().isSourceExisting() || paragraphCopy == null) continue;
log.debug("Detected LC paragraph copy operation of {} paragraph, detaching copied paragraph {}", new Object[]{sourcePath, destPath});
Utils.detachLiveCopy((Node)paragraphCopy.adaptTo(Node.class), true, false);
}
}
private static boolean filter(Modification modification) {
return Text.isDescendant((String)"/content", (String)modification.getSource()) && modification.getType() == ModificationType.COPY && modification.getSource().contains("/jcr:content/");
}
protected void bindLiveRelationshipManager(LiveRelationshipManager liveRelationshipManager) {
this.liveRelationshipManager = liveRelationshipManager;
}
protected void unbindLiveRelationshipManager(LiveRelationshipManager liveRelationshipManager) {
if (this.liveRelationshipManager == liveRelationshipManager) {
this.liveRelationshipManager = null;
}
}
}