Skip to content

Commit

Permalink
#291 DocumentRepository implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
j3nsch committed Aug 10, 2022
1 parent b7a0e4b commit e3cfd76
Show file tree
Hide file tree
Showing 9 changed files with 258 additions and 115 deletions.
57 changes: 0 additions & 57 deletions library/Opus/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
use Opus\Common\Model\ModelException;
use Opus\Common\ServerStateConstantsInterface;
use Opus\Common\Storage\FileNotFoundException;
use Opus\Db\TableGateway;
use Opus\Document\DocumentException;
use Opus\Identifier\Urn;
use Opus\Identifier\UUID;
Expand All @@ -73,7 +72,6 @@
use function is_null;
use function is_numeric;
use function is_object;
use function preg_match;
use function reset;
use function strtolower;
use function substr;
Expand Down Expand Up @@ -659,61 +657,6 @@ public static function getAll(?array $ids = null)
return self::getAllFrom(self::class, Db\Documents::class, $ids);
}

/**
* Returns the earliest date (server_date_published) of all documents.
*
* @deprecated
*
* TODO still in use in Application
*
* @return string|null /^\d{4}-\d{2}-\d{2}$/ on success, null otherwise
*/
public static function getEarliestPublicationDate()
{
$table = TableGateway::getInstance(Db\Documents::class);
$select = $table->select()->from($table, 'min(server_date_published) AS min_date')
->where('server_date_published IS NOT NULL')
->where('TRIM(server_date_published) != \'\'');
$timestamp = $table->fetchRow($select)->toArray();

if (! isset($timestamp['min_date'])) {
return null;
}

$matches = [];
if (preg_match("/^(\d{4}-\d{2}-\d{2})T/", $timestamp['min_date'], $matches) > 0) {
return $matches[1];
}
return null;
}

/**
* Bulk update of ServerDateModified for documents matching selection
*
* @param Date $date Date-Object holding the date to be set
* @param array $ids array of document ids
*/
public static function setServerDateModifiedByIds($date, $ids)
{
// Update wird nur ausgeführt, wenn IDs übergeben werden
if ($ids === null || count($ids) === 0) {
return;
}

$table = TableGateway::getInstance(self::$tableGatewayClass);

$where = $table->getAdapter()->quoteInto('id IN (?)', $ids);

try {
$table->update(['server_date_modified' => "$date"], $where);
} catch (Exception $e) {
$logger = Log::get();
if ($logger !== null) {
$logger->err(__METHOD__ . ' ' . $e);
}
}
}

/**
* Fetch all Opus\Collection objects for this document.
*
Expand Down
103 changes: 103 additions & 0 deletions library/Opus/DocumentRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

/**
* This file is part of OPUS. The software OPUS has been originally developed
* at the University of Stuttgart with funding from the German Research Net,
* the Federal Department of Higher Education and Research and the Ministry
* of Science, Research and the Arts of the State of Baden-Wuerttemberg.
*
* OPUS 4 is a complete rewrite of the original OPUS software and was developed
* by the Stuttgart University Library, the Library Service Center
* Baden-Wuerttemberg, the Cooperative Library Network Berlin-Brandenburg,
* the Saarland University and State Library, the Saxon State Library -
* Dresden State and University Library, the Bielefeld University Library and
* the University Library of Hamburg University of Technology with funding from
* the German Research Foundation and the European Regional Development Fund.
*
* LICENCE
* OPUS 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; either version 2 of the Licence, or any later version.
* OPUS 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 OPUS; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @copyright Copyright (c) 2022, OPUS 4 development team
* @license http://www.gnu.org/licenses/gpl.html General Public License
*/

namespace Opus;

use Exception;
use Opus\Common\Date;
use Opus\Common\DocumentRepositoryInterface;
use Opus\Common\Log;
use Opus\Db\TableGateway;

use function count;
use function is_array;
use function preg_match;

