xrx basic cruds create, read, update and delete and search xml data date: may 2011 dan mccreary...
TRANSCRIPT
XRX Basic CRUDSCreate, Read, Update and Delete and Search XML Data
Date: May 2011
Dan McCrearyPresidentDan McCreary & [email protected](952) 931-9198
M
D
Metadata Solutions
M
D Copyright 2008 Dan McCreary & Associates2
Outline
• Create new XML records using XForms– assigning sequential ids to each item (save-new.xq)
• Listing items– Creating a list of current items (list-items.xq)
• Reading XML records– creating read-only views of items (view-item.xq)
• Update existing records– use XQuery updates (update.xq)
• Deleting an item– How to delete with confirmation (delete-confirm.xq)
• Searching– How to build custom search forms
M
D Copyright 2008 Dan McCreary & Associates3
Architecturally Significant Functions
• Once you learn the basics CRUD(S) functions you can start to customize the item-manager code (S is for Search)
• Additional functions don’t require you to learn any additional architectural features
M
D Copyright 2008 Dan McCreary & Associates4
Sample Item
• To keep our examples simple, we are going to use a very simple “item” structure
<item> <id>47</id> <name>Item Forty Seven</name> <description>This is a detailed description of item 47.</description> <category>medium</category> <status>draft</status> <tag>tag-1</tag></item>
M
D Copyright 2008 Dan McCreary & Associates5
Folder Structure
apps/db
item-manager
data edit viewssearch
1.xml
2.xml
47.xml
99.xml
save-new.xq
update.xq
new-instance.xml
next-id.xml
edit.xq
delete.xq
view-item.xq
list-items.xq
search.xq
search.xhtml
M
D Copyright 2008 Dan McCreary & Associates6
List Items
• Simple for loop for all items in the data collection
M
D Copyright 2008 Dan McCreary & Associates7
list-items.xq <table> <thead> <tr> <th>ID</th> <th>Name</th>… </tr> </thead> <tbody>{ for $item in collection($collection)/item let $id := $item/id/text() return <tr> <td>{$id}</td> <td>{$item/name/text()}</td>… </tr> }</tbody></table>
M
D Copyright 2008 Dan McCreary & Associates8
Item Viewer
• Always provide a read-only viewer for your items• Never force a person to use a write/lock edit form to view a document• This causes unnecessary locking in the database
http://localhost:8080/exist/rest/db/apps/item-manager/views/view-item.xq?id=1
M
D Copyright 2008 Dan McCreary & Associates9
view-item.xq let $id := request:get-parameter('id', '')…<h1>View Item</h1> {let $item := collection($collection)/item[id = $id] return <table> <tbody> <tr><th>ID:</th><td>{$item/id/text()}</td></tr> <tr><th>Name:</th><td>{$item/faq-category-id/text()}</td></tr> <tr><th>Description:</th><td>{$item/description/text()}</td></tr> <tr><th>Category:</th><td>{$item/category/text()}</td></tr> <tr><th>Status:</th><td>{$item/status/text()}</td></tr> <tr><th>Tag:</th><td>{$item/tag/text()}</td></tr> </tbody> </table> }
M
D Copyright 2008 Dan McCreary & Associates10
edit.xq Architecture
• Generates an XHTML XForms on the fly
• Works for both new items and updates to items
• Calls save-new.xq if it has a new item
• Calls update.xq if it is updating an existing item
M
D Copyright 2008 Dan McCreary & Associates11
edit.xq
http://localhost:8080/exist/rest/db/apps/item-manager/edit/edit.xq?id=1
M
D Copyright 2008 Dan McCreary & Associates12
Excerpts from edit.xq
let $new := request:get-parameter('new', '')let $id := request:get-parameter('id', '')…let $file := if ($new) then ('new-instance.xml') else ( concat( $server-port, '/exist/rest', $collection, '/',
$id, '.xml'))… <xf:submission id="save" method="post" action="{if
($new='true') then ('save-new.xq') else ('update.xq')}" instance="my-task" replace="all"/>
M
D Copyright 2008 Dan McCreary & Associates13
New Instance
<item> <id/> <name/> <description/> <category/> <status/> <tag/></item>
http://localhost:8080/exist/rest/db/apps/item-manager/edit/new-instance.xq
Contains initial values of default form.Booleans must have true/false set.
M
D Copyright 2008 Dan McCreary & Associates14
XForms Controls for edit.xq <xf:input ref="name"> <xf:label>Name:</xf:label> </xf:input> <xf:textarea ref="description" class="description"> <xf:label>Question:</xf:label> </xf:textarea> <xf:select1 ref="category" class="category"> <xf:label>Category:</xf:label> <xf:item> <xf:label>Small</xf:label> <xf:value>small</xf:value> </xf:item> <xf:item> <xf:label>Medium</xf:label> <xf:value>medium</xf:value> </xf:item> <xf:item> <xf:label>Large</xf:label> <xf:value>large</xf:value> </xf:item> </xf:select1>
M
D Copyright 2008 Dan McCreary & Associates15
save-new.xq…(: this is gets the data from the XForms submission :)let $item := request:get-data()
(: this creates the new file with a still-empty id element :)let $store := xmldb:store($data-collection, $file, $item)
(: this adds the correct ID to the new document we just saved :)let $update-id := update replace doc(concat($data-collection, '/', $file))/item/id with
<id>{$id}</id>
(: this updates the next-id.xml file :)let $new-next-id := update replace doc($next-id-file-path)/data/next-id/text() with ($id + 1)
return<html>…
M
D Copyright 2008 Dan McCreary & Associates16
update.xq…(: Get the data from the XForms POST submission :)let $item := request:get-data()
(: this saves the new file and overwrites the old one :)let $store := xmldb:store($collection, $file, $item)
return<html> <head> <title>Update Confirmation</title> </head> <body> <a href="../index.xhtml">Item Home</a> > <a href="../views/list-items.xq">List all
Items</a> > <a href="../views/view-item.xq?id={$id}">View Item</a> <p>Item {$id} has been updated.</p> </body></html>
Note that save and update do not take URL parameters. They get all their data from the POST.
M
D Copyright 2008 Dan McCreary & Associates17
delete-confirm.xqxquery version "1.0";let $id := request:get-parameter("id", "")…return<html><body> <a href="../index.xhtml">Item Home</a> > <a href="../views/list-items.xq">List
Items</a> <h1>Are you sure you want to delete this Item?</h1> <b>Name: </b>{doc($doc)/item/name/text()}<br/> <b>Path: </b> {$doc} <br/> <br/> <a class="warn" href="delete.xq?id={$id}">Yes - Delete This Item?</a><a class="warn" href="../views/view-item.xq?id={$id}">Cancel (Back to View Item)</a> </body></html>
Note that save and update do not take URL parameters. They get all their data from the POST.
M
D Copyright 2008 Dan McCreary & Associates18
delete.xqlet $collection := '/db/apps/manage-items/data' (: this script takes the integer value of the id parameter passed via get :)let $id := xs:integer(request:get-parameter('id', ''))
(: this logs you into the collection :)let $login := xmldb:login($collection, ‘username', ‘password')
(: this constructs the filename from the id :)let $file := concat($id, '.xml')
(: this REALLY deletes the file :)let $store := xmldb:remove($collection, $file)
return<html>
Note that save and update do not take URL parameters. They get all their data from the POST.
M
D Copyright 2008 Dan McCreary & Associates19
How CRUD Applications Work Together
list-items.xq edit.xq
view-item.xq
delete-confirm.xq
delete.xq
search.xqsearch.xhtml
index.xhtml
Note: If you change a file name of an XQuery,make sure that everything that links to it is also updated
save-new.xq
update.xqoptional
M
D Copyright 2008 Dan McCreary & Associates20
Next Steps
• Create additional reports for users– Customized dashboards– Use status codes to filter data– Data quality reports
• Customize Forms for specific users
• Add role-based functions so each role has custom views and forms
• Build custom searches for specific users
M
D Copyright 2008 Dan McCreary & Associates21
Labs
Create your own application that includes the following functions:
1. List items by title and description2. View an item3. Search for an item4. Creates a new record with an XForms page5. Updates an record6. Deletes a record7. Run counts of item types