16
16
17
17
package com .example .vision ;
18
18
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 ;
26
22
27
23
import java .io .IOException ;
28
24
import java .io .PrintStream ;
37
33
/**
38
34
* This application demonstrates how to perform basic operations with Products in a Product Set.
39
35
*
40
- * For more information, see the tutorial page at
36
+ * <p> For more information, see the tutorial page at
41
37
* https://cloud.google.com/vision/product-search/docs/
42
38
*/
43
-
44
39
public class ProductInProductSetManagement {
45
40
46
41
// [START vision_product_search_add_product_to_product_set]
@@ -56,18 +51,20 @@ public class ProductInProductSetManagement {
56
51
public static void addProductToProductSet (
57
52
String projectId , String computeRegion , String productId , String productSetId )
58
53
throws IOException {
59
- ProductSearchClient client = ProductSearchClient .create ();
54
+ try ( ProductSearchClient client = ProductSearchClient .create ()) {
60
55
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 );
63
59
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 ();
66
62
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 );
69
65
70
- System .out .println (String .format ("Product added to product set." ));
66
+ System .out .println (String .format ("Product added to product set." ));
67
+ }
71
68
}
72
69
// [END vision_product_search_add_product_to_product_set]
73
70
@@ -82,25 +79,27 @@ public static void addProductToProductSet(
82
79
*/
83
80
public static void listProductsInProductSet (
84
81
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
+ }
104
103
}
105
104
}
106
105
// [END vision_product_search_list_products_in_product_set]
@@ -118,18 +117,21 @@ public static void listProductsInProductSet(
118
117
public static void removeProductFromProductSet (
119
118
String projectId , String computeRegion , String productId , String productSetId )
120
119
throws IOException {
121
- ProductSearchClient client = ProductSearchClient .create ();
120
+ try ( ProductSearchClient client = ProductSearchClient .create ()) {
122
121
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 );
125
125
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 );
128
129
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 );
131
132
132
- System .out .println (String .format ("Product removed from product set." ));
133
+ System .out .println (String .format ("Product removed from product set." ));
134
+ }
133
135
}
134
136
// [END vision_product_search_remove_product_from_product_set]
135
137
0 commit comments