javagdata-apigdata

Make a Post Request using Java client to the Google Documents List API using gdata.


I want to copy a document as mentioned here:

http://code.google.com/apis/documents/docs/3.0/developers_guide_protocol.html#CopyingDocs

I am using the latest Java Gdata Library, which doesn't have a nice wrapper method to do it, and I am a newb to the java gdata lib.

I have already got an authenticated DocsService, if that is useful.

Bonus points if you wrap it un into a method which takes two Strings, one being the source name and the other being the copy name.


Solution

  • Ok I did it....

    Just so everyone can see... this.dService is a DocsService, that already has been authenticated.

    // This method returns the Resource ID to be used to make the copy
    public String loadDocsSpreadsheetEntryId(String sourceName) {
        String resourceId = null;
        try {
    
        Logger.info("Loading feed URL");
        URL url = new URL("https://docs.google.com/feeds/default/private/full" );
        DocumentQuery query = new DocumentQuery(url);
        query.setTitleQuery(sourceName);
        query.setTitleExact(true);
    
        Logger.info("Loaded feed URL");
        DocumentListFeed dfeed = this.dService.getFeed(query, DocumentListFeed.class);
        Logger.info("got feed");
        for (DocumentListEntry entry : dfeed.getEntries()) {
            Logger.info(entry.getTitle().getPlainText());
            if(entry.getTitle().getPlainText().equalsIgnoreCase(sourceName))
            {
                Logger.info("found doc");
                resourceId = entry.getResourceId();
            }
         }
        } catch(Exception e) {
            logException(e, "Loading Source Spreadsheet to copy");
        }
    
        return resourceId;
    }
    
        public void createSpreadsheetFrom(String destination, String source) {
            try {
            URL entryUrl = new URL("http://docs.google.com/feeds/default/private/full");
            Map<String, String> parameters = new HashMap<String, String>();
            String resourceID = loadDocsSpreadsheetEntryId(source);
            Logger.info("Resource id %s", resourceID);
    
            DocumentListEntry newEntry = new DocumentListEntry();
            newEntry.setId(resourceID);
            newEntry.setTitle(new PlainTextConstruct(destination));
            this.dService.insert(entryUrl, newEntry);
    
            } catch(Exception e) {
                logException(e, "Copying Spreadsheet");
            }
    
    }