|
4 | 4 |
|
5 | 5 | package com.microsoft.graph.models.extensions; |
6 | 6 |
|
7 | | -import com.microsoft.graph.concurrency.*; |
8 | | -import com.microsoft.graph.core.*; |
9 | | -import com.microsoft.graph.models.extensions.*; |
| 7 | +import com.google.gson.JsonElement; |
10 | 8 | import com.microsoft.graph.models.generated.*; |
11 | | -import com.microsoft.graph.http.*; |
12 | | -import com.microsoft.graph.requests.extensions.*; |
13 | | -import com.microsoft.graph.requests.generated.*; |
14 | | -import com.microsoft.graph.options.*; |
15 | 9 | import com.microsoft.graph.serializer.*; |
16 | 10 |
|
17 | | -import java.util.Arrays; |
18 | | -import java.util.EnumSet; |
19 | | - |
| 11 | +import java.security.InvalidParameterException; |
20 | 12 | // This file is available for extending, afterwards please submit a pull request. |
21 | 13 |
|
22 | 14 | /** |
23 | 15 | * The class for the Planner External References. |
24 | 16 | */ |
25 | 17 | public class PlannerExternalReferences extends BasePlannerExternalReferences { |
26 | | - |
| 18 | + /* |
| 19 | + * Specifies the character - encoding pairs to apply on the external reference URLs |
| 20 | + */ |
| 21 | + private static String[][] conversions = { { "%", "%25" }, { "@", "%40" }, { ".", "%2E" }, { ":", "%3A" } }; |
| 22 | + |
| 23 | + /** |
| 24 | + * Gets external reference data for a given reference URL |
| 25 | + * @param url The external reference URL |
| 26 | + * @return The external reference |
| 27 | + */ |
| 28 | + public PlannerExternalReference reference(String url) { |
| 29 | + if (url == null || url.isEmpty()) { |
| 30 | + throw new InvalidParameterException("URL cannot be empty"); |
| 31 | + } |
| 32 | + if (this.additionalDataManager().containsKey(encode(url))) { |
| 33 | + JsonElement refObject = this.additionalDataManager().get(encode(url)); |
| 34 | + ISerializer serializer = this.getSerializer(); |
| 35 | + return serializer.deserializeObject( |
| 36 | + refObject.getAsString(), |
| 37 | + PlannerExternalReference.class |
| 38 | + ); |
| 39 | + } else { |
| 40 | + return null; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Adds a new external reference with the given URL and short name |
| 46 | + * @param url URL of the external reference |
| 47 | + * @param alias Short name for the external reference |
| 48 | + * @return The created external reference |
| 49 | + */ |
| 50 | + public PlannerExternalReference addReference(String url, String alias) { |
| 51 | + if (url == null || url.isEmpty()) { |
| 52 | + throw new InvalidParameterException("URL cannot be empty"); |
| 53 | + } |
| 54 | + if (alias == null || alias.isEmpty()) { |
| 55 | + throw new InvalidParameterException("Alias cannot be empty"); |
| 56 | + } |
| 57 | + |
| 58 | + PlannerExternalReference plannerExternalReference = new PlannerExternalReference(); |
| 59 | + plannerExternalReference.alias = alias; |
| 60 | + this.additionalDataManager().put(encode(url), plannerExternalReference.getRawObject()); |
| 61 | + return plannerExternalReference; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Encodes the URL of an external reference to be compatible with OData property naming requirements |
| 66 | + * @param externalReferenceUrl URL to encode |
| 67 | + * @return Encoded URL |
| 68 | + */ |
| 69 | + private static String encode(String externalReferenceUrl) { |
| 70 | + if (externalReferenceUrl == null || externalReferenceUrl.isEmpty()) { |
| 71 | + throw new InvalidParameterException("URL cannot be empty"); |
| 72 | + } |
| 73 | + |
| 74 | + for (int i = 0; i < conversions.length; i++) |
| 75 | + { |
| 76 | + externalReferenceUrl = externalReferenceUrl.replace(conversions[i][0], conversions[i][1]); |
| 77 | + } |
| 78 | + return externalReferenceUrl; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Decodes an encoded URL of an external reference |
| 83 | + * @param externalReferenceUrl URL to encode |
| 84 | + * @return Decoded URL |
| 85 | + */ |
| 86 | + private static String decode(String externalReferenceUrl) { |
| 87 | + if (externalReferenceUrl == null || externalReferenceUrl.isEmpty()) { |
| 88 | + throw new InvalidParameterException("URL cannot be empty"); |
| 89 | + } |
| 90 | + |
| 91 | + for (int i = conversions.length - 1; i >= 0; i--) { |
| 92 | + externalReferenceUrl = externalReferenceUrl.replace(conversions[i][1], conversions[i][0]); |
| 93 | + } |
| 94 | + return externalReferenceUrl; |
| 95 | + } |
27 | 96 | } |
0 commit comments