|
| 1 | +/* |
| 2 | + * Copyright 2022 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +// [START retail_crud_product] |
| 18 | + |
| 19 | +/* |
| 20 | + * Create product in a catalog using Retail API |
| 21 | + */ |
| 22 | + |
| 23 | +package product; |
| 24 | + |
| 25 | +import com.google.api.gax.rpc.NotFoundException; |
| 26 | +import com.google.cloud.retail.v2.CreateProductRequest; |
| 27 | +import com.google.cloud.retail.v2.DeleteProductRequest; |
| 28 | +import com.google.cloud.retail.v2.GetProductRequest; |
| 29 | +import com.google.cloud.retail.v2.PriceInfo; |
| 30 | +import com.google.cloud.retail.v2.Product; |
| 31 | +import com.google.cloud.retail.v2.Product.Availability; |
| 32 | +import com.google.cloud.retail.v2.Product.Type; |
| 33 | +import com.google.cloud.retail.v2.ProductServiceClient; |
| 34 | +import com.google.cloud.retail.v2.UpdateProductRequest; |
| 35 | +import java.io.IOException; |
| 36 | +import java.util.UUID; |
| 37 | + |
| 38 | +public class CrudProduct { |
| 39 | + |
| 40 | + public static void main(String[] args) throws IOException { |
| 41 | + // TODO(developer): Replace these variables before running the sample. |
| 42 | + String projectId = System.getenv("PROJECT_ID"); |
| 43 | + String generatedProductId = UUID.randomUUID().toString(); |
| 44 | + String defaultBranchName = |
| 45 | + String.format( |
| 46 | + "projects/%s/locations/global/catalogs/default_catalog/" + "branches/default_branch", |
| 47 | + projectId); |
| 48 | + String productName = String.format("%s/products/%s", defaultBranchName, generatedProductId); |
| 49 | + |
| 50 | + Product createdProduct = createProduct(generatedProductId, defaultBranchName); |
| 51 | + getProduct(productName); |
| 52 | + updateProduct(createdProduct, productName); |
| 53 | + deleteProduct(productName); |
| 54 | + } |
| 55 | + |
| 56 | + // generate product for create |
| 57 | + public static Product generateProduct() { |
| 58 | + float price = 30.0f; |
| 59 | + float originalPrice = 35.5f; |
| 60 | + |
| 61 | + PriceInfo priceInfo = |
| 62 | + PriceInfo.newBuilder() |
| 63 | + .setPrice(price) |
| 64 | + .setOriginalPrice(originalPrice) |
| 65 | + .setCurrencyCode("USD") |
| 66 | + .build(); |
| 67 | + |
| 68 | + return Product.newBuilder() |
| 69 | + .setTitle("Nest Mini") |
| 70 | + .setType(Type.PRIMARY) |
| 71 | + .addCategories("Speakers and displays") |
| 72 | + .addBrands("Google") |
| 73 | + .setPriceInfo(priceInfo) |
| 74 | + .setAvailability(Availability.IN_STOCK) |
| 75 | + .build(); |
| 76 | + } |
| 77 | + |
| 78 | + // generate product for update |
| 79 | + public static Product generateProductForUpdate(String productId, String productName) { |
| 80 | + final float price = 20.0f; |
| 81 | + final float originalPrice = 25.5f; |
| 82 | + |
| 83 | + PriceInfo priceInfo = |
| 84 | + PriceInfo.newBuilder() |
| 85 | + .setPrice(price) |
| 86 | + .setOriginalPrice(originalPrice) |
| 87 | + .setCurrencyCode("EUR") |
| 88 | + .build(); |
| 89 | + |
| 90 | + return Product.newBuilder() |
| 91 | + .setId(productId) |
| 92 | + .setName(productName) |
| 93 | + .setTitle("Updated Nest Mini") |
| 94 | + .setType(Type.PRIMARY) |
| 95 | + .addCategories("Updated Speakers and displays") |
| 96 | + .addBrands("Updated Google") |
| 97 | + .setAvailability(Availability.OUT_OF_STOCK) |
| 98 | + .setPriceInfo(priceInfo) |
| 99 | + .build(); |
| 100 | + } |
| 101 | + |
| 102 | + // call the Retail API to create product |
| 103 | + public static Product createProduct(String productId, String defaultBranchName) |
| 104 | + throws IOException { |
| 105 | + CreateProductRequest createProductRequest = |
| 106 | + CreateProductRequest.newBuilder() |
| 107 | + .setProduct(generateProduct()) |
| 108 | + .setProductId(productId) |
| 109 | + .setParent(defaultBranchName) |
| 110 | + .build(); |
| 111 | + System.out.printf("Create product request: %s%n", createProductRequest); |
| 112 | + |
| 113 | + Product createdProduct = ProductServiceClient.create().createProduct(createProductRequest); |
| 114 | + System.out.printf("Created product: %s%n", createdProduct); |
| 115 | + |
| 116 | + return createdProduct; |
| 117 | + } |
| 118 | + |
| 119 | + // get product |
| 120 | + public static Product getProduct(String productName) throws IOException { |
| 121 | + Product product = Product.newBuilder().build(); |
| 122 | + |
| 123 | + GetProductRequest getProductRequest = |
| 124 | + GetProductRequest.newBuilder().setName(productName).build(); |
| 125 | + |
| 126 | + try { |
| 127 | + product = ProductServiceClient.create().getProduct(getProductRequest); |
| 128 | + System.out.println("Get product response: " + product); |
| 129 | + return product; |
| 130 | + } catch (NotFoundException e) { |
| 131 | + System.out.printf("Product %s not found", productName); |
| 132 | + return product; |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + // update product |
| 137 | + public static void updateProduct(Product originalProduct, String productName) throws IOException { |
| 138 | + UpdateProductRequest updateProductRequest = |
| 139 | + UpdateProductRequest.newBuilder() |
| 140 | + .setProduct(generateProductForUpdate(originalProduct.getId(), productName)) |
| 141 | + .setAllowMissing(true) |
| 142 | + .build(); |
| 143 | + System.out.printf("Update product request: %s%n", updateProductRequest); |
| 144 | + |
| 145 | + Product updatedProduct = ProductServiceClient.create().updateProduct(updateProductRequest); |
| 146 | + System.out.printf("Updated product: %s%n", updatedProduct); |
| 147 | + } |
| 148 | + |
| 149 | + // delete product |
| 150 | + public static void deleteProduct(String productName) throws IOException { |
| 151 | + DeleteProductRequest deleteProductRequest = |
| 152 | + DeleteProductRequest.newBuilder().setName(productName).build(); |
| 153 | + System.out.printf("Delete product request %s%n", deleteProductRequest); |
| 154 | + |
| 155 | + ProductServiceClient.create().deleteProduct(deleteProductRequest); |
| 156 | + System.out.printf("Product %s was deleted.%n", productName); |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +// [END retail_crud_product] |
0 commit comments