Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dan McCreary President Dan McCreary & Associates (952) M D

Similar presentations


Presentation on theme: "Dan McCreary President Dan McCreary & Associates (952) M D"— Presentation transcript:

1 XRX Basic CRUD Create, Read, Update and Delete XML Data Date: 10/1/2008
Dan McCreary President Dan McCreary & Associates (952) M D Metadata Solutions

2 Copyright 2008 Dan McCreary & Associates
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 Copyright 2008 Dan McCreary & Associates

3 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 Copyright 2008 Dan McCreary & Associates

4 Copyright 2008 Dan McCreary & Associates
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> Copyright 2008 Dan McCreary & Associates

5 Copyright 2008 Dan McCreary & Associates
Folder Structure /db apps item-manager data edit search views 1.xml 47.xml new-instance.xml search.xq view-item.xq 2.xml 99.xml next-id.xml search.xhtml list-items.xq edit.xq save-new.xq update.xq delete.xq Copyright 2008 Dan McCreary & Associates

6 Copyright 2008 Dan McCreary & Associates
List Items Simple for loop for all items in the data collection Copyright 2008 Dan McCreary & Associates

7 Copyright 2008 Dan McCreary & Associates
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 <td>{$id}</td> <td>{$item/name/text()}</td> }</tbody></table> Copyright 2008 Dan McCreary & Associates

8 Copyright 2008 Dan McCreary & Associates
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 Copyright 2008 Dan McCreary & Associates

9 Copyright 2008 Dan McCreary & Associates
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> } Copyright 2008 Dan McCreary & Associates

10 Copyright 2008 Dan McCreary & Associates
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 Copyright 2008 Dan McCreary & Associates

11 Copyright 2008 Dan McCreary & Associates
edit.xq Copyright 2008 Dan McCreary & Associates

12 Copyright 2008 Dan McCreary & Associates
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"/> Copyright 2008 Dan McCreary & Associates

13 Copyright 2008 Dan McCreary & Associates
New Instance <item> <id/> <name/> <description/> <category/> <status/> <tag/> </item> Contains initial values of default form. Booleans must have true/false set. Copyright 2008 Dan McCreary & Associates

14 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:label>Medium</xf:label> <xf:value>medium</xf:value> <xf:label>Large</xf:label> <xf:value>large</xf:value> </xf:select1> Copyright 2008 Dan McCreary & Associates

15 Copyright 2008 Dan McCreary & Associates
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>… Copyright 2008 Dan McCreary & Associates

16 Copyright 2008 Dan McCreary & Associates
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. Copyright 2008 Dan McCreary & Associates

17 Copyright 2008 Dan McCreary & Associates
delete-confirm.xq xquery 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/> <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. Copyright 2008 Dan McCreary & Associates

18 Copyright 2008 Dan McCreary & Associates
delete.xq let $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. Copyright 2008 Dan McCreary & Associates

19 How CRUD Applications Work Together
search.xhtml search.xq index.xhtml view-item.xq save-new.xq list-items.xq edit.xq optional update.xq delete-confirm.xq delete.xq Note: If you change a file name of an XQuery, make sure that everything that links to it is also updated Copyright 2008 Dan McCreary & Associates

20 Copyright 2008 Dan McCreary & Associates
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 Copyright 2008 Dan McCreary & Associates

21 Copyright 2008 Dan McCreary & Associates
Thank You! Please contact me for more information: Native XML Databases Metadata Management Metadata Registries Service Oriented Architectures Business Intelligence and Data Warehouse Semantic Web Dan McCreary, President Dan McCreary & Associates Metadata Strategy Development (952) Copyright 2008 Dan McCreary & Associates


Download ppt "Dan McCreary President Dan McCreary & Associates (952) M D"

Similar presentations


Ads by Google