Skip to content

Commit

Permalink
Merge pull request #951 from oat-sa/feat/adf-836/imrpove-event-logs
Browse files Browse the repository at this point in the history
Feature/ADF-836/Improve EventLogs for storing all URIs of deleted resources
  • Loading branch information
shpran authored Dec 20, 2021
2 parents 9d85b70 + 160486a commit 60122e7
Show file tree
Hide file tree
Showing 21 changed files with 1,613 additions and 71 deletions.
71 changes: 71 additions & 0 deletions core/Context/AbstractContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2021 (original work) Open Assessment Technologies SA;
*/

declare(strict_types=1);

namespace oat\generis\model\Context;

use InvalidArgumentException;

abstract class AbstractContext implements ContextInterface
{
protected $parameters = [];

public function __construct(array $parameters)
{
foreach ($parameters as $parameter => $parameterValue) {
$this->setParameter($parameter, $parameterValue);
}
}

public function getParameter(string $parameter, $default = null)
{
$this->checkParameterSupport($parameter);

return $this->parameters[$parameter] ?? $default;
}

public function setParameter(string $parameter, $parameterValue): void
{
$this->checkParameterSupport($parameter);
$this->validateParameter($parameter, $parameterValue);

$this->parameters[$parameter] = $parameterValue;
}

abstract protected function getSupportedParameters(): array;

/**
* @param mixed $parameterValue
*/
abstract protected function validateParameter(string $parameter, $parameterValue): void;

private function checkParameterSupport(string $parameter): void
{
if (!in_array($parameter, $this->getSupportedParameters(), true)) {
throw new InvalidArgumentException(
sprintf(
'Context parameter %s is not supported.',
$parameter
)
);
}
}
}
38 changes: 38 additions & 0 deletions core/Context/ContextInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2021 (original work) Open Assessment Technologies SA;
*/

declare(strict_types=1);

namespace oat\generis\model\Context;

interface ContextInterface
{
/**
* @param mixed $default
*
* @return mixed
*/
public function getParameter(string $parameter, $default = null);

/**
* @param mixed $parameterValue
*/
public function setParameter(string $parameter, $parameterValue): void;
}
48 changes: 46 additions & 2 deletions core/data/event/ClassDeletedEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,21 @@

namespace oat\generis\model\data\event;

use core_kernel_classes_Class;
use JsonSerializable;
use oat\oatbox\event\Event;
use core_kernel_classes_Class;

class ClassDeletedEvent implements Event
class ClassDeletedEvent implements Event, JsonSerializable
{
/** @var core_kernel_classes_Class */
private $class;

/** @var core_kernel_classes_Class|null */
private $selectedClass;

/** @var core_kernel_classes_Class|null */
private $parentClass;

public function __construct(core_kernel_classes_Class $class)
{
$this->class = $class;
Expand All @@ -44,4 +51,41 @@ public function getClass(): core_kernel_classes_Class
{
return $this->class;
}

public function setSelectedClass(?core_kernel_classes_Class $class): self
{
$this->selectedClass = $class;

return $this;
}

public function setParentClass(?core_kernel_classes_Class $class): self
{
$this->parentClass = $class;

return $this;
}

public function jsonSerialize(): array
{
$data = [
'uri' => $this->class->getUri(),
];

if ($this->selectedClass !== null && $this->selectedClass !== $this->class) {
$data['selectedClass'] = [
'uri' => $this->selectedClass->getUri(),
'label' => $this->selectedClass->getLabel(),
];
}

if ($this->parentClass !== null) {
$data['parentClass'] = [
'uri' => $this->parentClass->getUri(),
'label' => $this->parentClass->getLabel(),
];
}

return $data;
}
}
68 changes: 53 additions & 15 deletions core/data/event/ResourceDeleted.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,29 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2018 (original work) Open Assessment Technologies SA;
*
*
* Copyright (c) 2018-2021 (original work) Open Assessment Technologies SA.
*/

declare(strict_types=1);

namespace oat\generis\model\data\event;

use JsonSerializable;
use oat\oatbox\event\Event;
use core_kernel_classes_Class;
use core_kernel_classes_Resource;

/**
* Class ResourceDeleted
* @package oat\generis\model\data\event
*/
class ResourceDeleted implements Event
class ResourceDeleted implements Event, JsonSerializable
{
/** @var string */
private $uri;

/** @var core_kernel_classes_Class|null */
private $selectedClass;

/** @var core_kernel_classes_Resource|null */
private $parentClass;

/**
* @param string $uri
*/
Expand All @@ -40,21 +46,53 @@ function __construct($uri)
$this->uri = $uri;
}

/**
* Return the URI of the deleted resource
* @return string
*/
function getId()
function getId(): string
{
return $this->uri;
}

/**
* (non-PHPdoc)
* @see \oat\oatbox\event\Event::getName()
* {@inheritdoc}
*/
function getName()
{
return __CLASS__;
}

public function setSelectedClass(?core_kernel_classes_Class $class): self
{
$this->selectedClass = $class;

return $this;
}

public function setParentClass(?core_kernel_classes_Class $class): self
{
$this->parentClass = $class;

return $this;
}

public function jsonSerialize(): array
{
$data = [
'uri' => $this->uri,
];

if ($this->selectedClass !== null) {
$data['selectedClass'] = [
'uri' => $this->selectedClass->getUri(),
'label' => $this->selectedClass->getLabel(),
];
}

if ($this->parentClass !== null) {
$data['parentClass'] = [
'uri' => $this->parentClass->getUri(),
'label' => $this->parentClass->getLabel(),
];
}

return $data;
}
}
47 changes: 30 additions & 17 deletions core/kernel/classes/class.Class.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,27 @@
* Copyright (c) 2002-2008 (original work) Public Research Centre Henri Tudor & University of Luxembourg (under the project TAO & TAO2);
* 2008-2010 (update and modification) Deutsche Institut für Internationale Pädagogische Forschung (under the project TAO-TRANSFER);
* 2009-2012 (update and modification) Public Research Centre Henri Tudor (under the project TAO-SUSTAIN & TAO-DEV);
* 2017 (update and modification) Open Assessment Technologies SA (under the project TAO-PRODUCT);
* 2017-2021 (update and modification) Open Assessment Technologies SA.
*/

