[131] | 1 | <?php |
---|
| 2 | |
---|
| 3 | // Survey database interface class as intermediate for storing data from the site to the RDF database |
---|
| 4 | require_once 'rdfConstants.php'; |
---|
| 5 | // Include RAP Library to write RDF files |
---|
| 6 | include(RDFAPI_INCLUDE_DIR . "RDFAPI.php"); |
---|
| 7 | |
---|
| 8 | /** |
---|
| 9 | * Description of ApplicationConnector |
---|
| 10 | * |
---|
| 11 | * @author jkraaijeveld |
---|
| 12 | */ |
---|
[149] | 13 | class ApplicationConnector implements IConnector{ |
---|
| 14 | |
---|
[131] | 15 | protected $model; |
---|
| 16 | protected $fileName = 'data/applications/applications.rdf'; |
---|
| 17 | |
---|
| 18 | /** |
---|
| 19 | * Constructor for ApplicationConnector. |
---|
| 20 | */ |
---|
[149] | 21 | public function __construct() |
---|
| 22 | { |
---|
[131] | 23 | //Ensure the required folder for this connector exists |
---|
[149] | 24 | if (!is_dir('data/applications/')) |
---|
| 25 | mkdir('data/applications/'); |
---|
[131] | 26 | } |
---|
[149] | 27 | |
---|
[131] | 28 | /** |
---|
| 29 | * function load() |
---|
| 30 | * Loads the file into the standard MemModel. |
---|
| 31 | */ |
---|
| 32 | public function load() { |
---|
| 33 | //Get the Memory Model from the ModelFactory |
---|
| 34 | $this->model = ModelFactory::getDefaultModel(); |
---|
[149] | 35 | |
---|
[131] | 36 | //Ensure the required file exists before loading |
---|
[149] | 37 | if(file_exists($this->fileName)) |
---|
[131] | 38 | $this->model->load($this->fileName); |
---|
| 39 | } |
---|
[149] | 40 | |
---|
[131] | 41 | /** |
---|
| 42 | * function save() |
---|
| 43 | * Saves the MemModel into the given file. |
---|
| 44 | */ |
---|
| 45 | public function save() { |
---|
[149] | 46 | $this->model->saveAs($this->fileName,'rdf'); |
---|
[131] | 47 | } |
---|
[149] | 48 | |
---|
[131] | 49 | /** |
---|
| 50 | * function get($arguments) |
---|
| 51 | * @param type $arguments : An array containing zero or more of the following keys: |
---|
[158] | 52 | * 'uid', 'title', 'description', 'style' |
---|
[131] | 53 | */ |
---|
| 54 | public function get($arguments) { |
---|
| 55 | $this->load(); |
---|
| 56 | //Determine which arguments are supplied |
---|
| 57 | $keys = array_keys($arguments); |
---|
| 58 | //Set default values for arguments |
---|
[158] | 59 | $uid = ""; $title = ""; $description = ""; $style = ""; |
---|
[131] | 60 | //Set the arguments if they are supplied |
---|
[149] | 61 | if(in_array("uid", $keys)) |
---|
[158] | 62 | $uid = "predicates:uid \"".$arguments["uid"]."\""; |
---|
[149] | 63 | if(in_array("title", $keys)) |
---|
[158] | 64 | $title = 'predicates:title \''.$arguments["title"].'\''; |
---|
[149] | 65 | if(in_array("description", $keys)) |
---|
[158] | 66 | $description = "predicates:description \"".$arguments["description"]."\""; |
---|
[149] | 67 | if(in_array("style", $keys)) |
---|
[158] | 68 | $style = "predicates:style \"".$arguments["style"]."\""; |
---|
[131] | 69 | |
---|
| 70 | //Create the querystring |
---|
| 71 | $querystring = ' |
---|
| 72 | PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> |
---|
| 73 | PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> |
---|
| 74 | SELECT ?uid, ?title, ?description, ?style |
---|
| 75 | WHERE |
---|
| 76 | { |
---|
| 77 | _application predicates:resource_type resources:application ; |
---|
| 78 | predicates:uid ?uid ; |
---|
| 79 | predicates:title ?title ; |
---|
| 80 | predicates:description ?description ; |
---|
[158] | 81 | predicates:style ?style ; ' |
---|
| 82 | . $uid . $title . $description . $style . ' |
---|
[131] | 83 | }'; |
---|
| 84 | |
---|
| 85 | //Query the model |
---|
| 86 | $results = $this->model->sparqlQuery($querystring); |
---|
| 87 | $applications = array(); |
---|
[149] | 88 | if(!empty($results)) |
---|
| 89 | { |
---|
[131] | 90 | //Run over all results and create appropriate Application objets |
---|
[149] | 91 | foreach($results as $result) |
---|
| 92 | { |
---|
| 93 | $applications[] = new Application($result['?uid']->label, $result['?title']->label, $result['?description']->label, $result['?style']->label); |
---|
[131] | 94 | } |
---|
| 95 | } |
---|
| 96 | return $applications; |
---|
| 97 | } |
---|
[149] | 98 | |
---|
[131] | 99 | /** |
---|
| 100 | * function set() |
---|
| 101 | * Finds the required databaseentry matching the given object and removes |
---|
| 102 | * it from the rdf file. Then adds the new RDF entry matching the object. |
---|
| 103 | * @param type $rToolObject: The ResearchToolObject to be saved. |
---|
| 104 | */ |
---|
[149] | 105 | public function set($rToolObject) |
---|
| 106 | { |
---|
[131] | 107 | $this->load(); |
---|
[149] | 108 | $resourceApplication = new Resource(APPLICATION.'/'.$rToolObject->uid); |
---|
[131] | 109 | //Remove the old value stored with the given id |
---|
| 110 | $this->model->subtract($this->model->find($resourceApplication, null, null)); |
---|
| 111 | |
---|
| 112 | //Add the new statements to the model |
---|
| 113 | $resourceApplicationType = new Resource(APPLICATION); |
---|
| 114 | $predicateRType = new Resource(RTYPE); |
---|
[149] | 115 | $this->model->add(new Statement($resourceApplication,$predicateRType,$resourceApplicationType)); |
---|
| 116 | |
---|
[131] | 117 | $literalApplicationID = new Literal($rToolObject->uid); |
---|
| 118 | $predicateUniqueID = new Resource(UID); |
---|
[149] | 119 | $this->model->add(new Statement($resourceApplication,$predicateUniqueID,$literalApplicationID)); |
---|
[131] | 120 | |
---|
| 121 | $applicationTitle = new Literal($rToolObject->title); |
---|
[149] | 122 | $predicateTitle = new Resource(TITLE); |
---|
| 123 | $this->model->add(new Statement($resourceApplication,$predicateTitle,$applicationTitle)); |
---|
[131] | 124 | |
---|
| 125 | $applicationDescription = new Literal($rToolObject->description); |
---|
| 126 | $predicateDescription = new Resource(DESCRIPTION); |
---|
[149] | 127 | $this->model->add(new Statement($resourceApplication,$predicateDescription,$applicationDescription)); |
---|
[131] | 128 | |
---|
| 129 | $applicationStyle = new Literal($rToolObject->style); |
---|
| 130 | $predicateStyle = new Resource(STYLE); |
---|
[149] | 131 | $this->model->add(new Statement($resourceApplication,$predicateStyle,$applicationStyle)); |
---|
| 132 | |
---|
[158] | 133 | $this->save(); |
---|
[131] | 134 | } |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | ?> |
---|