Skip to content

Commit

Permalink
Added sample application
Browse files Browse the repository at this point in the history
  • Loading branch information
dscape committed Mar 16, 2011
1 parent cf4fed0 commit 215ce4f
Show file tree
Hide file tree
Showing 46 changed files with 1,683 additions and 0 deletions.
15 changes: 15 additions & 0 deletions samples/world-leaders/README.md
@@ -0,0 +1,15 @@
# Readme
This work was done during one of the XQuery Basics Classes where customers asked for a demonstration of REST. None of the files was renamed and no code changes were needed to use rewrite.

It does a lazy loading of files, so they will be loaded the first time you call the application.

# Install
Simple create an HTTP App Server pointing to this directory and make rewrite.xqy the rewrite script for that App Server.

# Extra
Added a route to access countries by name, e.g. http://localhost/country/Argentina for demonstration purposes. It's not linked in the application but you can access it directly by URL.

# MarkLogic University
This application was developed as part of [MarkLogic University][1]. If you want to learn how it was built register in the XQuery Basics class. This is copyrighted by MarkLogic University and ok to use only for the purposes of this demonstration. Further inquiries can be sent to training at marklogic dot com.

[1]: http://www.marklogic.com/services/training.html
57 changes: 57 additions & 0 deletions samples/world-leaders/bycountry.xqy
@@ -0,0 +1,57 @@
xquery version "1.0-ml";

declare namespace wc = "http://marklogic.com/mlu/world-leaders" ;

import module namespace h = "helper.xqy" at "helper.xqy" ;

declare variable $country :=
xdmp:get-request-field( 'country', 'all')
;

xdmp:set-response-content-type("text/html; charset=utf-8"),
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">',
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>World Leaders</title>
<link href="/css/world-leaders.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div id="wrapper">
<a href="/"><img src="/images/logo.gif" width="427" height="76" /></a><br />
<span class="currently">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Currently in database: { h:total-leaders() }</span><br />
<br />
<br />
<br />
{ h:tabs( "country" ) }
{ h:greybar(
<form name="formcountry" method="get" action="/country" id="formcountry">
<p>Choose a country: </p>
<select name="country" id="country">
<option value="all">all</option>
{
for $opt in fn:distinct-values( //wc:country )
order by $opt
return <option value="{$opt}">
{ if($country=$opt)
then attribute selected { "selected" }
else (),
$opt}</option>
}
</select>
<input type="submit" value="go"/>
</form> ) }
<div id="content">
{
let $leaders :=
for $leader in /wc:leader
where $country = 'all' or $leader//wc:country = $country
order by $leader//wc:firstname
return $leader
return h:table( $leaders ) }
</div>
</div>
</body>
</html>
64 changes: 64 additions & 0 deletions samples/world-leaders/css/world-leaders.css
@@ -0,0 +1,64 @@
body {
background-color: #444;
font-family: Arial, Helvetica, sans-serif;
font-size: 13px;
margin: 30px;
}
#wrapper {
border: 6px solid #50AAE9;
width: 950px;
background-color: #1464AD;
background-image: url(../images/background.gif);
padding: 30px;
margin: auto;
}
.currently {
color: #9CCAF3;
}
#tabs {
width: 950px;
height: 30px;
}
img {
border-top-width: 0px;
border-right-width: 0px;
border-bottom-width: 0px;
border-left-width: 0px;
}

#graybar {
background-color: #999;
width: 950px;
padding-top: 10px;
padding-bottom: 10px;
}
#content {
background-color: #CCC;
width: 950px;
}

table {
border: 29px solid #ccc;
width: 100%;
}
th {
font-size: 12px;
color: #666;
text-align: left;
}

td {
color: #333;
font-size: 12px;
}
.summary {
font-size: 11px;
line-height: 16px;
padding-top: 6px;
padding-bottom: 2px;
}
form {
padding-top: 12px;
padding-bottom: 12px;
padding-left: 30px;
}
81 changes: 81 additions & 0 deletions samples/world-leaders/helper.xqy
@@ -0,0 +1,81 @@
module namespace h = "helper.xqy"
;

declare namespace wc = "http://marklogic.com/mlu/world-leaders"
;

