001package com.box.sdk; 002 003import com.eclipsesource.json.JsonArray; 004import com.eclipsesource.json.JsonObject; 005import java.util.List; 006 007/** 008 * Used to Setup Box Search Parameters 009 * 010 * <p>Advanced Search support here allows a number of parameters be specified to take full advantage of 011 * box search capabilities. Query parameter is required in all cases except when Metadata templates 012 * searching is being used.</p> 013 */ 014public class BoxSearchParameters { 015 private String query; 016 private List<String> fields; 017 private String scope; 018 private List<String> fileExtensions; 019 private DateRange createdRange; 020 private DateRange updatedRange; 021 private SizeRange sizeRange; 022 private List<String> ownerUserIds; 023 private List<String> ancestorFolderIds; 024 private List<String> contentTypes; 025 private String type; 026 private String trashContent; 027 private BoxMetadataFilter metadataFilter; 028 private String sort; 029 private String direction; 030 private Boolean includeRecentSharedLinks; 031 032 /** 033 * Creates a Box Search Parameters Objects without query set, specific for Metadata Only Searches. 034 */ 035 public BoxSearchParameters() { 036 } 037 038 /** 039 * Creates a Box Search Parameters Objects with a query initiated. 040 * 041 * @param query parameter. 042 */ 043 public BoxSearchParameters(String query) { 044 this.query = query; 045 } 046 047 /** 048 * Clears the Parameters before performing a new search. 049 * 050 * @return this.true; 051 */ 052 public boolean clearParameters() { 053 this.query = null; 054 this.fields = null; 055 this.scope = null; 056 this.fileExtensions = null; 057 this.createdRange = null; 058 this.updatedRange = null; 059 this.sizeRange = null; 060 this.ownerUserIds = null; 061 this.ancestorFolderIds = null; 062 this.contentTypes = null; 063 this.type = null; 064 this.trashContent = null; 065 this.metadataFilter = null; 066 this.sort = null; 067 this.direction = null; 068 return true; 069 } 070 071 /** 072 * Get existing query String that is being used. 073 * 074 * @return this.query string. 075 */ 076 public String getQuery() { 077 return this.query; 078 } 079 080 /** 081 * Set query string for that will be used to search. 082 * 083 * @param query is a String value. 084 */ 085 public void setQuery(String query) { 086 this.query = query; 087 } 088 089 /** 090 * Get the existing fields that are used for the search criteria. 091 * 092 * @return this.List of fields. 093 */ 094 public List<String> getFields() { 095 return this.fields; 096 } 097 098 /** 099 * Set the existing fields that are used for the search criteria. 100 * 101 * @param fields specify what fields to be returned. 102 */ 103 public void setFields(List<String> fields) { 104 this.fields = fields; 105 } 106 107 /** 108 * Get the scope on which you want to search, ["enterprise","scope"]. 109 * 110 * @return this.current scope that is set. 111 */ 112 public String getScope() { 113 return this.scope; 114 } 115 116 /** 117 * Set the scope for how you want to search, ["enterprise","scope"]. 118 * 119 * @param scope set scope you want to search. 120 */ 121 public void setScope(String scope) { 122 this.scope = scope; 123 } 124 125 /** 126 * Get file extension filter (jpg,docx). 127 * 128 * @return this.list of extensions. 129 */ 130 public List<String> getFileExtensions() { 131 return this.fileExtensions; 132 } 133 134 /** 135 * Set file extension by providing a list of strings [jpg,docx]. 136 * 137 * @param fileExtensions applied as a filter for extensions. 138 */ 139 public void setFileExtensions(List<String> fileExtensions) { 140 this.fileExtensions = fileExtensions; 141 } 142 143 /** 144 * Get the DateRange filter to specify the when a file was created. 145 * 146 * @return this.createdRange DateRange. 147 */ 148 public DateRange getCreatedRange() { 149 return this.createdRange; 150 } 151 152 /** 153 * Set the from DateRange filter to specify when a file was created. 154 * 155 * @param createdRange a start and end date on which a file was created. 156 */ 157 public void setCreatedRange(DateRange createdRange) { 158 this.createdRange = createdRange; 159 } 160 161 /** 162 * Get the DateRange filter to specify the when a file was updated. 163 * 164 * @return this.updatedRange DateRange. 165 */ 166 public DateRange getUpdatedRange() { 167 return this.updatedRange; 168 } 169 170 /** 171 * Set the from DateRange filter to specify when a file was updated. 172 * 173 * @param updatedRange a start and end date on which a file was updated. 174 */ 175 public void setUpdatedRange(DateRange updatedRange) { 176 this.updatedRange = updatedRange; 177 } 178 179 /** 180 * Return the size range that is being used as a filter. 181 * 182 * @return this.sizeRange. 183 */ 184 public SizeRange getSizeRange() { 185 return this.sizeRange; 186 } 187 188 /** 189 * Set the file size range for lower and upper bounds Bytes. 190 * 191 * @param sizeRange a size filter. 192 */ 193 public void setSizeRange(SizeRange sizeRange) { 194 this.sizeRange = sizeRange; 195 } 196 197 /** 198 * Return the list of owner id's that are being applied as a search filter on the results. 199 * 200 * @return ownerUserIds. 201 */ 202 public List<String> getOwnerUserIds() { 203 return this.ownerUserIds; 204 } 205 206 /** 207 * Set the owner id's to be applied as a filter to restrict the results on specific file owners. 208 * 209 * @param ownerUserIds applied ownerId's. 210 */ 211 public void setOwnerUserIds(List<String> ownerUserIds) { 212 this.ownerUserIds = ownerUserIds; 213 } 214 215 /** 216 * Return the list of folder ids that is currently being used as applied filter. 217 * 218 * @return ancestorFolderIds. 219 */ 220 public List<String> getAncestorFolderIds() { 221 return this.ancestorFolderIds; 222 } 223 224 /** 225 * Set the list of folder id's to be applied as a filter on the search filters. 226 * 227 * @param ancestorFolderIds a list of folder ids that will contain results to specific folders. 228 */ 229 public void setAncestorFolderIds(List<String> ancestorFolderIds) { 230 this.ancestorFolderIds = ancestorFolderIds; 231 } 232 233 /** 234 * Return content types that or being applied as a filter (name,description,tags,file_content). 235 * 236 * @return contentTypes. 237 */ 238 public List<String> getContentTypes() { 239 return this.contentTypes; 240 } 241 242 /** 243 * Set list of content types that will be used as a filters. 244 * 245 * @param contentTypes a list of content types such as (name,description,tags,file_content). 246 */ 247 public void setContentTypes(List<String> contentTypes) { 248 this.contentTypes = contentTypes; 249 } 250 251 /** 252 * Return the type you want to return in your search. 253 * 254 * @return String type. 255 */ 256 public String getType() { 257 return this.type; 258 } 259 260 /** 261 * Set the type you want to search for can be file, folder, or web_link. 262 * 263 * @param type can be file, folder, or web_link. 264 */ 265 public void setType(String type) { 266 this.type = type; 267 } 268 269 /** 270 * Return the specified trash search preference. 271 * 272 * @return String trashContent. 273 */ 274 public String getTrashContent() { 275 return this.trashContent; 276 } 277 278 /** 279 * Set trash filter. Can be trashed_only or non_trashed_only, without this parameter default to non_trashed_only. 280 * 281 * @param trashContent Can be trashed_only or non_trashed_only. 282 */ 283 public void setTrashContent(String trashContent) { 284 this.trashContent = trashContent; 285 } 286 287 /** 288 * Retrieve the existing BoxMetaDataFilter. 289 * 290 * @return this.BoxMetaDataFilter 291 */ 292 public BoxMetadataFilter getMetadataFilter() { 293 return this.metadataFilter; 294 } 295 296 /** 297 * Set the current list of Metadata Filters. 298 * 299 * @param metadataFilter a list of the current metadata filters. 300 */ 301 public void setMetadataFilter(BoxMetadataFilter metadataFilter) { 302 this.metadataFilter = metadataFilter; 303 } 304 305 /** 306 * Retrieve the sort field for Box Search. 307 * 308 * @return String identifier for Sort. 309 */ 310 public String getSort() { 311 return this.sort; 312 } 313 314 /** 315 * Set the sort field for Box Search. 316 * 317 * @param sortBy the field to sort the Box Search results by. 318 */ 319 public void setSort(String sortBy) { 320 this.sort = sortBy; 321 } 322 323 /** 324 * Retrieves the sort direction for Box Search results. 325 * 326 * @return The direction of the Box Search sort. 327 */ 328 public String getDirection() { 329 return this.direction; 330 } 331 332 /** 333 * Set the direction of the sort for the Box Search results. 334 * 335 * @param direction can be DESC or ASC. 336 */ 337 public void setDirection(String direction) { 338 this.direction = direction; 339 } 340 341 /** 342 * Checks String to see if the parameter is null. 343 * 344 * @param paramValue Object that will be checked if null. 345 * @return this.true if the parameter that is being checked is not null 346 */ 347 private boolean isNullOrEmpty(Object paramValue) { 348 boolean isNullOrEmpty = false; 349 if (paramValue == null) { 350 isNullOrEmpty = true; 351 } 352 if (paramValue instanceof String) { 353 if (((String) paramValue).trim().equalsIgnoreCase("")) { 354 isNullOrEmpty = true; 355 } 356 } else if (paramValue instanceof List) { 357 return ((List) paramValue).isEmpty(); 358 } 359 return isNullOrEmpty; 360 } 361 362 /** 363 * Add BoxMetaDataFilter to the JsonArray boxMetadataFilterRequestArray. 364 * 365 * @return JsonArray that is formated Json request 366 */ 367 private JsonArray formatBoxMetadataFilterRequest() { 368 JsonArray boxMetadataFilterRequestArray = new JsonArray(); 369 370 JsonObject boxMetadataFilter = new JsonObject() 371 .add("templateKey", this.metadataFilter.getTemplateKey()) 372 .add("scope", this.metadataFilter.getScope()) 373 .add("filters", this.metadataFilter.getFiltersList()); 374 boxMetadataFilterRequestArray.add(boxMetadataFilter); 375 376 return boxMetadataFilterRequestArray; 377 } 378 379 /** 380 * Concat a List into a CSV String. 381 * 382 * @param list list to concat 383 * @return csv string 384 */ 385 private String listToCSV(List<String> list) { 386 String csvStr = ""; 387 for (String item : list) { 388 csvStr += "," + item; 389 } 390 391 return csvStr.length() > 1 ? csvStr.substring(1) : csvStr; 392 } 393 394 /** 395 * Get the Query Paramaters to be used for search request. 396 * 397 * @return this.QueryStringBuilder. 398 */ 399 public QueryStringBuilder getQueryParameters() { 400 QueryStringBuilder builder = new QueryStringBuilder(); 401 402 if (this.isNullOrEmpty(this.query) && this.metadataFilter == null) { 403 throw new BoxAPIException( 404 "BoxSearchParameters requires either a search query or Metadata filter to be set." 405 ); 406 } 407 408 //Set the query of the search 409 if (!this.isNullOrEmpty(this.query)) { 410 builder.appendParam("query", this.query); 411 } 412 //Set the scope of the search 413 if (!this.isNullOrEmpty(this.scope)) { 414 builder.appendParam("scope", this.scope); 415 } 416 //Acceptable Value: "jpg,png" 417 if (!this.isNullOrEmpty(this.fileExtensions)) { 418 builder.appendParam("file_extensions", this.listToCSV(this.fileExtensions)); 419 } 420 //Created Date Range: From Date - To Date 421 if ((this.createdRange != null)) { 422 builder.appendParam("created_at_range", this.createdRange.buildRangeString()); 423 } 424 //Updated Date Range: From Date - To Date 425 if ((this.updatedRange != null)) { 426 builder.appendParam("updated_at_range", this.updatedRange.buildRangeString()); 427 } 428 //Filesize Range 429 if ((this.sizeRange != null)) { 430 builder.appendParam("size_range", this.sizeRange.buildRangeString()); 431 } 432 //Owner Id's 433 if (!this.isNullOrEmpty(this.ownerUserIds)) { 434 builder.appendParam("owner_user_ids", this.listToCSV(this.ownerUserIds)); 435 } 436 //Ancestor ID's 437 if (!this.isNullOrEmpty(this.ancestorFolderIds)) { 438 builder.appendParam("ancestor_folder_ids", this.listToCSV(this.ancestorFolderIds)); 439 } 440 //Content Types: "name, description" 441 if (!this.isNullOrEmpty(this.contentTypes)) { 442 builder.appendParam("content_types", this.listToCSV(this.contentTypes)); 443 } 444 //Type of File: "file,folder,web_link" 445 if (this.type != null) { 446 builder.appendParam("type", this.type); 447 } 448 //Trash Content 449 if (!this.isNullOrEmpty(this.trashContent)) { 450 builder.appendParam("trash_content", this.trashContent); 451 } 452 //Metadata filters 453 if (this.metadataFilter != null) { 454 builder.appendParam("mdfilters", this.formatBoxMetadataFilterRequest().toString()); 455 } 456 //Fields 457 if (!this.isNullOrEmpty(this.fields)) { 458 builder.appendParam("fields", this.listToCSV(this.fields)); 459 } 460 //Sort 461 if (!this.isNullOrEmpty(this.sort)) { 462 builder.appendParam("sort", this.sort); 463 } 464 //Direction 465 if (!this.isNullOrEmpty(this.direction)) { 466 builder.appendParam("direction", this.direction); 467 } 468 469 return builder; 470 } 471}