diff --git a/library/Opus/Document.php b/library/Opus/Document.php index c81f24d0..147f3f68 100644 --- a/library/Opus/Document.php +++ b/library/Opus/Document.php @@ -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; @@ -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; @@ -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. * diff --git a/library/Opus/DocumentRepository.php b/library/Opus/DocumentRepository.php new file mode 100644 index 00000000..37e34a2e --- /dev/null +++ b/library/Opus/DocumentRepository.php @@ -0,0 +1,103 @@ +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); + } + } + } +} diff --git a/library/Opus/Model/Plugin/AbstractCollection.php b/library/Opus/Model/Plugin/AbstractCollection.php index 528fd360..8c4b42e5 100644 --- a/library/Opus/Model/Plugin/AbstractCollection.php +++ b/library/Opus/Model/Plugin/AbstractCollection.php @@ -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; @@ -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() + ); } } diff --git a/library/Opus/Model/Plugin/InvalidateDocumentCache.php b/library/Opus/Model/Plugin/InvalidateDocumentCache.php index 3c48bd04..ef8c0195 100644 --- a/library/Opus/Model/Plugin/InvalidateDocumentCache.php +++ b/library/Opus/Model/Plugin/InvalidateDocumentCache.php @@ -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; @@ -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 + ); } } diff --git a/library/Opus/ModelFactory.php b/library/Opus/ModelFactory.php index 17e11f02..a65d4a01 100644 --- a/library/Opus/ModelFactory.php +++ b/library/Opus/ModelFactory.php @@ -31,8 +31,11 @@ namespace Opus; +use Opus\Common\Model\ModelException; use Opus\Common\Model\ModelFactoryInterface; +use function class_exists; + class ModelFactory implements ModelFactoryInterface { /** @@ -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(); } @@ -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); + } } } diff --git a/library/Opus/Person.php b/library/Opus/Person.php index f55a14ce..28c262e6 100644 --- a/library/Opus/Person.php +++ b/library/Opus/Person.php @@ -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 - * @author Ralf Claußnitzer - * @author Jens Schwidder */ 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; @@ -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 + ); } } } diff --git a/tests/Opus/DocumentRepositoryTest.php b/tests/Opus/DocumentRepositoryTest.php new file mode 100644 index 00000000..9b6af2b6 --- /dev/null +++ b/tests/Opus/DocumentRepositoryTest.php @@ -0,0 +1,90 @@ +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())); + } +} diff --git a/tests/Opus/DocumentTest.php b/tests/Opus/DocumentTest.php index 5adacf8b..8fc3ae46 100644 --- a/tests/Opus/DocumentTest.php +++ b/tests/Opus/DocumentTest.php @@ -48,8 +48,6 @@ use Opus\Common\Date; use Opus\Common\Model\ModelException; use Opus\Common\Model\NotFoundException; -use Opus\Db\Documents; -use Opus\Db\TableGateway; use Opus\DnbInstitute; use Opus\Document; use Opus\Enrichment; @@ -1579,25 +1577,6 @@ private function checkPersonAuthorSortOrderForDocument($document) $this->assertEquals(count($authors), count($uniqueNumbers)); } - public function testGetEarliestPublicationDate() - { - $nullDate = Document::getEarliestPublicationDate(); - $this->assertNull($nullDate, "Expected NULL on empty database."); - - // Insert valid entry through framework. - $document = new Document(); - $document->setServerDatePublished('2011-06-01T00:00:00Z'); - $document->store(); - $validDate = Document::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 = Document::getEarliestPublicationDate(); - $this->assertNull($invalidDate, "Expected NULL on invalid date."); - } - public function testGetDefaultsForPublicationState() { $doc = new Document(); @@ -2241,31 +2220,6 @@ public function testIsNewRecord() $this->assertFalse($doc->isNewRecord()); } - public function testSetServerDateModifiedByIds() - { - $doc = new Document(); - $doc1Id = $doc->store(); - - $doc = new Document(); - $doc2Id = $doc->store(); - - $doc = new Document(); - $doc3Id = $doc->store(); - - $date = new Date('2016-05-10'); - - Document::setServerDateModifiedByIds($date, [1, 3]); - - $doc = new Document($doc1Id); - $this->assertEquals('2016-05-10', $doc->getServerDateModified()); - - $doc = new Document($doc2Id); - $this->assertNotEquals('2016-05-10', $doc->getServerDateModified()); - - $doc = new Document($doc3Id); - $this->assertEquals('2016-05-10', $doc->getServerDateModified()); - } - public function testSetServerStateInvalidValue() { $doc = new Document(); diff --git a/tests/Opus/ModelFactoryTest.php b/tests/Opus/ModelFactoryTest.php index d31bfe95..0a00164a 100644 --- a/tests/Opus/ModelFactoryTest.php +++ b/tests/Opus/ModelFactoryTest.php @@ -33,7 +33,11 @@ use Opus\Common\Document as CommonDocument; use Opus\Common\DocumentInterface; +use Opus\Common\Model\ModelException; use Opus\Document; +use Opus\DocumentRepository; +use Opus\ModelFactory; +use Opus\Person; use OpusTest\TestAsset\TestCase; class ModelFactoryTest extends TestCase @@ -64,4 +68,32 @@ public function testGet() $this->assertEquals($docId, $model->getId()); $this->assertEquals('article', $model->getType()); } + + public function testGetRepository() + { + $modelFactory = new ModelFactory(); + + $documentRepository = $modelFactory->getRepository('Document'); + + $this->assertInstanceOf(DocumentRepository::class, $documentRepository); + } + + public function testGetRepositoryFallbackToModelClass() + { + $modelFactory = new ModelFactory(); + + $personRepository = $modelFactory->getRepository('Person'); + + $this->assertInstanceOf(Person::class, $personRepository); + } + + public function testGetRepositoryUnknownType() + { + $modelFactory = new ModelFactory(); + + $this->expectException(ModelException::class); + $this->expectExceptionMessage('Model class not found: Opus\UnknownModel'); + + $modelFactory->getRepository('UnknownModel'); + } }