source: Dev/LoggingTool/interfaces/TeamUpSpreadsheetOutput.cs @ 5

Last change on this file since 5 was 5, checked in by jkraaijeveld, 14 years ago
File size: 6.6 KB
Line 
1using System;
2using Google.GData.Client;
3using Google.GData.Extensions;
4using Google.GData.Spreadsheets;
5using System.Collections;
6using LoggingTool.models;
7using System.Collections.Generic;
8
9namespace LoggingTool.interfaces
10{
11    class TeamUpSpeadsheetOutput : ILoggerOutput
12    {
13        private SpreadsheetsService service;
14        private SpreadsheetEntry spreadsheet;
15
16        /* ******************************************************************************
17         * constructor TeamUpSpreadsheetOutput
18         * @description: creates the implementation of the ILoggerOutput interface
19         * ******************************************************************************/
20        public TeamUpSpeadsheetOutput(String username, String password, String spreadsheet)
21        {
22            //Log in to GoogleDocs and retrieve the right spreadsheet
23            service = new SpreadsheetsService("TeamUp Logging Spreadsheet");
24            service.setUserCredentials(username, password);
25
26            //Get the correct spreadsheet
27            SpreadsheetQuery query = new SpreadsheetQuery();
28            SpreadsheetFeed feed = service.Query(query);
29 
30            //If a spreadsheet with the given name exists, make that our spreadsheet
31            foreach (SpreadsheetEntry entry in feed.Entries)
32            {
33                if(entry.Title.Text.Equals(spreadsheet))
34                    this.spreadsheet = entry;
35            }
36         
37            //Else, state the spreadsheet is invalid
38            if(spreadsheet == null)
39            {
40                //TODO: give a nice error message.
41                Console.WriteLine("Spreadsheet " + spreadsheet + " not found");
42
43            }
44        }
45
46        /* ******************************************************************************
47         * function testConnection
48         * @return: boolean
49         * @description: returns true if the connection to the outside source is working
50         * ******************************************************************************/
51        public Boolean testConnection()
52        {
53            //TODO: Actually check for a connection.
54            return true;
55        }
56
57        /* ******************************************************************************
58         * function send
59         * @param: message : Loggermessage
60         * @return: boolean
61         * @description: Decides to which worksheet the message should go and passes it
62         *                  along. Returns false if the message is not the correct type.
63         * ******************************************************************************/
64        public Boolean send(LoggerMessage message)
65        {
66            //If the message if of the correct type...
67            if (message is TeamUpSpreadsheetMessage)
68            {
69                //Cast it and check for the message type and...
70                TeamUpSpreadsheetMessage msg = (TeamUpSpreadsheetMessage)message;
71                String queryString = "";
72                if (msg.Type == TeamUpSpreadsheetMessage.QUESTIONNAIRE)
73                    queryString = "Questionnaire";
74                else if(msg.Type == TeamUpSpreadsheetMessage.EVENT)
75                    queryString = "Events";
76
77                //Get the correct worksheet to perform the operation
78                AtomLink link = this.spreadsheet.Links.FindService(GDataSpreadsheetsNameTable.WorksheetRel, null);
79                WorksheetQuery workSheetQuery = new WorksheetQuery(link.HRef.ToString());
80                WorksheetFeed workSheetFeed = service.Query(workSheetQuery);
81                WorksheetEntry worksheet = null;
82                foreach (WorksheetEntry entry in workSheetFeed.Entries)
83                {
84                    if(entry.Title.Text.Equals(queryString))
85                        worksheet = entry;
86                }
87
88                //Return if the worksheet doesn't exist.
89                if(worksheet == null)
90                    return false;
91
92                //Else: send the message
93                return sendToWorksheet(worksheet, msg.Data);
94            }
95            else
96            {
97                return false;
98            }
99           
100        }
101       
102
103        /* ******************************************************************************
104         * function sendToWorksheet
105         * @param: worksheet : WorksheetEntry
106         * @param: row : List<String>
107         * @return: boolean
108         * @description: Takes the already formatted ArrayList and sends it to the specified worksheet
109         * ******************************************************************************/
110        Boolean sendToWorksheet(WorksheetEntry worksheet, List<String> row)
111        {
112            //TODO: Actually send stuff.
113            //Get the Google Docs URL
114            AtomLink listFeedLink = worksheet.Links.FindService(GDataSpreadsheetsNameTable.ListRel, null);
115           
116            //Create and perform the query to see what is in the spreadsheet
117            ListQuery query = new ListQuery(listFeedLink.HRef.ToString());
118            ListFeed feed = service.Query(query);
119           
120            //Get the first row of the spreadsheet
121            ListEntry firstRow;
122            if (feed.Entries.Count > 0)
123                firstRow = feed.Entries[0] as ListEntry;
124            else
125                return false;
126
127            ListEntry newRow = new ListEntry();
128
129            //Iterate through the first row elements and set the LocalNames and Values of the new cells
130            int i = 0;
131            foreach(ListEntry.Custom element in firstRow.Elements)
132            {
133               
134                ListEntry.Custom newElement = new ListEntry.Custom();
135                newElement.LocalName = element.LocalName;
136               
137                //Fillerhack, no session or playerID known atm.             
138                if(element.LocalName == "session")
139                {
140                    newElement.Value = "Filler";
141                }
142                //^--- FIX THIS
143                else if(i < row.Count)
144                {
145                    newElement.Value = row[i];
146                    i++;
147                }
148                Console.WriteLine("Worksheet "+worksheet.Title.Text+": Adding element in column " + element.LocalName + " with value " + newElement.Value);
149                newRow.Elements.Add(newElement);
150            }
151
152            //Try to insert the row
153            ListEntry insertedRow = feed.Insert(newRow) as ListEntry;
154            return true;
155           
156        }
157    }
158}
Note: See TracBrowser for help on using the repository browser.