|
| 1 | +package com.box.sdk; |
| 2 | + |
| 3 | +import com.eclipsesource.json.JsonArray; |
| 4 | +import com.eclipsesource.json.JsonObject; |
| 5 | + |
| 6 | +/** |
| 7 | + * The Metadata class represents one type instance of Box metadata. |
| 8 | + * |
| 9 | + * Learn more about Box metadata: |
| 10 | + * https://developers.box.com/metadata-api/ |
| 11 | + */ |
| 12 | +public class Metadata { |
| 13 | + private final JsonObject values; |
| 14 | + private JsonArray operations; |
| 15 | + |
| 16 | + /** |
| 17 | + * Creates an empty metadata. |
| 18 | + */ |
| 19 | + public Metadata() { |
| 20 | + this.values = new JsonObject(); |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * Creates a new metadata. |
| 25 | + * @param values The initial metadata values. |
| 26 | + */ |
| 27 | + public Metadata(JsonObject values) { |
| 28 | + this.values = values; |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Creates a copy of another metadata. |
| 33 | + * @param other The other metadata object to copy. |
| 34 | + */ |
| 35 | + public Metadata(Metadata other) { |
| 36 | + this.values = new JsonObject(other.values); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Returns the 36 character UUID to identify the metadata object. |
| 41 | + * @return the metadata ID. |
| 42 | + */ |
| 43 | + public String getID() { |
| 44 | + return this.values.get("$id").asString(); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Returns the metadata type. |
| 49 | + * @return the metadata type. |
| 50 | + */ |
| 51 | + public String getTypeName() { |
| 52 | + return this.values.get("$type").asString(); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Returns the parent object ID (typically the file ID). |
| 57 | + * @return the parent object ID. |
| 58 | + */ |
| 59 | + public String getParentID() { |
| 60 | + return this.values.get("$parent").asString(); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Adds a new metdata value. |
| 65 | + * @param path The path that designates the key. Must be prefixed with a "/". |
| 66 | + * @param value The value. |
| 67 | + * @return this metadata object. |
| 68 | + */ |
| 69 | + public Metadata add(String path, String value) { |
| 70 | + this.values.add(this.pathToProperty(path), value); |
| 71 | + this.addOp("add", path, value); |
| 72 | + return this; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Replaces an existing metdata value. |
| 77 | + * @param path The path that designates the key. Must be prefixed with a "/". |
| 78 | + * @param value The value. |
| 79 | + * @return this metadata object. |
| 80 | + */ |
| 81 | + public Metadata replace(String path, String value) { |
| 82 | + this.values.set(this.pathToProperty(path), value); |
| 83 | + this.addOp("replace", path, value); |
| 84 | + return this; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Removes an existing metadata value. |
| 89 | + * @param path The path that designates the key. Must be prefixed with a "/". |
| 90 | + * @return this metadata object. |
| 91 | + */ |
| 92 | + public Metadata remove(String path) { |
| 93 | + this.values.remove(this.pathToProperty(path)); |
| 94 | + this.addOp("remove", path, null); |
| 95 | + return this; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Tests that a property has the expected value. |
| 100 | + * @param path The path that designates the key. Must be prefixed with a "/". |
| 101 | + * @param value The expected value. |
| 102 | + * @return this metadata object. |
| 103 | + */ |
| 104 | + public Metadata test(String path, String value) { |
| 105 | + this.addOp("test", path, value); |
| 106 | + return this; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Returns a value. |
| 111 | + * @param key The metadata property name. |
| 112 | + * @return The metadata property value. |
| 113 | + */ |
| 114 | + public String get(String key) { |
| 115 | + return this.values.get(key).asString(); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Returns the JSON patch string with all operations. |
| 120 | + * @return the JSON patch string. |
| 121 | + */ |
| 122 | + public String getPatch() { |
| 123 | + if (this.operations == null) { |
| 124 | + return "[]"; |
| 125 | + } |
| 126 | + return this.operations.toString(); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Returns the JSON representation of this metadata. |
| 131 | + * @return the JSON representation of this metadata. |
| 132 | + */ |
| 133 | + @Override |
| 134 | + public String toString() { |
| 135 | + return this.values.toString(); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Converts a JSON patch path to a JSON property name. |
| 140 | + * @param path The path that designates the key. Must be prefixed with a "/". |
| 141 | + * @return The JSON property name. |
| 142 | + */ |
| 143 | + private String pathToProperty(String path) { |
| 144 | + return path.substring(1); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Adds a patch operation. |
| 149 | + * @param op The operation type. Must be add, replace, remove, or test. |
| 150 | + * @param path The path that designates the key. Must be prefixed with a "/". |
| 151 | + * @param value The value to be set. |
| 152 | + */ |
| 153 | + private void addOp(String op, String path, String value) { |
| 154 | + if (this.operations == null) { |
| 155 | + this.operations = new JsonArray(); |
| 156 | + } |
| 157 | + |
| 158 | + this.operations.add(new JsonObject() |
| 159 | + .add("op", op) |
| 160 | + .add("path", path) |
| 161 | + .add("value", value)); |
| 162 | + } |
| 163 | +} |
0 commit comments