public DocumentListEntry createFolder(String folderName, String roleString, List<String> emails)
      throws IOException, ServiceException {
    logger.info("\n\nFolder Exists? " + folderName);
    boolean folderFound = false;

    URL feedUri = new URL("https://docs.google.com/feeds/default/private/full/-/folder");
    DocumentListFeed feed = docsService.getFeed(feedUri, DocumentListFeed.class);
    DocumentListEntry folderEntry = new DocumentListEntry();

    for (DocumentListEntry entry : feed.getEntries()) {
      if (folderName.equalsIgnoreCase(entry.getTitle().getPlainText())) {
        folderFound = true;
        folderEntry = entry;
        break;
      }
    }

    if (!folderFound) {
      logger.info("\n\nCreating folder " + folderName);
      DocumentListEntry newEntry = new FolderEntry();
      newEntry.setTitle(new PlainTextConstruct(folderName));
      URL feedUrl = new URL("https://docs.google.com/feeds/default/private/full/");
      folderEntry = docsService.insert(feedUrl, newEntry);

      shareResource(roleString, emails, folderEntry);
    }

    return folderEntry;
  }
  public List<WorksheetEntry> copyDocument(
      DocumentListEntry documentListEntry,
      String newDocumentTitle,
      DocumentListEntry destinationFolderEntry)
      throws IOException, ServiceException {

    documentListEntry.setTitle(new PlainTextConstruct(newDocumentTitle));

    URL url = new URL("https://docs.google.com/feeds/default/private/full/");

    DocumentListEntry newDoc = docsService.insert(url, documentListEntry);

    String destFolder = ((MediaContent) destinationFolderEntry.getContent()).getUri();
    URL newUrl = new URL(destFolder);

    // Convert DocumentListEntry to SpreadsheetEntry
    DocumentListEntry newDocumentListEntry = docsService.insert(newUrl, newDoc);
    String spreadsheetURL =
        "https://spreadsheets.google.com/feeds/spreadsheets/" + newDocumentListEntry.getDocId();

    SpreadsheetEntry spreadsheetEntry =
        spreadsheetService.getEntry(new URL(spreadsheetURL), SpreadsheetEntry.class);

    return spreadsheetEntry.getWorksheets();
  }
  /**
   * Log in to Google, under the Google Spreadsheets and Docs account.
   *
   * @param username name of user to authenticate (e.g. [email protected])
   * @param password password to use for authentication
   * @throws AuthenticationException if the service is unable to validate the username and password.
   */
  public void login(String username, String password) throws AuthenticationException {
    spreadsheetService = new SpreadsheetService(APPLICATION_NAME);
    spreadsheetService.setUserCredentials(username, password);
    spreadsheetService.setConnectTimeout(0);
    spreadsheetService.setReadTimeout(0);

    docsService = new DocsService(APPLICATION_NAME);
    docsService.setUserCredentials(username, password);
    docsService.setConnectTimeout(0);
    docsService.setReadTimeout(0);
  }
  public com.google.gdata.data.docs.SpreadsheetEntry createSpreadsheet(
      String _title,
      DocumentListEntry _destinationFolderEntry,
      String _defaultWorksheetName,
      int _rows,
      int _columns)
      throws IOException, ServiceException {
    com.google.gdata.data.docs.SpreadsheetEntry newEntry =
        new com.google.gdata.data.docs.SpreadsheetEntry();
    newEntry.setTitle(new PlainTextConstruct(_title));

    String destFolder = ((MediaContent) _destinationFolderEntry.getContent()).getUri();
    URL destinationURL = new URL(destFolder);

    com.google.gdata.data.docs.SpreadsheetEntry newSpreadsheetEntry =
        docsService.insert(destinationURL, newEntry);
    // convert from Docs API Spreadsheet to Spreadsheet API Spreadsheet
    WorksheetEntry worksheet =
        getSpreadsheetEntryFromDocumentListEntry(newSpreadsheetEntry).getDefaultWorksheet();
    worksheet.setTitle(new PlainTextConstruct(_defaultWorksheetName));
    worksheet.setRowCount(_rows);
    worksheet.setColCount(_columns);
    worksheet.update();

    return newSpreadsheetEntry;
  }
