Cq560CatalogConverterImpl.java
14.1 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.cq.commerce.common.CommerceHelper
* com.day.cq.commons.Filter
* com.day.cq.commons.jcr.JcrUtil
* com.day.cq.wcm.api.Page
* com.day.cq.wcm.api.PageManager
* com.day.cq.wcm.api.WCMException
* com.day.cq.wcm.msm.api.LiveRelationship
* com.day.cq.wcm.msm.api.LiveRelationshipManager
* com.day.cq.wcm.msm.api.RolloutConfig
* com.day.cq.wcm.msm.api.RolloutManager
* com.day.cq.wcm.msm.api.RolloutManager$Trigger
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Value
* org.apache.felix.scr.annotations.Activate
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.jackrabbit.util.Text
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceUtil
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.commons.osgi.PropertiesUtil
* org.osgi.service.component.ComponentContext
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.commerce.pim.impl.cq560catalogconverter;
import com.adobe.cq.commerce.common.CommerceHelper;
import com.adobe.cq.commerce.pim.impl.cataloggenerator.CatalogUtils;
import com.adobe.cq.commerce.pim.impl.cq560catalogconverter.Cq560CatalogConverter;
import com.day.cq.commons.Filter;
import com.day.cq.commons.jcr.JcrUtil;
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.LiveRelationship;
import com.day.cq.wcm.msm.api.LiveRelationshipManager;
import com.day.cq.wcm.msm.api.RolloutConfig;
import com.day.cq.wcm.msm.api.RolloutManager;
import java.util.Collection;
import java.util.Dictionary;
import java.util.Iterator;
import java.util.List;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Value;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.jackrabbit.util.Text;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=1, label="%cq560catalogconverterimpl.name", description="%cq560catalogconverterimpl.description")
@Service
@Property(name="service.description", value={"Updates 5.6.0 catalogs and catalog blueprints to 5.6.1 format"})
public class Cq560CatalogConverterImpl
implements Cq560CatalogConverter {
private final Logger log = LoggerFactory.getLogger(Cq560CatalogConverterImpl.class);
@Reference
private LiveRelationshipManager liveRelationshipManager;
static final String DEFAULT_TEMPLATES_FOLDER_TITLE = "Template Pages";
@Property(label="%Template Pages Folder Name", description="%Folder name where template pages will be created", value={"Template Pages"})
private static final String TEMPLATES_FOLDER_TITLE = "cq.commerce.templatepagesfoldername";
private static String templatesFolderTitle;
private static String[] rolloutConfigs;
@Activate
protected void activate(ComponentContext ctx) throws Exception {
templatesFolderTitle = PropertiesUtil.toString(ctx.getProperties().get("cq.commerce.templatepagesfoldername"), (String)"Template Pages");
}
@Override
public void updateInstanceMap(ResourceResolver resolver, Page blueprintRoot) {
this.log.info("Creating catalog instance map for blueprint {}", (Object)blueprintRoot.getPath());
PageManager pageManager = (PageManager)resolver.adaptTo(PageManager.class);
try {
Collection targets = this.liveRelationshipManager.getLiveRelationships(blueprintRoot, null, null, false);
for (LiveRelationship target : targets) {
Page targetCatalog;
boolean isCatalogTarget = false;
for (RolloutConfig config : target.getRolloutConfigs()) {
if (!config.getPath().equals("/etc/msm/rolloutconfigs/catalog")) continue;
isCatalogTarget = true;
break;
}
if (!isCatalogTarget || (targetCatalog = pageManager.getContainingPage(target.getTargetPath())) == null) continue;
CatalogUtils.registerCatalogInstance(blueprintRoot, targetCatalog, rolloutConfigs);
}
}
catch (Exception e) {
this.log.error("Failed to update catalog instance map", (Throwable)e);
}
}
@Override
public void updateBlueprint(ResourceResolver resolver, Page blueprintRoot) throws WCMException {
if (blueprintRoot.getContentResource("cq:CatalogSyncConfig") == null) {
this.updateInstanceMap(resolver, blueprintRoot);
}
PageManager pageManager = (PageManager)resolver.adaptTo(PageManager.class);
String parentPath = Text.getRelativeParent((String)blueprintRoot.getPath(), (int)1);
String templatesFolderName = JcrUtil.createValidName((String)templatesFolderTitle);
Page templatesFolder = pageManager.getPage(parentPath + "/" + templatesFolderName);
if (templatesFolder == null) {
try {
this.log.info("Creating page {} to hold template pages", (Object)(parentPath + "/" + templatesFolderName));
templatesFolder = pageManager.create(parentPath, templatesFolderName, null, templatesFolderTitle);
Node contentNode = (Node)templatesFolder.getContentResource().adaptTo(Node.class);
contentNode.setProperty("sling:resourceType", "commerce/components/templatepagefolder");
}
catch (Exception e) {
throw new WCMException("Couldn't create folder for template pages", (Throwable)e);
}
}
try {
this.log.info("Updating catalog blueprint {}", (Object)blueprintRoot.getPath());
this.updateBlueprintPage(resolver, templatesFolder, blueprintRoot);
((Node)blueprintRoot.getContentResource().adaptTo(Node.class)).setProperty("cq:blueprintVersion", 561);
}
catch (Exception e) {
throw new WCMException("Exception encountered updating catalog blueprint", (Throwable)e);
}
}
private void updateBlueprintPage(ResourceResolver resolver, Page templatesFolder, Page blueprint) throws WCMException, RepositoryException {
Resource references = blueprint.getContentResource("templates");
if (references != null) {
Node referencesNode = (Node)references.adaptTo(Node.class);
if (referencesNode.hasProperty("catalog")) {
this.validateTemplateReference(resolver, templatesFolder, referencesNode, "catalog");
}
if (referencesNode.hasProperty("section")) {
this.validateTemplateReference(resolver, templatesFolder, referencesNode, "section");
}
if (referencesNode.hasProperty("product")) {
this.validateTemplateReference(resolver, templatesFolder, referencesNode, "product");
}
if (referencesNode.hasProperty("cq_rolloutConfigs")) {
referencesNode.setProperty("cq_rolloutConfigs", (Value[])null);
}
}
Iterator i = blueprint.listChildren();
while (i.hasNext()) {
this.updateBlueprintPage(resolver, templatesFolder, (Page)i.next());
}
}
private void validateTemplateReference(ResourceResolver resolver, Page templatesFolder, Node references, String referenceName) throws WCMException, RepositoryException {
if (references.hasProperty(referenceName)) {
String templatePath = references.getProperty(referenceName).getString();
Resource resource = resolver.getResource(templatePath);
if (resource == null) {
throw new WCMException("Template reference not found");
}
if (((Node)resource.adaptTo(Node.class)).isNodeType("cq:Template")) {
Page templatePage = this.getTemplatePage(templatesFolder, templatePath);
if (templatePage == null) {
PageManager pageManager = (PageManager)resolver.adaptTo(PageManager.class);
String name = Text.getName((String)templatePath);
String title = (String)ResourceUtil.getValueMap((Resource)resource).get("jcr:title", (Object)"");
templatePage = pageManager.create(templatesFolder.getPath(), name, templatePath, title);
}
references.setProperty(referenceName, templatePage.getPath());
}
}
}
private Page getTemplatePage(Page templatesFolder, String templatePath) {
class TemplateFilter
implements Filter<Page> {
private String templatePath;
public TemplateFilter(String templatePath) {
this.templatePath = templatePath;
}
public boolean includes(Page page) {
return ((String)page.getProperties().get("cq:template", (Object)"")).equals(this.templatePath);
}
}
Iterator i = templatesFolder.listChildren((Filter)new TemplateFilter(templatePath), true);
if (i.hasNext()) {
return (Page)i.next();
}
return null;
}
@Override
public void updateCatalog(ResourceResolver resolver, Page catalogRoot) throws WCMException {
try {
this.log.info("Updating product catalog {}", (Object)catalogRoot.getPath());
this.firstPass(catalogRoot);
this.secondPass((PageManager)resolver.adaptTo(PageManager.class), catalogRoot, null, CatalogUtils.getConfigs(resolver, rolloutConfigs));
}
catch (Exception e) {
throw new WCMException("Exception encountered while updating product catalog", (Throwable)e);
}
}
private void firstPass(Page catalogPage) throws WCMException, RepositoryException {
Resource contentResource = catalogPage.getContentResource();
ValueMap properties = ResourceUtil.getValueMap((Resource)contentResource);
Node contentNode = (Node)catalogPage.getContentResource().adaptTo(Node.class);
if (((String)properties.get("cq:commerceType", (Object)"")).equals("section")) {
LiveRelationship lr = this.liveRelationshipManager.getLiveRelationship(contentResource, false);
String blueprintPath = lr.getSourcePath();
blueprintPath = blueprintPath.substring(0, blueprintPath.length() - "/jcr:content".length());
contentNode.setProperty("cq:catalogBlueprint", blueprintPath);
} else {
Resource productReference = CommerceHelper.findProductResource((Page)catalogPage);
if (productReference != null) {
contentNode.setProperty("cq:productMaster", (String)ResourceUtil.getValueMap((Resource)productReference).get("productData", (Object)""));
}
}
Iterator i = catalogPage.listChildren();
while (i.hasNext()) {
this.firstPass((Page)i.next());
}
}
private void secondPass(PageManager pageManager, Page catalogPage, Page parentBlueprint, RolloutConfig[] rolloutConfigs) throws WCMException, RepositoryException {
Resource contentResource = catalogPage.getContentResource();
ValueMap properties = ResourceUtil.getValueMap((Resource)contentResource);
if (properties.get("cq:catalogBlueprint", String.class) != null) {
this.liveRelationshipManager.endRelationship(contentResource, false);
String blueprintPath = (String)properties.get("cq:catalogBlueprint", String.class);
parentBlueprint = pageManager.getPage(blueprintPath);
if (parentBlueprint != null) {
Page templatePage = CatalogUtils.getTemplatePage(parentBlueprint, null);
if (templatePage != null) {
this.liveRelationshipManager.establishRelationship(templatePage, catalogPage, false, false, rolloutConfigs);
} else {
this.log.warn("Section blueprint {} didn't provide a valid section template page", (Object)parentBlueprint.getPath());
}
} else {
this.log.warn("Catalog blueprint path {} doesn't point to a page", (Object)blueprintPath);
}
} else if (properties.get("cq:productMaster", String.class) != null) {
if (parentBlueprint != null) {
Page templatePage = CatalogUtils.getTemplatePage(parentBlueprint, "product");
if (templatePage != null) {
this.liveRelationshipManager.establishRelationship(templatePage, catalogPage, false, false, rolloutConfigs);
} else {
this.log.warn("Section blueprint {} didn't provide a valid product template page", (Object)parentBlueprint.getPath());
}
} else {
this.log.warn("Found a product page but the parent section had no blueprint");
}
}
Iterator i = catalogPage.listChildren();
while (i.hasNext()) {
this.secondPass(pageManager, (Page)i.next(), parentBlueprint, rolloutConfigs);
}
}
static {
rolloutConfigs = new String[]{"/etc/msm/rolloutconfigs/catalogpagecontent", "/etc/msm/rolloutconfigs/catalogrollouthooks"};
}
protected void bindLiveRelationshipManager(LiveRelationshipManager liveRelationshipManager) {
this.liveRelationshipManager = liveRelationshipManager;
}
protected void unbindLiveRelationshipManager(LiveRelationshipManager liveRelationshipManager) {
if (this.liveRelationshipManager == liveRelationshipManager) {
this.liveRelationshipManager = null;
}
}
}