Skip to content

Commit

Permalink
Fixed json list issue.
Browse files Browse the repository at this point in the history
Created model-to-json function to convert model to json.
Removed some unused files and cleaned up some dead code.  Added some unit tests for core framework
  • Loading branch information
garyvidal committed May 17, 2012
1 parent acec8f9 commit e235b1b
Show file tree
Hide file tree
Showing 36 changed files with 1,549 additions and 3,065 deletions.
Binary file modified docs/Architecture.vsd
Binary file not shown.
Binary file modified docs/XQuerRail Framework Getting Started Guide.docx
Binary file not shown.
Binary file modified docs/~$uerRail Framework Getting Started Guide.docx
Binary file not shown.
6 changes: 5 additions & 1 deletion src/_config/config.xml
Expand Up @@ -10,7 +10,6 @@

<plugin-config resource="/_config/plugin.xml"/>


<controller-scripts-directory value="/resources/scripts"/>
<controller-stylesheet-directory value="/resources/stylesheets"/>

Expand Down Expand Up @@ -41,6 +40,11 @@
<stylesheet-directory value="/application/stylesheets"/>
</application>

<application name="test" namespace="http://www.xquerrail-framework.com/test" uri="/test-application">
<domain resource="/test-application/domains/application-domain.xml"/>
<script-directory value="/test-application/scripts"/>
<stylesheet-directory value="/test-application/stylesheets"/>
</application>

<!--Interceptors-->
<interceptors config="/_config/interceptor.xml">
Expand Down
36 changes: 8 additions & 28 deletions src/_config/routes.xml
Expand Up @@ -2,8 +2,9 @@
<route id="_resources" pattern="^/resources/*" is-resource="true">
<prepend></prepend>
</route>
<route id="_xray" pattern="^/lib/xray" is-resource="true">
<prepend></prepend>

<route id="_xray" pattern="^/(xray)" is-resource="true">
<prepend>/_framework/lib</prepend>
</route>

<route id="_login" pattern="^/login.html">
Expand All @@ -14,36 +15,15 @@
<default key="_controller">application:default:logout:html</default>
</route>

<route id="_thumbnails" pattern="^/thumbnails(.*)">
<default key="_controller">application:default:thumbnail:binary</default>
<param key="thumb-uri">$1</param>
</route>
<route id="_default" pattern="^/$">
<default key="_controller">application:default:index:html</default>
</route>
<route id="_default" pattern="^/staging/?$">
<default key="_controller">application:default:staging:html</default>
</route>
<route id="_validationManager" pattern="^/validationManager/?$">
<default key="_controller">application:validationManager:index:html</default>
</route>
<route id="_validationManager" pattern="^/validationManager/(\i\c*[^\.])\.(xml|json)/?$">
<default key="_controller">application:validationManager:$1:$2</default>
</route>
<route id="_packageManager" pattern="^/packageManager/(\i\c*[^\.])\.(xml|json)/?$">
<default key="_controller">application:packageManager:$1:$2</default>
</route>

<route id="_assets" pattern="^/assets(\i\c*[^\.])\.(xml|json)/?$">
<default key="_controller">application:assets:$1:$2</default>
</route>

<route id="_admin" pattern="^/admin/?$">
<default key="_controller">application:default:admin:html</default>
</route>


<route id="_listxml" pattern="^/(\i\c*)/list.xml$">
<default key="_controller">application:$1:list:xml</default>
</route>

<route id="_lookup" pattern="^/(\i\c*[^/])/lookup\.(xml|json)$">
<default key="_controller">application:$1:lookup:$2</default>
</route>
Expand Down Expand Up @@ -72,11 +52,11 @@
<route id="default_index_format" pattern="^/(\i\c*)/(\i\c*).(json|html)$">
<default key="_controller">application:$1:$2:$3</default>
</route>
<route id="_default_get" pattern="^/(.*[^/])/(\i\c*[^/])\.(json|html)$" method="get">
<route id="_default_get" pattern="^/(.*[^/])/(\i\c*[^/])\.(json|html|xml)$" method="get">
<default key="_controller">application:$1:get:$3</default>
</route>