class DocumentRepository implements DocumentRepositoryInterface
{
/** @var string TableGateway class for 'documents' table TODO use constant */
protected static $documentTableClass = Db\Documents::class;

/**
* Returns the earliest date (server_date_published) of all documents.
*
* TODO return Date instead of string
*
* @return string|null /^\d{4}-\d{2}-\d{2}$/ on success, null otherwise
*/
public function getEarliestPublicationDate()
{
$table = TableGateway::getInstance(self::$documentTableClass);
$select = $table->select()->from($table, 'min(server_date_published) AS min_date')
->where('server_date_published IS NOT NULL')
->where('TRIM(server_date_published) != \'\'');
$timestamp = $table->fetchRow($select)->toArray();

if (! isset($timestamp['min_date'])) {
return null;
}

$matches = [];
if (preg_match("/^(\d{4}-\d{2}-\d{2})T/", $timestamp['min_date'], $matches) > 0) {
return $matches[1];
}
return null;
}

/**
* Bulk update of ServerDateModified for documents matching selection
*
* TODO remove support for string date? - check if it works at all
*
* @param string|Date $date Date-Object holding the date to be set
* @param int[] $ids array of document ids
*/
public function setServerDateModifiedForDocuments($date, $ids)
{
// Update wird nur ausgeführt, wenn IDs übergeben werden
if ($ids === null || ! is_array($ids) || count($ids) === 0) {
return;
}

$table = TableGateway::getInstance(self::$documentTableClass);

$where = $table->getAdapter()->quoteInto('id IN (?)', $ids);

try {
$table->update(['server_date_modified' => "$date"], $where);
} catch (Exception $e) {
$logger = Log::get();
if ($logger !== null) {
$logger->err(__METHOD__ . ' ' . $e);
}
}
}
}
6 changes: 5 additions & 1 deletion library/Opus/Model/Plugin/AbstractCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use Opus\Common\CollectionInterface;
use Opus\Common\Date;
use Opus\Common\Model\Plugin\AbstractPlugin;
use Opus\Common\Repository;
use Opus\Db\Collections;
use Opus\Db\TableGateway;
use Opus\Document;
Expand Down Expand Up @@ -73,6 +74,9 @@ protected function updateDocuments($collection)
$date = new Date();
$date->setNow();

Document::setServerDateModifiedByIds($date, $documentFinder->ids());
Repository::getInstance()->getModelRepository(Document::class)->setServerDateModifiedForDocuments(
$date,
$documentFinder->ids()
);
}
}
6 changes: 5 additions & 1 deletion library/Opus/Model/Plugin/InvalidateDocumentCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use Opus\Common\Date;
use Opus\Common\Model\ModelInterface;
use Opus\Common\Model\Plugin\AbstractPlugin;
use Opus\Common\Repository;
use Opus\Document;
use Opus\DocumentFinder;
use Opus\DocumentFinder\DocumentFinderException;
Expand Down Expand Up @@ -185,7 +186,10 @@ protected function invalidateDocumentCacheFor(ModelInterface $model)
if ($this->_updateServerDateModified) {
$date = new Date();
$date->setNow();
Document::setServerDateModifiedByIds($date, $ids);
Repository::getInstance()->getModelRepository(Document::class)->setServerDateModifiedForDocuments(
$date,
$ids
);
}
}

Expand Down
19 changes: 17 additions & 2 deletions library/Opus/ModelFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@

namespace Opus;

use Opus\Common\Model\ModelException;
use Opus\Common\Model\ModelFactoryInterface;

use function class_exists;

class ModelFactory implements ModelFactoryInterface
{
/**
Expand All @@ -46,6 +49,10 @@ public function create($type)

$modelClass = 'Opus\\' . $type;

if (! class_exists($modelClass)) {
throw new ModelException("Model class not found: $modelClass");
}

return new $modelClass();
}

Expand All @@ -64,10 +71,18 @@ public function get($type, $modelId)
/**
* @param string $type
* @return mixed
*
* TODO reuse repository instance
*/
public function getRepository($type)
{
// TODO in old implementation model classes also serve as "repositories"
return $this->create($type);
$repositoryClass = 'Opus\\' . $type . 'Repository';

if (class_exists($repositoryClass)) {
return new $repositoryClass();
} else {
// TODO in old implementation model classes also serve as "repositories"
return $this->create($type);
}
}
}
14 changes: 6 additions & 8 deletions library/Opus/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,15 @@
* along with OPUS; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @copyright Copyright (c) 2008-2018, OPUS 4 development team
* @copyright Copyright (c) 2008, OPUS 4 development team
* @license http://www.gnu.org/licenses/gpl.html General Public License
*
* @category Framework
* @package Opus
* @author Felix Ostrowski <[email protected]>
* @author Ralf Claußnitzer <[email protected]>
* @author Jens Schwidder <[email protected]>
*/