declare function h:total-leaders() {
xdmp:estimate( /wc:leader ) } ;

declare function h:greybar() {
h:greybar( () ) };

declare function h:greybar( $form ) {
<div id="graybar"> { $form } </div>
};

declare function h:table () {
h:table( /wc:leader ) } ;

declare function h:table( $leaders ) {
<table cellspacing="0">
<tr>
<th>F Name</th>
<th>L Name</th>
<th>Country</th>
<th>Title</th>
<th>H of State</th>
<th><p>H of Govt</p></th>
<th>Start Date</th>
<th>End Date</th>
<th>Age</th>
<th>Gender</th>
</tr>
<tr>
<td colspan="10"><hr/></td>
</tr>
{ for $leader in $leaders
let $firstName := $leader //wc:firstname /fn:string()
let $lastName := fn:string( $leader //wc:lastname )
let $country := $leader //wc:country /fn:string()
let $currentPosition := $leader //wc:position [1]
let $title := $currentPosition /wc:title /fn:string()
let $startDate := $currentPosition /wc:startdate /fn:string()
let $endDate := $currentPosition /wc:enddate /fn:string()
let $hos := $currentPosition /@hos /fn:string()
let $hog := $currentPosition /@hog /fn:string()
let $gender := $leader //wc:gender /fn:string()
let $age := $leader //wc:dob /fn:string()
let $description := $leader //wc:summary /fn:string()
let $hos-color := if ($hos) then "#689747" else "#C40E1C"
let $hog-color := if ($hog) then "#689747" else "#C40E1C"
return (
<tr>
<td><b>{$firstName}</b></td>
<td><b>{$lastName}</b></td>
<td>{$country}</td>
<td>{$title}</td>
<td style="background:{$hos-color}; color:{$hos-color}">{$hos}</td>
<td style="background:{$hog-color}; color:{$hog-color}">{$hog}</td>
<td>{$startDate}</td>
<td>{$endDate}</td>
<td>{$age}</td>
<td>{$gender}</td>
</tr> ,
<tr>
<td colspan="10" class="summary">{$description}</td>
</tr> ) }
</table>
} ;

declare function h:tabs( $page ) {
let $iSel := if ( $page = 'index' ) then "_selected" else ()
let $cSel := if ( $page = 'country' ) then "_selected" else ()
let $dSel := if ( $page = 'date' ) then "_selected" else ()
let $sSel := if ( $page = 'search' ) then "_selected" else ()
return <div id="tabs">
<a href="/"><img src="/images/byname{$iSel}.gif" width="121" height="30"/></a>
<a href="/country"><img src="/images/bycountry{$cSel}.gif" width="121" height="30" /></a>
<a href="/search"><img src="/images/search{$sSel}.gif" width="121" height="30" /></a>
</div>
};
Binary file added samples/world-leaders/images/background.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added samples/world-leaders/images/bycountry.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added samples/world-leaders/images/bycountry_selected.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added samples/world-leaders/images/bydate.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added samples/world-leaders/images/bydate_selected.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added samples/world-leaders/images/byname.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added samples/world-leaders/images/byname_selected.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added samples/world-leaders/images/graybar.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added samples/world-leaders/images/logo.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added samples/world-leaders/images/logo_r1_c2.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added samples/world-leaders/images/search.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added samples/world-leaders/images/search_selected.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions samples/world-leaders/index.xqy
@@ -0,0 +1,31 @@
xquery version "1.0-ml";

declare namespace wc = "http://marklogic.com/mlu/world-leaders" ;

import module namespace h = "helper.xqy" at "helper.xqy" ;

xdmp:set-response-content-type("text/html; charset=utf-8"),
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">',
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>World Leaders</title>
<link href="/css/world-leaders.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div id="wrapper">
<a href="/"><img src="/images/logo.gif" width="427" height="76" /></a><br />
<span class="currently">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Currently in database: { h:total-leaders() }</span><br />
<br />
<br />
<br />
{ h:tabs( "index" ) }
{ h:greybar( ) }
<div id="content">
{ h:table() }
</div>
</div>
</body>
</html>

0 comments on commit 215ce4f

Please sign in to comment.