16
16
17
17
package com .example .vision ;
18
18
19
- // [START product_search_import]
20
19
import com .google .cloud .vision .v1p3beta1 .LocationName ;
21
20
import com .google .cloud .vision .v1p3beta1 .Product ;
22
21
import com .google .cloud .vision .v1p3beta1 .ProductName ;
23
22
import com .google .cloud .vision .v1p3beta1 .ProductSearchClient ;
24
23
import com .google .cloud .vision .v1p3beta1 .ProductSet ;
25
24
import com .google .cloud .vision .v1p3beta1 .ProductSetName ;
26
- import com .google .cloud .vision .v1p3beta1 .UpdateProductSetRequest ;
27
25
import com .google .protobuf .FieldMask ;
28
26
29
27
import java .io .IOException ;
35
33
import net .sourceforge .argparse4j .inf .Namespace ;
36
34
import net .sourceforge .argparse4j .inf .Subparser ;
37
35
import net .sourceforge .argparse4j .inf .Subparsers ;
38
- // [END product_search_import]
39
36
40
37
/**
41
38
* This application demonstrates how to perform basic operations with Products in a Product Set.
46
43
47
44
public class ProductInProductSetManagement {
48
45
49
- // [START product_search_add_product_to_product_set ]
46
+ // [START vision_product_search_add_product_to_product_set ]
50
47
/**
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.
100
49
*
101
50
* @param projectId - Id of the project.
102
51
* @param computeRegion - Region name.
103
52
* @param productId - Id of the product.
104
53
* @param productSetId - Id of the product set.
105
54
* @throws IOException - on I/O errors.
106
55
*/
107
- public static void removeProductFromProductSet (
56
+ public static void addProductToSet (
108
57
String projectId , String computeRegion , String productId , String productSetId )
109
58
throws IOException {
110
59
ProductSearchClient client = ProductSearchClient .create ();
@@ -115,14 +64,14 @@ public static void removeProductFromProductSet(
115
64
// Get the full path of the product.
116
65
String productPath = ProductName .of (projectId , computeRegion , productId ).toString ();
117
66
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 );
120
69
121
- System .out .println (String .format ("Product removed from product set." ));
70
+ System .out .println (String .format ("Product added to product set." ));
122
71
}
123
- // [END product_search_remove_product_from_product_set ]
72
+ // [END vision_product_search_add_product_to_product_set ]
124
73
125
- // [START product_search_list_products_in_product_set ]
74
+ // [START vision_product_search_list_products_in_product_set ]
126
75
/**
127
76
* List all products in a product set.
128
77
*
@@ -154,38 +103,35 @@ public static void listProductsInProductSet(
154
103
String .format ("Product labels: %s\n " , product .getProductLabelsList ().toString ()));
155
104
}
156
105
}
157
- // [END product_search_list_products_in_product_set ]
106
+ // [END vision_product_search_list_products_in_product_set ]
158
107
159
- // [START product_search_list_products ]
108
+ // [START vision_product_search_remove_product_from_product_set ]
160
109
/**
161
- * List all products .
110
+ * Remove a product from a product set .
162
111
*
163
112
* @param projectId - Id of the project.
164
113
* @param computeRegion - Region name.
114
+ * @param productId - Id of the product.
115
+ * @param productSetId - Id of the product set.
165
116
* @throws IOException - on I/O errors.
166
117
*/
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 {
168
121
ProductSearchClient client = ProductSearchClient .create ();
169
122
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 ();
172
128
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." ));
187
133
}
188
- // [END product_search_list_products ]
134
+ // [END vision_product_search_remove_product_from_product_set ]
189
135
190
136
public static void main (String [] args ) throws Exception {
191
137
ProductInProductSetManagement productInProductSetManagement =
@@ -196,15 +142,14 @@ public static void main(String[] args) throws Exception {
196
142
public static void argsHelper (String [] args , PrintStream out ) throws Exception {
197
143
ArgumentParser parser = ArgumentParsers .newFor ("" ).build ();
198
144
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" );
200
149
201
150
Subparser listProductInProductSetParser = subparsers .addParser ("list_products_in_product_set" );
202
151
listProductInProductSetParser .addArgument ("productSetId" );
203
152
204
- Subparser updateProductSetParser = subparsers .addParser ("update_product_set" );
205
- updateProductSetParser .addArgument ("productSetId" );
206
- updateProductSetParser .addArgument ("productSetDisplayName" );
207
-
208
153
Subparser removeProductFromProductSetParser =
209
154
subparsers .addParser ("remove_product_from_product_set" );
210
155
removeProductFromProductSetParser .addArgument ("productId" );
@@ -216,23 +161,17 @@ public static void argsHelper(String[] args, PrintStream out) throws Exception {
216
161
Namespace ns = null ;
217
162
try {
218
163
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" ));
221
167
}
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" ));
228
170
}
229
171
if (ns .get ("command" ).equals ("remove_product_from_product_set" )) {
230
172
removeProductFromProductSet (
231
173
projectId , computeRegion , ns .getString ("productId" ), ns .getString ("productSetId" ));
232
174
}
233
- if (ns .get ("command" ).equals ("list_products_in_product_set" )) {
234
- listProductsInProductSet (projectId , computeRegion , ns .getString ("productSetId" ));
235
- }
236
175
237
176
} catch (ArgumentParserException e ) {
238
177
parser .handleError (e );
0 commit comments