Exemplo n.º 5
0
  @RequestMapping("home")
  public ModelAndView welcomeHandler(HttpServletRequest req, HttpServletResponse res) {
    ModelAndView mv = new ModelAndView("home");

    HttpSession session = req.getSession();
    UserInfo ui = (UserInfo) session.getAttribute("userInfo");

    String accessToken = ui.getAccess_token();

    DocsService service = new DocsService("mayRoro");
    service.setHeader("Authorization", "OAuth " + accessToken);

    try {
      URL metafeedUrl =
          new URL(
              "https://spreadsheets.google.com/feeds/spreadsheets/private/full?title="
                  + ConstFunc.SPREADSHEET_PREFIX);
      SpreadsheetFeed feed = service.getFeed(metafeedUrl, SpreadsheetFeed.class);
      List<SpreadsheetEntry> spreadsheets = feed.getEntries();
      for (int i = 0; i < spreadsheets.size(); i++) {
        SpreadsheetEntry entry = spreadsheets.get(i);
        entry.setTitle(
            new PlainTextConstruct(
                entry.getTitle().getPlainText().substring(ConstFunc.SPREADSHEET_PREFIX.length())));
        System.out.println("Self: " + entry.getSelfLink().getHref());
      }
      mv.addObject("spreadsheets", spreadsheets);
    } catch (Exception e) {
      e.printStackTrace();
    }

    //		************* Seznam vseh map *************
    //		try {
    //			URL metafeedUrl = new URL("https://docs.google.com/feeds/default/private/full/-/folder");
    //			Feed feed = service.getFeed(metafeedUrl, Feed.class);
    //			List<Entry> spreadsheets = feed.getEntries();
    //			for (int i = 0; i < spreadsheets.size(); i++) {
    //				Entry entry = spreadsheets.get(i);
    //				System.out.println("\t" + entry.getTitle().getPlainText());
    //			}
    //			mv.addObject("spreadsheets", spreadsheets);
    //		} catch (Exception e) {
    //			e.printStackTrace();
    //		}

    return mv;
  }
  public DocumentListEntry getDocument(URL folderFeedUri, String documentName)
      throws IOException, ServiceException {
    DocumentQuery query = new DocumentQuery(folderFeedUri);
    query.setTitleQuery(documentName);
    query.setTitleExact(true);
    query.setMaxResults(1);
    DocumentListFeed feed = docsService.getFeed(query, DocumentListFeed.class);
    DocumentListEntry doc = null;
    if (!feed.getEntries().isEmpty()) {
      doc = feed.getEntries().get(0);
    }

    return doc;
  }
Exemplo n.º 7
0
  @RequestMapping("maut/{spreadsheetID}")
  public ModelAndView maut(
      HttpServletRequest req, HttpServletResponse res, @PathVariable String spreadsheetID) {
    ModelAndView mv = new ModelAndView("maut");

    HttpSession session = req.getSession();
    String accessToken = ((UserInfo) session.getAttribute("userInfo")).getAccess_token();

    DocsService service = new DocsService("mayRoro");
    service.setHeader("Authorization", "OAuth " + accessToken);

    try {
      URL spreadsheetUrl =
          new URL(
              "https://spreadsheets.google.com/feeds/spreadsheets/private/full/" + spreadsheetID);
      SpreadsheetEntry spreadsheet = service.getEntry(spreadsheetUrl, SpreadsheetEntry.class);

      System.out.println("Title: " + spreadsheet.getTitle().getPlainText());

      for (WorksheetEntry we : spreadsheet.getWorksheets()) {
        System.out.println(we.getTitle().getPlainText() + ": " + we.getId());
      }

      spreadsheet.setTitle(
          new PlainTextConstruct(
              spreadsheet
                  .getTitle()
                  .getPlainText()
                  .substring(ConstFunc.SPREADSHEET_PREFIX.length())));
      mv.addObject("spreadsheet", spreadsheet);
    } catch (Exception e) {
      e.printStackTrace();
    }

    return mv;
  }
  public void shareResource(String roleString, String email, DocumentListEntry documentListEntry)
      throws IOException {
    AclRole role = new AclRole(roleString);
    AclScope scope = new AclScope(AclScope.Type.USER, email);

    AclEntry aclEntry = new AclEntry();
    aclEntry.setRole(role);
    aclEntry.setScope(scope);

    try {
      docsService.insert(new URL(documentListEntry.getAclFeedLink().getHref()), aclEntry);
    } catch (ServiceException se) {
      logger.info("Unable to share resource or resource already shared.");
    }
  }
  public String getFolderURI(String _folderName) throws IOException, ServiceException {

    URL feedUri = new URL("https://docs.google.com/feeds/default/private/full/-/folder");
    DocumentListFeed feed = docsService.getFeed(feedUri, DocumentListFeed.class);
    DocumentListEntry folderEntry = new DocumentListEntry();

    for (DocumentListEntry entry : feed.getEntries()) {
      if (_folderName.equalsIgnoreCase(entry.getTitle().getPlainText())) {
        folderEntry = entry;
        break;
      }
    }
    String folderURIString = null;

    if (folderEntry.getContent() != null) {
      folderURIString = getDocumentFeedUri(folderEntry);
    }

    return folderURIString;
  }
 public DocumentListFeed getDocsFeedForFolder(String _folderResourceId)
     throws IOException, ServiceException {
   URL url = new URL(_folderResourceId);
   logger.info("folder url: " + url);
   return docsService.getFeed(url, DocumentListFeed.class);
 }