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