Presentation is loading. Please wait.

Presentation is loading. Please wait.

XRX Basic CRUDS Create, Read, Update and Delete and Search XML Data Date: May 2011 Dan McCreary President Dan McCreary & Associates

Similar presentations


Presentation on theme: "XRX Basic CRUDS Create, Read, Update and Delete and Search XML Data Date: May 2011 Dan McCreary President Dan McCreary & Associates"— Presentation transcript:

1 XRX Basic CRUDS Create, Read, Update and Delete and Search XML Data Date: May 2011 Dan McCreary President Dan McCreary & Associates dan@danmccreary.com (952) 931-9198 M D Metadata Solutions

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

3 M D 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

4 M D Copyright 2008 Dan McCreary & Associates 4 Sample Item To keep our examples simple, we are going to use a very simple “item” structure 47 Item Forty Seven This is a detailed description of item 47. medium draft tag-1

5 M D Copyright 2008 Dan McCreary & Associates 5 Folder Structure apps /db item-manager dataeditviewssearch 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

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

7 M D Copyright 2008 Dan McCreary & Associates 7 list-items.xq ID Name … { for $item in collection($collection)/item let $id := $item/id/text() return {$id} {$item/name/text()} … }

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

9 M D Copyright 2008 Dan McCreary & Associates 9 view-item.xq let $id := request:get-parameter('id', '') … View Item {let $item := collection($collection)/item[id = $id] return ID: {$item/id/text()} Name: {$item/faq-category-id/text()} Description: {$item/description/text()} Category: {$item/category/text()} Status: {$item/status/text()} Tag: {$item/tag/text()} }

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

11 M D Copyright 2008 Dan McCreary & Associates 11 edit.xq http://localhost:8080/exist/rest/db/apps/item-manager/edit/edit.xq?id=1

12 M D Copyright 2008 Dan McCreary & Associates 12 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')) …

13 M D Copyright 2008 Dan McCreary & Associates 13 New Instance 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.

14 M D Copyright 2008 Dan McCreary & Associates 14 XForms Controls for edit.xq Name: Question: Category: Small small Medium medium Large large

15 M D Copyright 2008 Dan McCreary & Associates 15 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} (: 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 …

16 M D Copyright 2008 Dan McCreary & Associates 16 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 Update Confirmation Item Home > List all Items > View Item Item {$id} has been updated. Note that save and update do not take URL parameters. They get all their data from the POST.

17 M D Copyright 2008 Dan McCreary & Associates 17 delete-confirm.xq xquery version "1.0"; let $id := request:get-parameter("id", "") … return Item Home > List Items Are you sure you want to delete this Item? Name: {doc($doc)/item/name/text()} Path: {$doc} Yes - Delete This Item? Cancel (Back to View Item) Note that save and update do not take URL parameters. They get all their data from the POST.

18 M D Copyright 2008 Dan McCreary & Associates 18 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 Note that save and update do not take URL parameters. They get all their data from the POST.

19 M D Copyright 2008 Dan McCreary & Associates 19 How CRUD Applications Work Together list-items.xqedit.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.xq optional

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

21 M D Copyright 2008 Dan McCreary & Associates 21 Labs Create your own application that includes the following functions: 1.List items by title and description 2.View an item 3.Search for an item 4.Creates a new record with an XForms page 5.Updates an record 6.Deletes a record 7.Run counts of item types


Download ppt "XRX Basic CRUDS Create, Read, Update and Delete and Search XML Data Date: May 2011 Dan McCreary President Dan McCreary & Associates"

Similar presentations


Ads by Google