use oat\generis\model\data\event\ClassDeletedEvent;
use oat\generis\model\data\event\ResourceCreated;
use oat\generis\model\OntologyRdf;
use oat\generis\model\resource\ResourceCollection;
use oat\oatbox\event\EventManager;
use oat\oatbox\event\EventManagerAwareTrait;
use oat\generis\model\data\event\ResourceCreated;
use oat\generis\model\resource\ResourceCollection;
use oat\generis\model\resource\Repository\ClassRepository;
use oat\generis\model\resource\Context\ResourceRepositoryContext;
use oat\generis\model\resource\Contract\ResourceRepositoryInterface;

/**
* The class of rdfs:classes. It implements basic tests like isSubClassOf(Class
* instances, properties and subclasses retrieval, but also enable to edit it
* setSubClassOf setProperty, etc.
*
*
* @author [email protected]
* @package generis
*
* @see http://www.w3.org/RDF/
* @see http://www.w3.org/TR/rdf-schema/
*
*/
class core_kernel_classes_Class extends core_kernel_classes_Resource
{
Expand Down Expand Up @@ -425,22 +425,30 @@ public function deleteInstances($resources, $deleteReference = false)
}

/**
* Short description of method delete
* @deprecated Use \oat\generis\model\resource\Repository\ClassRepository::delete() instead
*
* @access public
* @author Jerome Bogaerts, <[email protected]>
* @param boolean deleteReference
* @return boolean
*
* @param bool deleteReference
*
* @return bool
*/
public function delete($deleteReference = false)
{
$delete = (bool)$this->getImplementation()->delete($this, $deleteReference);

if ($delete) {
$this->getEventManager()->trigger(new ClassDeletedEvent($this));
try {
$this->getClassRepository()->delete(
new ResourceRepositoryContext(
[
ResourceRepositoryContext::PARAM_CLASS => $this,
ResourceRepositoryContext::PARAM_DELETE_REFERENCE => $deleteReference,
]
)
);

return true;
} catch (Throwable $exception) {
return false;
}

return $delete;
}

/**
Expand All @@ -458,4 +466,9 @@ public function exists()
// we know that the class exists.
return (bool) (count($this->getParentClasses(false)) > 0);
}

private function getClassRepository(): ResourceRepositoryInterface
{
return $this->getServiceManager()->getContainer()->get(ClassRepository::class);
}
}
Loading

0 comments on commit 60122e7

Please sign in to comment.