forked from GoogleCloudPlatform/java-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasicCompanySample.java
201 lines (173 loc) · 6.18 KB
/
BasicCompanySample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.samples;
import com.google.api.services.jobs.v3.CloudTalentSolution;
import com.google.api.services.jobs.v3.model.Company;
import com.google.api.services.jobs.v3.model.CreateCompanyRequest;
import com.google.api.services.jobs.v3.model.UpdateCompanyRequest;
import java.io.IOException;
import java.util.Random;
/**
* This file contains the basic knowledge about company and job, including:
*
* - Construct a company with required fields
*
* - Create a company
*
* - Get a company
*
* - Update a company
*
* - Update a company with field mask
*
* - Delete a company
*/
public final class BasicCompanySample {
private static final String DEFAULT_PROJECT_ID =
"projects/" + System.getenv("GOOGLE_CLOUD_PROJECT");
private static CloudTalentSolution talentSolutionClient =
JobServiceQuickstart.getTalentSolutionClient();
// [START basic_company]
/** Generate a company */
public static Company generateCompany() {
// distributor company id should be a unique Id in your system.
String companyName = "company:" + String.valueOf(new Random().nextLong());
Company company =
new Company()
.setDisplayName("Google")
.setHeadquartersAddress("1600 Amphitheatre Parkway Mountain View, CA 94043")
.setExternalId(companyName);
System.out.println("Company generated: " + company);
return company;
}
// [END basic_company]
// [START job_create_company]
// [START create_company]
/** Create a company. */
public static Company createCompany(Company companyToBeCreated) throws IOException {
try {
CreateCompanyRequest createCompanyRequest =
new CreateCompanyRequest().setCompany(companyToBeCreated);
Company companyCreated =
talentSolutionClient
.projects()
.companies()
.create(DEFAULT_PROJECT_ID, createCompanyRequest)
.execute();
System.out.println("Company created: " + companyCreated);
return companyCreated;
} catch (IOException e) {
System.out.println("Got exception while creating company");
throw e;
}
}
// [END create_company]
// [END job_create_company]
// [START job_get_company]
// [START get_company]
/** Get a company. */
public static Company getCompany(String companyName) throws IOException {
try {
Company companyExisted =
talentSolutionClient.projects().companies().get(companyName).execute();
System.out.println("Company existed: " + companyExisted);
return companyExisted;
} catch (IOException e) {
System.out.println("Got exception while getting company");
throw e;
}
}
// [END get_company]
// [END job_get_company]
// [START job_update_company]
// [START update_company]
/** Updates a company. */
public static Company updateCompany(String companyName, Company companyToBeUpdated)
throws IOException {
try {
UpdateCompanyRequest updateCompanyRequest =
new UpdateCompanyRequest().setCompany(companyToBeUpdated);
Company companyUpdated =
talentSolutionClient
.projects()
.companies()
.patch(companyName, updateCompanyRequest)
.execute();
System.out.println("Company updated: " + companyUpdated);
return companyUpdated;
} catch (IOException e) {
System.out.println("Got exception while updating company");
throw e;
}
}
// [END update_company]
// [END job_update_company]
// [START job_update_company_with_field_mask]
// [START update_company_with_field_mask]
/** Updates a company. */
public static Company updateCompanyWithFieldMask(
String companyName, String fieldMask, Company companyToBeUpdated) throws IOException {
try {
// String foo = String.format("?updateCompanyFields=%s",fieldMask);
UpdateCompanyRequest updateCompanyRequest =
new UpdateCompanyRequest().setUpdateMask(fieldMask).setCompany(companyToBeUpdated);
Company companyUpdated =
talentSolutionClient
.projects()
.companies()
.patch(companyName, updateCompanyRequest)
.execute();
System.out.println("Company updated: " + companyUpdated);
return companyUpdated;
} catch (IOException e) {
System.out.println("Got exception while updating company");
throw e;
}
}
// [END update_company_with_field_mask]
// [END job_update_company_with_field_mask]
// [START delete_company]
/** Delete a company. */
public static void deleteCompany(String companyName) throws IOException {
try {
talentSolutionClient.projects().companies().delete(companyName).execute();
System.out.println("Company deleted");
} catch (IOException e) {
System.out.println("Got exception while deleting company");
throw e;
}
}
// [END delete_company]
public static void main(String... args) throws Exception {
// Construct a company
Company companyToBeCreated = generateCompany();
// Create a company
Company companyCreated = createCompany(companyToBeCreated);
// Get a company
String companyName = companyCreated.getName();
getCompany(companyName);
// Update a company
Company companyToBeUpdated = companyCreated.setCareerSiteUri("https://elgoog.im/");
updateCompany(companyName, companyToBeUpdated);
// Update a company with field mask
updateCompanyWithFieldMask(
companyName,
"displayName",
new Company().setDisplayName("changedTitle").setName(companyCreated.getName()));
// Delete a company
deleteCompany(companyName);
}
}