1616
1717package com .example .vision ;
1818
19- // [START product_search_import]
2019import com .google .cloud .vision .v1p3beta1 .LocationName ;
2120import com .google .cloud .vision .v1p3beta1 .Product ;
2221import com .google .cloud .vision .v1p3beta1 .ProductName ;
2322import com .google .cloud .vision .v1p3beta1 .ProductSearchClient ;
2423import com .google .cloud .vision .v1p3beta1 .ProductSet ;
2524import com .google .cloud .vision .v1p3beta1 .ProductSetName ;
26- import com .google .cloud .vision .v1p3beta1 .UpdateProductSetRequest ;
2725import com .google .protobuf .FieldMask ;
2826
2927import java .io .IOException ;
3533import net .sourceforge .argparse4j .inf .Namespace ;
3634import net .sourceforge .argparse4j .inf .Subparser ;
3735import net .sourceforge .argparse4j .inf .Subparsers ;
38- // [END product_search_import]
3936
4037/**
4138 * This application demonstrates how to perform basic operations with Products in a Product Set.
4643
4744public class ProductInProductSetManagement {
4845
49- // [START product_search_add_product_to_product_set ]
46+ // [START vision_product_search_add_product_to_product_set ]
5047 /**
51- * Update a product set.
52- *
53- * @param projectId - Id of the project.
54- * @param computeRegion - Region name.
55- * @param productSetId - Id of the product set.
56- * @param productSetDisplayName - Display name of the product set.
57- * @throws IOException - on I/O errors.
58- */
59- public static void addProductToProductSet (
60- String projectId , String computeRegion , String productSetId , String productSetDisplayName )
61- throws IOException {
62- ProductSearchClient client = ProductSearchClient .create ();
63-
64- // Get the full path of the product set.
65- String productSetPath = ProductSetName .of (projectId , computeRegion , productSetId ).toString ();
66-
67- // Update the product set display name.
68- ProductSet productSet =
69- ProductSet .newBuilder ()
70- .setName (productSetPath )
71- .setDisplayName (productSetDisplayName )
72- .build ();
73-
74- FieldMask updateMask = FieldMask .newBuilder ().addPaths ("display_name" ).build ();
75-
76- UpdateProductSetRequest request =
77- UpdateProductSetRequest .newBuilder ()
78- .setProductSet (productSet )
79- .setUpdateMask (updateMask )
80- .build ();
81-
82- ProductSet updatedProductSet = client .updateProductSet (request );
83-
84- // Display the updated set information
85- System .out .println (String .format ("Product set name: %s" , updatedProductSet .getName ()));
86- System .out .println (
87- String .format (
88- "Product set id: %s" ,
89- updatedProductSet
90- .getName ()
91- .substring (updatedProductSet .getName ().lastIndexOf ('/' ) + 1 )));
92- System .out .println (
93- String .format ("Updated product set display name: %s" , updatedProductSet .getDisplayName ()));
94- }
95- // [END product_search_add_product_to_product_set]
96-
97- // [START product_search_remove_product_from_product_set]
98- /**
99- * Remove a product from a product set.
48+ * Add a product to a product set.
10049 *
10150 * @param projectId - Id of the project.
10251 * @param computeRegion - Region name.
10352 * @param productId - Id of the product.
10453 * @param productSetId - Id of the product set.
10554 * @throws IOException - on I/O errors.
10655 */
107- public static void removeProductFromProductSet (
56+ public static void addProductToSet (
10857 String projectId , String computeRegion , String productId , String productSetId )
10958 throws IOException {
11059 ProductSearchClient client = ProductSearchClient .create ();
@@ -115,14 +64,14 @@ public static void removeProductFromProductSet(
11564 // Get the full path of the product.
11665 String productPath = ProductName .of (projectId , computeRegion , productId ).toString ();
11766
118- // Remove the product from the product set.
119- client .removeProductFromProductSet (productSetPath , productPath );
67+ // Add the product to the product set.
68+ client .addProductToProductSet (productSetPath , productPath );
12069
121- System .out .println (String .format ("Product removed from product set." ));
70+ System .out .println (String .format ("Product added to product set." ));
12271 }
123- // [END product_search_remove_product_from_product_set ]
72+ // [END vision_product_search_add_product_to_product_set ]
12473
125- // [START product_search_list_products_in_product_set ]
74+ // [START vision_product_search_list_products_in_product_set ]
12675 /**
12776 * List all products in a product set.
12877 *
@@ -154,38 +103,35 @@ public static void listProductsInProductSet(
154103 String .format ("Product labels: %s\n " , product .getProductLabelsList ().toString ()));
155104 }
156105 }
157- // [END product_search_list_products_in_product_set ]
106+ // [END vision_product_search_list_products_in_product_set ]
158107
159- // [START product_search_list_products ]
108+ // [START vision_product_search_remove_product_from_product_set ]
160109 /**
161- * List all products .
110+ * Remove a product from a product set .
162111 *
163112 * @param projectId - Id of the project.
164113 * @param computeRegion - Region name.
114+ * @param productId - Id of the product.
115+ * @param productSetId - Id of the product set.
165116 * @throws IOException - on I/O errors.
166117 */
167- public static void listProducts (String projectId , String computeRegion ) throws IOException {
118+ public static void removeProductFromProductSet (
119+ String projectId , String computeRegion , String productId , String productSetId )
120+ throws IOException {
168121 ProductSearchClient client = ProductSearchClient .create ();
169122
170- // A resource that represents Google Cloud Platform location.
171- LocationName projectLocation = LocationName .of (projectId , computeRegion );
123+ // Get the full path of the product set.
124+ ProductSetName productSetPath = ProductSetName .of (projectId , computeRegion , productSetId );
125+
126+ // Get the full path of the product.
127+ String productPath = ProductName .of (projectId , computeRegion , productId ).toString ();
172128
173- // List all the products available in the region.
174- for (Product product : client .listProducts (projectLocation ).iterateAll ()) {
175- // Display the product information
176- System .out .println (String .format ("\n Product name: %s" , product .getName ()));
177- System .out .println (
178- String .format (
179- "Product id: %s" ,
180- product .getName ().substring (product .getName ().lastIndexOf ('/' ) + 1 )));
181- System .out .println (String .format ("Product display name: %s" , product .getDisplayName ()));
182- System .out .println (String .format ("Product category: %s" , product .getProductCategory ()));
183- System .out .println ("Product labels:" );
184- System .out .println (
185- String .format ("Product labels: %s" , product .getProductLabelsList ().toString ()));
186- }
129+ // Remove the product from the product set.
130+ client .removeProductFromProductSet (productSetPath , productPath );
131+
132+ System .out .println (String .format ("Product removed from product set." ));
187133 }
188- // [END product_search_list_products ]
134+ // [END vision_product_search_remove_product_from_product_set ]
189135
190136 public static void main (String [] args ) throws Exception {
191137 ProductInProductSetManagement productInProductSetManagement =
@@ -196,15 +142,14 @@ public static void main(String[] args) throws Exception {
196142 public static void argsHelper (String [] args , PrintStream out ) throws Exception {
197143 ArgumentParser parser = ArgumentParsers .newFor ("" ).build ();
198144 Subparsers subparsers = parser .addSubparsers ().dest ("command" );
199- subparsers .addParser ("list_products" );
145+
146+ Subparser addProductParser = subparsers .addParser ("add_product_to_product_set" );
147+ addProductParser .addArgument ("productSetId" );
148+ addProductParser .addArgument ("productId" );
200149
201150 Subparser listProductInProductSetParser = subparsers .addParser ("list_products_in_product_set" );
202151 listProductInProductSetParser .addArgument ("productSetId" );
203152
204- Subparser updateProductSetParser = subparsers .addParser ("update_product_set" );
205- updateProductSetParser .addArgument ("productSetId" );
206- updateProductSetParser .addArgument ("productSetDisplayName" );
207-
208153 Subparser removeProductFromProductSetParser =
209154 subparsers .addParser ("remove_product_from_product_set" );
210155 removeProductFromProductSetParser .addArgument ("productId" );
@@ -216,23 +161,17 @@ public static void argsHelper(String[] args, PrintStream out) throws Exception {
216161 Namespace ns = null ;
217162 try {
218163 ns = parser .parseArgs (args );
219- if (ns .get ("command" ).equals ("list_products" )) {
220- listProducts (projectId , computeRegion );
164+ if (ns .get ("command" ).equals ("add_product_to_product_set" )) {
165+ addProductToSet (
166+ projectId , computeRegion , ns .getString ("productId" ), ns .getString ("productSetId" ));
221167 }
222- if (ns .get ("command" ).equals ("update_product_set" )) {
223- addProductToProductSet (
224- projectId ,
225- computeRegion ,
226- ns .getString ("productSetId" ),
227- ns .getString ("productSetDisplayName" ));
168+ if (ns .get ("command" ).equals ("list_products_in_product_set" )) {
169+ listProductsInProductSet (projectId , computeRegion , ns .getString ("productSetId" ));
228170 }
229171 if (ns .get ("command" ).equals ("remove_product_from_product_set" )) {
230172 removeProductFromProductSet (
231173 projectId , computeRegion , ns .getString ("productId" ), ns .getString ("productSetId" ));
232174 }
233- if (ns .get ("command" ).equals ("list_products_in_product_set" )) {
234- listProductsInProductSet (projectId , computeRegion , ns .getString ("productSetId" ));
235- }
236175
237176 } catch (ArgumentParserException e ) {
238177 parser .handleError (e );
0 commit comments