AbstractBlueprintImporter.java
6.24 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* aQute.bnd.annotation.ConsumerType
* com.day.cq.commons.jcr.JcrUtil
* com.day.cq.wcm.api.Page
* com.day.cq.wcm.api.PageManager
* com.day.cq.wcm.api.WCMException
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.servlet.ServletException
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.SlingHttpServletResponse
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.commerce.pim.common;
import aQute.bnd.annotation.ConsumerType;
import com.adobe.cq.commerce.pim.api.CatalogBlueprintImporter;
import com.adobe.cq.commerce.pim.common.AbstractImporter;
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 java.io.IOException;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.servlet.ServletException;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(componentAbstract=1, metatype=1)
@Service
@ConsumerType
public abstract class AbstractBlueprintImporter
extends AbstractImporter
implements CatalogBlueprintImporter {
private static final Logger log = LoggerFactory.getLogger(AbstractBlueprintImporter.class);
protected String basePath = "/content/catalogs";
protected long startTime;
protected int catalogCount;
protected int sectionCount;
@Override
public void importBlueprints(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
if (!this.validateInput(request, response)) {
return;
}
ResourceResolver resourceResolver = request.getResourceResolver();
Session session = (Session)resourceResolver.adaptTo(Session.class);
String storeName = request.getParameter("storeName");
String storePath = request.getParameter("storePath");
String provider = request.getParameter("provider");
this.initTicker(request.getParameter("tickertoken"), session);
Boolean incrementalImport = false;
if (request.getParameter("incrementalImport") != null) {
incrementalImport = true;
}
this.catalogCount = 0;
this.sectionCount = 0;
this.run(resourceResolver, storePath != null ? storePath : this.basePath, storeName, incrementalImport, provider);
long millis = System.currentTimeMillis() - this.startTime;
long seconds = millis / 1000;
if (seconds > 120) {
log.info("Imported " + this.catalogCount + " catalogs with " + this.sectionCount + " sections in " + seconds / 60 + " minutes.");
} else {
log.info("Imported " + this.catalogCount + " catalogs with " + this.sectionCount + " sections in " + seconds + " seconds.");
}
String msg = "" + this.catalogCount + " catalogs created with " + this.sectionCount + " sections.";
if (this.getErrorCount() > 0) {
msg = msg + " " + this.getErrorCount() + " errors encountered.";
}
if (this.catalogCount > 0) {
this.respondWithMessages(response, msg);
} else {
response.sendError(500, msg);
}
}
protected abstract boolean validateInput(SlingHttpServletRequest var1, SlingHttpServletResponse var2) throws IOException;
protected Page createCatalog(PageManager pageManager, String storePath, String catalogTitle, Session session) throws RepositoryException, WCMException {
Page catalog = pageManager.create(storePath, AbstractBlueprintImporter.mangleName(catalogTitle), "/libs/commerce/templates/catalog", catalogTitle);
Node contentNode = (Node)catalog.getContentResource().adaptTo(Node.class);
this.setProperty(contentNode, "templates/cq_rolloutConfigs", "[/etc/msm/rolloutconfigs/catalog]");
++this.catalogCount;
this.logMessage("Created catalog " + catalog.getPath(), false);
this.checkpoint(session, false);
return catalog;
}
protected Page createSection(PageManager pageManager, String parentPath, String[] hierarchy, String sectionTitle, Session session) throws RepositoryException, WCMException {
for (int i = 0; i < hierarchy.length - 1; ++i) {
parentPath = parentPath + "/" + AbstractBlueprintImporter.mangleName(hierarchy[i]);
}
String sectionName = hierarchy[hierarchy.length - 1];
Page section = pageManager.create(parentPath, AbstractBlueprintImporter.mangleName(sectionName), "/libs/commerce/templates/section", sectionTitle);
++this.sectionCount;
this.logMessage("Created section " + section.getPath(), false);
this.updateTicker(this.makeTickerMessage());
this.checkpoint(session, false);
return section;
}
protected void setProperty(Node node, String propertyPath, String value) throws RepositoryException {
String propertyName = propertyPath;
if (propertyPath.contains("/")) {
node = JcrUtil.createPath((Node)node, (String)propertyPath.substring(0, propertyPath.lastIndexOf("/")), (boolean)false, (String)"nt:unstructured", (String)"nt:unstructured", (Session)node.getSession(), (boolean)false);
propertyName = propertyPath.substring(propertyPath.lastIndexOf("/") + 1);
}
if (value.startsWith("[") && value.endsWith("]")) {
String[] values = value.substring(1, value.length() - 1).split(",");
node.setProperty(propertyName, values);
} else {
node.setProperty(propertyName, value);
}
}
protected String makeTickerMessage() {
return "" + this.sectionCount + " section blueprints imported/updated";
}
}