|
| 1 | +package com.dotcms.rest.api.v1.content; |
| 2 | + |
| 3 | +import com.dotcms.variant.VariantAPI; |
| 4 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 5 | +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; |
| 6 | + |
| 7 | +import java.io.Serializable; |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.HashMap; |
| 10 | +import java.util.List; |
| 11 | +import java.util.Map; |
| 12 | +import java.util.Optional; |
| 13 | + |
| 14 | +import static com.liferay.util.StringPool.BLANK; |
| 15 | + |
| 16 | +/** |
| 17 | + * This class contains all the information needed to search for content in the dotCMS repository. |
| 18 | + * All data is sent via JSON using the following format: |
| 19 | + * <pre> |
| 20 | + * <code> |
| 21 | + * { |
| 22 | + * "globalSearch": "", |
| 23 | + * "searchableFieldsByContentType": { |
| 24 | + * "{{CONTENT_TYPE_ID_OR_VAR_NAME}}": { |
| 25 | + * "binary": "{{STRING}}", |
| 26 | + * "blockEditor": "{{STRING}}", |
| 27 | + * "category": "{{STRING: Comma-separated list of Category IDs}}", |
| 28 | + * "checkbox": "{{STRING: Comma-separated list of values}}", |
| 29 | + * "custom": "{{STRING}}", |
| 30 | + * "date": "{{STRING}}: Can use ranges by including the [ and ] characters", |
| 31 | + * "dateAndTime": "{{STRING}}: Date or date and time. Can use ranges by including |
| 32 | + * the [ and ] characters", |
| 33 | + * "json": "{{STRING}: Matches any String in the JSON object}", |
| 34 | + * "keyValue": "{{STRING}: Matches keys and values in |
| 35 | + * "multiSelect": "{{STRING: Comma-separated list of values, not labels}}", |
| 36 | + * "radio": "{{STRING}: Matches values, not labels}", |
| 37 | + * "relationships": "{{STRING:ID of the contentlet that must be referenced by the |
| 38 | + * content(s) you want to query}}", |
| 39 | + * "select": "{{STRING}: Matches values, not labels}", |
| 40 | + * "tag": "{{STRING:comma-separated list of Tag names}}", |
| 41 | + * "title": "{{STRING}}", |
| 42 | + * "textArea": "{{STRING}}", |
| 43 | + * "time": "{{STRING}: Can use ranges by including the [ and ] characters}", |
| 44 | + * "wysiwyg": "{{STRING}}" |
| 45 | + * } |
| 46 | + * }, |
| 47 | + * "systemSearchableFields": { |
| 48 | + * "siteId": "{{STRING}}", |
| 49 | + * "languageId": {{INTEGER}}, |
| 50 | + * "workflowSchemeId": "{{STRING}}", |
| 51 | + * "workflowStepId": "{{STRING}}", |
| 52 | + * "variantName": "{{STRING}}", |
| 53 | + * "systemHostContent": {{BOOLEAN}} |
| 54 | + * }, |
| 55 | + * "archivedContent": {{BOOLEAN}}, |
| 56 | + * "unpublishedContent": {{BOOLEAN}}, |
| 57 | + * "lockedContent": {{BOOLEAN}}, |
| 58 | + * "orderBy": "{{STRING}}", |
| 59 | + * "page": {{INTEGER}}, |
| 60 | + * "perPage": {{INTEGER}} |
| 61 | + * } |
| 62 | + * </code> |
| 63 | + * </pre> |
| 64 | + * |
| 65 | + * @author Jose Castro |
| 66 | + * @since Jan 29th, 2025 |
| 67 | + */ |
| 68 | +@JsonDeserialize(builder = ContentSearchForm.Builder.class) |
| 69 | +public class ContentSearchForm implements Serializable { |
| 70 | + |
| 71 | + private final String globalSearch; |
| 72 | + private final Map<String, Map<String, Object>> searchableFieldsByContentType; |
| 73 | + private final Map<String, Object> systemSearchableFields; |
| 74 | + |
| 75 | + private final String archivedContent; |
| 76 | + private final String unpublishedContent; |
| 77 | + private final String lockedContent; |
| 78 | + |
| 79 | + private final String orderBy; |
| 80 | + private final int page; |
| 81 | + private final int perPage; |
| 82 | + |
| 83 | + /** |
| 84 | + * Creates an instance of this class using the provided Builder. |
| 85 | + * |
| 86 | + * @param builder The {@link Builder} instance to use. |
| 87 | + */ |
| 88 | + private ContentSearchForm(final Builder builder) { |
| 89 | + this.globalSearch = builder.globalSearch; |
| 90 | + this.searchableFieldsByContentType = builder.searchableFieldsByContentType; |
| 91 | + this.systemSearchableFields = builder.systemSearchableFields; |
| 92 | + |
| 93 | + this.lockedContent = builder.lockedContent; |
| 94 | + this.unpublishedContent = builder.unpublishedContent; |
| 95 | + this.archivedContent = builder.archivedContent; |
| 96 | + |
| 97 | + this.orderBy = builder.orderBy; |
| 98 | + this.page = builder.page; |
| 99 | + this.perPage = builder.perPage; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Returns the global search term. This attribute represents, for instance, the global search |
| 104 | + * box that you can see in the {@code Search} portlet, and in the dynamic search dialog in the |
| 105 | + * {@code Relationships} field. |
| 106 | + * |
| 107 | + * @return The global search term. |
| 108 | + */ |
| 109 | + public String globalSearch() { |
| 110 | + return this.globalSearch; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Returns a map containing all the searchable fields for each content type. The key of the map |
| 115 | + * is the content type ID or variable name, and the value is another map containing the fields |
| 116 | + * and their values. |
| 117 | + * |
| 118 | + * @return A map containing all the searchable fields for each content type. |
| 119 | + */ |
| 120 | + public Map<String, Map<String, Object>> searchableFields() { |
| 121 | + return this.searchableFieldsByContentType; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Returns a list of searchable fields for a specific content type. |
| 126 | + * |
| 127 | + * @param contentTypeId The ID or variable name of the content type. |
| 128 | + * |
| 129 | + * @return A list of searchable fields for the specified content type. |
| 130 | + */ |
| 131 | + public List<String> searchableFields(final String contentTypeId) { |
| 132 | + return null != this.searchableFieldsByContentType |
| 133 | + ? new ArrayList<>(this.searchableFieldsByContentType.getOrDefault(contentTypeId, new HashMap<>()).keySet()) |
| 134 | + : List.of(); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Returns an Optional with the value of a specific field for a specific content type. |
| 139 | + * |
| 140 | + * @param contentTypeIdOrVar The ID or variable name of the content type. |
| 141 | + * @param fieldVarName The variable name of the field. |
| 142 | + * |
| 143 | + * @return An {@link Optional} with the value of the field, or an empty Optional if the field is |
| 144 | + * not found. |
| 145 | + */ |
| 146 | + public Optional<Object> searchableFieldsByContentTypeAndField(final String contentTypeIdOrVar, |
| 147 | + final String fieldVarName) { |
| 148 | + return null != this.searchableFieldsByContentType && null != this.searchableFieldsByContentType.get(contentTypeIdOrVar) |
| 149 | + ? Optional.of(this.searchableFieldsByContentType.get(contentTypeIdOrVar).get(fieldVarName)) |
| 150 | + : Optional.empty(); |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Returns a map containing all the system searchable fields. These fields are used to filter |
| 155 | + * content based on system properties like the site ID, language ID, workflow scheme ID, etc. |
| 156 | + * and not actual fields in a Content Type. |
| 157 | + * |
| 158 | + * @return A map containing all the system searchable fields. |
| 159 | + */ |
| 160 | + public Map<String, Object> systemSearchableFields() { |
| 161 | + return null != this.systemSearchableFields |
| 162 | + ? this.systemSearchableFields |
| 163 | + : Map.of(); |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Returns the site ID to filter the content by. |
| 168 | + * |
| 169 | + * @return The site ID to filter the content by. |
| 170 | + */ |
| 171 | + public String siteId() { |
| 172 | + return (String) this.systemSearchableFields().getOrDefault("siteId", BLANK); |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Returns the language ID to filter the content by. |
| 177 | + * |
| 178 | + * @return The language ID to filter the content by. |
| 179 | + */ |
| 180 | + public int languageId() { |
| 181 | + return (int) this.systemSearchableFields().getOrDefault("languageId", -1); |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * Returns the workflow scheme ID to filter the content by. |
| 186 | + * |
| 187 | + * @return The workflow scheme ID to filter the content by. |
| 188 | + */ |
| 189 | + public String workflowSchemeId() { |
| 190 | + return (String) this.systemSearchableFields().getOrDefault("workflowSchemeId", BLANK); |
| 191 | + } |
| 192 | + |
| 193 | + /** |
| 194 | + * Returns the workflow step ID to filter the content by. |
| 195 | + * |
| 196 | + * @return The workflow step ID to filter the content by. |
| 197 | + */ |
| 198 | + public String workflowStepId() { |
| 199 | + return (String) this.systemSearchableFields().getOrDefault("workflowStepId", BLANK); |
| 200 | + } |
| 201 | + |
| 202 | + /** |
| 203 | + * Returns the variant name to filter the content by. |
| 204 | + * |
| 205 | + * @return The variant name to filter the content by. |
| 206 | + */ |
| 207 | + public String variantName() { |
| 208 | + return (String) this.systemSearchableFields().getOrDefault("variantName", VariantAPI.DEFAULT_VARIANT.name()); |
| 209 | + } |
| 210 | + |
| 211 | + /** |
| 212 | + * Returns a boolean indicating whether the generated Lucene query must look for content living |
| 213 | + * under System host or not. |
| 214 | + * |
| 215 | + * @return If the generated Lucene query must look for content living under System host, returns |
| 216 | + * {@code true}. |
| 217 | + */ |
| 218 | + public boolean systemHostContent() { |
| 219 | + return (boolean) this.systemSearchableFields().getOrDefault("systemHostContent", true); |
| 220 | + } |
| 221 | + |
| 222 | + /** |
| 223 | + * Returns the criterion being used to filter results by. |
| 224 | + * |
| 225 | + * @return The criterion being used to filter results by. |
| 226 | + */ |
| 227 | + public String orderBy() { |
| 228 | + return this.orderBy; |
| 229 | + } |
| 230 | + |
| 231 | + /** |
| 232 | + * Returns a list of content type IDs that can be used to filter the search results. |
| 233 | + * |
| 234 | + * @return A list of content type IDs that can be used to filter the search results. |
| 235 | + */ |
| 236 | + public List<String> contentTypeIds() { |
| 237 | + if (null == this.searchableFieldsByContentType) { |
| 238 | + return List.of(); |
| 239 | + } |
| 240 | + return new ArrayList<>(this.searchableFieldsByContentType.keySet()); |
| 241 | + } |
| 242 | + |
| 243 | + /** |
| 244 | + * Returns a boolean indicating whether the search results must include archived content or not. |
| 245 | + * |
| 246 | + * @return If the search results must include archived content, returns {@code true}. |
| 247 | + */ |
| 248 | + public String archivedContent() { |
| 249 | + return this.archivedContent; |
| 250 | + } |
| 251 | + |
| 252 | + /** |
| 253 | + * Returns a boolean indicating whether the search results must include unpublished content or |
| 254 | + * not. |
| 255 | + * |
| 256 | + * @return If the search results must include unpublished content, returns {@code true}. |
| 257 | + */ |
| 258 | + public String unpublishedContent() { |
| 259 | + return this.unpublishedContent; |
| 260 | + } |
| 261 | + |
| 262 | + /** |
| 263 | + * Returns a boolean indicating whether the search results must include locked content or not. |
| 264 | + * |
| 265 | + * @return If the search results must include locked content, returns {@code true}. |
| 266 | + */ |
| 267 | + public String lockedContent() { |
| 268 | + return this.lockedContent; |
| 269 | + } |
| 270 | + |
| 271 | + /** |
| 272 | + * Returns the page number to be used to paginate the search results. |
| 273 | + * |
| 274 | + * @return The page number to be used to paginate the search results. |
| 275 | + */ |
| 276 | + public int page() { |
| 277 | + return this.page; |
| 278 | + } |
| 279 | + |
| 280 | + /** |
| 281 | + * Returns the number of results to be shown per page. |
| 282 | + * |
| 283 | + * @return The number of results to be shown per page. |
| 284 | + */ |
| 285 | + public int perPage() { |
| 286 | + return this.perPage; |
| 287 | + } |
| 288 | + |
| 289 | + /** |
| 290 | + * Returns the offset to be used to paginate the search results. |
| 291 | + * |
| 292 | + * @return The offset to be used to paginate the search results. |
| 293 | + */ |
| 294 | + public int offset() { |
| 295 | + if (this.page != 0) { |
| 296 | + return this.perPage * (this.page - 1); |
| 297 | + } |
| 298 | + return 0; |
| 299 | + } |
| 300 | + |
| 301 | + @Override |
| 302 | + public String toString() { |
| 303 | + return "ContentSearchForm{" + |
| 304 | + "globalSearch='" + globalSearch + '\'' + |
| 305 | + ", searchableFieldsByContentType=" + searchableFieldsByContentType + |
| 306 | + ", systemSearchableFields=" + systemSearchableFields + |
| 307 | + ", archivedContent='" + archivedContent + '\'' + |
| 308 | + ", unpublishedContent='" + unpublishedContent + '\'' + |
| 309 | + ", lockedContent='" + lockedContent + '\'' + |
| 310 | + ", orderBy='" + orderBy + '\'' + |
| 311 | + ", page=" + page + |
| 312 | + ", perPage=" + perPage + |
| 313 | + '}'; |
| 314 | + } |
| 315 | + |
| 316 | + /** |
| 317 | + * Allows you to create an instance of the {@link ContentSearchForm} class using a Builder. |
| 318 | + */ |
| 319 | + public static final class Builder { |
| 320 | + |
| 321 | + @JsonProperty |
| 322 | + private String globalSearch = BLANK; |
| 323 | + @JsonProperty |
| 324 | + private Map<String, Map<String, Object>> searchableFieldsByContentType = new HashMap<>(); |
| 325 | + @JsonProperty |
| 326 | + private Map<String, Object> systemSearchableFields; |
| 327 | + |
| 328 | + @JsonProperty |
| 329 | + private String archivedContent = BLANK; |
| 330 | + @JsonProperty |
| 331 | + private String unpublishedContent = BLANK; |
| 332 | + @JsonProperty |
| 333 | + private String lockedContent = BLANK; |
| 334 | + |
| 335 | + @JsonProperty |
| 336 | + private String orderBy = BLANK; |
| 337 | + @JsonProperty |
| 338 | + private int page = 0; |
| 339 | + @JsonProperty |
| 340 | + private int perPage = 0; |
| 341 | + |
| 342 | + public Builder globalSearch(final String globalSearch) { |
| 343 | + this.globalSearch = globalSearch; |
| 344 | + return this; |
| 345 | + } |
| 346 | + |
| 347 | + public Builder searchableFieldsByContentType(final Map<String, Map<String, Object>> searchableFields) { |
| 348 | + this.searchableFieldsByContentType = searchableFields; |
| 349 | + return this; |
| 350 | + } |
| 351 | + |
| 352 | + public Builder systemSearchableFields(final Map<String, Object> systemSearchableFields) { |
| 353 | + this.systemSearchableFields = systemSearchableFields; |
| 354 | + return this; |
| 355 | + } |
| 356 | + |
| 357 | + public Builder archivedContent(final String archivedContent) { |
| 358 | + this.archivedContent = archivedContent; |
| 359 | + return this; |
| 360 | + } |
| 361 | + |
| 362 | + public Builder unpublishedContent(final String unpublishedContent) { |
| 363 | + this.unpublishedContent = unpublishedContent; |
| 364 | + return this; |
| 365 | + } |
| 366 | + |
| 367 | + public Builder lockedContent(final String lockedContent) { |
| 368 | + this.lockedContent = lockedContent; |
| 369 | + return this; |
| 370 | + } |
| 371 | + |
| 372 | + public Builder orderBy(final String orderBy) { |
| 373 | + this.orderBy = orderBy; |
| 374 | + return this; |
| 375 | + } |
| 376 | + |
| 377 | + public Builder page(final int page) { |
| 378 | + this.page = page; |
| 379 | + return this; |
| 380 | + } |
| 381 | + |
| 382 | + public Builder perPage(final int perPage) { |
| 383 | + this.perPage = perPage; |
| 384 | + return this; |
| 385 | + } |
| 386 | + |
| 387 | + public ContentSearchForm build() { |
| 388 | + return new ContentSearchForm(this); |
| 389 | + } |
| 390 | + |
| 391 | + } |
| 392 | + |
| 393 | +} |
0 commit comments