From 23acce12ef16428543e1eba22003fae6b9b7a5db Mon Sep 17 00:00:00 2001 From: Mysaa Date: Mon, 6 Feb 2023 17:54:20 +0100 Subject: [PATCH] =?UTF-8?q?Ajout=20des=20scripts=20pour=20mettre=20les=20d?= =?UTF-8?q?onn=C3=A9es=20dans=20la=20BDD,=20correction=20des=20attributs?= =?UTF-8?q?=20Doctrine=20(certains,=20pas=20tous)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Controller/JsonReaderController.php | 229 ++++++++++++------ src/Entity/Floor.php | 4 +- src/Entity/Place.php | 17 +- src/Entity/PlaceName.php | 2 +- .../{RoomUsersName.php => RoomUserName.php} | 3 +- src/Repository/PlaceNameRepository.php | 49 ++++ src/Repository/RoomUserNameRepository.php | 49 ++++ 7 files changed, 262 insertions(+), 91 deletions(-) rename src/Entity/{RoomUsersName.php => RoomUserName.php} (89%) create mode 100644 src/Repository/PlaceNameRepository.php create mode 100644 src/Repository/RoomUserNameRepository.php diff --git a/src/Controller/JsonReaderController.php b/src/Controller/JsonReaderController.php index 9801349..05c9749 100644 --- a/src/Controller/JsonReaderController.php +++ b/src/Controller/JsonReaderController.php @@ -7,19 +7,31 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; use Doctrine\Persistence\ManagerRegistry; +use Doctrine\ORM\EntityManagerInterface; use App\Repository\SiteRepository; -use App\Entity\Room; +use App\Repository\BuildingRepository; +use App\Repository\FloorRepository; +use App\Entity\Building; use App\Entity\Cylinder; -use App\Entity\Point; +use App\Entity\Edition; +use App\Entity\Floor; +use App\Entity\Place; +use App\Entity\PlaceName; use App\Entity\PlaneSurface; +use App\Entity\Point; +use App\Entity\Polyhedron; +use App\Entity\PolySurface; +use App\Entity\RoomUserName; use App\Entity\Site; +use App\Entity\ThreeDObjectFile; + class JsonReaderController extends AbstractController { #[Route('/api/debug/loadJson')] - public function index(SiteRepository $srep): Response + public function index(Request $request): Response { - + return new Response(print_r($request,true)); $response = new Response(); $response->setStatusCode(Response::HTTP_OK); @@ -27,28 +39,147 @@ class JsonReaderController extends AbstractController } #[Route('/api/debug/initialData')] - public function init(SiteRepository $srep, BuildingRepository $brep, FloorRepository $frep): Response + public function init(Request $request, SiteRepository $srep, BuildingRepository $brep, FloorRepository $frep, ManagerRegistry $mr): Response { - $s = new Site(); - $p->setX(); - $p->setY(); - $p->setZ(); - $prep->add($p); + //return new Response(print_r(json_decode($request->getContent()),true)); + + $monod = new Site(); + $monod->setZeroLatitude(45.72980); + $monod->setZeroLongitude(4.82777); + $srep->add($monod); + + $descartes = new Site(); + $descartes->setZeroLatitude(45.73261); + $descartes->setZeroLongitude(4.83293); + $srep->add($descartes); + + $mgn1 = new Building(); + $mgn1->setSite($monod); + $brep->add($mgn1); + + $floors = array(); + for($i=0;$i<7;$i++){ + $floor = new Floor(); + $floor->setBuilding($mgn1); + $floor->setAltitude(0.45*$i); + $frep->add($floor); + $floors[$i] = $floor; + } + + $data = json_decode($request->getContent(),true); + + // Adding all the rooms + $rooms2Data = $data['rooms2']; + $cor2Data = $data['cor2']; + $rooms3Data = $data['rooms3']; + $cor3Data = $data['cor3']; + $rooms4Data = $data['rooms4']; + $cor4Data = $data['cor4']; + $rooms34Data = $data['bothRooms']; + $roomsXData = $data['allRooms']; + + // We create the rooms + $rooms2 = []; + foreach($rooms2Data as $rData) { + $rooms2[] = JsonReaderController::addRoom($rData, "R", $mr); + } + $cor2 = JsonReaderController::addRoom($cor2Data, "C", $mr); + $rooms3 = []; + foreach($rooms3Data as $rData) { + $rooms3[] = JsonReaderController::addRoom($rData, "R", $mr); + } + $cor3 = JsonReaderController::addRoom($cor3Data, "C", $mr); + $rooms4 = []; + foreach($rooms4Data as $rData) { + $rooms4[] = JsonReaderController::addRoom($rData, "R", $mr); + } + $cor4 = JsonReaderController::addRoom($cor4Data, "C", $mr); + $rooms34 = []; + foreach($rooms34Data as $rData) { + $rooms34[] = JsonReaderController::addRoom($rData, "R", $mr); + } + $roomsX = []; + foreach($roomsXData as $rData) { + $roomsX[] = JsonReaderController::addRoom($rData, "R", $mr); + } + + + // Connect all rooms + foreach($rooms2 as $r){ + $floors[4]->addPlace($r); + $cor2->addConnection($r); + } + foreach($rooms3 as $r){ + $floors[5]->addPlace($r); + $cor3->addConnection($r); + } + foreach($rooms4 as $r){ + $floors[6]->addPlace($r); + $cor4->addConnection($r); + } + foreach($rooms34 as $r){ + $floors[5]->addPlace($r); + $floors[6]->addPlace($r); + $cor3->addConnection($r); + $cor4->addConnection($r); + } + foreach($roomsX as $r){ + for($i=0;$i<7;$i++){ + $floors[$i]->addPlace($r); + } + $cor2->addConnection($r); + $cor3->addConnection($r); + $cor4->addConnection($r); + } + + $mr->getManager()->flush(); + + return new Response("All Done !"); + } - #[Route('/api/debug/newRoom')] - public function addRoom(Request $request, ManagerRegistry $mr): Response { + #[Route('/api/debug/dropAllDatabaseThereIsNoWayIGotTheNameOfThisPageWrong')] + public function dropDatabase(EntityManagerInterface $em): Response + { + $entityClasses = array( + Building::class, + Cylinder::class, + Edition::class, + Floor::class, + Place::class, + PlaceName::class, + PlaneSurface::class, + Point::class, + Polyhedron::class, + PolySurface::class, + RoomUserName::class, + Site::class, + ThreeDObjectFile::class + ); + foreach($entityClasses as $eclass){ + $repository = $em->getRepository($eclass); + $entities = $repository->findAll(); + + foreach ($entities as $entity) { + $em->remove($entity); + } + } + $em->flush(); + + return new Response('', Response::HTTP_OK); + } + + public function addRoom(array $room, string $roomType, ManagerRegistry $mr): Place { $manager = $mr->getManager(); - - //return new Response(print_r($request->request->all())); - $height = $request->request->get('height'); - $z = $request->request->get('z'); - $name = $request->request->get('name'); - $points = json_decode($request->request->get('points')); + $height = (float) $room['height']; + $z = (float) $room['z']; + $name = $room['name']; + $points = json_decode($room['points'],true); - $r = new Room(); + $r = new Place(); + $r->setType($roomType); $cyl = new Cylinder(); $cyl->setHeight($height); $cbas = new PlaneSurface(); @@ -66,66 +197,8 @@ class JsonReaderController extends AbstractController $manager->persist($cyl); $r->addCylinderRepresentation($cyl); $manager->persist($r); - $manager->flush(); - return new Response($r->getId()); - } - - #[Route('/api/debug/newCorridor')] - public function addCorridor(Request $request, ManagerRegistry $mr, FloorRepository $frep): Response { - - $manager = $mr->getManager(); - - //return new Response(print_r($request->request->all())); - - $z = $request->request->get('z'); - $name = $request->request->get('name'); - $floorid = $request->request->get('floorid'); - //$points = json_decode($request->request->get('points')); - - $f = $manager->getRepository(Floor::class)->find($floorid); - $c = new Corridor(); - /*$cyl = new Cylinder(); - $cyl->setHeight($height); - $cbas = new PlaneSurface(); - foreach($points as $px) { - $p = new Point(); - $p->setX($px[0]); - $p->setY($px[1]); - $p->setZ($z); - $manager->persist($p); - $cbas->addPolygonpoint($p); - } - - $manager->persist($cbas); - $cyl->setCylinderBase($cbas); - $manager->persist($cyl); - $r->addCylinderRepresentation($cyl);*/ - $f->addCorridor($c); - $manager->persist($c); - $manager->flush(); - - return new Response($c->getId()); - } - - #[Route('/api/debug/connect')] - public function connect(Request $request, ManagerRegistry $mr, PlaceRepository $prep): Response { - - $manager = $mr->getManager(); - - //return new Response(print_r($request->request->all())); - - $aid = $request->request->get('a'); - $bid = $request->request->get('b'); - - $a = $prep->find($aid); - $b = $prep->find($bid); - - $a->addConnection($b); - - $manager->flush(); - - return new Response("OK"); + return $r; } } diff --git a/src/Entity/Floor.php b/src/Entity/Floor.php index 4f52473..65d03aa 100644 --- a/src/Entity/Floor.php +++ b/src/Entity/Floor.php @@ -69,7 +69,7 @@ class Floor { if (!$this->places->contains($place)) { $this->places[] = $place; - $place->setFloor($this); + $place->addFloor($this); } return $this; } @@ -79,7 +79,7 @@ class Floor if ($this->places->removeElement($place)) { // set the owning side to null (unless already changed) if ($place->getFloor() === $this) { - $place->setFloor(null); + $place->addFloor(null); } } diff --git a/src/Entity/Place.php b/src/Entity/Place.php index aafe1bf..a9b59e1 100644 --- a/src/Entity/Place.php +++ b/src/Entity/Place.php @@ -19,7 +19,6 @@ class Place #[ORM\Column(type: "integer")] private $id; - #[ORM\Type] #[ORM\Column(type: "string")] // Either 'C'orridor , 'S'tairs , 'E'levator or 'R'oom private $type; @@ -132,7 +131,7 @@ class Place return $this->floors; } - public function addFloor(self $floor): self + public function addFloor(Floor $floor): self { if (!$this->floors->contains($floor)) { $this->floors[] = $floor; @@ -140,7 +139,7 @@ class Place return $this; } - public function removeFloor(self $floor): self + public function removeFloor(Floor $floor): self { $this->floors->removeElement($floor); return $this; @@ -264,7 +263,7 @@ class Place { if (!$this->CylinderRepresentation->contains($cylinderRepresentation)) { $this->CylinderRepresentation[] = $cylinderRepresentation; - $cylinderRepresentation->setRoom($this); + $cylinderRepresentation->setPlace($this); } return $this; @@ -274,8 +273,8 @@ class Place { if ($this->CylinderRepresentation->removeElement($cylinderRepresentation)) { // set the owning side to null (unless already changed) - if ($cylinderRepresentation->getRoom() === $this) { - $cylinderRepresentation->setRoom(null); + if ($cylinderRepresentation->getPlace() === $this) { + $cylinderRepresentation->setPlace(null); } } @@ -295,7 +294,7 @@ class Place { if (!$this->PolySurfaceRepresentation->contains($polySurfaceRepresentation)) { $this->PolySurfaceRepresentation[] = $polySurfaceRepresentation; - $polySurfaceRepresentation->setRoom($this); + $polySurfaceRepresentation->setPlace($this); } return $this; @@ -305,8 +304,8 @@ class Place { if ($this->PolySurfaceRepresentation->removeElement($polySurfaceRepresentation)) { // set the owning side to null (unless already changed) - if ($polySurfaceRepresentation->getRoom() === $this) { - $polySurfaceRepresentation->setRoom(null); + if ($polySurfaceRepresentation->getPlace() === $this) { + $polySurfaceRepresentation->setPlace(null); } } diff --git a/src/Entity/PlaceName.php b/src/Entity/PlaceName.php index 7d1a08d..4357536 100644 --- a/src/Entity/PlaceName.php +++ b/src/Entity/PlaceName.php @@ -2,7 +2,7 @@ namespace App\Entity; -use App\Repository\PolyhedronRepository; +use App\Repository\PlaceNameRepository; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; diff --git a/src/Entity/RoomUsersName.php b/src/Entity/RoomUserName.php similarity index 89% rename from src/Entity/RoomUsersName.php rename to src/Entity/RoomUserName.php index 4f4930a..d4aa4b1 100644 --- a/src/Entity/RoomUsersName.php +++ b/src/Entity/RoomUserName.php @@ -2,11 +2,12 @@ namespace App\Entity; +use App\Repository\RoomUserNameRepository; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; -#[ORM\Entity(repositoryClass: RoomUserRepository::class)] +#[ORM\Entity(repositoryClass: RoomUserNameRepository::class)] class RoomUserName { #[ORM\Id] diff --git a/src/Repository/PlaceNameRepository.php b/src/Repository/PlaceNameRepository.php new file mode 100644 index 0000000..a9393c2 --- /dev/null +++ b/src/Repository/PlaceNameRepository.php @@ -0,0 +1,49 @@ + + */ +class PlaceNameRepository extends ServiceEntityRepository +{ + public function __construct(ManagerRegistry $registry) + { + parent::__construct($registry, PlaceName::class); + } + + public function add(PlaceName $entity, bool $flush = false): void + { + $this->getEntityManager()->persist($entity); + + if ($flush) { + $this->getEntityManager()->flush(); + } + } + + public function remove(PlaceName $entity, bool $flush = false): void + { + $this->getEntityManager()->remove($entity); + + if ($flush) { + $this->getEntityManager()->flush(); + } + } + /** + * @return PlaceName[] Returns an array of PlaceName objects + */ + public function findPlaceNameById(int $id): array + { + return $this->createQueryBuilder('p') + ->andWhere('p.id = :val') + ->setParameter('val', $id) + ->orderBy('p.id', 'ASC') + ->getQuery() + ->getResult() + ; + } +} diff --git a/src/Repository/RoomUserNameRepository.php b/src/Repository/RoomUserNameRepository.php new file mode 100644 index 0000000..8ddeec7 --- /dev/null +++ b/src/Repository/RoomUserNameRepository.php @@ -0,0 +1,49 @@ + + */ +class RoomUserNameRepository extends ServiceEntityRepository +{ + public function __construct(ManagerRegistry $registry) + { + parent::__construct($registry, RoomUserName::class); + } + + public function add(RoomUserName $entity, bool $flush = false): void + { + $this->getEntityManager()->persist($entity); + + if ($flush) { + $this->getEntityManager()->flush(); + } + } + + public function remove(RoomUserName $entity, bool $flush = false): void + { + $this->getEntityManager()->remove($entity); + + if ($flush) { + $this->getEntityManager()->flush(); + } + } + /** + * @return RoomUserName[] Returns an array of RoomUserName objects + */ + public function findPlaceNameById(int $id): array + { + return $this->createQueryBuilder('p') + ->andWhere('p.id = :val') + ->setParameter('val', $id) + ->orderBy('p.id', 'ASC') + ->getQuery() + ->getResult() + ; + } +}