source: Dev/branches/jos-branch/classes/ApplicationConnector.php @ 128

Last change on this file since 128 was 128, checked in by jkraaijeveld, 14 years ago

Finished ApplicationConnector?

File size: 5.0 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["id"]."\"";
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                if(!empty($results))
92                        {
93                        $applications = array();
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                        //Return the list of application objects
100                        return $applications;
101                }
102                else
103                {
104                        return array();
105                }
106    }
107   
108    /**
109     * function set()
110     * Finds the required databaseentry matching the given object and removes
111     * it from the rdf file. Then adds the new RDF entry matching the object.
112     * @param type $rToolObject: The ResearchToolObject to be saved.
113     */
114    public function set($rToolObject)
115    {
116        $resourceApplication = new Resource(APPLICATION.'/'.$rToolObject->uid);
117                //Remove the old value stored with the given id
118                $this->model->subtract($this->model->find($resourceApplication, null, null));
119               
120                //Add the new statements to the model
121        $resourceApplicationType = new Resource(APPLICATION);
122        $predicateRType = new Resource(RTYPE);
123        $this->model->add(new Statement($resourceApplication,$predicateRType,$resourceApplicationType));
124               
125        $literalApplicationID = new Literal($rToolObject->uid);
126        $predicateUniqueID = new Resource(UID);
127        $this->model->add(new Statement($resourceApplication,$predicateUniqueID,$literalApplicationID));
128
129        $applicationTitle = new Literal($rToolObject->title);
130        $predicateTitle = new Resource(TITLE); 
131        $this->model->add(new Statement($resourceApplication,$predicateTitle,$applicationTitle));
132
133        $applicationDescription = new Literal($rToolObject->description);
134        $predicateDescription = new Resource(DESCRIPTION);
135        $this->model->add(new Statement($resourceApplication,$predicateDescription,$applicationDescription));
136
137        $applicationStyle = new Literal($rToolObject->style);
138        $predicateStyle = new Resource(STYLE);
139        $this->model->add(new Statement($resourceApplication,$predicateStyle,$applicationStyle));
140               
141                $this->save();
142    }
143}
144
145?>
Note: See TracBrowser for help on using the repository browser.