source: Dev/trunk/classes/ApplicationConnector.php @ 171

Last change on this file since 171 was 171, checked in by jkraaijeveld, 13 years ago

Iteration of refactoring in the Connector code.

  • Abstracted load() and save() to a new superclass Connector. Every connector extends Connector.
  • Query arguments are now correctly created by Connector's createArguments() function, removing redundant if/if/if statements in every get() function.
  • Every sidefunction which previously used a seperate query to get data now uses the RAP findRegex method, should increase performance.
  • Question arguments have been changed slightly: 'uid' is now 'code' and 'answers' is now 'definedanswers' to avoid confusion.
File size: 3.7 KB
Line 
1<?php
2
3// Survey database interface class as intermediate for storing data from the site to the RDF database
4require_once 'rdfConstants.php';
5// Include RAP Library to write RDF files
6include(RDFAPI_INCLUDE_DIR . "RDFAPI.php");
7
8/**
9 * Description of ApplicationConnector
10 *
11 * @author jkraaijeveld
12 */
13class ApplicationConnector extends Connector{
14   
15    /**
16     * Constructor for ApplicationConnector.
17     */
18    public function __construct()
19    {
20                $this->fileName = 'data/applications/applications.rdf';
21        //Ensure the required folder for this connector exists
22        if (!is_dir('data/applications/'))
23            mkdir('data/applications/');       
24    }
25   
26    /**
27     * function get($arguments)
28     * @param type $arguments : An array containing zero or more of the following keys:
29     * 'uid', 'title', 'description', 'style'
30     */
31    public function get($arguments) {
32        $this->load();
33
34        //Create the querystring
35        $querystring = '
36            PREFIX  predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
37            PREFIX  resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
38            SELECT ?uid, ?title, ?description, ?style
39            WHERE       
40            {
41                    _application        predicates:resource_type        resources:application ;
42                                        predicates:uid ?uid ;
43                                        predicates:title ?title ;
44                                        predicates:description ?description ;
45                                        predicates:style ?style ; '
46                    . $this->createArguments($arguments) . '
47            }';
48
49        //Query the model
50        $results = $this->model->sparqlQuery($querystring);
51        $applications = array();
52        if(!empty($results))
53        {
54            //Run over all results and create appropriate Application objets
55            foreach($results as $result)
56            {
57                    $applications[] = new Application($result['?uid']->label, $result['?title']->label, $result['?description']->label, $result['?style']->label);
58            }
59        }
60        return $applications;
61    }
62   
63    /**
64     * function set()
65     * Finds the required databaseentry matching the given object and removes
66     * it from the rdf file. Then adds the new RDF entry matching the object.
67     * @param type $rToolObject: The ResearchToolObject to be saved.
68     */
69    public function set($rToolObject)
70    {
71        $this->load();
72        $resourceApplication = new Resource(APPLICATION.'/'.$rToolObject->uid);
73        //Remove the old value stored with the given id
74        $this->model->subtract($this->model->find($resourceApplication, null, null));
75
76        //Add the new statements to the model
77        $resourceApplicationType = new Resource(APPLICATION);
78        $predicateRType = new Resource(RTYPE);
79        $this->model->add(new Statement($resourceApplication,$predicateRType,$resourceApplicationType));
80               
81        $literalApplicationID = new Literal($rToolObject->uid);
82        $predicateUniqueID = new Resource(UID);
83        $this->model->add(new Statement($resourceApplication,$predicateUniqueID,$literalApplicationID));
84
85        $applicationTitle = new Literal($rToolObject->title);
86        $predicateTitle = new Resource(TITLE); 
87        $this->model->add(new Statement($resourceApplication,$predicateTitle,$applicationTitle));
88
89        $applicationDescription = new Literal($rToolObject->description);
90        $predicateDescription = new Resource(DESCRIPTION);
91        $this->model->add(new Statement($resourceApplication,$predicateDescription,$applicationDescription));
92
93        $applicationStyle = new Literal($rToolObject->style);
94        $predicateStyle = new Resource(STYLE);
95        $this->model->add(new Statement($resourceApplication,$predicateStyle,$applicationStyle));
96               
97                $this->save();
98    }
99}
100
101?>
Note: See TracBrowser for help on using the repository browser.