📄 eventpublisher.java
字号:
} catch (com.google.gdata.util.AuthenticationException authEx) { throw new EPAuthenticationException("Bad calendar credentials"); } this.baseService = baseService; return baseService; } else { return this.baseService; } } /** * Takes the event objects passed and publishes each of them to Google Base * * @param eventList The list of <code>Event</code>s to publish */ public void publishEventsToBase(List<Event> eventList) { Iterator<Event> i = eventList.iterator(); while (i.hasNext()) { Event event = i.next(); try { publishEventToBase(event); } catch (EPAuthenticationException e) { System.err.println("Authentication problem when publishing events: " + e.getMessage()); } catch (IOException e) { System.err.println("IOException when publishing events: " + e.getMessage()); } catch (ServiceException e) { e.printStackTrace(); System.err.println("ServiceException when publishing events: " + e.getMessage()); } } } /** * Takes the event objects passed and publishes each of them to Calendar * * @param eventList The list of <code>Event</code>s to publish */ public void publishEventsToCalendar(List<Event> eventList) { Iterator<Event> i = eventList.iterator(); while (i.hasNext()) { Event event = i.next(); try { publishEventToCalendar(event); } catch (EPAuthenticationException e) { System.err.println("Authentication problem when publishing events: " + e.getMessage()); } catch (IOException e) { System.err.println("IOException when publishing events: " + e.getMessage()); } catch (ServiceException e) { e.printStackTrace(); System.err.println("ServiceException when publishing events: " + e.getMessage()); } } } /** * Publishes an individual event to Calendar * * @param event The <code>Event</code> to publish * @throws EPAuthenticationException * @throws IOException * @throws ServiceException */ private void publishEventToCalendar(Event event) throws EPAuthenticationException, IOException, ServiceException { CalendarService calService = getCalService(); CalendarEventEntry entry = null; if (event.getCalendarUrl() != null) { // updating event entry = calService.getEntry(event.getCalendarUrl(), CalendarEventEntry.class); } else { // publishing new event entry = new CalendarEventEntry(); } // set data on event entry.setTitle(new PlainTextConstruct(event.getTitle())); entry.setContent(new PlainTextConstruct(event.getDescription())); When when = new When(); DateTime startDateTime = new DateTime(event.getStartDate()); startDateTime.setDateOnly(true); // we must add 1 day to the event as the end-date is exclusive Calendar endDateCal = new GregorianCalendar(); endDateCal.setTime(event.getEndDate()); endDateCal.add(Calendar.DATE, 1); DateTime endDateTime = new DateTime(endDateCal.getTime()); endDateTime.setDateOnly(true); when.setStartTime(startDateTime); when.setEndTime(endDateTime); entry.getTimes().add(when); if (event.getCalendarUrl() != null) { // updating event entry.update(); } else { // insert event CalendarEventEntry resultEntry = calService.insert(calUrl, entry); updateSsEventEditUrl(event.getSsEditUrl(), resultEntry.getEditLink().getHref(), null); } } /** * Publishes an individual event to Base * * @param event The <code>Event</code> to publish * @throws EPAuthenticationException * @throws IOException * @throws ServiceException */ private void publishEventToBase(Event event) throws EPAuthenticationException, IOException, ServiceException { GoogleBaseService baseService = getBaseService(); GoogleBaseEntry entry = null; if (event.getBaseUrl() != null) { // updating base entry entry = baseService.getEntry(event.getBaseUrl(), GoogleBaseEntry.class); entry = new GoogleBaseEntry(); } else { // publishing new entry entry = new GoogleBaseEntry(); } // prepare an 'events and activities' item for publishing GoogleBaseAttributesExtension gbaseAttributes = entry.getGoogleBaseAttributes(); entry.setTitle(new PlainTextConstruct(event.getTitle())); entry.setContent(new PlainTextConstruct(event.getDescription())); gbaseAttributes.setItemType("events and activities"); // this sample currently only demonstrates publishing all-day events // an event in Google Base must have a start-time and end-time, so // this simulates that by adding 1 day to the end-date specified, if the // start and end times are identical DateTime startDateTime = new DateTime(event.getStartDate()); startDateTime.setDateOnly(true); DateTime endDateTime = null; if (event.getStartDate().equals(event.getEndDate())) { Calendar endDateCal = new GregorianCalendar(); endDateCal.setTime(event.getEndDate()); endDateCal.add(Calendar.DATE, 1); endDateTime = new DateTime(endDateCal.getTime()); } else { endDateTime = new DateTime(event.getEndDate()); } endDateTime.setDateOnly(true); gbaseAttributes.addDateTimeRangeAttribute("event date range", new DateTimeRange(startDateTime, endDateTime)); gbaseAttributes.addTextAttribute("event performer", "Google mashup test"); gbaseAttributes.addUrlAttribute("performer url", "http://code.google.com/apis/gdata.html"); if (event.getBaseUrl() != null) { // updating event baseService.update(event.getBaseUrl(), entry); } else { // insert event GoogleBaseEntry resultEntry = baseService.insert( FeedURLFactory.getDefault().getItemsFeedURL(), entry); updateSsEventEditUrl(event.getSsEditUrl(), null, resultEntry.getEditLink().getHref() ); }} /** * Updates the Google Base and/or Calendar edit URLs in the spreadsheet. * The storage of these edit URLs enables further runs of this code to * publish events as new Calendar and Base entries only if the events had * not been previously published * * @param ssEditUrl The edit <code>URL</code> for the spreadsheet * @param calEditUrl The edit URL to be set in the spreadsheet or null * @param baseEditUrl The edit URL to be set in the spreadsheet or null * @throws EPAuthenticationException * @throws IOException * @throws ServiceException */ private void updateSsEventEditUrl(URL ssEditUrl, String calEditUrl, String baseEditUrl) throws EPAuthenticationException, IOException, ServiceException { SpreadsheetService ssService = getSsService(); ListEntry ssEntry = ssService.getEntry(ssEditUrl, ListEntry.class); // remove spaces in the URL String calUrlFieldName = fieldMap.getCalendarUrlColumn(); if (calEditUrl == null && (ssEntry.getCustomElements().getValue(calUrlFieldName) == null || "".equals(ssEntry.getCustomElements().getValue(calUrlFieldName)))){ calEditUrl = " "; } if (calEditUrl != null) { ssEntry.getCustomElements().setValueLocal( calUrlFieldName, calEditUrl); } // remove spaces in the URL String baseUrlFieldName = fieldMap.getBaseUrlColumn(); if (baseEditUrl == null && (ssEntry.getCustomElements().getValue(baseUrlFieldName) == null || "".equals(ssEntry.getCustomElements().getValue(baseUrlFieldName)))){ baseEditUrl = " "; } if (baseEditUrl != null) { ssEntry.getCustomElements().setValueLocal( baseUrlFieldName, baseEditUrl); } ssEntry.update(); } /** * Retrieves a <code>List</code> of <code>SpreadsheetEntry</code> instances * available from the list of Spreadsheets accessible from the authenticated * account. * * @throws EPAuthenticationException */ private List<SpreadsheetEntry> getSsListHelper() throws EPAuthenticationException { List<SpreadsheetEntry> returnList = null; try { SpreadsheetService ssService = getSsService(); SpreadsheetFeed ssFeed = ssService.getFeed( new URL(SPREADSHEETS_META_FEED), SpreadsheetFeed.class); returnList = ssFeed.getEntries(); } catch (com.google.gdata.util.AuthenticationException authEx) { throw new EPAuthenticationException( "SS list read access not available"); } catch (com.google.gdata.util.ServiceException svcex) { System.err.println("ServiceException while retrieving " + "available spreadsheets: " + svcex.getMessage()); returnList = null; } catch (java.io.IOException ioex) { System.err.println("IOException while retrieving " + "available spreadsheets: " + ioex.getMessage()); returnList = null; } return returnList; } /** * Retrieves a <code>List</code> of <code>WorksheetEntry</code> instances * available from the list of Worksheets in the specified feed * * @param wsFeedUrl The feed of worksheets * @throws EPAuthenticationException * @return List of worksheets in the specified feed */ private List<WorksheetEntry> getWsListHelper(String wsFeedUrl) throws EPAuthenticationException { List<WorksheetEntry> returnList = null; try { SpreadsheetService ssService = getSsService(); WorksheetFeed wsFeed = ssService.getFeed( new URL(wsFeedUrl), WorksheetFeed.class); returnList = wsFeed.getEntries(); } catch (com.google.gdata.util.AuthenticationException authEx) { throw new EPAuthenticationException( "WS list read access not available"); } catch (com.google.gdata.util.ServiceException svcex) { System.err.println("ServiceException while retrieving " + "available worksheets: " + svcex.getMessage()); returnList = null; } catch (java.io.IOException ioex) { System.err.println("IOException while retrieving " + "available worksheets: " + ioex.getMessage()); returnList = null; } return returnList; } /** * Retrieves a list of column headers in the specified cell feed * * @param cellFeedUrl The cell feed * @return <code>List</code> of column headers as <code>String</code>s * @throws EPAuthenticationException */ public List<String> getColumnList(String cellFeedUrl) throws EPAuthenticationException { List<String> returnList = new LinkedList<String>(); List<CellEntry> columnList = getColumnListHelper(cellFeedUrl); for (CellEntry columnEntry : columnList) { returnList.add(columnEntry.getCell().getValue()); } return returnList; } /** * Retrieves a list of column headers in the specified cell feed * * @param cellFeedUrl The cell feed * @return <code>List</code> of column headers as <code>CellEntry</code>s * @trhows EPAuthenticationException */ private List<CellEntry> getColumnListHelper(String cellFeedUrl) throws EPAuthenticationException { List<CellEntry> returnList = null; try { SpreadsheetService ssService = getSsService(); CellFeed cellFeed = ssService.getFeed( new URL(cellFeedUrl + "?min-row=1&max-row=1"), CellFeed.class); returnList = cellFeed.getEntries(); } catch (com.google.gdata.util.AuthenticationException authEx) { throw new EPAuthenticationException( "SS read access not available"); } catch (com.google.gdata.util.ServiceException svcex) { // log general service exception System.err.println("ServiceException while retrieving " + "column list: " + svcex.getMessage()); returnList = null; } catch (java.io.IOException ioex) { System.err.println("IOException while retrieving " + "column list: " + ioex.getMessage()); returnList = null; } return returnList; } /** * Retrieves all <code>ListEntry</code> objects from the spreadsheet * * @return <code>List</code> of <code>ListEntry</code> instances * @throws EPAuthenticationException */ private List<ListEntry> getSsEntryListHelper() throws EPAuthenticationException { List<ListEntry> returnList = null; try { SpreadsheetService ssService = getSsService(); ListFeed listFeed = ssService.getFeed(ssUrl, ListFeed.class); returnList = listFeed.getEntries(); } catch (com.google.gdata.util.AuthenticationException authEx) { throw new EPAuthenticationException("SS read access not available"); } catch (com.google.gdata.util.ServiceException svcex) { System.err.println("ServiceException while retrieving " + "entry list: " + svcex.getMessage()); returnList = null; } catch (java.io.IOException ioex) { System.err.println("IOException while retrieving " + "entry list: " + ioex.getMessage()); returnList = null; } return returnList; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -