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; }
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; }