Google Drive Spreadsheet Data JavaAgent


/**
 * Created from copy: 2014.05.17.2.34.AM
 * GoogleAddRowForXpagesJavaAgent | WorksheetsAddJavaAgent.java
 * Inser Row(s) to Worksheets in specific Spreadsheet, housed on Google Drive
 */
//...
//Google imports
import com.google.gdata.client.spreadsheet.*;
import com.google.gdata.data.spreadsheet.*;
import com.google.gdata.util.*; //...
//Java imports
import java.io.IOException;
import java.net.*;
import java.util.*;

//...
//Lotus Domino imports
import lotus.domino.AgentBase;
import lotus.domino.AgentContext;
import lotus.domino.Document;
import lotus.domino.Session;
import lotus.domino.View;
import lotus.domino.local.Database;

/**
 * @author Dököll Solutions, Inc.
 * @version 2014.05.17.2.34.AM
 * 
 */
public class WorksheetsAddJavaAgent extends AgentBase {

	//declare and initialise variables for Worksheets data
	static String GOOGLE_SHEET_VIEW = "GoogleWorksheetsDataView";
	private String FirstName = "firstname";
	private String LastName = "lastname";
	private String Age = "age";
	private String Height = "height";

	// 2014.05.18.10.39.PM

	public void NotesMain() {

		try {

			Session session = getSession();
			// load info to console for debugging purposes

			// load agentContext
			AgentContext agentContext = session.getAgentContext();
			// ...
			// find database based on session found
			Database database = (Database) agentContext.getCurrentDatabase();
			// Find view in question according to current database
			View view = database.getView(GOOGLE_SHEET_VIEW);
			// declare document variables
			Document currDoc;
			Document tempDoc;
			// grab first doc
			currDoc = view.getFirstDocument();

			while (currDoc != null) {
				// prepare values to plug into Google Spredsheet/Worksheet(s)
				String SubjectFirstName = currDoc
						.getItemValueString("subjectFirst");
				String SubjectLastName = currDoc
						.getItemValueString("subjectLast");
				String SubjectAge = currDoc.getItemValueString("subjectAge");
				String SubjectHeight = currDoc
						.getItemValueString("subjectHeight");

				//TODO: Get a Google Drive account before running this code
				//      Add a Spreadsheet, and a new Worksheet
				//...
				// grab Google Creds
				String USERNAME = "youraccountusername@gmail.com";
				String PASSWORD = "yourgooglepassword";

				// reference Service type
				SpreadsheetService service = new SpreadsheetService(
						"CompanyWorksheetsAdd");
				try {
					service.setUserCredentials(USERNAME, PASSWORD);
				} catch (AuthenticationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				// TODO: Authorize the service object for a specific user (see
				// other sections)

				// Define the URL to request. This should never change.
				URL SPREADSHEET_FEED_URL = null;
				try {
					SPREADSHEET_FEED_URL = new URL(
							"https://spreadsheets.google.com/feeds/spreadsheets/private/full");
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				try {
					// Make a request to the API and get all spreadsheets.
					SpreadsheetFeed feed = (SpreadsheetFeed) service.getFeed(
							SPREADSHEET_FEED_URL, SpreadsheetFeed.class);
					List<SpreadsheetEntry> spreadsheets = feed.getEntries();

					if (spreadsheets.size() == 0) {
						// TODO: There were no spreadsheets, act accordingly.
					}

					// TODO: Choose a spreadsheet more intelligently based on
					// your
					// app's needs.
					SpreadsheetEntry spreadsheet = spreadsheets.get(0);
					System.out.println(spreadsheet.getTitle().getPlainText());

					// Get the first worksheet of the first spreadsheet.
					// TODO: Choose a worksheet more intelligently based on your
					// app's needs.
					WorksheetFeed worksheetFeed = service.getFeed(spreadsheet
							.getWorksheetFeedUrl(), WorksheetFeed.class);
					List<WorksheetEntry> worksheets = worksheetFeed
							.getEntries();
					WorksheetEntry worksheet = worksheets.get(1);

					// Fetch the list feed of the worksheet.
					URL listFeedUrl = worksheet.getListFeedUrl();
					ListFeed listFeed = (ListFeed) service.getFeed(listFeedUrl,
							ListFeed.class);

					// Create a local representation of the new row.
					ListEntry row = new ListEntry();
					row.getCustomElements().setValueLocal(FirstName,
							SubjectFirstName);
					row.getCustomElements().setValueLocal(LastName,
							SubjectLastName);
					row.getCustomElements().setValueLocal(Age, SubjectAge);
					row.getCustomElements()
							.setValueLocal(Height, SubjectHeight);

					// Send the new row to the API for insertion.
					row = (ListEntry) service.insert(listFeedUrl, row);
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ServiceException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				// Get next document
				tempDoc = view.getNextDocument(currDoc);

				// recycle currDoc
				currDoc.recycle();
				// set currDoc to tempDoc
				currDoc = tempDoc;

				session.recycle();

			}

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
All code submitted to OpenNTF XSnippets, whether submitted as a "Snippet" or in the body of a Comment, is provided under the Apache License Version 2.0. See Terms of Use for full details.
No comments yetLogin first to comment...