<route id="_default_update" pattern="^/(.*[^/])/(\i\c*[^/])\.(json|html)$" method="post">
<route id="_default_update" pattern="^/(.*[^/])/(\i\c*[^/])\.(json|html|xml)$" method="post">
<default key="_controller">application:$1:update:$3</default>
</route>

Expand Down
6 changes: 3 additions & 3 deletions src/_framework/base/base-controller.xqy
Expand Up @@ -234,8 +234,8 @@ declare function controller:show()

declare function controller:new()
{(
response:set-template("edit"),
response:set-view("edit"),
response:set-template("main"),
response:set-view("new"),
response:flush()
)};

Expand Down Expand Up @@ -308,7 +308,7 @@ declare function controller:post()
{
let $model := controller:model()
let $uuidField := $model/domain:element[@name = "uuid"]
let $key := domain:get-field-key($uuidField)
let $key := domain:get-field-id($uuidField)

let $uuid := fn:data(request:body()//*[fn:local-name(.) = $key])
let $product :=
Expand Down
138 changes: 28 additions & 110 deletions src/_framework/base/base-model.xqy
Expand Up @@ -68,8 +68,8 @@ as xs:string?
let $idfield := $domain-model/domain:element[@identity eq 'true']
let $uuidfield := $domain-model/domain:element[@name eq 'uuid']

let $uuidKey := domain:get-field-key($uuidfield)
let $idKey := domain:get-field-key($idfield)
let $uuidKey := domain:get-field-id($uuidfield)
let $idKey := domain:get-field-id($idfield)

let $id := map:get($params,$idKey)
let $uuid := map:get($params, $uuidKey)
Expand Down Expand Up @@ -121,7 +121,7 @@ as element()?
return
(: Check if the document exists first before trying to create it :)
if ($current) then
fn:error(xs:QName("DOCUMENT-EXISTS"),fn:concat("The document already exists. Duplicate ID: ", $id))
fn:error(xs:QName("DOCUMENT-EXISTS"),fn:concat("The document already exists. Duplicate ID: ", $id),$current)
else
(: Validate the parameters before trying to build the document :)
let $validation := () (: model:validate($domain-model,$params) :)
Expand Down Expand Up @@ -179,9 +179,10 @@ declare function model:get($domain-model as element(domain:model), $params as ma
(: Retrieve document identity and namspace to help build query :)
let $idfield := fn:data($domain-model/domain:element[@identity eq 'true']/@name)
let $name := fn:data($domain-model/@name)
let $nameSpace := fn:data(model:get-namespaces($domain-model)/@namespace-uri)
let $stmt := fn:string(<stmt>
cts:search(
let $nameSpace := domain:get-field-namespace($domain-model)
let $stmt := fn:string(
<stmt>
cts:search(
{
(: Build a query to search within the give document :)
if ($domain-model/@persistence = 'document') then
Expand All @@ -197,19 +198,24 @@ declare function model:get($domain-model as element(domain:model), $params as ma
fn:concat('fn:doc("', $domain-model/domain:document/text() , '")', $xpath )
else
(: otherwise for document persistance search against the proper root node :)
fn:concat("/*:",$name, "[fn:namespace-uri(.) = '", $nameSpace, "']")
fn:concat("/ *:",$name, "[fn:namespace-uri(.) = '", $nameSpace, "']")
},
cts:or-query(
(cts:element-range-query(fn:QName($nameSpace,"uuid"),"=",$id),
cts:element-value-query(fn:QName($nameSpace,"{$idfield}"),$id,"exact"))
(cts:element-range-query(fn:QName("{$nameSpace}","uuid"),"=","{$id}"),
cts:element-value-query(fn:QName("{$nameSpace}","{$idfield}"),"{$id}","exact"))
), ("filtered"))
</stmt>)

return
return (
(: Execute statement :)
xdmp:value($stmt)
xdmp:log($stmt),
xdmp:eval($stmt)
)
};

(:~
: Creates an update statement for a given model.
~:)
declare function model:update($domain-model as element(domain:model), $params as map:map)
{
let $current := model:get($domain-model,$params)
Expand Down Expand Up @@ -275,7 +281,7 @@ declare function model:recursive-build(
$updates as map:map
) {
let $type := fn:data($context/@type)
let $key := domain:get-field-key($context)
let $key := domain:get-field-id($context)
let $current-value := domain:get-field-value($context,$key,$current)
return
typeswitch($context)
Expand All @@ -285,7 +291,7 @@ declare function model:recursive-build(
for $a in $context/domain:attribute
return
model:recursive-build($a, $mode,$current,$updates)
let $ns := ($context/@namespace)[1]
let $ns := domain:get-field-namespace($context)
let $nses := model:get-namespaces($context)
let $localname := fn:data($context/@name)
let $default := fn:data($context/@default)
Expand All @@ -294,7 +300,6 @@ declare function model:recursive-build(
for $nsi in $nses
return
namespace {$nsi/@prefix}{$nsi/@namespace-uri},

$attributes,

for $n in $context/(domain:element|domain:container)
Expand All @@ -307,7 +312,7 @@ declare function model:recursive-build(
for $a in $context/domain:attribute
return
model:recursive-build($a, $mode, $current, $updates)
let $ns := ($context/@namespace,$context/ancestor::domain:model/@namespace)[1]
let $ns := domain:get-field-namespace($context)
let $localname := fn:data($context/@name)
let $default := (fn:data($context/@default),"")[1]
let $occurrence := ($context/@occurrence,"?")
Expand All @@ -334,7 +339,7 @@ declare function model:recursive-build(

(: Build out any domain Attributes :)
case element(domain:attribute) return
let $ns := ($context/@namespace,$context/ancestor::domain:model/@namespace)[1]
let $ns := ($context/@namespace-uri,$context/@namespace)[1] (:Attributes are only in namespace if they are declared:)
let $localname := fn:data($context/@name)
let $default := (fn:data($context/@default),"")[1]
let $occurrence := ($context/@occurrence,"?")
Expand All @@ -347,7 +352,7 @@ declare function model:recursive-build(

(: Build out any domain Containers :)
case element(domain:container) return
let $ns := ($context/@namespace,$context/ancestor::domain:model/@namespace)[1]
let $ns := domain:get-field-namespace($context)
let $localname := fn:data($context/@name)
return
element {(fn:QName($ns,$localname))}{
Expand Down Expand Up @@ -460,7 +465,7 @@ as element(list)?
let $end := if ($total > $last) then $last else $total

return
<list>
<list type="{$name}">
<currentpage>{$page}</currentpage>
<pagesize>{$pageSize}</pagesize>
<totalpages>{fn:ceiling($total div $pageSize)}</totalpages>
Expand Down Expand Up @@ -602,7 +607,7 @@ declare function model:get-references($field as element(), $params as map:map) {
declare function model:get-model-references($reference as element(domain:element), $params as map:map)
as element()*
{
let $fieldKey := domain:get-field-key($reference)
let $fieldKey := domain:get-field-id($reference)
let $name := fn:data($reference/@name)
let $tokens := fn:tokenize($reference/@reference, ":")
let $type := $tokens[2]
Expand All @@ -613,7 +618,7 @@ declare function model:get-references($field as element(), $params as map:map) {
if(fn:function-available($tokens[3])) then
let $domain-model := domain:get-domain-model($type)
let $identity := $domain-model/domain:element[@identity eq 'true']
let $key := domain:get-field-key($identity)
let $key := domain:get-field-id($identity)
return
for $id at $pos in map:get($params,$fieldKey)
let $newParams := map:map()
Expand Down Expand Up @@ -669,7 +674,7 @@ declare private function model:get-application-reference($field,$params){
if($ref-parent eq "application" and $ref-type eq "model")
then
let $domains := xdmp:value(fn:concat("domain:model-",$ref-action))
let $key := domain:get-field-key($field)
let $key := domain:get-field-id($field)
return
for $value in map:get($params, $key)
let $domain := $domains[@name = $value]
Expand Down Expand Up @@ -699,7 +704,7 @@ as element(validationError)*
{
for $element in $domain-model/(domain:attribute | domain:element)
let $name := fn:data($element/@name)
let $key := domain:get-field-key($element)
let $key := domain:get-field-id($element)
let $type := domain:resolve-datatype($element)
let $value := model:build-value($element,"validate", map:get($params,$key),())
let $occurence := $element/@occurence
Expand Down Expand Up @@ -839,7 +844,7 @@ declare function model:build-value(
then fn:data($current)
else model:generate-uuid()
case element(reference) return
let $fieldKey := domain:get-field-key($field)
let $fieldKey := domain:get-field-id($field)
let $map := map:map()
let $_ := map:put($map, $fieldKey, $value)
return
Expand Down Expand Up @@ -871,90 +876,3 @@ declare function model:build-value(
default return
fn:data($value)
};




(:
declare function model:log-update($update as item()*, $params as map:map)
as empty-sequence()
{
let $reportUUID := util:generate-guid-element()
let $article := $update[1]/uuid/text()
let $modified := $update[1]/*:modified
let $user := $update[1]/*:modifiedBy
let $reportDir := config:get-audit-dir()
let $date := fn:substring-before(fn:string($modified),"T")
let $tokens := fn:tokenize($date,"-")
let $uri := fn:concat(fn:string-join(($reportDir,$tokens,$reportUUID/text()),"/"),".xml")
let $report :=
<report>
{ ($reportUUID, $modified, $user) }
<audit>{
for $key in map:keys($update[2])
let $value := map:get($update[2],$key)
let $nodeName := fn:string(fn:node-name($value))
return
element { $nodeName } {
attribute node { $key },
$value/node()
}
}</audit>
</report>
let $_ := xdmp:document-insert($uri,$report)
return ()
};
:)



(:
(\:~
: Update Operation packageType
: @param $domain-model the model of the document
: @param $id the id of the doucment being retreived (Can be UUID or Model Identity)
: @param $params the values to fill into the element
:\)
declare function model:update-x($domain-model as element(domain:model), $params as map:map)
as element()?
{
let $validation := model:validate($domain-model,$params)
return
if(fn:count($validation) > 0) then
fn:error(xs:QName("VALIDATION-ERROR"), fn:concat("The document trying to be updated contains validation errors"), $validation)
else
let $name := $domain-model/@name
let $current := model:get($domain-model,$params)
(\: Expand out all references and replace the params map :\)
let $_ :=
for $ref in $domain-model/domain:element[fn:boolean(./@reference)]
let $refName := fn:data($ref/@name)
let $values := map:get($params, $refName)
return
map:put($params,$refName,model:get-references($ref,$params))
(\: Create the update system information for tracking :\)
let $_ :=
for $item in model:create-system-node($domain-model,$params)
let $itemName := fn:string(fn:node-name($item))
return
map:put($params, $itemName, $item/*)
let $update := (\: updater:update($current,$params) :\)
model:recursive-update($domain-model, (), $params)
return
(\: Execute the insert function :\)
($update[1],
if ($current) then
(\: let $log := model:log-update($update,$params)
return
:\)xdmp:node-replace($current,$update[1])
else
fn:error(xs:QName("ERROR"), "Trying to update a document that does not exist.")
)
};:)
2 changes: 1 addition & 1 deletion src/_framework/base/views/base.edit.html.xqy
Expand Up @@ -5,7 +5,7 @@ xquery version "1.0-ml";
declare default element namespace "http://www.w3.org/1999/xhtml";
declare namespace domain = "http://www.xquerrail-framework.com/domain";

import module namespace form = "http://www.xquerrail-framework.com/formbuilder" at "/_framework/helpers/formbuilder.xqy";
import module namespace form = "http://www.xquerrail-framework.com/form-builder" at "/_framework/helpers/form-builder.xqy";
import module namespace response = "http://www.xquerrail-framework.com/response" at "/_framework/response.xqy";

declare option xdmp:output "indent-untyped=yes";
Expand Down

0 comments on commit e235b1b

Please sign in to comment.