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

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

Refactored queries / cleaned up some code.

File size: 4.9 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     * @param type $arguments : An array containing zero or more of the following keys:
52     * 'uid', 'title', 'description', 'style'
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
59        $uid = ""; $title = ""; $description = ""; $style = "";
60        //Set the arguments if they are supplied
61        if(in_array("uid", $keys))
62            $uid = "predicates:uid \"".$arguments["uid"]."\"";
63        if(in_array("title", $keys))
64            $title = 'predicates:title \''.$arguments["title"].'\'';
65        if(in_array("description", $keys))
66            $description = "predicates:description \"".$arguments["description"]."\"";
67        if(in_array("style", $keys))
68            $style = "predicates:style \"".$arguments["style"]."\"";   
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 ;
81                                        predicates:style ?style ; '
82                    . $uid . $title . $description . $style . '
83            }';
84
85        //Query the model
86        $results = $this->model->sparqlQuery($querystring);
87        $applications = array();
88        if(!empty($results))
89        {
90            //Run over all results and create appropriate Application objets
91            foreach($results as $result)
92            {
93                    $applications[] = new Application($result['?uid']->label, $result['?title']->label, $result['?description']->label, $result['?style']->label);
94            }
95        }
96        return $applications;
97    }
98   
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     */
105    public function set($rToolObject)
106    {
107        $this->load();
108        $resourceApplication = new Resource(APPLICATION.'/'.$rToolObject->uid);
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);
115        $this->model->add(new Statement($resourceApplication,$predicateRType,$resourceApplicationType));
116               
117        $literalApplicationID = new Literal($rToolObject->uid);
118        $predicateUniqueID = new Resource(UID);
119        $this->model->add(new Statement($resourceApplication,$predicateUniqueID,$literalApplicationID));
120
121        $applicationTitle = new Literal($rToolObject->title);
122        $predicateTitle = new Resource(TITLE); 
123        $this->model->add(new Statement($resourceApplication,$predicateTitle,$applicationTitle));
124
125        $applicationDescription = new Literal($rToolObject->description);
126        $predicateDescription = new Resource(DESCRIPTION);
127        $this->model->add(new Statement($resourceApplication,$predicateDescription,$applicationDescription));
128
129        $applicationStyle = new Literal($rToolObject->style);
130        $predicateStyle = new Resource(STYLE);
131        $this->model->add(new Statement($resourceApplication,$predicateStyle,$applicationStyle));
132               
133                $this->save();
134    }
135}
136
137?>
Note: See TracBrowser for help on using the repository browser.