xquery web apps - marklogic developer community · pdf filexquery web apps for java...

26
XQuery Web Apps for Java Developers

Upload: duongdien

Post on 06-Feb-2018

228 views

Category:

Documents


1 download

TRANSCRIPT

XQuery Web Appsfor Java Developers

Common Java SetupInstall database (SQLServer, PostgreSQL)

Install app container (Tomcat, Websphere)

Configure ORB (Hibernate)

Configure MVC framework (Spring)

Configure Authentication (Acegi)

Configure presentation (JSF, JSP, Tapestry)

Configure search index and engine (Lucene)

Java CharacteristicsObject-oriented, uses imperative statements to change state

Java Code Example

myList.add(myObject);

fos.write(myBytes);

out.close();

panel.paint();

myThread.start();

Java CharacteristicsObject-oriented, uses imperative statements to change state

Java CharacteristicsObject-oriented, uses imperative statements to change state

Uses getters and setters to access data

Java Code Example

user.setName(“Bob”);

vector.getElementAt(i);

thing.setPath(“/a/b/c”);

request.getParameter(“search”);

Java CharacteristicsObject-oriented, uses imperative statements to change state

Uses getters and setters to access data

Connects to a DB and sends a SQL string to be interpreted

Java Code Example

connection = DriverManager.getConnection(“jdbc...”);...r = statement.executeQuery(“SELECT * FROM BOOKS WHERE...”);

Java CharacteristicsObject-oriented, uses imperative statements to change state

Uses getters and setters to access data

Connects to a DB and sends a SQL string to be interpreted

Starts and commits transactions against a DB

Java Code Example

myTransaction.start();

//do a bunch of stuff

myTransaction.commit();

Java CharacteristicsObject-oriented, uses imperative statements to change state

Uses getters and setters to access data

Connects to a DB and sends a SQL string to be interpreted

Starts and commits transactions against a DB

Uses threads and “synchronize(d)” for concurrent processing

Java Code Example

public synchronized void increment() { counter++;

}

Java CharacteristicsObject-oriented, uses imperative statements to change state

Uses getters and setters to access data

Connects to a DB and sends a SQL string to be interpreted

Starts and commits transactions against a DB

Uses threads and “synchronize(d)” for concurrent processing

Uses MVC for separation of concerns

Almost everything is an object

XQuery App StrategiesFramework

Let MarkLogic fill the framework space

Write mostly XML to HTML translations

Put reusable and common code in library modules

Run the app as a restricted user

Let MarkLogic handle transactions

Don’t get too fancy

XQuery Code Example

<book> <isbn>123456789</isbn> <title>Inferno</title> <author>Dante</author></book>

<select name="book"> <option value="123456789">Inferno by Dante</option> <option value="987654321">Republic by Plato</option> <option value="123432187">Gallic Wars by Caesar</option></select>

XQuery Code Examplexquery version '1.0-ml';

let $books := fn:collection()/book

let $books-dropdown := ( let $select-options := ( for $book in $books let $isbn := $book/isbn let $title := $book/title let $author := $book/author return <option value="{$isbn/text()}">{$title/text()} by {$author/text()}</option> ) return <select name="book"> {$select-options} </select> )

return $books-dropdown

XQuery Code Examplexquery version '1.0-ml';

declare function local:build-option($book) { let $isbn := $book/isbn let $title := $book/title let $author := $book/author return <option value="{$isbn/text()}">{$title/text()} by {$author/text()}</option>};

let $books := fn:collection()/book

let $books-dropdown := ( let $select-options := ( for $book in $books return local:build-option($book) ) return <select name="book"> {$select-options} </select>)

return $books-dropdown

XQuery Code Examplexquery version '1.0-ml';

declare function local:build-option($book) { let $isbn := $book/isbn let $title := $book/title let $author := $book/author return <option value="{$isbn/text()}">{$title/text()} by {$author/text()}</option>};

let $books := fn:collection()/book

let $books-dropdown := ( <select name="book"> {$books/local:build-option(.)} </select> )

return $books-dropdown

XQuery Code Examplexquery version '1.0-ml';

<select name="book"> {fn:collection()/book/<option value="{./isbn/text()}">{./title/text()} by {./author/text()}</option>}</select>

=><select name="book"> <option value="123456789">Inferno by Dante</option> <option value="987654321">Republic by Plato</option> <option value="123432187">Gallic Wars by Caesar</option></select>

XQuery Code Examplexquery version '1.0-ml';

<select name="book"> {fn:collection()/book/<option value="{./isbn/text()}">{./title/text()} by {./author/text()/fn:upper-case(.)}</option>}</select>

=><select name="book"> <option value="123456789">Inferno by DANTE</option> <option value="987654321">Republic by PLATO</option> <option value="123432187">Gallic Wars by CAESAR</option></select>

XQuery App StrategiesCode

Write elegant expressions

Use composed expressions (use few types)

Embrace “the step”

Write utility or helper functions instead of business logic

Use recursion (consider xslt)

Code ExamplesPerson[] persons = {alice, bob, greg};

StringBuffer sb = new StringBuffer(“Hello”);for (int i=0; i<persons.length; i++) {

sb.append(“, “, persons[i].getName())}

=> Hello, Alice, Bob, Greg

xquery version '1.0-ml';

declare function local:add-name($greeting, $persons) { let $person := $persons[1] let $new-greeting := fn:concat($greeting, ", ", $person/name/text()) return if ($persons[2]) then (local:add-name($new-greeting, $persons[2 to fn:last()])) else ($new-greeting)};

local:add-name("Hello", fn:collection()/person)

Code Examples

xquery version '1.0-ml';

fn:concat("Hello, ", fn:string-join(fn:collection()/person/name/text(), ", "))

=> Hello, Alice, Bob, Greg

XQuery App Strategies

Strive for elegance

If you are writing a lot of code, you are probably thinking about it in the wrong way

Questions