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