Changeset 303


Ignore:
Timestamp:
03/04/12 15:48:42 (13 years ago)
Author:
hendrikvanantwerpen
Message:

[Server] Refactored model classes with some meta-programming. Specific classes only define their fields and inherit from class RdfObject?. Changes to handle the new model objects correctly.
[Client] Added rft/store module for uniform resource access. Removed dependencies on 'uid' field name. Added support for references without loading full object nor exposing uri.
[Client] Added reset() to QuestionWidget?.
[RDFAPI] Fixed PHP warning.

Location:
Dev/branches/rest-dojo-ui
Files:
2 added
12 edited

Legend:

Unmodified
Added
Removed
  • Dev/branches/rest-dojo-ui/client/rft/pages/questions.js

    r288 r303  
    11define(['dojo/_base/declare','dojo/_base/lang','dojo/_base/array','dojo/_base/event',
    2     'dojo/_base/Deferred','dojo/dom-construct','dojo/store/JsonRest','dijit/layout/ContentPane','dijit/TitlePane',
     2    'dojo/_base/Deferred','dojo/dom-construct','rft/store','dijit/TitlePane',
    33    'rft/ui/_Page','rft/ui/LineWithActionsWidget'],
    4     function(declare,lang,array,event,Deferred,domConstruct,JsonRest,ContentPane,TitlePane,_Page,LineWithActionsWidget) {
     4    function(declare,lang,array,event,Deferred,domConstruct,store,TitlePane,_Page,LineWithActionsWidget) {
    55        return declare('rft.pages.questions',[_Page],{
    66            constructor: function() {
     
    99            },
    1010            onVisit: function() {
    11                 this._store = new JsonRest({
    12                     target:"../server/api.php/data/Question/",
    13                     idProperty: 'uid'
    14                 });
     11                this._store = store.getStore('Question');               
    1512                this._refresh(true);
    1613            },
    1714            _refresh: function(initial) {
    18                 Deferred.when( this._store.query() ).then(lang.hitch(this,function(results){
     15                Deferred.when( this._store.query() )
     16                .then(lang.hitch(this,function(results){
    1917                    array.forEach(results,lang.hitch(this,'_addQuestion'));
    2018                    initial && this.accordion.selectChild(true);
     
    2220            },
    2321            _addQuestion: function(q) {
    24                 var uid = q.uid;
     22                var uid = q.getUid();
    2523                var question = this.questions[uid];
    2624                if ( !question ) {
     
    5452                var placeNode = this.accordion.domNode;
    5553                var placePos = 'last';
    56                 array.some(widgets,lang.hitch(this,function(widget,idx) {
     54                array.some(widgets,lang.hitch(this,function(widget) {
    5755                    if ( widget.title == category ) {
    5856                        containerWidget = widget;
  • Dev/branches/rest-dojo-ui/client/rft/pages/sessions.html

    r274 r303  
    2424            var submitHandler = lang.hitch(that,function(evt){
    2525                var newObj = this.form.get('value');
    26                 debugger;
    2726                // mixin newObj in obj.
    2827                // store.put(newObj);
  • Dev/branches/rest-dojo-ui/client/rft/pages/survey.html

    r292 r303  
    11<div data-dojo-type="rft.pages.survey">
    22    <h1 data-rft-attach-point="header">(default)</h1>
     3    <div>Created by <span data-rft-attach-point="creator"></span><div>
    34    <form data-dojo-type="dijit.form.Form" data-rft-attach-point="form" data-rft-attach-event="onSubmit:onSave">
    45        <div style="display: block; clear: both;">
  • Dev/branches/rest-dojo-ui/client/rft/pages/survey.js

    r292 r303  
    1 define(['dojo/_base/declare','dojo/_base/lang','dojo/_base/event','dojo/_base/Deferred','dojo/store/JsonRest','rft/ui/_Page'],
    2     function(declare,lang,event,Deferred,JsonRest,_Page){
     1define(['dojo/_base/declare','dojo/_base/lang','dojo/_base/event','dojo/_base/Deferred','rft/store','rft/ui/_Page'],
     2    function(declare,lang,event,Deferred,store,_Page){
    33        return declare('rft.pages.survey',[_Page],{
    44            object: null,
    55            postCreate: function() {
    66                this.inherited(arguments);
    7                 this._store = new JsonRest({
    8                     target:"../server/api.php/data/Survey/",
    9                     idProperty: 'uid'
    10                 });
     7                this._store = store.getStore('Survey');
    118            },
    129            onVisit: function() {
     
    1613                        this.object = obj;
    1714                        this.setFields(obj);
     15                        return Deferred.when( obj.creator && store.dereference(obj.creator) );
     16                    }))
     17                    .then(lang.hitch(this,function(obj){
     18                        this.creator.innerHTML = (obj && obj.email) || 'unknown';
    1819                    }));
    1920                } else {
  • Dev/branches/rest-dojo-ui/client/rft/pages/surveys.js

    r292 r303  
    1 define(['dojo/_base/declare','dojo/_base/lang','dojo/store/JsonRest','dojo/data/ObjectStore','rft/content','rft/ui/_Page'],
    2     function(declare,lang,JsonRest,ObjectStore,content,_Page){
     1define(['dojo/_base/declare','dojo/_base/lang','dojo/_base/Deferred','dojo/data/ObjectStore','rft/auth','rft/store','rft/content','rft/ui/_Page'],
     2    function(declare,lang,Deferred,ObjectStore,auth,store,content,_Page){
    33        return declare('rft.pages.surveys',[_Page],{
    44            selectedObject: null,
    55            postCreate: function() {
    6                 this._store = new JsonRest({
    7                     target:"../server/api.php/data/Survey/",
    8                     idProperty: 'uid'
    9                 });
     6                this._store = store.getStore('Survey');
    107            },
    118            onVisit: function() {
     
    1411                this.grid.on('rowclick',lang.hitch(this,function(evt){
    1512                    this.selectedObject = evt.grid.getItem(evt.rowIndex);
    16                     this.btnEdit.set('disabled',!!this.selectedObject);
     13                    this.btnEdit.set('disabled',!this.selectedObject);
    1714                }));
    1815
    1916                this.grid.on('rowdblclick',lang.hitch(this,function(evt){
    2017                    var obj = evt.grid.getItem(evt.rowIndex);
    21                     content.goTo('/survey',{uid:obj.uid});
     18                    content.goTo('/survey',{uid:obj.getUid()});
    2219                }));
    23                
     20                               
    2421                this.btnNew.on('click',lang.hitch(this,function(){
    25                     this._store.add({},function(obj) {
    26                         content.goTo('/survey',{uid:this.selectedObject.uid});
     22                    Deferred.when( this._store.add({'creator':auth.getUser()}) )
     23                    .then(function(obj) {
     24                        content.goTo('/survey',{uid:obj.getUid()});
    2725                    });
    2826                }));
     
    3028                this.btnEdit.on('click',lang.hitch(this,function(){
    3129                    if ( this.selectedObject ) {
    32                         content.goTo('/survey',{uid:this.selectedObject.uid});
     30                        content.goTo('/survey',{uid:this.selectedObject.getUid()});
    3331                    }
    3432                   
  • Dev/branches/rest-dojo-ui/client/rft/ui/QuestionWidget.js

    r288 r303  
    1 define(['dojo/_base/declare','dojo/dom-construct','dijit/_WidgetBase',
     1define(['dojo/_base/declare','dojo/_base/lang','dojo/dom-construct','dijit/_WidgetBase',
    22    'dijit/_TemplatedMixin','dijit/_WidgetsInTemplateMixin',
    33    'dojo/text!./templates/QuestionWidget.html','dijit/form/TextBox',
    44    'dijit/form/Textarea','./MultipleChoiceWidget'],
    5     function(declare,domConstruct,_WidgetBase,_TemplatedMixin,_WidgetsInTemplateMixin,templateString,TextBox,Textarea,MultipleChoiceWidget){
     5    function(declare,lang,domConstruct,_WidgetBase,_TemplatedMixin,_WidgetsInTemplateMixin,templateString,TextBox,Textarea,MultipleChoiceWidget){
    66        return declare('rft.ui.QuestionWidget',[_WidgetBase,_TemplatedMixin,_WidgetsInTemplateMixin],{
    77            templateString: templateString,
    88            mode: 'view', // view || edit
    99            name: '',
     10            value: null,
    1011            _type: null,
    1112            _widgetCache: null,
    1213            constructor: function() {
    1314                this.inherited(arguments);
     15                this.value = {};
    1416                this._widgetCache = {};
    1517            },
    1618            postCreate: function() {
     19                this._resetValue = this.value;
    1720                this.typeSelector.set('disabled', this.mode == 'edit');
    1821            },
    1922            _setValueAttr: function(value) {
    20                 value.type && this._onTypeChange(value.type);
     23                this.value = value;
     24                this._onTypeChange(value.type || 'string');
    2125                this.ourForm.set('value',value);
    2226            },
    2327            _getValueAttr: function() {
    24                 return this.ourForm.get('value');
     28                var value = this.ourForm.get('value');
     29                lang.mixin(this.value,value);
     30                return this._question;
    2531            },
    2632            _onTypeChange: function(type) {
     
    6066                }
    6167                return widget;
     68            },
     69            reset: function() {
     70                this.ourForm.reset();
     71                this._setValueAttr(this._resetValue);
    6272            }
    6373        });
  • Dev/branches/rest-dojo-ui/client/rft/ui/templates/QuestionWidget.html

    r288 r303  
    22    <form data-dojo-type="dijit.form.Form" data-dojo-attach-point="ourForm">
    33        <fieldset>
    4             <input data-dojo-type="dijit.form.TextBox" name="uid" type="text" class="dijitHidden" />
    54            <label for="code" class="loginLabel">Code</label>
    65            <input data-dojo-type="dijit.form.TextBox" name="code" type="text" class="loginInput" />
  • Dev/branches/rest-dojo-ui/server/api.php

    r292 r303  
    44ini_set('display_errors', True);
    55
     6define("RDFAPI_INCLUDE_DIR", "rdfapi/");
    67require_once 'tonic/lib/tonic.php';
     8require_once 'classes/Model.php';
    79require_once 'classes/master.php';
    810
     
    1315
    1416function set_session_cookie($response, $user) {
    15     $response->addHeader('Set-Cookie', 'rft_uid=' . $user->uid . '; Max-Age: 3600; Path=' . $response->request->baseUri);
     17    $response->addHeader('Set-Cookie', 'rft_uid=' . $user->getUid() . '; Max-Age: 3600; Path=' . $response->request->baseUri);
    1618}
    1719
     
    8789class DataResource extends Resource {
    8890
    89     function getTypeAndUid($request) {
     91    private function getTypeAndUid($request) {
    9092        $uri = get_clean_uri($request);
    9193        $path = explode('/', $uri);
     
    155157            $object->$key = $val;
    156158        }
    157         if (isset($object->creator)) {
    158             $object->creator = $user;
    159         }
    160         if (!$object->save()) {
    161             throw new ResponseException("Save failed", Response::INTERNALSERVERERROR);
    162         }
     159        $object->save();
    163160
    164161        $response->body = $object;
  • Dev/branches/rest-dojo-ui/server/classes/Auth.php

    r274 r303  
    77    public static function register($email, $password) {
    88        $user_results = User::get(array("email" => $email));
    9         if ( !empty($user_results) ) {
     9        if (!empty($user_results)) {
    1010            throw new Exception("User with email $email already exists.");
    1111        }
    1212        $salt = rand();
    13         $user = new User(null,$email,sha1($password.$salt),$salt);
     13        $user = new User(null, array(
     14                    'email' => $email,
     15                    'passwordHash' => sha1($password . $salt),
     16                    'passwordSalt' => $salt));
    1417        $user->save();
    1518        return $user;
    1619    }
    17    
     20
    1821    public static function login($email, $password) {
    1922        $user_results = User::get(array("email" => $email));
     
    2225        }
    2326        $user = $user_results[0];
    24         if (sha1($password.$user->passwordSalt) != $user->passwordHash) {
     27        if (sha1($password . $user->passwordSalt) != $user->passwordHash) {
    2528            throw new Exception("Wrong password.");
    2629        }
     
    3538        return $user_results[0];
    3639    }
    37    
     40
    3841}
    3942
  • Dev/branches/rest-dojo-ui/server/classes/Marshaller.php

    r274 r303  
    44
    55    const OBJ_TYPE = '__objectType';
     6    const OBJ_UID = '__objectUid';
     7    const REF_TYPE = '__referenceType';
     8    const REF_UID = '__referenceUid';
    69
    710    private static function isWrappedObject($obj) {
     
    912    }
    1013
     14    private static function isWrappedReference($obj) {
     15        return isset($obj[self::REF_TYPE]);
     16    }
     17
    1118    public static function marshall($obj) {
    1219        switch (gettype($obj)) {
    1320            case 'array':
    14                 return array_map(array('Marshaller','marshall'), $obj);
     21                return array_map(array('Marshaller', 'marshall'), $obj);
    1522            case 'object':
    1623                return static::wrapObject($obj);
     
    2431        if ($objectType == 'DateTime') {
    2532            return array(self::OBJ_TYPE => 'DateTime', 'timestamp' => $obj->getTimestamp());
     33        } else if ($obj instanceof RdfObject) {
     34            $array = array(
     35                self::OBJ_TYPE => $obj->getType(),
     36                self::OBJ_UID => $obj->getUid()
     37            );
     38            foreach ($obj as $prop => $val) {
     39                $array[$prop] = static::marshall($val);
     40            }
     41            return $array;
     42        } else if ($obj instanceof RdfPointer) {
     43            return array(
     44                self::REF_TYPE => $obj->getType(),
     45                self::REF_UID => $obj->getUid()
     46            );
    2647        } else {
    27             $newObj = static::marshall((array) $obj);
    28             $newObj[self::OBJ_TYPE] = get_class($obj);
    29             return $newObj;
     48            return static::marshall((array) $obj);
    3049        }
    3150    }
     
    3857                if (static::isWrappedObject($obj)) {
    3958                    return static::unwrapObject($obj);
     59                } else if (static::isWrappedReference($obj)) {
     60                    return static::unwrapReference($obj);
    4061                } else {
    41                     return array_map(array('Marshaller','unmarshall'), $obj);
     62                    return array_map(array('Marshaller', 'unmarshall'), $obj);
    4263                }
    4364            default:
     
    4768
    4869    private static function unwrapObject($obj) {
    49         $objectType = $obj[self::OBJ_TYPE];
    50         unset($obj[self::OBJ_TYPE]);
     70        if (isset($obj[self::OBJ_TYPE])) {
     71            $objectType = $obj[self::OBJ_TYPE];
     72            unset($obj[self::OBJ_TYPE]);
     73        }
     74        if (isset($obj[self::OBJ_UID])) {
     75            $objectUid = $obj[self::OBJ_UID];
     76            unset($obj[self::OBJ_UID]);
     77        }
    5178        if ($objectType == 'DateTime') {
    5279            $date = new DateTime();
    5380            $date->setTimestamp($obj['timestamp']);
    5481            return $date;
    55         } else if (is_subclass_of($objectType, 'ResearchToolObjectInterface')) {
    56             return $objectType::create((object)static::unmarshall($obj));
     82        } else if (is_subclass_of($objectType, 'RdfObject')) {
     83            $rdfObject = new $objectType($objectUid);
     84            foreach ($obj as $prop => $val) {
     85                $rdfObject->$prop = static::unmarshall($val);
     86            }
     87            return $rdfObject;
    5788        } else {
    5889            return $obj;
     
    6091    }
    6192
     93    private static function unwrapReference($ref) {
     94        $refType = $ref[self::REF_TYPE];
     95        $refUid = $ref[self::REF_UID];
     96        return new RdfPointer(RdfObject::typeAndUidToResourceUri($refType, $refUid));
     97    }
     98   
    6299}
    63100
  • Dev/branches/rest-dojo-ui/server/classes/master.php

    r256 r303  
    88 * @author fpvanagthoven
    99 */
    10 // Determine our absolute document root
    11 define('DOC_ROOT', realpath(dirname(__FILE__) . '/../'));
    1210
    1311function __autoload($class_name) {
    1412        if (file_exists('classes/' . $class_name . '.php'))
    15                 require 'classes/' . $class_name . '.php';
    16         else if (file_exists('classes/models/' . $class_name . '.php'))
    17                 require 'classes/models/' . $class_name . '.php';
    18         else if (file_exists('classes/widgets/' . $class_name . '.php'))
    19                 require 'classes/widgets/' . $class_name . '.php';
    20         else if (file_exists('classes/styles/' . $class_name . '.php'))
    21                 require 'classes/styles/' . $class_name . '.php';
     13                require_once 'classes/' . $class_name . '.php';
    2214}
    2315
  • Dev/branches/rest-dojo-ui/server/rdfapi/syntax/RdfSerializer.php

    r256 r303  
    196196                }
    197197                // check if model is empty
    198                 if ($model->size() == 0) return "<". $this->rdf_qname_prefix . RDF_RDF ." xmlns:rdf='".RDF_NAMESPACE_URI."' />";
     198                if ($model->size() == 0) {
     199            $elementStr = "<". $this->rdf_qname_prefix . RDF_RDF ." xmlns:rdf='".RDF_NAMESPACE_URI."' />";
     200            return $elementStr;
     201        }
    199202
    200203                foreach($nsps as $ns => $pre){
Note: See TracChangeset for help on using the changeset viewer.