private static String folderContains(String filename, String folderID) { // Returns the ID of a specified filename in a folder or null if it does not exist File child = null; ChildList children = null; try { children = service.children().list(folderID).execute(); for (ChildReference element : children.getItems()) { child = service.files().get(element.getId()).execute(); if (child.getTitle().equals(filename)) { break; } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); // System.out.println("children: "+children.toPrettyString()); // System.out.println("child: "+child.toPrettyString()); } if (child != null && child.getTitle().equals(filename)) { return child.getId(); } return null; }
@Override public void init() throws IOException { // Detect and store change number long latestChange = changes(null); if (latestChange != -1) stub.storage().put(StorageService.REMOTE_CHANGE, latestChange); // Download files stub.storage().put(StorageService.REMOTE_TO_LOCAL, new HashMap<String, String>()); stub.storage().put(StorageService.LOCAL_TO_REMOTE, new HashMap<String, String>()); String remoteRoot = Constants.FOLDER_ROOT; java.io.File localRoot = Configuration.getLocalRoot(); if (!localRoot.exists()) { localRoot.mkdirs(); } String localRelative = DriveUtils.relativePath(localRoot); stub.storage().remoteToLocal().put(remoteRoot, localRelative); stub.storage().localToRemote().put(localRelative, remoteRoot); for (ChildReference childref : drive.children().list(remoteRoot).execute().getItems()) { stub.transmit().download(childref.getId(), localRoot); } stub.storage().put(StorageService.SNAPSHOT, stub.snapshot().make()); }
private static String determineParent(String email, String workflow) { String root = "0B7Jfx3RRVE5YenZEY1N5cE5pRms"; String parentFolder = null; String level = root; String parent = root; boolean lowest = false; // Checks for user level = folderContains(email, level); if (level != null) { DateFormat today = new SimpleDateFormat("E, MM/dd/yyyy"); Date now = new Date(); String nowStr = today.format(now); // Checks for today's date parent = level; level = folderContains(nowStr, level); if (level != null) { // Checks for a workflowID folder parent = level; level = folderContains(workflow, level); // System.out.println("level: "+level); if (level != null) { // Finds the highest folder number; add 1 and creates it. try { File child = null; int lastRun = 0; int tmp = lastRun; ChildList children = service.children().list(level).execute(); for (ChildReference element : children.getItems()) { child = service.files().get(element.getId()).execute(); try { tmp = Integer.parseInt(child.getTitle()); if (tmp > lastRun) { lastRun = tmp; } } catch (NumberFormatException e) { // TODO Auto-generated catch block e.printStackTrace(); } } lastRun += 1; String next = new Integer(lastRun).toString(); // System.out.println("level: "+level); // System.out.println("next: "+next); parentFolder = createFolderWithParentAndTitle(level, next); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { lowest = true; } } else { parentFolder = createFolderWithParentAndTitle(parent, workflow); } } else { parentFolder = createFolderWithParentAndTitle(parent, nowStr); } } else { parentFolder = createFolderWithParentAndTitle(parent, email); } try { File file = service.files().get(parentFolder).execute(); service.permissions().insert(file.getId(), perm).execute(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (!lowest) parentFolder = determineParent(email, workflow); return parentFolder; }