AbstractProductImporter.java
10.5 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* aQute.bnd.annotation.ConsumerType
* com.day.cq.commons.jcr.JcrUtil
* javax.jcr.Node
* javax.jcr.NodeIterator
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.Value
* javax.servlet.ServletException
* org.apache.felix.scr.annotations.Activate
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Service
* org.apache.jackrabbit.util.Text
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.SlingHttpServletResponse
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.commons.osgi.PropertiesUtil
* org.osgi.service.component.ComponentContext
* 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.ProductImporter;
import com.adobe.cq.commerce.pim.common.AbstractImporter;
import com.day.cq.commons.jcr.JcrUtil;
import java.io.IOException;
import java.util.Calendar;
import java.util.Dictionary;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
import javax.servlet.ServletException;
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.Service;
import org.apache.jackrabbit.util.Text;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(componentAbstract=1, metatype=1)
@Service
@ConsumerType
public abstract class AbstractProductImporter
extends AbstractImporter
implements ProductImporter {
private static final Logger log = LoggerFactory.getLogger(AbstractProductImporter.class);
protected String basePath = "/etc/commerce/products";
private int BUCKET_MAX;
private static final int DEFAULT_BUCKET_SIZE = 500;
@Property(label="Bucket Size", description="Maximum products per section before bucketing, and maximum in each bucket", intValue={500})
public static final String BUCKET_SIZE_PROP_NAME = "cq.commerce.productimporter.bucketsize";
protected String NN_BUCKET = "bucket";
protected String NT_BUCKET = "sling:Folder";
private int productCount;
private int variationCount;
@Activate
@Override
protected void activate(ComponentContext ctx) throws Exception {
super.activate(ctx);
this.BUCKET_MAX = PropertiesUtil.toInteger(ctx.getProperties().get("cq.commerce.productimporter.bucketsize"), (int)500);
}
@Override
public void importProducts(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
long startTime = System.currentTimeMillis();
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.productCount = 0;
this.variationCount = 0;
this.run(resourceResolver, storePath != null ? storePath : this.basePath, storeName, incrementalImport, provider);
long millis = System.currentTimeMillis() - startTime;
long seconds = millis / 1000;
if (seconds > 120) {
log.info("Imported " + this.productCount + " products in " + seconds / 60 + " minutes.");
} else {
log.info("Imported " + this.productCount + " products in " + seconds + " seconds.");
}
String summary = "" + this.productCount + " products and " + this.variationCount + " variants created/updated.";
if (this.getErrorCount() > 0) {
summary = summary + " " + this.getErrorCount() + " errors encountered.";
}
this.respondWithMessages(response, summary);
}
protected abstract boolean validateInput(SlingHttpServletRequest var1, SlingHttpServletResponse var2) throws IOException;
private void demoteProductChildrenToBucket(Node parent, Session session) throws RepositoryException {
Node bucket = JcrUtil.createUniqueNode((Node)parent, (String)this.NN_BUCKET, (String)this.NT_BUCKET, (Session)session);
NodeIterator children = parent.getNodes();
long productCount = 0;
while (children.hasNext()) {
Node child = (Node)children.next();
if (!child.hasProperty("cq:commerceType") || !child.getProperty("cq:commerceType").getString().equals("product")) continue;
String oldPath = child.getPath();
String newPath = JcrUtil.copy((Node)child, (Node)bucket, (String)child.getName()).getPath();
child.remove();
this.updateLoggedEvents(oldPath, newPath);
++productCount;
}
bucket.setProperty("cq:importCount", productCount);
parent.setProperty("cq:importCount", (Value)null);
}
protected Node createProduct(String path, Session session) throws RepositoryException {
long count;
String parentPath = Text.getRelativeParent((String)path, (int)1);
Node parent = JcrUtil.createPath((String)parentPath, (boolean)false, (String)"sling:Folder", (String)"sling:Folder", (Session)session, (boolean)false);
boolean bucketing = false;
if (parent.hasProperty("cq:importBucket")) {
parent = parent.getNode(parent.getProperty("cq:importBucket").getString());
bucketing = true;
}
long l = count = parent.hasProperty("cq:importCount") ? parent.getProperty("cq:importCount").getLong() + 1 : 1;
if (count > (long)this.BUCKET_MAX) {
if (!bucketing) {
this.demoteProductChildrenToBucket(parent, session);
} else {
parent = parent.getParent();
}
Node bucket = JcrUtil.createUniqueNode((Node)parent, (String)this.NN_BUCKET, (String)this.NT_BUCKET, (Session)session);
parent.setProperty("cq:importBucket", bucket.getName());
parent = bucket;
count = 1;
}
parent.setProperty("cq:importCount", count);
Node product = JcrUtil.createUniqueNode((Node)parent, (String)Text.getName((String)path), (String)"nt:unstructured", (Session)session);
product.setProperty("cq:commerceType", "product");
product.setProperty("sling:resourceType", "commerce/components/product");
product.setProperty("jcr:lastModified", Calendar.getInstance());
++this.productCount;
this.logEvent("com/adobe/cq/commerce/pim/PRODUCT_ADDED", product.getPath());
this.logMessage("Created product " + product.getPath(), false);
this.updateTicker(this.makeTickerMessage());
this.checkpoint(session, false);
return product;
}
protected String makeTickerMessage() {
return "" + this.productCount + " products imported/updated";
}
protected void productUpdated(Node product) throws RepositoryException {
++this.productCount;
this.logEvent("com/adobe/cq/commerce/pim/PRODUCT_MODIFIED", product.getPath());
this.logMessage("Updated product " + product.getPath(), false);
this.updateTicker(this.makeTickerMessage());
this.checkpoint(product.getSession(), false);
}
protected void productDeleted(Node product) throws RepositoryException {
this.logEvent("com/adobe/cq/commerce/pim/PRODUCT_DELETED", product.getPath());
this.logMessage("Deleted product " + product.getPath(), false);
this.updateTicker(this.makeTickerMessage());
this.checkpoint(product.getSession(), false);
}
protected Node createVariant(Node parentProduct, String name) throws RepositoryException {
Node variant = JcrUtil.createUniqueNode((Node)parentProduct, (String)name, (String)"nt:unstructured", (Session)parentProduct.getSession());
variant.setProperty("cq:commerceType", "variant");
variant.setProperty("sling:resourceType", "commerce/components/product");
variant.setProperty("jcr:lastModified", Calendar.getInstance());
++this.variationCount;
Node baseProduct = this.getBaseProduct(parentProduct);
if (baseProduct != null) {
this.logEvent("com/adobe/cq/commerce/pim/PRODUCT_MODIFIED", baseProduct.getPath());
}
this.logMessage("Created variation " + variant.getPath(), false);
this.checkpoint(parentProduct.getSession(), false);
return variant;
}
protected void variantUpdated(Node variant) throws RepositoryException {
++this.variationCount;
Node baseProduct = this.getBaseProduct(variant);
if (baseProduct != null) {
this.logEvent("com/adobe/cq/commerce/pim/PRODUCT_MODIFIED", baseProduct.getPath());
}
this.logMessage("Updated variation " + variant.getPath(), false);
this.updateTicker(this.makeTickerMessage());
this.checkpoint(variant.getSession(), false);
}
protected Node createImage(Node product) throws RepositoryException {
Node image = product.addNode("image", "nt:unstructured");
image.setProperty("sling:resourceType", "commerce/components/product/image");
image.setProperty("jcr:lastModified", Calendar.getInstance());
Node baseProduct = this.getBaseProduct(product);
if (baseProduct != null) {
this.logEvent("com/adobe/cq/commerce/pim/PRODUCT_MODIFIED", baseProduct.getPath());
}
this.logMessage("Created image " + image.getPath(), false);
this.checkpoint(product.getSession(), false);
return image;
}
protected Node getBaseProduct(Node node) throws RepositoryException {
while (node != null && !node.getProperty("cq:commerceType").getString().equals("product")) {
node = node.getParent();
}
return node;
}
}