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