using System; using Google.GData.Client; using Google.GData.Extensions; using Google.GData.Spreadsheets; using System.Collections; using LoggingTool.models; using System.Collections.Generic; namespace LoggingTool.interfaces { class TeamUpSpeadsheetOutput : ILoggerOutput { private SpreadsheetsService service; private SpreadsheetEntry spreadsheet; /* ****************************************************************************** * constructor TeamUpSpreadsheetOutput * @description: creates the implementation of the ILoggerOutput interface * ******************************************************************************/ public TeamUpSpeadsheetOutput(String username, String password, String spreadsheet) { //Log in to GoogleDocs and retrieve the right spreadsheet service = new SpreadsheetsService("TeamUp Logging Spreadsheet"); service.setUserCredentials(username, password); //Get the correct spreadsheet SpreadsheetQuery query = new SpreadsheetQuery(); SpreadsheetFeed feed = service.Query(query); //If a spreadsheet with the given name exists, make that our spreadsheet foreach (SpreadsheetEntry entry in feed.Entries) { if(entry.Title.Text.Equals(spreadsheet)) this.spreadsheet = entry; } //Else, state the spreadsheet is invalid if(spreadsheet == null) { //TODO: give a nice error message. Console.WriteLine("Spreadsheet " + spreadsheet + " not found"); } } /* ****************************************************************************** * function testConnection * @return: boolean * @description: returns true if the connection to the outside source is working * ******************************************************************************/ public Boolean testConnection() { //TODO: Actually check for a connection. return true; } /* ****************************************************************************** * function send * @param: message : Loggermessage * @return: boolean * @description: Decides to which worksheet the message should go and passes it * along. Returns false if the message is not the correct type. * ******************************************************************************/ public Boolean send(LoggerMessage message) { //If the message if of the correct type... if (message is TeamUpSpreadsheetMessage) { //Cast it and check for the message type and... TeamUpSpreadsheetMessage msg = (TeamUpSpreadsheetMessage)message; String queryString = ""; if (msg.Type == TeamUpSpreadsheetMessage.QUESTIONNAIRE) queryString = "Questionnaire"; else if(msg.Type == TeamUpSpreadsheetMessage.EVENT) queryString = "Events"; //Get the correct worksheet to perform the operation AtomLink link = this.spreadsheet.Links.FindService(GDataSpreadsheetsNameTable.WorksheetRel, null); WorksheetQuery workSheetQuery = new WorksheetQuery(link.HRef.ToString()); WorksheetFeed workSheetFeed = service.Query(workSheetQuery); WorksheetEntry worksheet = null; foreach (WorksheetEntry entry in workSheetFeed.Entries) { if(entry.Title.Text.Equals(queryString)) worksheet = entry; } //Return if the worksheet doesn't exist. if(worksheet == null) return false; //Else: send the message return sendToWorksheet(worksheet, msg.Data); } else { return false; } } /* ****************************************************************************** * function sendToWorksheet * @param: worksheet : WorksheetEntry * @param: row : List * @return: boolean * @description: Takes the already formatted ArrayList and sends it to the specified worksheet * ******************************************************************************/ Boolean sendToWorksheet(WorksheetEntry worksheet, List row) { //TODO: Actually send stuff. //Get the Google Docs URL AtomLink listFeedLink = worksheet.Links.FindService(GDataSpreadsheetsNameTable.ListRel, null); //Create and perform the query to see what is in the spreadsheet ListQuery query = new ListQuery(listFeedLink.HRef.ToString()); ListFeed feed = service.Query(query); //Get the first row of the spreadsheet ListEntry firstRow; if (feed.Entries.Count > 0) firstRow = feed.Entries[0] as ListEntry; else return false; ListEntry newRow = new ListEntry(); //Iterate through the first row elements and set the LocalNames and Values of the new cells int i = 0; foreach(ListEntry.Custom element in firstRow.Elements) { ListEntry.Custom newElement = new ListEntry.Custom(); newElement.LocalName = element.LocalName; //Fillerhack, no session or playerID known atm. if(element.LocalName == "session") { newElement.Value = "Filler"; } //^--- FIX THIS else if(i < row.Count) { newElement.Value = row[i]; i++; } Console.WriteLine("Worksheet "+worksheet.Title.Text+": Adding element in column " + element.LocalName + " with value " + newElement.Value); newRow.Elements.Add(newElement); } //Try to insert the row ListEntry insertedRow = feed.Insert(newRow) as ListEntry; return true; } } }