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