Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 66
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
tao_models_classes_import_RdfImporter
0.00% covered (danger)
0.00%
0 / 66
0.00% covered (danger)
0.00%
0 / 5
650
0.00% covered (danger)
0.00%
0 / 1
 getLabel
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getForm
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 import
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
20
 flatImport
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
12
 importProperties
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 1
272
1<?php
2
3/**
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; under version 2
7 * of the License (non-upgradable).
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 *
18 * Copyright (c) 2013 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
19 *
20 *
21 */
22
23use EasyRdf\Format;
24use EasyRdf\Graph;
25use oat\generis\model\OntologyRdf;
26use oat\generis\model\OntologyRdfs;
27use oat\oatbox\event\EventManagerAwareTrait;
28use oat\tao\model\event\RdfImportEvent;
29use oat\tao\model\import\ImportHandlerHelperTrait;
30use oat\tao\model\import\TaskParameterProviderInterface;
31use Zend\ServiceManager\ServiceLocatorAwareInterface;
32
33/**
34 * RDF Import Handler
35 *
36 * @access  public
37 * @author  Joel Bout, <joel@taotesting.com>
38 * @package tao
39 */
40class tao_models_classes_import_RdfImporter implements
41    tao_models_classes_import_ImportHandler,
42    ServiceLocatorAwareInterface,
43    TaskParameterProviderInterface
44{
45    use EventManagerAwareTrait;
46    use ImportHandlerHelperTrait;
47
48    /**
49     * (non-PHPdoc)
50     * @see tao_models_classes_import_ImportHandler::getLabel()
51     */
52    public function getLabel()
53    {
54        return __('RDF');
55    }
56
57    /**
58     * (non-PHPdoc)
59     * @see tao_models_classes_import_ImportHandler::getForm()
60     */
61    public function getForm()
62    {
63        $form = new tao_models_classes_import_RdfImportForm();
64
65        return $form->getForm();
66    }
67
68    /**
69     * @param core_kernel_classes_Class $class
70     * @param tao_helpers_form_Form|array $form
71     * @param string|null $userId owner of the resource
72     * @return common_report_Report
73     * @throws common_Exception
74     * @throws common_exception_Error
75     */
76    public function import($class, $form, $userId = null)
77    {
78        $parser = new tao_models_classes_Parser($this->fetchUploadedFile($form), ['extension' => 'rdf']);
79        $parser->validate();
80
81        if (!$parser->isValid()) {
82            $report = common_report_Report::createFailure(__('Nothing imported'));
83            $report->add($parser->getReport());
84        } else {
85            $report = $this->flatImport($parser->getContent(), $class);
86
87            if (!$report->containsError()) {
88                $this->getUploadService()->remove($parser->getSource());
89            }
90
91            if ($report->getType() == common_report_Report::TYPE_SUCCESS) {
92                $this->getEventManager()->trigger(new RdfImportEvent($report));
93            }
94        }
95
96        return $report;
97    }
98
99    /**
100     * Imports the rdf file into the selected class
101     *
102     * @param string                    $content
103     * @param core_kernel_classes_Class $class
104     * @return common_report_Report
105     */
106    protected function flatImport($content, core_kernel_classes_Class $class)
107    {
108        $report = common_report_Report::createSuccess(__('Data imported successfully'));
109
110        $graph = new Graph();
111        $graph->parse($content);
112
113        // keep type property
114        $map = [
115            OntologyRdf::RDF_PROPERTY => OntologyRdf::RDF_PROPERTY
116        ];
117
118        foreach ($graph->resources() as $resource) {
119            $map[$resource->getUri()] = common_Utils::getNewUri();
120        }
121
122        $format = Format::getFormat('php');
123        $data = $graph->serialise($format);
124
125        foreach ($data as $subjectUri => $propertiesValues) {
126            $resource = new core_kernel_classes_Resource($map[$subjectUri]);
127            $subreport = $this->importProperties($resource, $propertiesValues, $map, $class);
128            $report->add($subreport);
129        }
130
131        return $report;
132    }
133
134    /**
135     * Import the properties of the resource
136     *
137     * @param core_kernel_classes_Resource $resource
138     * @param array                        $propertiesValues
139     * @param array                        $map
140     * @param core_kernel_classes_Class    $class
141     * @return common_report_Report
142     */
143    protected function importProperties(core_kernel_classes_Resource $resource, $propertiesValues, $map, $class)
144    {
145        if (isset($propertiesValues[OntologyRdf::RDF_TYPE])) {
146            // assuming single Type
147            if (count($propertiesValues[OntologyRdf::RDF_TYPE]) > 1) {
148                return new common_report_Report(
149                    common_report_Report::TYPE_ERROR,
150                    __('Resource not imported due to multiple types')
151                );
152            } else {
153                foreach ($propertiesValues[OntologyRdf::RDF_TYPE] as $k => $v) {
154                    $classType = isset($map[$v['value']])
155                        ? new core_kernel_classes_Class($map[$v['value']])
156                        : $class;
157                    //$resource->setType($classType);
158                    $classType->createInstance(null, null, $resource->getUri());
159                }
160            }
161            unset($propertiesValues[OntologyRdf::RDF_TYPE]);
162        }
163
164        if (isset($propertiesValues[OntologyRdfs::RDFS_SUBCLASSOF])) {
165            $resource = new core_kernel_classes_Class($resource);
166            // assuming single subclass
167            if (
168                isset($propertiesValues[OntologyRdf::RDF_TYPE])
169                && count($propertiesValues[OntologyRdf::RDF_TYPE]) > 1
170            ) {
171                return new common_report_Report(
172                    common_report_Report::TYPE_ERROR,
173                    __('Resource not imported due to multiple super classes')
174                );
175            }
176            foreach ($propertiesValues[OntologyRdfs::RDFS_SUBCLASSOF] as $k => $v) {
177                $classSup = isset($map[$v['value']])
178                    ? new core_kernel_classes_Class($map[$v['value']])
179                    : $class;
180                $resource->setSubClassOf($classSup);
181            }
182
183            unset($propertiesValues[OntologyRdfs::RDFS_SUBCLASSOF]);
184        }
185        foreach ($propertiesValues as $prop => $values) {
186            $property = new core_kernel_classes_Property(isset($map[$prop]) ? $map[$prop] : $prop);
187            foreach ($values as $k => $v) {
188                $value = isset($map[$v['value']]) ? $map[$v['value']] : $v['value'];
189                if (isset($v['lang'])) {
190                    $resource->setPropertyValueByLg($property, $value, $v['lang']);
191                } else {
192                    $resource->setPropertyValue($property, $value);
193                }
194            }
195        }
196        $msg = $resource instanceof core_kernel_classes_Class
197            ? __('Successfully imported class "%s"', $resource->getLabel())
198            : __('Successfully imported "%s"', $resource->getLabel());
199
200        return new common_report_Report(common_report_Report::TYPE_SUCCESS, $msg, $resource);
201    }
202}