diff --git a/api/migrations/Version20240314120005.php b/api/migrations/Version20240314120005.php new file mode 100644 index 000000000..923adc92d --- /dev/null +++ b/api/migrations/Version20240314120005.php @@ -0,0 +1,39 @@ +addSql('CREATE SEQUENCE database_id_seq INCREMENT BY 1 MINVALUE 1 START 1'); + $this->addSql('CREATE TABLE database (id UUID NOT NULL, name VARCHAR(255) NOT NULL, description TEXT DEFAULT NULL, reference VARCHAR(255) NOT NULL, version VARCHAR(255) NOT NULL DEFAULT \'0.0.0\', uri VARCHAR(255) NOT NULL, date_created TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, date_modified TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, PRIMARY KEY(id))'); + $this->addSql('ALTER TABLE organization ADD database_id UUID DEFAULT NULL'); + $this->addSql('ALTER TABLE organization ADD CONSTRAINT FK_C1EE637CF0AA09DB FOREIGN KEY (database_id) REFERENCES database (id) NOT DEFERRABLE INITIALLY IMMEDIATE'); + $this->addSql('CREATE INDEX IDX_C1EE637CF0AA09DB ON organization (database_id)'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('ALTER TABLE organization DROP CONSTRAINT FK_C1EE637CF0AA09DB'); + $this->addSql('DROP INDEX IDX_C1EE637CF0AA09DB'); + $this->addSql('ALTER TABLE organization DROP database_id'); + $this->addSql('DROP SEQUENCE database_id_seq CASCADE'); + $this->addSql('DROP TABLE database'); + } +} diff --git a/api/src/Entity/Database.php b/api/src/Entity/Database.php new file mode 100644 index 000000000..1a2bbf60a --- /dev/null +++ b/api/src/Entity/Database.php @@ -0,0 +1,368 @@ +getName(); + } + + public function __construct() + { + $this->organizations = new ArrayCollection(); + } + + /** + * Create or update this Database from an external schema array. + * + * This function is used to update and create databases form database.json objects. + * + * @param array $schema The schema to load. + * + * @return $this This Database. + */ + public function fromSchema(array $schema): self + { + // Basic stuff + if (array_key_exists('$id', $schema)) { + $this->setReference($schema['$id']); + } + if (array_key_exists('version', $schema)) { + $this->setVersion($schema['version']); + } + + array_key_exists('title', $schema) ? $this->setName($schema['title']) : ''; + array_key_exists('description', $schema) ? $this->setDescription($schema['description']) : ''; + array_key_exists('uri', $schema) ? $this->setUri($schema['uri']) : ''; + + return $this; + } + + /** + * Convert this Gateway to a schema. + * + * @return array Schema array. + */ + public function toSchema(): array + { + // Do not return jwt, secret, password or apikey this way! + return [ + '$id' => $this->getReference(), //@todo dit zou een interne uri verwijzing moeten zijn maar hebben we nog niet + '$schema' => 'https://docs.commongateway.nl/schemas/Database.schema.json', + 'title' => $this->getName(), + 'description' => $this->getDescription(), + 'version' => $this->getVersion(), + 'name' => $this->getName(), + 'uri' => $this->getUri(), + ]; + } + + public function getId() + { + return $this->id; + } + + public function setId(string $id): self + { + $this->id = Uuid::fromString($id); + + return $this; + } + + public function getName(): ?string + { + return $this->name; + } + + public function setName(string $name): self + { + $this->name = $name; + + return $this; + } + + public function getDescription(): ?string + { + return $this->description; + } + + public function setDescription(?string $description): self + { + $this->description = $description; + + return $this; + } + + public function getReference(): ?string + { + return $this->reference; + } + + public function setReference(string $reference): self + { + $this->reference = $reference; + + return $this; + } + + public function getVersion(): ?string + { + return $this->version; + } + + public function setVersion(string $version): self + { + $this->version = $version; + + return $this; + } + + public function getUri(): ?string + { + return $this->uri; + } + + public function setUri(?string $uri): self + { + $this->uri = $uri; + + return $this; + } + + /** + * @return Collection|Organization[] + */ + public function getOrganizations(): Collection + { + return $this->organizations; + } + + public function addOrganization(Organization $organization): self + { + if (!$this->organizations->contains($organization)) { + $this->organizations[] = $organization; + $organization->setDatabase($this); + } + + return $this; + } + + public function removeOrganization(Organization $organization): self + { + if ($this->organizations->removeElement($organization)) { + // set the owning side to null (unless already changed) + if ($organization->getDatabase() === $this) { + $organization->setDatabase(null); + } + } + + return $this; + } + + public function getDateCreated(): ?DateTimeInterface + { + return $this->dateCreated; + } + + public function setDateCreated(DateTimeInterface $dateCreated): self + { + $this->dateCreated = $dateCreated; + + return $this; + } + + public function getDateModified(): ?DateTimeInterface + { + return $this->dateModified; + } + + public function setDateModified(DateTimeInterface $dateModified): self + { + $this->dateModified = $dateModified; + + return $this; + } +} diff --git a/api/src/Entity/ObjectEntity.php b/api/src/Entity/ObjectEntity.php index 8f9095279..416723afe 100644 --- a/api/src/Entity/ObjectEntity.php +++ b/api/src/Entity/ObjectEntity.php @@ -1127,6 +1127,11 @@ public function toArray(array $configuration = []): array 'level' => $configuration['level'], 'dateCreated' => $this->getDateCreated() ? $this->getDateCreated()->format('c') : null, 'dateModified' => $this->getDateModified() ? $this->getDateModified()->format('c') : null, + 'database' => [ + 'id' => $this->getOrganization() && $this->getOrganization()->getDatabase() ? $this->getOrganization()->getDatabase()->getId()->toString() : null, + 'name' => $this->getOrganization() && $this->getOrganization()->getDatabase() ? $this->getOrganization()->getDatabase()->getName() : null, + 'ref' => $this->getOrganization() && $this->getOrganization()->getDatabase() ? $this->getOrganization()->getDatabase()->getReference() : null + ], 'owner' => [ 'id' => $this->getOwner(), 'name' => isset($configuration['user']) ? $configuration['user']->getName() : $this->getOwner(), diff --git a/api/src/Entity/Organization.php b/api/src/Entity/Organization.php index 7043caac4..24f85144d 100644 --- a/api/src/Entity/Organization.php +++ b/api/src/Entity/Organization.php @@ -20,6 +20,7 @@ use Symfony\Component\Serializer\Annotation\Groups; use Symfony\Component\Serializer\Annotation\MaxDepth; use Symfony\Component\Validator\Constraints as Assert; +use Symfony\Component\VarDumper\Cloner\Data; /** * This entity holds the information about an Organization. @@ -119,6 +120,17 @@ class Organization */ private string $version = '0.0.0'; + /** + * The database that this organization uses to store its objects. + * + * @Groups({"read", "write"}) + * + * @ORM\ManyToOne(targetEntity=Database::class, inversedBy="organizations") + * + * @MaxDepth(1) + */ + private ?Database $database = null; + /** * @Groups({"read", "write"}) * @@ -286,6 +298,18 @@ public function setVersion(?string $version): self return $this; } + public function getDatabase(): ?Database + { + return $this->database; + } + + public function setDatabase(?Database $database): self + { + $this->database = $database; + + return $this; + } + /** * @return Collection|Application[] */ diff --git a/api/src/Repository/DatabaseRepository.php b/api/src/Repository/DatabaseRepository.php new file mode 100644 index 000000000..a3da5d9ff --- /dev/null +++ b/api/src/Repository/DatabaseRepository.php @@ -0,0 +1,50 @@ +createQueryBuilder('d') + ->andWhere('d.exampleField = :val') + ->setParameter('val', $value) + ->orderBy('d.id', 'ASC') + ->setMaxResults(10) + ->getQuery() + ->getResult() + ; + } + */ + + /* + public function findOneBySomeField($value): ?Database + { + return $this->createQueryBuilder('d') + ->andWhere('d.exampleField = :val') + ->setParameter('val', $value) + ->getQuery() + ->getOneOrNullResult() + ; + } + */ +}