LiveRelationshipEditingUtil.java
9.71 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.wcm.api.Page
* com.day.cq.wcm.api.PageManager
* com.day.cq.wcm.api.WCMException
* com.day.cq.wcm.msm.api.LiveCopy
* com.day.cq.wcm.msm.api.LiveRelationship
* com.day.cq.wcm.msm.api.LiveRelationshipManager
* com.day.cq.wcm.msm.api.LiveStatus
* com.day.cq.wcm.msm.api.RolloutConfig
* javax.jcr.Node
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.nodetype.NodeType
* org.apache.commons.lang.StringUtils
* org.apache.jackrabbit.util.Text
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.resource.ModifiableValueMap
* org.apache.sling.api.resource.PersistenceException
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceMetadata
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceUtil
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.wcm.msm.impl;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.WCMException;
import com.day.cq.wcm.msm.api.LiveCopy;
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.api.RolloutConfig;
import com.day.cq.wcm.msm.impl.LiveCopyManagerImpl;
import com.day.cq.wcm.msm.impl.Utils;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.nodetype.NodeType;
import org.apache.commons.lang.StringUtils;
import org.apache.jackrabbit.util.Text;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceMetadata;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LiveRelationshipEditingUtil {
private static final String DEEP_CANCEL_PARAM = "msm:status/msm:isCancelledForChildren";
private static Logger log = LoggerFactory.getLogger(LiveRelationshipEditingUtil.class);
private LiveRelationshipEditingUtil() {
}
public static Resource createResource(LiveRelationship relationship, Resource resource, LiveRelationshipManager relationshipManager) throws WCMException, RepositoryException {
Node parent;
ResourceResolver resolver = resource.getResourceResolver();
Resource targetParent = resolver.getResource(Text.getRelativeParent((String)resource.getPath(), (int)1));
if (!ResourceUtil.isSyntheticResource((Resource)targetParent) && (parent = (Node)targetParent.adaptTo(Node.class)) != null) {
Session session = parent.getSession();
LiveRelationship parentShip = relationshipManager.getLiveRelationship(targetParent, false);
if (parentShip != null) {
Node target;
String name = resource.getName();
if (!StringUtils.isEmpty((String)resource.getResourceMetadata().getResolutionPathInfo())) {
name = name.substring(0, name.indexOf(resource.getResourceMetadata().getResolutionPathInfo()));
}
if (session.nodeExists(relationship.getSourcePath())) {
String nt = session.getNode(relationship.getSourcePath()).getPrimaryNodeType().getName();
target = parent.addNode(name, nt);
} else {
target = parent.isNodeType("cq:LiveSync") ? parent.addNode(name, "nt:unstructured") : parent.addNode(name);
}
return resolver.getResource(target.getPath());
}
}
return null;
}
public static void processCancel(SlingHttpServletRequest request, LiveRelationshipManager relationshipManager) throws WCMException, RepositoryException {
Resource resource = request.getResource();
ResourceResolver resolver = resource.getResourceResolver();
LiveRelationship currentRelation = relationshipManager.getLiveRelationship(resource, true);
if (currentRelation == null) {
return;
}
boolean isCancelled = currentRelation.getStatus().isCancelled();
String cancelParamValue = request.getParameter("msm:status/msm:isCancelled");
boolean requestCancel = Boolean.valueOf(cancelParamValue);
boolean requestCancelDeep = Boolean.valueOf(request.getParameter("msm:status/msm:isCancelledForChildren"));
if (requestCancel != isCancelled) {
if (requestCancel) {
if (ResourceUtil.isNonExistingResource((Resource)resource) && (resource = LiveRelationshipEditingUtil.createResource(currentRelation, resource, relationshipManager)) != null) {
currentRelation = relationshipManager.getLiveRelationship(resource, false);
}
if (currentRelation != null && currentRelation.getStatus().isTargetExisting()) {
relationshipManager.cancelRelationship(resolver, currentRelation, requestCancelDeep, true);
}
} else {
relationshipManager.reenableRelationship(resolver, currentRelation, false);
if (!currentRelation.getStatus().isPage()) {
LiveRelationship pageRelation;
Date compRollout;
Page page;
PageManager pm = (PageManager)resolver.adaptTo(PageManager.class);
Page page2 = page = pm == null ? null : pm.getContainingPage(resource);
if (page != null && page.hasContent() && (pageRelation = relationshipManager.getLiveRelationship(page.getContentResource(), false)).getStatus().getLastRolledOut() != null && ((compRollout = currentRelation.getStatus().getLastRolledOut()) == null || compRollout.before(pageRelation.getStatus().getLastRolledOut()))) {
ModifiableValueMap contentProp = (ModifiableValueMap)page.getContentResource().adaptTo(ModifiableValueMap.class);
if (compRollout == null) {
contentProp.remove((Object)"cq:lastRolledout");
} else {
Calendar newVal = Calendar.getInstance();
newVal.setTime(compRollout);
contentProp.put((Object)"cq:lastRolledout", (Object)newVal);
}
}
}
((Session)resolver.adaptTo(Session.class)).save();
}
} else if (isCancelled && requestCancelDeep != currentRelation.getStatus().isCancelledForChildren()) {
relationshipManager.cancelRelationship(resolver, currentRelation, requestCancelDeep, true);
}
}
public static /* varargs */ void editLiveCopy(ResourceResolver resolver, LiveCopyManagerImpl lcManager, LiveRelationshipManager relationshipManager, LiveRelationship relationship, boolean setDeep, String ... rolloutConfigs) throws RepositoryException, WCMException {
Boolean deepToSave;
Boolean bl = deepToSave = setDeep == relationship.getLiveCopy().isDeep() ? null : Boolean.valueOf(setDeep);
if (deepToSave != null || rolloutConfigs != null) {
LiveCopyManagerImpl.LiveCopyImpl lc;
log.debug("LiveCopy at {} changed: try to update", (Object)relationship.getLiveCopy().getPath());
String pagePath = Utils.getPagePath(relationship.getTargetPath());
if (relationship.getLiveCopy().getPath().equals(pagePath)) {
log.debug("Edit local LiveCopy");
lc = lcManager.get(relationship.getLiveCopy().getPath());
if (deepToSave != null) {
lc.setDeep(deepToSave);
if (!deepToSave.booleanValue()) {
Resource resource = resolver.getResource(lc.getPath());
Page page = (Page)resource.adaptTo(Page.class);
if (page != null) {
Iterator children = page.listChildren();
while (children.hasNext()) {
relationshipManager.endRelationship((Resource)((Page)children.next()).adaptTo(Resource.class), false);
}
} else {
for (Resource child : resource.getChildren()) {
relationshipManager.endRelationship(child, false);
}
}
}
}
if (rolloutConfigs != null) {
lc.setRolloutConfigs(rolloutConfigs);
}
} else {
log.debug("Start new LiveCopy to apply changes in settings at {}", (Object)pagePath);
lc = lcManager.create(Utils.getPagePath(relationship.getTargetPath()), Utils.getPagePath(relationship.getSourcePath()), setDeep, new RolloutConfig[0]);
if (rolloutConfigs != null) {
lc.setRolloutConfigs(rolloutConfigs);
}
}
try {
resolver.commit();
lcManager.get(lc.getPath());
log.debug("Updated LiveCopy, LiveCopy {}", (Object)pagePath);
}
catch (PersistenceException e) {
throw new WCMException("Failed to commit changes to Configuration:", (Throwable)e);
}
}
}
public static void updateRolloutDate(ResourceResolver resolver, LiveRelationship liveRelationship) {
}
}