This repository has been archived by the owner on Oct 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
ImportProductsInlineSource.java
207 lines (175 loc) · 7.15 KB
/
ImportProductsInlineSource.java
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
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Import products into a catalog from inline source using Retail API
*/
package product;
import com.google.cloud.ServiceOptions;
import com.google.cloud.retail.v2.ColorInfo;
import com.google.cloud.retail.v2.FulfillmentInfo;
import com.google.cloud.retail.v2.ImportMetadata;
import com.google.cloud.retail.v2.ImportProductsRequest;
import com.google.cloud.retail.v2.ImportProductsResponse;
import com.google.cloud.retail.v2.PriceInfo;
import com.google.cloud.retail.v2.Product;
import com.google.cloud.retail.v2.ProductInlineSource;
import com.google.cloud.retail.v2.ProductInputConfig;
import com.google.cloud.retail.v2.ProductServiceClient;
import com.google.longrunning.Operation;
import com.google.longrunning.OperationsClient;
import com.google.protobuf.FieldMask;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
public class ImportProductsInlineSource {
public static void main(String[] args) throws IOException, InterruptedException {
// TODO(developer): Replace these variables before running the sample.
String projectId = ServiceOptions.getDefaultProjectId();
String branchName =
String.format(
"projects/%s/locations/global/catalogs/default_catalog/branches/0", projectId);
ImportProductsRequest importRequest = getImportProductsInlineRequest(getProducts(), branchName);
waitForOperationCompletion(importRequest);
}
public static ImportProductsRequest getImportProductsInlineRequest(
List<Product> productsToImport, String branchName) {
ProductInlineSource inlineSource =
ProductInlineSource.newBuilder().addAllProducts(productsToImport).build();
ProductInputConfig inputConfig =
ProductInputConfig.newBuilder().setProductInlineSource(inlineSource).build();
ImportProductsRequest importRequest =
ImportProductsRequest.newBuilder()
.setParent(branchName)
.setInputConfig(inputConfig)
.build();
System.out.printf("Import products from inline source request: %s%n", importRequest);
return importRequest;
}
public static List<Product> getProducts() {
List<Product> products = new ArrayList<>();
Product product1;
Product product2;
float price1 = 16f;
float originalPrice1 = 45.0f;
float cost1 = 12.0f;
PriceInfo priceInfo1 =
PriceInfo.newBuilder()
.setPrice(price1)
.setOriginalPrice(originalPrice1)
.setCost(cost1)
.setCurrencyCode("USD")
.build();
ColorInfo colorInfo1 =
ColorInfo.newBuilder()
.addColorFamilies("Blue")
.addAllColors(Arrays.asList("Light blue", "Blue", "Dark blue"))
.build();
FulfillmentInfo fulfillmentInfo1 =
FulfillmentInfo.newBuilder()
.setType("pickup-in-store")
.addAllPlaceIds(Arrays.asList("store1", "store2"))
.build();
FieldMask fieldMask1 =
FieldMask.newBuilder()
.addAllPaths(Arrays.asList("title", "categories", "price_info", "color_info"))
.build();
// TO CHECK ERROR HANDLING COMMENT OUT THE PRODUCT TITLE HERE:
product1 =
Product.newBuilder()
.setTitle("#IamRemarkable Pen")
.setId(UUID.randomUUID().toString())
.addAllCategories(Collections.singletonList("Office"))
.setUri(
"https://shop.googlemerchandisestore.com/Google+Redesign/"
+ "Office/IamRemarkable+Pen")
.addBrands("#IamRemarkable")
.setPriceInfo(priceInfo1)
.setColorInfo(colorInfo1)
.addFulfillmentInfo(fulfillmentInfo1)
.setRetrievableFields(fieldMask1)
.build();
float price2 = 35f;
float originalPrice2 = 45.0f;
float cost2 = 12.0f;
PriceInfo priceInfo2 =
PriceInfo.newBuilder()
.setPrice(price2)
.setOriginalPrice(originalPrice2)
.setCost(cost2)
.setCurrencyCode("USD")
.build();
ColorInfo colorInfo2 =
ColorInfo.newBuilder()
.addColorFamilies("Blue")
.addAllColors(Collections.singletonList("Sky blue"))
.build();
FulfillmentInfo fulfillmentInfo2 =
FulfillmentInfo.newBuilder()
.setType("pickup-in-store")
.addAllPlaceIds(Arrays.asList("store2", "store3"))
.build();
FieldMask fieldMask2 =
FieldMask.newBuilder()
.addAllPaths(Arrays.asList("title", "categories", "price_info", "color_info"))
.build();
product2 =
Product.newBuilder()
.setTitle("Android Embroidered Crewneck Sweater")
.setId(UUID.randomUUID().toString())
.addCategories("Apparel")
.setUri(
"https://shop.googlemerchandisestore.com/Google+Redesign/"
+ "Apparel/Android+Embroidered+Crewneck+Sweater")
.addBrands("Android")
.setPriceInfo(priceInfo2)
.setColorInfo(colorInfo2)
.addFulfillmentInfo(fulfillmentInfo2)
.setRetrievableFields(fieldMask2)
.build();
products.add(product1);
products.add(product2);
return products;
}
public static void waitForOperationCompletion(ImportProductsRequest importRequest)
throws IOException, InterruptedException {
try (ProductServiceClient serviceClient = ProductServiceClient.create()) {
String operationName = serviceClient.importProductsCallable().call(importRequest).getName();
System.out.printf("OperationName = %s\n", operationName);
OperationsClient operationsClient = serviceClient.getOperationsClient();
Operation operation = operationsClient.getOperation(operationName);
while (!operation.getDone()) {
// Keep polling the operation periodically until the import task is done.
Thread.sleep(30_000);
operation = operationsClient.getOperation(operationName);
}
if (operation.hasMetadata()) {
ImportMetadata metadata = operation.getMetadata().unpack(ImportMetadata.class);
System.out.printf(
"Number of successfully imported products: %s\n", metadata.getSuccessCount());
System.out.printf(
"Number of failures during the importing: %s\n", metadata.getFailureCount());
}
if (operation.hasResponse()) {
ImportProductsResponse response =
operation.getResponse().unpack(ImportProductsResponse.class);
System.out.printf("Operation result: %s%n", response);
}
}
}
}