Changeset 315


Ignore:
Timestamp:
03/16/12 14:04:47 (13 years ago)
Author:
jkraaijeveld
Message:

[Server] Started work on validating if a change is valid. RdfObjects? now need a getModifiablefields method which returns the fields which can be modified for an object in its particular state. The objects which have different modifiable fields depending on the situation (see state diagram) need a 'state' value, which is an integer for now. Evaluate now checks whether a modified field is valid and throws an exception if its not.

Location:
Dev/branches/jos-branch
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • Dev/branches/jos-branch

  • Dev/branches/jos-branch/server/classes/Model.php

    r314 r315  
    5959     * per type, because the result is cached.
    6060     */
    61     protected abstract static function createFIELDS();
     61        protected abstract static function createFIELDS();
     62
     63        /**
     64         * Provides the modifiable fields of this object, throws an exception otherwise.
     65         * Needs to be implemented by every class to specify what fields are modifiable
     66         * in each state.
     67         */
     68        protected abstract function getModifiableFields();
     69
     70        /**
     71         * Returns whether the object is removable in its current state. Needs to be
     72         * implemented by every class.
     73         */
     74//      protected abstract function isRemovable();
     75
    6276
    6377    // instance fields
    6478
    6579    private $uid = null;
    66     private $uri = null;
     80        private $uri = null;
    6781    private $values = array();
    6882
     
    7791    public function getType() {
    7892        return static::TYPE();
    79     }
     93        }
    8094
    8195    // PHP magic functions to expose values
     
    97111            throw new Exception("Field " . $name . " not defined on " . self::TYPE());
    98112        }
    99     }
     113        }
    100114
    101115    private function get_field($name) {
     
    119133
    120134    public function __set($name, $value) {
    121         self::ensure_field($name);
     135                self::ensure_field($name);
    122136        $this->values[$name] = $value;
    123137    }
    124138
    125139    public function __unset($name) {
    126         unset($this->values[$name]);
     140            unset($this->values[$name]);
    127141    }
    128142
     
    162176    }
    163177
    164     protected function evaluate() {
     178        protected function evaluate() {
     179                $realClass = self::TYPE();
     180                $oldResults = $realClass::get(array("uid" => $this->uid));
    165181        foreach (static::FIELDS() as $name => $field) {
    166182            if ($field->required()
    167                     && (!is_set($this->values[$name])
     183                    && (!isset($this->values[$name])
    168184                    || ( $field->multiple() && empty($this->values[$name]) ) )) {
    169185                throw new Exception("Required field $name not found.");
    170             }
     186                        }
     187                        if (!empty($oldResults)
     188                                && $this->values[$name] != $oldResults[0]->values[$name]
     189                                && !in_array($name, $this->getModifiableFields()))     
     190                        {
     191                                $state = "";
     192                                if(isset($this->values[$state]))
     193                                        $state = $this->values[$state];
     194                                throw new Exception("Field " . $name . " is not modifiable on type " . $realClass . " in current state " . $state);
     195                        }
     196
    171197        }
    172198    }
     
    174200    public function save() {
    175201        $this->evaluate();
    176 
     202               
    177203        $model = self::get_model();
    178204
    179205        // create new resource and remove old saved resource
    180         $resource = new Resource($this->uri);
     206                $resource = new Resource($this->uri);
    181207        $model->subtract($model->find($resource, null, null));
    182208
     
    259285                    }
    260286                }
    261                 $info = self::resourceUriToTypeAndUid($uri);
     287                                $info = self::resourceUriToTypeAndUid($uri);
    262288                $objects[] = new $realClass($info['uid'], $values);
    263289            }
     
    423449}
    424450
     451class RdfInteger implements RdfType {
     452        static function save($model, $resource, $name, $value) {
     453                $model->add(new Statement($resource, new Resource(RDfObject::PREDICATES_NS . $name),
     454                        new Literal($value)));
     455        }
     456        static function load($value) {
     457                return (int) $value->label;
     458        }
     459        static function predicate($name, $value) {
     460                return 'str(?' . $name .') = "' . $value . '"';
     461        }
     462}
     463
     464function RdfInteger() {
     465        return new RdfInteger();
     466}
     467
    425468class RdfReference implements RdfType {
    426469
     
    454497            'values' => RdfField(RdfString(), array(RdfField::MULTIPLE))
    455498        );
    456     }
    457 
     499        }
     500
     501        protected function getModifiableFields() {
     502                return array('question', 'values');
     503        }
    458504}
    459505
     
    467513            'answers' => RdfField(RdfReference(), array(RdfField::MULTIPLE))
    468514        );
    469     }
    470 
     515        }
     516
     517        protected function getModifiableFields() {
     518                return array('survey', 'respondent', 'answers');
     519        }
    471520}
    472521
     
    479528            'style' => RdfField(RdfString())
    480529        );
    481     }
    482 
     530        }
     531
     532        protected function getModifiableFields() {
     533                return array('title', 'description', 'style');
     534        }
    483535}
    484536
     
    493545        );
    494546    }
     547        protected function getModifiableFields() {
     548                return array();
     549        }
    495550
    496551}
     
    508563        );
    509564    }
     565        protected function getModifiableFields() {
     566                return array();
     567        }
    510568
    511569}
     
    521579        );
    522580    }
     581        protected function getModifiableFields() {
     582                switch($this->getState())
     583                {
     584                        case 0: return array('title', 'pipeline');
     585                        case 1: return array('title');
     586                }
     587        }
    523588
    524589}
     
    539604        );
    540605    }
     606        protected function getModifiableFields() {
     607                return array();
     608        }
    541609
    542610}
     
    552620        );
    553621    }
     622        protected function getModifiableFields() {
     623                switch($this->getState())
     624                {
     625                        case 0: return array('title', 'description', 'creator', 'questions');
     626                        case 1: return array('title', 'description', 'questions');
     627                        default: return array('title', 'description');
     628                }
     629        }
    554630
    555631}
     
    567643        );
    568644    }
     645        protected function getModifiableFields() {
     646                return array();
     647        }
    569648
    570649}
     
    579658        );
    580659    }
     660        protected function getModifiableFields() {
     661                return array('passwordHash', 'passwordSalt');
     662        }
    581663
    582664}
Note: See TracChangeset for help on using the changeset viewer.