1616
1717package com .example .vision ;
1818
19- import com .google .cloud .vision .v1p3beta1 .LocationName ;
20- import com .google .cloud .vision .v1p3beta1 .Product ;
21- import com .google .cloud .vision .v1p3beta1 .ProductName ;
22- import com .google .cloud .vision .v1p3beta1 .ProductSearchClient ;
23- import com .google .cloud .vision .v1p3beta1 .ProductSet ;
24- import com .google .cloud .vision .v1p3beta1 .ProductSetName ;
25- import com .google .protobuf .FieldMask ;
19+ import com .google .cloud .vision .v1 .Product ;
20+ import com .google .cloud .vision .v1 .ProductName ;
21+ import com .google .cloud .vision .v1 .ProductSearchClient ;
2622
2723import java .io .IOException ;
2824import java .io .PrintStream ;
3733/**
3834 * This application demonstrates how to perform basic operations with Products in a Product Set.
3935 *
40- * For more information, see the tutorial page at
36+ * <p> For more information, see the tutorial page at
4137 * https://cloud.google.com/vision/product-search/docs/
4238 */
43-
4439public class ProductInProductSetManagement {
4540
4641 // [START vision_product_search_add_product_to_product_set]
@@ -56,18 +51,20 @@ public class ProductInProductSetManagement {
5651 public static void addProductToProductSet (
5752 String projectId , String computeRegion , String productId , String productSetId )
5853 throws IOException {
59- ProductSearchClient client = ProductSearchClient .create ();
54+ try ( ProductSearchClient client = ProductSearchClient .create ()) {
6055
61- // Get the full path of the product set.
62- ProductSetName productSetPath = ProductSetName .of (projectId , computeRegion , productSetId );
56+ // Get the full path of the product set.
57+ String formattedName =
58+ ProductSearchClient .formatProductSetName (projectId , computeRegion , productSetId );
6359
64- // Get the full path of the product.
65- String productPath = ProductName .of (projectId , computeRegion , productId ).toString ();
60+ // Get the full path of the product.
61+ String productPath = ProductName .of (projectId , computeRegion , productId ).toString ();
6662
67- // Add the product to the product set.
68- client .addProductToProductSet (productSetPath , productPath );
63+ // Add the product to the product set.
64+ client .addProductToProductSet (formattedName , productPath );
6965
70- System .out .println (String .format ("Product added to product set." ));
66+ System .out .println (String .format ("Product added to product set." ));
67+ }
7168 }
7269 // [END vision_product_search_add_product_to_product_set]
7370
@@ -82,25 +79,27 @@ public static void addProductToProductSet(
8279 */
8380 public static void listProductsInProductSet (
8481 String projectId , String computeRegion , String productSetId ) throws IOException {
85- ProductSearchClient client = ProductSearchClient .create ();
86-
87- // Get the full path of the product set.
88- ProductSetName productSetPath = ProductSetName .of (projectId , computeRegion , productSetId );
89-
90- // List all the products available in the product set.
91- for (Product product :
92- client .listProductsInProductSet (productSetPath .toString ()).iterateAll ()) {
93- // Display the product information
94- System .out .println (String .format ("Product name: %s" , product .getName ()));
95- System .out .println (
96- String .format (
97- "Product id: %s" ,
98- product .getName ().substring (product .getName ().lastIndexOf ('/' ) + 1 )));
99- System .out .println (String .format ("Product display name: %s" , product .getDisplayName ()));
100- System .out .println (String .format ("Product description: %s" , product .getDescription ()));
101- System .out .println (String .format ("Product category: %s" , product .getProductCategory ()));
102- System .out .println (
103- String .format ("Product labels: %s\n " , product .getProductLabelsList ().toString ()));
82+ try (ProductSearchClient client = ProductSearchClient .create ()) {
83+
84+ // Get the full path of the product set.
85+ String formattedName =
86+ ProductSearchClient .formatProductSetName (projectId , computeRegion , productSetId );
87+ // List all the products available in the product set.
88+ for (Product product : client .listProductsInProductSet (formattedName ).iterateAll ()) {
89+ // Display the product information
90+ System .out .println (String .format ("Product name: %s" , product .getName ()));
91+ System .out .println (
92+ String .format (
93+ "Product id: %s" ,
94+ product .getName ().substring (product .getName ().lastIndexOf ('/' ) + 1 )));
95+ System .out .println (String .format ("Product display name: %s" , product .getDisplayName ()));
96+ System .out .println (String .format ("Product description: %s" , product .getDescription ()));
97+ System .out .println (String .format ("Product category: %s" , product .getProductCategory ()));
98+ System .out .println ("Product labels: " );
99+ for (Product .KeyValue element : product .getProductLabelsList ()) {
100+ System .out .println (String .format ("%s: %s" , element .getKey (), element .getValue ()));
101+ }
102+ }
104103 }
105104 }
106105 // [END vision_product_search_list_products_in_product_set]
@@ -118,18 +117,21 @@ public static void listProductsInProductSet(
118117 public static void removeProductFromProductSet (
119118 String projectId , String computeRegion , String productId , String productSetId )
120119 throws IOException {
121- ProductSearchClient client = ProductSearchClient .create ();
120+ try ( ProductSearchClient client = ProductSearchClient .create ()) {
122121
123- // Get the full path of the product set.
124- ProductSetName productSetPath = ProductSetName .of (projectId , computeRegion , productSetId );
122+ // Get the full path of the product set.
123+ String formattedParent =
124+ ProductSearchClient .formatProductSetName (projectId , computeRegion , productSetId );
125125
126- // Get the full path of the product.
127- String productPath = ProductName .of (projectId , computeRegion , productId ).toString ();
126+ // Get the full path of the product.
127+ String formattedName =
128+ ProductSearchClient .formatProductName (projectId , computeRegion , productId );
128129
129- // Remove the product from the product set.
130- client .removeProductFromProductSet (productSetPath , productPath );
130+ // Remove the product from the product set.
131+ client .removeProductFromProductSet (formattedParent , formattedName );
131132
132- System .out .println (String .format ("Product removed from product set." ));
133+ System .out .println (String .format ("Product removed from product set." ));
134+ }
133135 }
134136 // [END vision_product_search_remove_product_from_product_set]
135137
0 commit comments