*/ abstract class AbstractCreateOrReuse extends ConfigurableService implements CreateOrReuseInterface { use OntologyAwareTrait; /** * Returns the common parent all resources have * * @return \core_kernel_classes_Class */ abstract public function getRootClass(); /** * List of keys that need to be identical between * two resources to represent equivalence * * @return string[] */ abstract public function getUniquePredicates(); /** * WILL break on non smooth implementations * * @return ComplexSearchService */ protected function getSearchService() { return $this->getModel()->getSearchInterface(); } /** * * @param array $values * @return TaoResultSet */ protected function searchResource(array $values) { $searchService = $this->getSearchService(); $gateWay = $searchService->getGateway(); $searchQueryBuilder = $gateWay->query(); $searchService->searchType($searchQueryBuilder, $this->getRootClass()->getUri(), true); $criterion = $searchQueryBuilder->newQuery(); foreach ($this->getUniquePredicates() as $field) { if (array_key_exists($field, $values)) { $value = $values[$field]; $criterion->add($field)->equals($value); } else { \common_Logger::i('Predicate ' . $field . ' is not found.'); } } $searchQueryBuilder->setCriteria($criterion)->setLimit(1); return $gateWay->search($searchQueryBuilder); } /** * return a new resource * @param array $values * @return core_kernel_classes_Resource */ protected function createResource(array $values) { return $this->getRootClass()->createInstanceWithProperties($values); } /** * * @param array $values * @return boolean * @throws DuplicateResourceException */ public function hasResource(array $values) { $result = $this->searchResource($values); $count = $result->count(); if ($count === 1) { return true; } elseif ($count === 0) { return false; } else { throw new DuplicateResourceException($this->getRootClass()->getUri(), $values); } } /** * * @param array $values * @return core_kernel_classes_Resource * @throws DuplicateResourceException */ public function getResource(array $values) { $result = $this->searchResource($values); $count = $result->count(); if ($count === 1) { return $result->current(); } elseif ($count === 0) { return $this->createResource($values); } else { throw new DuplicateResourceException($this->getRootClass()->getUri(), $values); } } }