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

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

Added missing classes for the database connection

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