1- // Copyright 2018 Google LLC
1+ // Copyright 2022 Google LLC
22//
33// Licensed under the Apache License, Version 2.0 (the "License");
44// you may not use this file except in compliance with the License.
2323import com .google .ads .googleads .v11 .resources .GoogleAdsField ;
2424import com .google .ads .googleads .v11 .services .GoogleAdsFieldServiceClient ;
2525import com .google .ads .googleads .v11 .services .GoogleAdsFieldServiceClient .SearchGoogleAdsFieldsPagedResponse ;
26- import com .google .common .collect .Lists ;
2726import java .io .FileNotFoundException ;
2827import java .io .IOException ;
2928import java .util .ArrayList ;
3029import java .util .Collections ;
3130import java .util .List ;
3231
3332/**
34- * Gets the metadata, such as whether the artifact is selectable, filterable and sortable, of an
35- * artifact. The artifact can be either a resource (such as customer, campaign) or a field (such as
36- * metrics.impressions, campaign.id). It'll also show the data type and artifacts that are
37- * selectable with the artifact.
33+ * Searches for {@link GoogleAdsField}s that match a given prefix, retrieving metadata such as
34+ * whether the field is selectable, filterable, or sortable, along with the data type and the fields
35+ * that are selectable with the field. Each {@link GoogleAdsField} represents either a resource
36+ * (such as {@code customer}, {@code campaign}) or a field (such as {@code metrics.impressions},
37+ * {@code campaign.id}).
3838 */
39- public class GetArtifactMetadata {
39+ public class SearchForGoogleAdsFields {
4040
41- private static class GetArtifactMetadataParams extends CodeSampleParams {
41+ private static class SearchForGoogleAdsFieldsParams extends CodeSampleParams {
4242
43- @ Parameter (names = ArgumentNames .ARTIFACT_NAME , required = true )
44- private String artifactName ;
43+ @ Parameter (names = ArgumentNames .NAME_PREFIX , required = true )
44+ private String namePrefix ;
4545 }
4646
4747 public static void main (String [] args ) {
48- GetArtifactMetadataParams params = new GetArtifactMetadataParams ();
48+ SearchForGoogleAdsFieldsParams params = new SearchForGoogleAdsFieldsParams ();
4949 if (!params .parseArguments (args )) {
5050
5151 // Either pass the required parameters for this example on the command line, or insert them
5252 // into the code here. See the parameter class definition above for descriptions.
53- params .artifactName = "INSERT_ARTIFACT_NAME_HERE " ;
53+ params .namePrefix = "INSERT_NAME_PREFIX_HERE " ;
5454 }
5555
5656 GoogleAdsClient googleAdsClient = null ;
@@ -66,7 +66,7 @@ public static void main(String[] args) {
6666 }
6767
6868 try {
69- new GetArtifactMetadata ().runExample (googleAdsClient , params .artifactName );
69+ new SearchForGoogleAdsFields ().runExample (googleAdsClient , params .namePrefix );
7070 } catch (GoogleAdsException gae ) {
7171 // GoogleAdsException is the base class for most exceptions thrown by an API request.
7272 // Instances of this exception have a message and a GoogleAdsFailure that contains a
@@ -87,55 +87,58 @@ public static void main(String[] args) {
8787 * Runs the example.
8888 *
8989 * @param googleAdsClient the Google Ads API client.
90- * @param artifactName the name of artifact to get its metadata .
90+ * @param namePrefix the name prefix to use in the query .
9191 * @throws GoogleAdsException if an API request failed with one or more service errors.
9292 */
93- private void runExample (GoogleAdsClient googleAdsClient , String artifactName ) {
93+ private void runExample (GoogleAdsClient googleAdsClient , String namePrefix ) {
9494 try (GoogleAdsFieldServiceClient googleAdsFieldServiceClient =
9595 googleAdsClient .getLatestVersion ().createGoogleAdsFieldServiceClient ()) {
96- // Searches for an artifact whose name is the same as the specified artifactName .
96+ // Searches for all fields whose name begins with the specified namePrefix .
9797 SearchGoogleAdsFieldsPagedResponse searchGoogleAdsFieldsPagedResponse =
9898 googleAdsFieldServiceClient .searchGoogleAdsFields (
9999 String .format (
100- "SELECT name, category, selectable, filterable, sortable, selectable_with, "
101- + "data_type, is_repeated WHERE name = '%s'" ,
102- artifactName ));
100+ "SELECT "
101+ + "name, "
102+ + "category, "
103+ + "selectable, "
104+ + "filterable, "
105+ + "sortable, "
106+ + "data_type, "
107+ + "is_repeated, "
108+ + "selectable_with "
109+ // The double "%%" below will produce a single "%" in the string returned by
110+ // String.format. A single "%" is the wildcard token in the Google Ads Query
111+ // language.
112+ + "WHERE name LIKE '%s%%'" ,
113+ namePrefix ));
103114
104- // Gets all returned artifacts and prints out their metadata.
105- List <GoogleAdsField > googleAdsFields =
106- Lists .newArrayList (searchGoogleAdsFieldsPagedResponse .iterateAll ());
107- for (GoogleAdsField googleAdsField : googleAdsFields ) {
115+ if (searchGoogleAdsFieldsPagedResponse .getPage ().getResponse ().getTotalResultsCount () == 0 ) {
108116 System .out .printf (
109- "An artifact named '%s' with category '%s' and data type '%s' %s selectable, %s "
110- + "filterable, %s sortable and %s repeated.%n%n" ,
111- googleAdsField .getName (),
112- googleAdsField .getCategory (),
113- googleAdsField .getDataType (),
114- getIsOrIsNot (googleAdsField .getSelectable ()),
115- getIsOrIsNot (googleAdsField .getFilterable ()),
116- getIsOrIsNot (googleAdsField .getSortable ()),
117- getIsOrIsNot (googleAdsField .getIsRepeated ()));
117+ "No GoogleAdsFields found with a name that begins with '%s'.%n" , namePrefix );
118+ return ;
119+ }
118120
119- // Sorts the list of artifact names that are selectable with the specified artifact.
120- List <String > selectableArtifacts = new ArrayList (googleAdsField .getSelectableWithList ());
121- Collections .sort (selectableArtifacts );
121+ // Retrieves each matching GoogleAdsField and prints its metadata.
122+ for (GoogleAdsField googleAdsField : searchGoogleAdsFieldsPagedResponse .iterateAll ()) {
123+ System .out .printf ("%s:%n" , googleAdsField .getName ());
124+ System .out .printf (" %-16s %s%n" , "category:" , googleAdsField .getCategory ());
125+ System .out .printf (" %-16s %s%n" , "data type:" , googleAdsField .getDataType ());
126+ System .out .printf (" %-16s %s%n" , "selectable:" , googleAdsField .getSelectable ());
127+ System .out .printf (" %-16s %s%n" , "filterable:" , googleAdsField .getFilterable ());
128+ System .out .printf (" %-16s %s%n" , "sortable:" , googleAdsField .getSortable ());
129+ System .out .printf (" %-16s %s%n" , "repeated:" , googleAdsField .getIsRepeated ());
122130
123- System .out .println ("The artifact can be selected with the following artifacts:" );
124- for (String selectableField : selectableArtifacts ) {
125- System .out .println (selectableField );
131+ // Prints the list of fields that are selectable with the field.
132+ List <String > selectableWithFields = new ArrayList (googleAdsField .getSelectableWithList ());
133+ if (!selectableWithFields .isEmpty ()) {
134+ // Sorts and then prints the list.
135+ Collections .sort (selectableWithFields );
136+ System .out .printf (" %s%n" , "selectable with:" );
137+ for (String selectableWithField : selectableWithFields ) {
138+ System .out .printf (" %s%n" , selectableWithField );
139+ }
126140 }
127141 }
128-
129- if (googleAdsFields .isEmpty ()) {
130- System .err .printf ("The specified artifact '%s' doesn't exist.%n" , artifactName );
131- }
132142 }
133143 }
134-
135- /**
136- * Returns "is" when the specified value is true and "is not" when the specified value is false.
137- */
138- private String getIsOrIsNot (boolean value ) {
139- return value ? "is" : "is not" ;
140- }
141144}
0 commit comments