PageRenderTime 40ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/src/ucmsite/pagegeneration/GoogleAccessor.java

https://bitbucket.org/leebeckman/ucmubcae
Java | 340 lines | 281 code | 59 blank | 0 comment | 30 complexity | fa0b768402fdba053d025ca95b96adce MD5 | raw file
  1. package ucmsite.pagegeneration;
  2. import java.io.ByteArrayInputStream;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.File;
  6. import java.net.MalformedURLException;
  7. import java.net.URL;
  8. import java.util.HashMap;
  9. import java.util.Iterator;
  10. import java.util.LinkedList;
  11. import java.util.List;
  12. import java.util.Set;
  13. import javax.swing.text.rtf.RTFEditorKit;
  14. import org.apache.poi.hwpf.extractor.WordExtractor;
  15. import ucmsite.util.AppEngineLogger;
  16. import com.google.appengine.repackaged.com.google.common.base.genfiles.ByteArray;
  17. import com.google.gdata.client.DocumentQuery;
  18. import com.google.gdata.client.GoogleService;
  19. import com.google.gdata.client.calendar.CalendarService;
  20. import com.google.gdata.client.media.MediaService;
  21. import com.google.gdata.client.spreadsheet.SpreadsheetService;
  22. import com.google.gdata.client.photos.*;
  23. import com.google.gdata.data.Category;
  24. import com.google.gdata.data.Link;
  25. import com.google.gdata.data.MediaContent;
  26. import com.google.gdata.data.media.IMediaContent;
  27. import com.google.gdata.data.media.MediaSource;
  28. import com.google.gdata.data.photos.*;
  29. import com.google.gdata.data.spreadsheet.CellEntry;
  30. import com.google.gdata.data.spreadsheet.CellFeed;
  31. import com.google.gdata.data.spreadsheet.SpreadsheetEntry;
  32. import com.google.gdata.data.spreadsheet.SpreadsheetFeed;
  33. import com.google.gdata.data.spreadsheet.WorksheetEntry;
  34. import com.google.gdata.client.docs.DocsService;
  35. import com.google.gdata.data.docs.DocumentEntry;
  36. import com.google.gdata.data.docs.DocumentListEntry;
  37. import com.google.gdata.data.docs.DocumentListFeed;
  38. import com.google.gdata.data.docs.FolderEntry;
  39. import com.google.gdata.util.AuthenticationException;
  40. import com.google.gdata.util.ContentType;
  41. import com.google.gdata.util.ServiceException;
  42. public class GoogleAccessor {
  43. private static GoogleAccessor self = null;
  44. private SpreadsheetService sheetService;
  45. private CalendarService calendarService;
  46. private GoogleService docService;
  47. private PicasawebService picService;
  48. private String username;
  49. private HashMap<String, UserFeed> albumMap;
  50. private HashMap<String, AlbumFeed> picMap;
  51. private GoogleAccessor(String password, String username) {
  52. try {
  53. sheetService = new SpreadsheetService("ucmubc");
  54. sheetService.setUserCredentials(username, password);
  55. calendarService = new CalendarService("ucmubc");
  56. calendarService.setUserCredentials(username, password);
  57. docService = new DocsService("ucmubc");
  58. docService.setUserCredentials(username, password);
  59. picService = new PicasawebService("ucmubc");
  60. picService.setUserCredentials(username, password);
  61. this.username = username.split("@")[0];
  62. albumMap = new HashMap<String, UserFeed>();
  63. picMap = new HashMap<String, AlbumFeed>();
  64. } catch (AuthenticationException e) {
  65. e.printStackTrace();
  66. }
  67. }
  68. public static GoogleAccessor getGoogleAccessor(String password, String username) {
  69. if (self == null) {
  70. self = new GoogleAccessor(password, username);
  71. }
  72. return self;
  73. }
  74. public static GoogleAccessor getGoogleAccessor() {
  75. return self;
  76. }
  77. public List<DocumentListEntry> getFolderDocuments(List<String> folders) {
  78. while(true) {
  79. try {
  80. URL docURI = new URL("http://docs.google.com/feeds/documents/private/full/");
  81. DocumentListFeed docFeed = docService.getFeed(docURI, DocumentListFeed.class);
  82. List<DocumentListEntry> docEntries = docFeed.getEntries();
  83. List<DocumentListEntry> removeEntries = new LinkedList<DocumentListEntry>();
  84. valid:
  85. for(DocumentListEntry entry : docEntries) {
  86. for(Link folder : entry.getParentLinks()) {
  87. if (folders.contains(folder.getTitle().trim())) {
  88. continue valid;
  89. }
  90. }
  91. removeEntries.add(entry);
  92. }
  93. for(DocumentListEntry entry : removeEntries) {
  94. docEntries.remove(entry);
  95. }
  96. return docEntries;
  97. } catch (java.io.IOException e) {
  98. }
  99. catch (Exception e) {
  100. return null;
  101. }
  102. }
  103. }
  104. public InputStream getXmlDocument(DocumentListEntry document) {
  105. while(true) {
  106. try {
  107. String docResId = document.getResourceId();
  108. String docId = docResId.substring(docResId.lastIndexOf(":") + 1);
  109. String type = "doc";
  110. String exportUrl = "https://docs.google.com/feeds/download/documents/Export?docId=" +
  111. docId + "&exportFormat=" + type;
  112. MediaContent mc = new MediaContent();
  113. mc.setUri(exportUrl);
  114. mc.setMimeType(ContentType.TEXT_XML);
  115. docService.setConnectTimeout(0);
  116. MediaSource ms = ((MediaService) docService).getMedia(mc);
  117. InputStream inStream = null;
  118. inStream = ms.getInputStream();
  119. WordExtractor extractor = new WordExtractor(inStream);
  120. String extractStr = extractor.getText().replaceAll("[^\\p{Print}]", "");
  121. InputStream returnStream = new ByteArrayInputStream(extractStr.getBytes());
  122. return returnStream;
  123. } catch (java.io.IOException e) {
  124. }
  125. catch (Exception e) {
  126. return null;
  127. }
  128. }
  129. }
  130. public InputStream getDocument(String foldername, String title) {
  131. while(true) {
  132. try {
  133. URL docURI = new URL("http://docs.google.com/feeds/documents/private/full/?category=" + foldername);
  134. DocumentListFeed docFeed = docService.getFeed(docURI, DocumentListFeed.class);
  135. List<DocumentListEntry> docEntries = docFeed.getEntries();
  136. DocumentListEntry docEntry = null;
  137. found:
  138. for (int i = 0; i < docEntries.size(); i++) {
  139. docEntry = docEntries.get(i);
  140. String test = docEntry.getUpdated().toString();
  141. if (docEntry.getTitle().getPlainText().trim().equals(title)) {
  142. break found;
  143. }
  144. }
  145. String docResId = docEntry.getResourceId();
  146. String docId = docResId.substring(docResId.lastIndexOf(":") + 1);
  147. String type = "doc";
  148. String exportUrl = "https://docs.google.com/feeds/download/documents/Export?docId=" +
  149. docId + "&exportFormat=" + type;
  150. MediaContent mc = new MediaContent();
  151. mc.setUri(exportUrl);
  152. mc.setMimeType(ContentType.TEXT_XML);
  153. docService.setConnectTimeout(0);
  154. MediaSource ms = ((MediaService) docService).getMedia(mc);
  155. InputStream inStream = null;
  156. inStream = ms.getInputStream();
  157. WordExtractor extractor = new WordExtractor(inStream);
  158. String extractStr = extractor.getText().replaceAll("[^\\p{Print}]", "");
  159. InputStream returnStream = new ByteArrayInputStream(extractStr.getBytes());
  160. return returnStream;
  161. } catch (java.io.IOException e) {
  162. }
  163. catch (Exception e) {
  164. return null;
  165. }
  166. }
  167. }
  168. public void resetPhotoMaps() {
  169. albumMap.clear();
  170. picMap.clear();
  171. }
  172. public String getPicasaURL(String picasaSrc) {
  173. try {
  174. String album = picasaSrc.split("/")[0];
  175. String title = picasaSrc.split("/")[1];
  176. String albumStr = "http://picasaweb.google.com/data/feed/api/user/" + username + "/?kind=album";
  177. URL albumUrl = new URL(albumStr);
  178. UserFeed afeed = albumMap.get(albumStr);
  179. if (afeed == null) {
  180. afeed = picService.getFeed(albumUrl, UserFeed.class);
  181. albumMap.put(albumStr, afeed);
  182. }
  183. String albumid = null;
  184. for(AlbumEntry alb : afeed.getAlbumEntries()) {
  185. if (alb.getTitle().getPlainText().trim().equals(album)) {
  186. int slashpos = alb.getId().lastIndexOf("/");
  187. albumid = alb.getId().substring(slashpos + 1);
  188. break;
  189. }
  190. }
  191. String picStr = "http://picasaweb.google.com/data/feed/api/user/" + username + "/albumid/" + albumid;
  192. URL picURL = new URL(picStr);
  193. AlbumFeed pfeed = picMap.get(picStr);
  194. if (pfeed == null) {
  195. pfeed = picService.getFeed(picURL, AlbumFeed.class);
  196. picMap.put(picStr, pfeed);
  197. }
  198. for(PhotoEntry photo : pfeed.getPhotoEntries()) {
  199. if (photo.getTitle().getPlainText().trim().equals(title)) {
  200. return photo.getMediaContents().get(0).getUrl();
  201. }
  202. }
  203. return null;
  204. } catch (Exception e) {
  205. e.printStackTrace();
  206. return null;
  207. }
  208. }
  209. public String getFormAddress(String title) {
  210. try {
  211. URL sheetURL = new URL("https://spreadsheets.google.com/feeds/spreadsheets/private/full");
  212. SpreadsheetFeed tableFeed = sheetService.getFeed(sheetURL, SpreadsheetFeed.class);
  213. List<SpreadsheetEntry> tables = tableFeed.getEntries();
  214. SpreadsheetEntry entry = null;
  215. for (int i = 0; i < tables.size(); i++) {
  216. entry = tables.get(i);
  217. if (entry.getTitle().getPlainText().trim().equals(title))
  218. break;
  219. }
  220. return entry.getKey();
  221. } catch (Exception e) {
  222. return null;
  223. }
  224. }
  225. public Iterator<SpreadSheet> getSpreadSheet(String title, String tabtitle) {
  226. try {
  227. URL sheetURL = new URL("https://spreadsheets.google.com/feeds/spreadsheets/private/full");
  228. SpreadsheetFeed tableFeed = sheetService.getFeed(sheetURL, SpreadsheetFeed.class);
  229. List<SpreadsheetEntry> tables = tableFeed.getEntries();
  230. SpreadsheetEntry entry = null;
  231. for (int i = 0; i < tables.size(); i++) {
  232. entry = tables.get(i);
  233. if (entry.getTitle().getPlainText().trim().equals(title))
  234. break;
  235. }
  236. Iterator workSheets = entry.getWorksheets().iterator();
  237. List<SpreadSheet> returnSheets = new LinkedList<SpreadSheet>();
  238. while (workSheets.hasNext()) {
  239. WorksheetEntry worksheetEntry = (WorksheetEntry) workSheets.next();
  240. String workSheetName = worksheetEntry.getTitle().getPlainText().trim();
  241. if (tabtitle != null && !workSheetName.equals(tabtitle))
  242. continue;
  243. URL cellFeedUrl = worksheetEntry.getCellFeedUrl();
  244. CellFeed cellfeed = sheetService.getFeed(cellFeedUrl, CellFeed.class);
  245. LinkedList<Iterator<String>> rows = new LinkedList<Iterator<String>>();
  246. int lastRow = 0;
  247. LinkedList<String> cols = new LinkedList<String>();
  248. for (CellEntry cell : cellfeed.getEntries()) {
  249. if (cell.getCell().getRow() != lastRow && lastRow != 0) {
  250. rows.add(cols.iterator());
  251. cols = new LinkedList<String>();
  252. cols.add(cell.getCell().getValue());
  253. }
  254. else {
  255. cols.add(cell.getCell().getValue());
  256. }
  257. lastRow = cell.getCell().getRow();
  258. }
  259. rows.add(cols.iterator());
  260. returnSheets.add(new SpreadSheet(workSheetName, rows.iterator()));
  261. }
  262. return returnSheets.iterator();
  263. } catch (Exception e) {
  264. return null;
  265. }
  266. }
  267. public class SpreadSheet {
  268. String name = null;
  269. Iterator sheet = null;
  270. public SpreadSheet(String sname, Iterator ssheet) {
  271. name = sname;
  272. sheet = ssheet;
  273. }
  274. public String getName() {
  275. return name;
  276. }
  277. public Iterator<Iterator<String>> getSheet() {
  278. return sheet;
  279. }
  280. }
  281. }