namespace Opus;

use Opus\Common\Date;
use Opus\Common\Model\ModelException;
use Opus\Common\Repository;
use Opus\Db\LinkPersonsDocuments;
use Opus\Db\TableGateway;
use Opus\Model\AbstractDb;
Expand Down Expand Up @@ -698,7 +693,10 @@ public static function updateAll($person, $changes, $documents = null)
$date = new Date();
$date->setNow();

Document::setServerDateModifiedByIds($date, $documentIds);
Repository::getInstance()->getModelRepository(Document::class)->setServerDateModifiedForDocuments(
$date,
$documentIds
);
}
}
}
Expand Down
90 changes: 90 additions & 0 deletions tests/Opus/DocumentRepositoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

/**
* This file is part of OPUS. The software OPUS has been originally developed
* at the University of Stuttgart with funding from the German Research Net,
* the Federal Department of Higher Education and Research and the Ministry
* of Science, Research and the Arts of the State of Baden-Wuerttemberg.
*
* OPUS 4 is a complete rewrite of the original OPUS software and was developed
* by the Stuttgart University Library, the Library Service Center
* Baden-Wuerttemberg, the Cooperative Library Network Berlin-Brandenburg,
* the Saarland University and State Library, the Saxon State Library -
* Dresden State and University Library, the Bielefeld University Library and
* the University Library of Hamburg University of Technology with funding from
* the German Research Foundation and the European Regional Development Fund.
*
* LICENCE
* OPUS 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; either version 2 of the Licence, or any later version.
* OPUS 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 OPUS; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @copyright Copyright (c) 2022, OPUS 4 development team
* @license http://www.gnu.org/licenses/gpl.html General Public License
*/

namespace OpusTest;

use Opus\Common\Date;
use Opus\Common\Document;
use Opus\Common\Repository;
use Opus\Db\Documents;
use Opus\Db\TableGateway;
use Opus\DocumentRepository;
use OpusTest\TestAsset\TestCase;

class DocumentRepositoryTest extends TestCase
{
public function testGetEarliestPublicationDate()
{
$documents = Repository::getInstance()->getModelRepository(Document::class);

$nullDate = $documents->getEarliestPublicationDate();
$this->assertNull($nullDate, "Expected NULL on empty database.");

// Insert valid entry through framework.
$document = Document::new();
$document->setServerDatePublished('2011-06-01T00:00:00Z');
$document->store();
$validDate = $documents->getEarliestPublicationDate();
$this->assertEquals('2011-06-01', $validDate);

// Insert invalid entry into database...
$table = TableGateway::getInstance(Documents::class);
$table->insert(['server_date_published' => '1234', 'server_date_created' => '1234']);
$invalidDate = $documents->getEarliestPublicationDate();
$this->assertNull($invalidDate, "Expected NULL on invalid date.");
}

public function testSetServerDateModifiedForDocuments()
{
$doc = Document::new();
$doc1Id = $doc->store();

$doc = Document::new();
$doc2Id = $doc->store();

$doc = Document::new();
$doc3Id = $doc->store();

$date = new Date('2016-05-10');

$documentRepository = new DocumentRepository();
$documentRepository->setServerDateModifiedForDocuments($date, [$doc1Id, $doc3Id]);

$doc = Document::get($doc1Id);
$this->assertEquals(0, $date->compare($doc->getServerDateModified()));

$doc = Document::get($doc2Id);
$this->assertNotEquals(0, $date->compare($doc->getServerDateModified()));

$doc = Document::get($doc3Id);
$this->assertEquals(0, $date->compare($doc->getServerDateModified()));
}
}
Loading

0 comments on commit e3cfd76

Please sign in to comment.