XQuery Web Apps for Java Developers
Common Java Setup Install 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 Characteristics Object-oriented, uses imperative statements to change state
Java Code Example mylist.add(myobject); fos.write(mybytes); out.close(); panel.paint(); mythread.start();
Java Characteristics Object-oriented, uses imperative statements to change state
Java Characteristics Object-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 Characteristics Object-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 Characteristics Object-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 Characteristics Object-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 Characteristics Object-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 Strategies Framework 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 Example xquery 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 Example xquery 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 Example xquery 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 Example xquery 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 Example xquery 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 Strategies Code 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 Examples Person[] 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