Sample Code For Use In Embedding Event Retrieval Interface
Java
Thank you to David Park for this code sample.
A sample class that puts together interface variables and fetches from the Event Retrieval
/*
* CalendarFetch.java
*
* Created on April 22, 2004, 9:05 AM
*
* Sample class utilizing the Sundial interface.
*
* PLEASE NOTE: the basic functionality of this class is sound, but it has not
* been thoroughly tested. Please test the vitality of any calendar retrieval
* variables you plan to use before deploying to a production environment.
*
*/
package edu.columbia.udar.calendar;
import java.io.*;
import java.util.*;
import java.net.*;
/**
* @author David Park (djp22@columbia.edu); updated for Sundial by jeng
*/
public class CalendarFetch {
static final String calendarUrl = "https://calendar.columbia.edu/sundial/webapi/get.php?";
// Criteria Vals
private String dateEnd;
private String dateStart;
private String dateUpdated;
private String keyword;
private ArrayList typeCodes;
// Presentation Vars
private String context;
private String maxEvents;
private String returnType;
// Branding override
private String footer;
private String header;
private String noRecords;
/** Creates a new instance of CalendarFetch */
public CalendarFetch() {
// desired default values for this implementation
context = "embedded";
dateStart = "";
dateEnd = "";
dateUpdated = "";
footer = "";
header = "";
keyword = "";
noRecords = "";
typeCodes = new ArrayList();
}
/** Retrieves calendar events using class variables **/
public String fetchFromCalendar(){
StringBuffer fetched = new StringBuffer();
StringBuffer queryString = new StringBuffer("?");
StringBuffer types = new StringBuffer();
//build comma-separated strings of type codes and locationIDs
for (Iterator it = typeCodes.iterator(); it.hasNext(); ){
types.append((String)it.next());
if (it.hasNext()) types.append(",");
}
queryString.append("dateStart=" + dateStart);
queryString.append("&dateEnd=" + dateEnd);
queryString.append("&dateUpdated=" + dateUpdated);
queryString.append("&keyword=" + keyword);
queryString.append("&type=" + types.toString());
queryString.append("&context=" + context);
queryString.append("&maxEvents=" + maxEvents);
queryString.append("&returnType=" + returnType);
queryString.append("&noRecords=" + noRecords);
queryString.append("&footer=" + footer);
queryString.append("&header=" + header);
try {
URL calendar = new URL(calendarUrl + queryString.toString());
//get reader for url and add results to buffer
BufferedReader in = new BufferedReader(new InputStreamReader(calendar.openStream()));
String line;
while ((line = in.readLine()) != null){
fetched.append(line);
}
in.close();
}
catch (Exception e){
fetched.append("Error while retrieving events: " + e.getMessage());
}
//return the buffer as a string
return fetched.toString();
}
/* get/set methods for class variables like a good, lil' Java bean. */
......
}
|
A sample JSP page that uses the above sample class to put straight to a page.
<%@page contentType="text/html" import="java.util.*"%>
<jsp:useBean id="fetcher" scope="request" class="edu.columbia.udar.calendar.CalendarFetch" />
<% //set some fo the fetcher's properties, then put it to work
ArrayList types = new ArrayList();
types.add("cu250_lecture");
types.add("cu250_on_tour");
fetcher.setTypeCodes(types);
fetcher.setContext("embedded");
fetcher.setDateEnd("R90"); //90 days from now
String events = fetcher.fetchFromCalendar();
%>
<html>
<head>
<title>Sundial Example With JSP</title>
</head>
<body>
<div style="width: 740px;">
<h1 class="alumniTitle">A Sampling of Events</h1>
<%= events %>
</div>
</body>
</html>
|
Perl
The following Perl program is an example of a standalone application and demonstrates how one might use Sundial's HTTP Event Retrieval Interface to obtain event information. Note that it relies upon the LWP and URI CPAN Packages.
#!/usr/bin/perl
#LWP package is needed for all things web.
#if you don't have it, CPAN will take care of you
use LWP;
#URI::Escape is needed to url encode strings
use URI::Escape;
#use strict to keep things clean
use strict;
my $ua; #user agent object
my $agentString; #the user agent string
my $url; #the url to be used in our request
my $req; #the request object passed to $ua
my $res; #result of the request
my %criteria; #hash of possible calendar parameters
#set this string to whatever you'd like to appear in the server logs
$agentString = "Columbia College Information Technology";
# Sundial's HTTP Event Retrieval Interface URI
$url = "https://calendar.columbia.edu/sundial/webapi/get.php?";
%criteria = ( "dateStart" => "11-01-2004",
"dateEnd" => "R720",
"returnType" => "",
"dateUpdated" => "",
"maxEvents" => 4,
);
#formulate our get string from our criteria
foreach my $key (keys %criteria) {
$url .= $key . "=" . uri_escape($criteria{$key}) . "&";
}
#instantiate new useragent;
$ua = LWP::UserAgent->new;
#set UserAgent String
$ua->agent($agentString);
#create request
$req = HTTP::Request->new(GET => $url);
#send request
$res = $ua->request($req);
# Check the outcome of the response
if ($res->is_success) {
print $res->content;
} else {
print "Bad luck this time\n";
}
|
PHP
The following bit of PHP code provides an example of how one might embed the calendar's output within one's web page.
<?php
// Sundial's HTTP Event Retrieval Interface URI
$url = "https://calendar.columbia.edu/sundial/webapi/get.php?";
// establish the criteria we're going to use in retrieving events.
// in this case we're requesting all events from now onwards for the
// next 720 days, but no more than the first 100 events are to be
// returned
$criteria = array("dateEnd" => "R30",
"maxEvents" => "10",
"viewType" => "list"
);
// formulate our get string from our criteria
// of course, we're going to urlencode the criteria values
foreach ($criteria as $key => $value) {
$url .= "&" . $key . "=" . urlencode($value);
}
// execute the request and write the output returned from the calendar
// direct to the output buffer
readfile($url);
?>
|
Revised: May 07, 2009
