|
| 1 | +package com.arangodb.entity; |
| 2 | + |
| 3 | +import com.google.gson.annotations.SerializedName; |
| 4 | + |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.Map; |
| 7 | + |
| 8 | +/** |
| 9 | + * Created by gschwab on 1/14/15. |
| 10 | + */ |
| 11 | +public class BaseDocument extends BaseEntity implements DocumentHolder { |
| 12 | + |
| 13 | + public static final String REV = "_rev"; |
| 14 | + |
| 15 | + public static final String KEY = "_key"; |
| 16 | + |
| 17 | + /** |
| 18 | + * the documents revision number |
| 19 | + */ |
| 20 | + @SerializedName("_rev") |
| 21 | + long documentRevision; |
| 22 | + |
| 23 | + /** |
| 24 | + * the document handle |
| 25 | + */ |
| 26 | + @SerializedName("_id") |
| 27 | + String documentHandle; |
| 28 | + |
| 29 | + /** |
| 30 | + * the document key |
| 31 | + */ |
| 32 | + @SerializedName("_key") |
| 33 | + String documentKey; |
| 34 | + |
| 35 | + /** |
| 36 | + * the map containing the key value pairs |
| 37 | + */ |
| 38 | + Map<String, Object> properties = new HashMap<String, Object>(); |
| 39 | + |
| 40 | + /** |
| 41 | + * create an empty BaseDocument |
| 42 | + */ |
| 43 | + public BaseDocument() { |
| 44 | + this.init(); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * create an empty BaseDocument with a given key |
| 49 | + * |
| 50 | + * @param documentKey the unique key of the document |
| 51 | + */ |
| 52 | + public BaseDocument(String documentKey) { |
| 53 | + this.init(); |
| 54 | + this.documentKey = documentKey; |
| 55 | + } |
| 56 | + |
| 57 | +// /** |
| 58 | +// * @param keyValues a set of key/value pairs containing the attributes for the document. |
| 59 | +// * The length has to be even and each even entry has to be of type String. |
| 60 | +// * If not an empty document will be created |
| 61 | +// */ |
| 62 | +// public BaseDocument(Object ...keyValues) { |
| 63 | +// this(null, keyValues); |
| 64 | +// } |
| 65 | +// |
| 66 | +// /** |
| 67 | +// * create a BaseDocument with a given key and attributes defined in keyValues |
| 68 | +// * |
| 69 | +// * @param documentKey the unique key of the document |
| 70 | +// * @param keyValues a set of key/value pairs containing the attributes for the document. |
| 71 | +// * The length has to be even and each even entry has to be of type String. |
| 72 | +// * If not an empty document will be created |
| 73 | +// */ |
| 74 | +// public BaseDocument(String documentKey, Object ...keyValues) { |
| 75 | +// this.init(); |
| 76 | +// if (documentKey != null) { |
| 77 | +// this.documentKey = documentKey; |
| 78 | +// } |
| 79 | +// if (checkKeyValues(keyValues)) { |
| 80 | +// for (int i = 0; i < keyValues.length; i = i+2) { |
| 81 | +// if (keyValues[i] == REV) { |
| 82 | +// this.documentRevision = (Long) keyValues[i+1]; |
| 83 | +// } else if (keyValues[i] == KEY && documentKey == null) { |
| 84 | +// this.documentKey = (String) keyValues[i+1]; |
| 85 | +// } else { |
| 86 | +// this.addAttribute((String) keyValues[i], keyValues[i + 1]); |
| 87 | +// } |
| 88 | +// } |
| 89 | +// } |
| 90 | +// } |
| 91 | + |
| 92 | + /** |
| 93 | + * create an BaseDocument with given attributes |
| 94 | + * |
| 95 | + * @param properties the attributes (key/value) of the document to be created |
| 96 | + */ |
| 97 | + public BaseDocument(Map<String, Object> properties) { |
| 98 | + this(null, properties); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * create an BaseDocument with given key and attributes |
| 103 | + * |
| 104 | + * @param documentKey the unique key of the document |
| 105 | + * @param properties the attributes (key/value) of the document to be created |
| 106 | + */ |
| 107 | + public BaseDocument(String documentKey, Map<String, Object> properties) { |
| 108 | + this.init(); |
| 109 | + if (documentKey != null) { |
| 110 | + this.documentKey = documentKey; |
| 111 | + } |
| 112 | + if (properties.containsKey(REV)) { |
| 113 | + this.documentRevision = (Long) properties.get(REV); |
| 114 | + properties.remove(REV); |
| 115 | + } |
| 116 | + if (properties.containsKey(KEY)) { |
| 117 | + if (documentKey == null) { |
| 118 | + this.documentKey = (String) properties.get(KEY); |
| 119 | + } |
| 120 | + properties.remove(KEY); |
| 121 | + } |
| 122 | + this.properties = properties; |
| 123 | + } |
| 124 | + |
| 125 | + private void init () { |
| 126 | + //this.properties = new HashMap<String, Object>(); |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public long getDocumentRevision() { |
| 131 | + return this.documentRevision; |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public String getDocumentHandle() { |
| 136 | + return this.documentHandle; |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public String getDocumentKey() { |
| 141 | + return this.documentKey; |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public void setDocumentRevision(long documentRevision) { |
| 146 | + this.documentRevision = documentRevision; |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public void setDocumentHandle(String documentHandle) { |
| 151 | + this.documentHandle = documentHandle; |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public void setDocumentKey(String documentKey) { |
| 156 | + this.documentKey = documentKey; |
| 157 | + } |
| 158 | + |
| 159 | + public Map<String, Object> getProperties() { |
| 160 | + return properties; |
| 161 | + } |
| 162 | + |
| 163 | + public void setProperties(Map<String, Object> properties) { |
| 164 | + this.properties = properties; |
| 165 | + } |
| 166 | + |
| 167 | + |
| 168 | + /** |
| 169 | + * add an attribute to the document. If the key already exists, the value of the attribute will be replaced, |
| 170 | + * |
| 171 | + * @param key the key of the attribute |
| 172 | + * @param value the value of the attribute |
| 173 | + */ |
| 174 | + public void addAttribute(String key, Object value) { |
| 175 | + this.properties.put(key, value); |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * update the value of the attribute with the given key |
| 180 | + * |
| 181 | + * @param key the key of the attribute |
| 182 | + * @param value the value of the attribute ti replace the old value |
| 183 | + */ |
| 184 | + public void updateAttribute (String key, Object value) { |
| 185 | + this.properties.replace(key, value); |
| 186 | + } |
| 187 | + |
| 188 | +// /** |
| 189 | +// * check the list if it is suitable |
| 190 | +// * |
| 191 | +// * @param keyValues |
| 192 | +// * @return true, if the list has an even number and is an alternating sequence of instances of String and Object. |
| 193 | +// */ |
| 194 | +// private boolean checkKeyValues(Object... keyValues) { |
| 195 | +// if (keyValues.length %2 != 0) { |
| 196 | +// return false; |
| 197 | +// } |
| 198 | +// for (int i = 0; i < keyValues.length; i = i+2) { |
| 199 | +// if (! (keyValues[i] instanceof String)) { |
| 200 | +// return false; |
| 201 | +// } |
| 202 | +// } |
| 203 | +// return true; |
| 204 | +// } |
| 205 | +} |
0 commit comments