Last active
December 27, 2017 19:30
-
-
Save allenmichael/91226921d6fe9ce2cb39b1c97d998e0c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.box; | |
import java.util.ArrayList; | |
import com.box.sdk.BoxDeveloperEditionAPIConnection; | |
import com.box.sdk.BoxFile; | |
import com.box.sdk.BoxFolder; | |
import com.box.sdk.BoxItem; | |
import com.eclipsesource.json.JsonArray; | |
import com.eclipsesource.json.JsonObject; | |
public class BoxFolderTreeBuilder { | |
private BoxDeveloperEditionAPIConnection _boxClient; | |
private String _rootFolderId; | |
private int _maxDepth; | |
private static final String DEFAULT_ROOT_FOLDER_ID = "0"; | |
private static final int DEFAULT_MAX_DEPTH = -1; | |
public BoxFolderTreeBuilder(BoxDeveloperEditionAPIConnection boxClient) { | |
this(boxClient, DEFAULT_ROOT_FOLDER_ID, DEFAULT_MAX_DEPTH); | |
} | |
public BoxFolderTreeBuilder(BoxDeveloperEditionAPIConnection boxClient, String rootFolderId) { | |
this(boxClient, rootFolderId, DEFAULT_MAX_DEPTH); | |
} | |
public BoxFolderTreeBuilder(BoxDeveloperEditionAPIConnection boxClient, int maxDepth) { | |
this(boxClient, DEFAULT_ROOT_FOLDER_ID, maxDepth); | |
} | |
public BoxFolderTreeBuilder(BoxDeveloperEditionAPIConnection boxClient, String rootFolderId, int maxDepth) { | |
this._boxClient = boxClient; | |
this._rootFolderId = rootFolderId; | |
this._maxDepth = maxDepth; | |
} | |
public BoxFolderTree buildFolderTreeWithFlatLists() { | |
BoxFolderTree tree = new BoxFolderTree(); | |
tree.setRootId(this._rootFolderId); | |
ArrayList<BoxFolderTreeFolder> rootFolderChildren = new ArrayList<>(); | |
BoxFolder folder = new BoxFolder(this._boxClient, this._rootFolderId); | |
String path = this._rootFolderId; | |
for (BoxItem.Info itemInfo : folder) { | |
if (itemInfo instanceof BoxFile.Info) { | |
BoxFile.Info fileInfo = (BoxFile.Info) itemInfo; | |
BoxFolderTreeFile treeFile = new BoxFolderTreeFile(fileInfo, path); | |
tree.files.add(treeFile); | |
} else if (itemInfo instanceof BoxFolder.Info) { | |
BoxFolder.Info folderInfo = (BoxFolder.Info) itemInfo; | |
BoxFolderTreeFolder treeFolder = new BoxFolderTreeFolder(folderInfo, path); | |
tree.folders.add(treeFolder); | |
rootFolderChildren.add(treeFolder); | |
} | |
} | |
tree = Dive(tree, rootFolderChildren, 1); | |
return tree; | |
} | |
private BoxFolderTree Dive(BoxFolderTree tree, ArrayList<BoxFolderTreeFolder> children, int currentDepth) { | |
if (inTooDeep(currentDepth)) { | |
return tree; | |
} else { | |
currentDepth++; | |
ArrayList<BoxFolderTreeFolder> additionalChildren = new ArrayList<>(); | |
for (BoxFolderTreeFolder child : children) { | |
BoxFolder folderItems = new BoxFolder(this._boxClient, child.getItem().getID()); | |
int foundFolder = -1; | |
for (int i = 0; i < tree.folders.size(); i++) { | |
if (child.getItem().getID() == tree.folders.get(i).getItem().getID()) { | |
foundFolder = i; | |
} | |
} | |
String path = String.format("%s/%s", child.getPath(), child.getItem().getID()); | |
for (BoxItem.Info item : folderItems.getChildren()) { | |
if (foundFolder >= 0) { | |
tree.folders.get(foundFolder).children.add(new BoxFolderTreeItem(item)); | |
} | |
if (item instanceof BoxFile.Info) { | |
BoxFile.Info fileInfo = (BoxFile.Info) item; | |
tree.files.add(new BoxFolderTreeFile(fileInfo, path)); | |
} else if (item instanceof BoxFolder.Info) { | |
BoxFolder.Info folderInfo = (BoxFolder.Info) item; | |
BoxFolderTreeFolder nestedFolder = new BoxFolderTreeFolder(folderInfo, path); | |
tree.folders.add(nestedFolder); | |
additionalChildren.add(nestedFolder); | |
} | |
} | |
} | |
if (additionalChildren.size() == 0) { | |
return tree; | |
} else { | |
return Dive(tree, additionalChildren, currentDepth); | |
} | |
} | |
} | |
private Boolean inTooDeep(int depthCount) { | |
if (this._maxDepth < 0) { | |
return false; | |
} else { | |
return (depthCount >= this._maxDepth); | |
} | |
} | |
public class BoxFolderTree { | |
private String rootId; | |
private ArrayList<BoxFolderTreeFile> files = new ArrayList<>(); | |
private ArrayList<BoxFolderTreeFolder> folders = new ArrayList<>(); | |
public String getRootId() { | |
return this.rootId; | |
} | |
public void setRootId(String id) { | |
this.rootId = id; | |
} | |
public String writeJSON() { | |
JsonObject requestJSON = new JsonObject(); | |
JsonArray filesJSON = new JsonArray(); | |
for (BoxFolderTreeFile file : files) { | |
filesJSON.add(file.convertToJSON()); | |
} | |
JsonArray foldersJSON = new JsonArray(); | |
for (BoxFolderTreeFolder folder : folders) { | |
foldersJSON.add(folder.convertToJSON()); | |
} | |
requestJSON.add("rootId", this.rootId); | |
requestJSON.add("files", filesJSON); | |
requestJSON.add("folders", foldersJSON); | |
return requestJSON.toString(); | |
} | |
} | |
public class BoxFolderTreeItem { | |
private BoxItem.Info item; | |
public BoxFolderTreeItem(BoxItem.Info item) { | |
this.item = item; | |
} | |
public JsonObject convertToJSON() { | |
JsonObject itemJSON = new JsonObject(); | |
itemJSON.add("id", this.item.getID()); | |
itemJSON.add("name", this.item.getName()); | |
String type = (item instanceof BoxFile.Info == true) ? "file" : "folder"; | |
itemJSON.add("type", type); | |
return itemJSON; | |
} | |
} | |
public class BoxFolderTreeFile { | |
private BoxFile.Info item; | |
private String path; | |
public BoxFolderTreeFile(BoxFile.Info item, String path) { | |
this.item = item; | |
this.path = path; | |
} | |
public BoxFile.Info getItem() { | |
return this.item; | |
} | |
public void setItem(BoxFile.Info info) { | |
this.item = info; | |
} | |
public String getPath() { | |
return this.path; | |
} | |
public void setPath(String path) { | |
this.path = path; | |
} | |
public JsonObject convertToJSON() { | |
JsonObject itemJSON = new JsonObject(); | |
JsonObject fileJSON = new JsonObject(); | |
fileJSON.add("id", this.item.getID()); | |
fileJSON.add("type", "file"); | |
fileJSON.add("name", this.item.getName()); | |
itemJSON.add("path", this.path); | |
itemJSON.add("item", fileJSON); | |
return itemJSON; | |
} | |
} | |
public class BoxFolderTreeFolder { | |
private BoxFolder.Info item; | |
private String path; | |
private ArrayList<BoxFolderTreeItem> children; | |
public BoxFolderTreeFolder(BoxFolder.Info item, String path) { | |
this.item = item; | |
this.path = path; | |
this.children = new ArrayList<>(); | |
} | |
public BoxFolder.Info getItem() { | |
return this.item; | |
} | |
public void setItem(BoxFolder.Info info) { | |
this.item = info; | |
} | |
public String getPath() { | |
return this.path; | |
} | |
public void setPath(String path) { | |
this.path = path; | |
} | |
public JsonObject convertFolderToJSON() { | |
JsonObject itemJSON = new JsonObject(); | |
JsonObject folderJSON = new JsonObject(); | |
folderJSON.add("id", this.item.getID()); | |
folderJSON.add("type", "folder"); | |
folderJSON.add("name", this.item.getName()); | |
itemJSON.add("path", this.path); | |
itemJSON.add("item", folderJSON); | |
return itemJSON; | |
} | |
public JsonObject convertToJSON() { | |
JsonObject folderJSON = this.convertFolderToJSON(); | |
JsonArray children = new JsonArray(); | |
for (BoxFolderTreeItem item : this.children) { | |
children.add(item.convertToJSON()); | |
} | |
folderJSON.add("children", children); | |
return folderJSON; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment