コメント

1件のコメント

  • kendomen

    We do programmatically passing in a path (eg. "test/ken/root") and it parses the path and creates folders.

     

    BoxFolder rootBoxFolder = BoxFolder.getRootFolder(api);  // start from root
    BoxFolder parentBoxFolder = getFolderFromPath(api, rootBoxFolder, "test/ken/root");
    
    ...
    
    private static BoxFolder getFolderFromPath(BoxAPIConnection api, BoxFolder startingFolder, String folderPath) {
    
            BoxFolder boxFolder = startingFolder;
    
            String pattern = Pattern.quote(System.getProperty("file.separator"));
            String[] splittedFolderName = folderPath.split(pattern);
    
            for (String folderName : splittedFolderName) {
                if (!"".equals(folderName)) {
                    //System.out.println(folderName);
                    boxFolder = createOrGetFolder(api, boxFolder, folderName);
                }
            }
            return boxFolder;
        }
    
        private static BoxFolder createOrGetFolder(BoxAPIConnection api, BoxFolder startingFolder, String folderName) {
            BoxFolder boxFolder = null;
            boolean FOUND = false;
    
            try {
                for (BoxItem.Info folderItemInfo : startingFolder.getChildren()) {
                    if (folderItemInfo.getName().equals(folderName) && folderItemInfo instanceof BoxFolder.Info) {
                        logger.info("found folder: " + folderItemInfo.getName() + "[" + folderItemInfo.getID() + "]");
                        boxFolder = (BoxFolder) folderItemInfo.getResource();
                        FOUND = true;
                    }
                }
    
                if (!FOUND) {
                    logger.info("Creating folder... " + folderName);
                    BoxFolder.Info childFolderInfo = startingFolder.createFolder(folderName);
                    boxFolder = childFolderInfo.getResource();
                }
            }
            catch (BoxAPIException be) {
                // 401 - why would this happen??? but it does
                api.refresh();  // so I need to do this
    
                try {
                    Thread.sleep(5000);
                }
                catch (Exception e) {
                    logger.info(e.getMessage());
                }
                createOrGetFolder(api, startingFolder, folderName);
            }
    
            return boxFolder;
        }

    Full example can be found here:  https://github.com/kendomen/BoxJava

     

     

     

    0
    コメントアクション Permalink

サインインしてコメントを残してください。