|
7 | 7 | import retrofit2.Response; |
8 | 8 |
|
9 | 9 | import java.io.IOException; |
10 | | -import java.util.HashMap; |
11 | | -import java.util.LinkedHashMap; |
12 | | -import java.util.List; |
13 | | -import java.util.Map; |
| 10 | +import java.util.*; |
| 11 | +import java.util.stream.Collectors; |
14 | 12 |
|
15 | 13 | /** |
16 | 14 | * The type Taxonomy. |
@@ -38,134 +36,265 @@ public Taxonomy(APIService service, Config config, LinkedHashMap<String, Object> |
38 | 36 | this.config = config; |
39 | 37 | } |
40 | 38 |
|
| 39 | + |
41 | 40 | /** |
42 | | - * You can retrieve filtered entries using taxonomy through two different endpoints: |
43 | | - * |
44 | | - * @param queryParams the query parameters should be like below |
45 | | - * <ul> |
46 | | - * <li> <code> <b>IN Operator</b> : Get all entries for a specific taxonomy that satisfy the given conditions provided in the '$in' query.<br> Your query should be as follows:<br> <code> {"taxonomies.taxonomy_uid" : { "$in" : ["term_uid1" , "term_uid2" ] }} </code> </li> <br> <li> <code> <b>OR Operator</b> : Get all entries for a specific taxonomy that satisfy at least one of the given conditions provided in the “$or” query. <br> Your query should be as follows: <br> <code> query={ $or: [ { "taxonomies.taxonomy_uid_1" : "term_uid1" }, { "taxonomies.taxonomy_uid_2" : "term_uid2" } ] }</code> |
47 | | - * </li> |
48 | | - * <br> |
49 | | - * <li> |
50 | | - * <b>AND Operator :</b><br> |
51 | | - * Get all entries for a specific taxonomy that satisfy all the conditions provided in the “$and” query.<br> |
52 | | - * <p> |
53 | | - * Your query should be as follows: <br> |
54 | | - * <code> |
55 | | - * { |
56 | | - * $and: [ |
57 | | - * { "taxonomies.taxonomy_uid_1" : "term_uid1" }, |
58 | | - * { "taxonomies.taxonomy_uid_2" : "term_uid2" } |
59 | | - * ] |
60 | | - * } |
61 | | - * |
62 | | - * </code> |
63 | | - * </li> |
64 | | - * <br> |
65 | | - * |
66 | | - * <li> |
67 | | - * <b>Exists Operator :</b> <br> |
68 | | - * Get all entries for a specific taxonomy that if the value of the field, mentioned in the condition, exists. <br> |
69 | | - * <p> |
70 | | - * Your query should be as follows: |
71 | | - * <code> |
72 | | - * <code>{"taxonomies.taxonomy_uid" : { "$exists": true }} |
73 | | - * </code> |
74 | | - * </li><br> |
75 | | - * <li> |
76 | | - * <b>Equal and Below Operator :</b> <br> |
77 | | - * Get all entries for a specific taxonomy that match a specific term and all its descendant terms, requiring only the target term and a specified level. <br> |
78 | | - * <p> |
79 | | - * Your query should be as follows: |
80 | | - * <code> |
81 | | - * <code>{ |
82 | | - * "taxonomies.taxonomy_uid" : { "$eq_below": "term_uid", "level" : 2}} |
83 | | - * </code> |
84 | | - * </li> |
85 | | - * |
86 | | - * |
87 | | - * </ul> |
88 | | - * @return instance of {@link Taxonomy} |
| 41 | + * Get all entries for a specific taxonomy that satisfy the given conditions provided in the '$in' query. |
| 42 | + * Your query should be as follows: |
| 43 | + * <p> |
| 44 | + * <pre> |
| 45 | + * {"taxonomies.taxonomy_uid" : { "$in" : ["term_uid1" , "term_uid2" ] }} |
| 46 | + * </pre> |
| 47 | + * <p> |
| 48 | + * <b>Example:</b> If you want to retrieve entries with the color taxonomy applied and linked to the term red and/or yellow. |
| 49 | + * <p> |
| 50 | + * <pre> |
| 51 | + * {"taxonomies.color" : { "$in" : ["red" , "yellow" ] }} |
| 52 | + * </pre> |
| 53 | + * |
| 54 | + * @param taxonomy the key of the taxonomy to query |
| 55 | + * @param listOfItems the list of taxonomy fields |
| 56 | + * @return an instance of the Taxonomy with the specified conditions added to the query |
89 | 57 | */ |
90 | | - public Taxonomy query(Map<String, Object> queryParams) { |
91 | | - this.query.putAll(queryParams); |
| 58 | + public Taxonomy in(String taxonomy, String[] listOfItems) { |
| 59 | + String formattedValues = Arrays.stream(listOfItems).map(value -> "\"" + value.trim() + "\"").collect(Collectors.joining(" , ")); |
| 60 | + |
| 61 | + String stringify = "{ \"$in\" : [" + formattedValues + "] }}"; |
| 62 | + this.query.put(taxonomy, stringify); |
92 | 63 | return this; |
93 | 64 | } |
94 | 65 |
|
95 | 66 |
|
96 | 67 | /** |
97 | | - * Get all entries for a specific taxonomy that satisfy the given conditions provided in the query. |
98 | | - * |
99 | | - * @param key the key of the taxonomy to query |
100 | | - * @param listOfItems the list of taxonomy fields |
101 | | - * Example: If you want to retrieve entries with the color taxonomy applied and linked to the term red and/or yellow. |
102 | | - * <code> |
103 | | - * String key = "taxonomies.taxonomy_uid"; |
104 | | - * String[] listOfItem = {"term_uid1", "term_uid2"}; |
105 | | - * taxonomy.in(key, listOfItem); |
106 | | - * taxonomy.query(query).find(new TaxonomyCallback() { |
107 | | - * @Override public void onFailure(Request request, ResponseBody errorMessage) { |
108 | | - * System.out.println("Failing API call : " + errorMessage.toString()); |
| 68 | + * <b>OR Operator :</b> |
109 | 69 | * <p> |
110 | | - * } |
111 | | - * @Override public void onResponse(ResponseBody response) { |
112 | | - * System.out.println("Response : " + response.toString()); |
| 70 | + * Get all entries for a specific taxonomy that satisfy at least one of the given conditions provided in the “$or” query. |
113 | 71 | * <p> |
114 | | - * } |
115 | | - * }); |
116 | | - * </code> |
| 72 | + * Your query should be as follows: |
| 73 | + * <p> |
| 74 | + * <pre> |
| 75 | + * |
| 76 | + * { $or: [ |
| 77 | + * { "taxonomies.taxonomy_uid_1" : "term_uid1" }, |
| 78 | + * { "taxonomies.taxonomy_uid_2" : "term_uid2" } |
| 79 | + * ]} |
| 80 | + * |
| 81 | + * </pre> |
| 82 | + * Example: If you want to retrieve entries with either the color or size taxonomy applied and linked to the terms yellow and small, respectively. |
| 83 | + * <br> |
| 84 | + * <pre> |
| 85 | + * |
| 86 | + * {$or: [ |
| 87 | + * { "taxonomies.color" : "yellow" }, |
| 88 | + * { "taxonomies.size" : "small" } |
| 89 | + * ]} |
| 90 | + * |
| 91 | + * |
| 92 | + * </pre> |
| 93 | + * |
| 94 | + * @param listOfItems |
| 95 | + * @return |
117 | 96 | */ |
118 | | - public Taxonomy in(@NotNull String key, @NotNull String[] listOfItems) { |
119 | | - HashMap<String, Object> param = new HashMap<>(); |
120 | | - param.put("$in", listOfItems); |
121 | | - this.query.put(key, param); |
122 | | - return this; |
123 | | - } |
124 | | - |
125 | | - |
126 | 97 | public Taxonomy or(@NotNull List<HashMap<String, String>> listOfItems) { |
127 | 98 | for (int i = 0; i < listOfItems.size(); i++) { |
128 | 99 | HashMap<String, String> param = listOfItems.get(i); |
129 | | - |
130 | 100 | if (i > 0) { |
131 | 101 | this.query.put("$or", listOfItems.toArray()); |
132 | 102 | } |
133 | | - |
134 | 103 | this.query.put("$or", param); |
135 | 104 | } |
136 | | - |
137 | 105 | return this; |
138 | 106 | } |
139 | 107 |
|
140 | 108 |
|
| 109 | + /** |
| 110 | + * <b>AND Operator :</b> |
| 111 | + * <p> |
| 112 | + * Get all entries for a specific taxonomy that satisfy all the conditions provided in the “$and” query. |
| 113 | + * <p> |
| 114 | + * Your query should be as follows: |
| 115 | + * |
| 116 | + * <pre> |
| 117 | + * { |
| 118 | + * $and: [ |
| 119 | + * { "taxonomies.taxonomy_uid_1" : "term_uid1" }, |
| 120 | + * { "taxonomies.taxonomy_uid_2" : "term_uid2" } |
| 121 | + * ] |
| 122 | + * } |
| 123 | + * </pre> |
| 124 | + * <b>Example:</b> If you want to retrieve entries with the color and computers taxonomies applied and linked to the terms red and laptop, respectively. |
| 125 | + * |
| 126 | + * <pre> |
| 127 | + * { |
| 128 | + * $and: [ |
| 129 | + * { "taxonomies.color" : "red" }, |
| 130 | + * { "taxonomies.computers" : "laptop" } |
| 131 | + * ] |
| 132 | + * } |
| 133 | + * </pre> |
| 134 | + * |
| 135 | + * @param listOfItems the list of items to that you want to include in the query string |
| 136 | + * @return instance of the Taxonomy |
| 137 | + */ |
141 | 138 | public Taxonomy and(@NotNull List<HashMap<String, String>> listOfItems) { |
142 | 139 | for (int i = 0; i < listOfItems.size(); i++) { |
143 | 140 | HashMap<String, String> param = listOfItems.get(i); |
144 | | - |
145 | 141 | if (i > 0) { |
146 | 142 | this.query.put("$and", listOfItems.toArray()); |
147 | 143 | } |
148 | | - |
149 | 144 | this.query.put("$and", param); |
150 | 145 | } |
151 | | - |
152 | 146 | return this; |
153 | 147 | } |
154 | 148 |
|
155 | 149 |
|
156 | | - public Taxonomy exists(@NotNull String name, @NotNull Boolean value) { |
| 150 | + /** |
| 151 | + * <b>Exists Operator :</b> |
| 152 | + * <p> |
| 153 | + * Get all entries for a specific taxonomy that if the value of the field, mentioned in the condition, exists. |
| 154 | + * <p> |
| 155 | + * Your query should be as follows: |
| 156 | + * <pre> |
| 157 | + * {"taxonomies.taxonomy_uid" : { "$exists": true }} |
| 158 | + * </pre> |
| 159 | + * Example: If you want to retrieve entries with the color taxonomy applied. |
| 160 | + * <pre> |
| 161 | + * {"taxonomies.color" : { "$exists": true }} |
| 162 | + * </pre> |
| 163 | + * |
| 164 | + * @param taxonomy the taxonomy |
| 165 | + * @param value the value of the field |
| 166 | + * @return instance of Taxonomy |
| 167 | + */ |
| 168 | + public Taxonomy exists(@NotNull String taxonomy, @NotNull Boolean value) { |
157 | 169 | HashMap<String, Boolean> param = new HashMap<>(); |
158 | 170 | param.put("$exists", value); |
159 | | - this.query.put(name, param); |
| 171 | + this.query.put(taxonomy, param); |
160 | 172 | return this; |
161 | 173 | } |
162 | 174 |
|
| 175 | + |
| 176 | + /** |
| 177 | + * <b>Equal and Below Operator :</b> |
| 178 | + * <p> |
| 179 | + * Get all entries for a specific taxonomy that match a specific term and all its descendant terms, requiring only the target term and a specified level. |
| 180 | + * <p> |
| 181 | + * Note: If you don't specify the level, the default behavior is to retrieve terms up to level 10. |
| 182 | + * |
| 183 | + * <pre>{"taxonomies.taxonomy_uid" : { "$eq_below": "term_uid", "level" : 2}}</pre> |
| 184 | + * |
| 185 | + * <b>Example:</b> If you want to retrieve all entries with terms nested under blue, such as navy blue and sky blue, while also matching entries with the target term blue. |
| 186 | + * |
| 187 | + * <pre>{"taxonomies.color" : { "$eq_below": "blue" }}</pre> |
| 188 | + * |
| 189 | + * @param taxonomy the taxonomy |
| 190 | + * @param termsUid the term uid |
| 191 | + * @return instance of Taxonomy |
| 192 | + */ |
| 193 | + public Taxonomy equalAndBelow(@NotNull String taxonomy, @NotNull String termsUid) { |
| 194 | + HashMap<String, String> param = new HashMap<>(); |
| 195 | + param.put("$eq_below", termsUid); |
| 196 | + this.query.put(taxonomy, param); |
| 197 | + return this; |
| 198 | + } |
| 199 | + |
| 200 | + /** |
| 201 | + * Note: If you don't specify the level, the default behavior is to retrieve terms up to level 10. |
| 202 | + * |
| 203 | + * @param taxonomy the taxonomy |
| 204 | + * @param termsUid the terms |
| 205 | + * @param level the level to retrieve terms up to mentioned level |
| 206 | + * @return instance of Taxonomy |
| 207 | + */ |
| 208 | + public Taxonomy equalAndBelowWithLevel(@NotNull String taxonomy, @NotNull String termsUid, @NotNull int level) { |
| 209 | + Map<String, Object> innerMap = new HashMap<>(); |
| 210 | + innerMap.put("$eq_below", termsUid + ", level: " + level); |
| 211 | + this.query.put(taxonomy, innerMap); |
| 212 | + return this; |
| 213 | + } |
| 214 | + |
| 215 | + |
| 216 | + /** |
| 217 | + * <b>Below Operator</b> |
| 218 | + * <br> |
| 219 | + * <p> |
| 220 | + * Get all entries for a specific taxonomy that match all of their descendant terms by specifying only the target term and a specific level. |
| 221 | + * <br> |
| 222 | + * <b>Note:</b> If you don't specify the level, the default behavior is to retrieve terms up to level 10. |
| 223 | + * <br> |
| 224 | + * <pre>{"taxonomies.taxonomy_uid" : { "$below": "term_uid", "level" : 2}}</pre> |
| 225 | + * |
| 226 | + * <b>Example:</b> If you want to retrieve all entries containing terms nested under blue, such as navy blue and sky blue, but exclude entries that solely have the target term blue. |
| 227 | + * |
| 228 | + * <pre>{"taxonomies.color" : { "$below": "blue" }}</pre> |
| 229 | + * |
| 230 | + * @param taxonomy the taxonomy |
| 231 | + * @param termsUid the terms uid |
| 232 | + * @return instance of Taxonomy |
| 233 | + */ |
| 234 | + public Taxonomy below(@NotNull String taxonomy, @NotNull String termsUid) { |
| 235 | + HashMap<String, String> param = new HashMap<>(); |
| 236 | + param.put("$below", termsUid); |
| 237 | + this.query.put(taxonomy, param); |
| 238 | + return this; |
| 239 | + } |
| 240 | + |
| 241 | + |
| 242 | + /** |
| 243 | + * <b>Equal and Above Operator :</b> |
| 244 | + * <p> |
| 245 | + * Get all entries for a specific taxonomy that match a specific term and all its ancestor terms, requiring only the target term and a specified level. |
| 246 | + * |
| 247 | + * <b>Note:</b> If you don't specify the level, the default behavior is to retrieve terms up to level 10. |
| 248 | + * <p> |
| 249 | + * <pre>{"taxonomies.taxonomy_uid": { "$eq_above": "term_uid", "level": 2 }}</pre> |
| 250 | + * <p> |
| 251 | + * Example: If you want to obtain all entries that include the term led and its parent term tv. |
| 252 | + * <p> |
| 253 | + * <pre>{"taxonomies.appliances": { "$eq_above": "led"}}</pre> |
| 254 | + * |
| 255 | + * @param taxonomy the taxonomy |
| 256 | + * @param termUid the term uid |
| 257 | + * @return instance of Taxonomy |
| 258 | + */ |
| 259 | + public Taxonomy equalAbove(@NotNull String taxonomy, @NotNull String termUid) { |
| 260 | + Map<String, Object> innerMap = new HashMap<>(); |
| 261 | + innerMap.put("$eq_above", termUid); |
| 262 | + this.query.put(taxonomy, innerMap); |
| 263 | + return this; |
| 264 | + } |
| 265 | + |
| 266 | + |
| 267 | + /** |
| 268 | + * <b>Above Operator :</b> |
| 269 | + * <p> |
| 270 | + * Get all entries for a specific taxonomy that match only the parent term(s) of a specified target term, excluding the target term itself. You can also specify a specific level. |
| 271 | + * <p> |
| 272 | + * Note: If you don't specify the level, the default behavior is to retrieve terms up to level 10. |
| 273 | + * |
| 274 | + * <pre>{ "taxonomies.taxonomy_uid": { "$above": "term_uid", "level": 2 }}</pre> |
| 275 | + * <p> |
| 276 | + * Example: If you wish to match entries with the term tv but exclude the target term led. |
| 277 | + * |
| 278 | + * <pre>{"taxonomies.appliances": { "$above": "led" }}</pre> |
| 279 | + * |
| 280 | + * @param taxonomy the taxonomy |
| 281 | + * @param termUid the term uid |
| 282 | + * @return instance of {@link Taxonomy} |
| 283 | + */ |
| 284 | + public Taxonomy above(@NotNull String taxonomy, @NotNull String termUid) { |
| 285 | + Map<String, Object> innerMap = new HashMap<>(); |
| 286 | + innerMap.put("$above", termUid); |
| 287 | + this.query.put(taxonomy, innerMap); |
| 288 | + return this; |
| 289 | + } |
| 290 | + |
| 291 | + |
163 | 292 | /** |
164 | | - * Make request call. |
| 293 | + * To verify the payload |
165 | 294 | * |
166 | | - * @return the call |
| 295 | + * @return instance of Call<ResponseBody> |
167 | 296 | */ |
168 | | - Call<ResponseBody> makeRequest() { |
| 297 | + protected Call<ResponseBody> makeRequest() { |
169 | 298 | HashMap<String, Object> map = new HashMap<>(); |
170 | 299 | map.put("query", query); |
171 | 300 | return this.service.getTaxonomy(this.headers, map); |
|
0 commit comments