Skip to content

Commit

Permalink
Add Import and Export for processing entity (#59)
Browse files Browse the repository at this point in the history
* Add Import and Export for processing entity

* Add import and export for Pia entity

* Add pia import from processing json
* Add a status class to translate status
* Rename applied_adjustements pia field to applied_adjustments

* Fix pia test

* Add new fields in processing entity

* Update processing tests
  • Loading branch information
Fabrice-li authored and GlennCavarle committed Sep 25, 2018
1 parent a827b39 commit 4b0ed39
Show file tree
Hide file tree
Showing 23 changed files with 1,694 additions and 41 deletions.
34 changes: 18 additions & 16 deletions src/Controller/Pia/PiaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
namespace PiaApi\Controller\Pia;

use FOS\RestBundle\Controller\Annotations as FOSRest;
use FOS\RestBundle\View\View;
use Nelmio\ApiDocBundle\Annotation as Nelmio;
use PiaApi\DataExchange\Transformer\JsonToEntityTransformer;
use PiaApi\DataExchange\Transformer\PiaTransformer;
use PiaApi\DataHandler\RequestDataHandler;
use PiaApi\Entity\Pia\Pia;
use PiaApi\Entity\Pia\ProcessingTemplate;
use PiaApi\Entity\Pia\Processing;
use PiaApi\Exception\DataImportException;
use PiaApi\Entity\Pia\Answer;
use PiaApi\Entity\Pia\Measure;
use PiaApi\Entity\Pia\Evaluation;
Expand All @@ -33,16 +33,16 @@
class PiaController extends RestController
{
/**
* @var jsonToEntityTransformer
* @var PiaTransformer
*/
protected $jsonToEntityTransformer;
protected $piaTransformer;

public function __construct(
PropertyAccessorInterface $propertyAccessor,
JsonToEntityTransformer $jsonToEntityTransformer
PiaTransformer $piaTransformer
) {
parent::__construct($propertyAccessor);
$this->jsonToEntityTransformer = $jsonToEntityTransformer;
$this->piaTransformer = $piaTransformer;
}

/**
Expand Down Expand Up @@ -458,13 +458,17 @@ public function deleteAction(Request $request, $id)
*/
public function importAction(Request $request)
{
$importData = $request->get('data', null);
if ($importData === null) {
return $this->view($importData, Response::HTTP_BAD_REQUEST);
$data = $request->get('pia');
$processing = $this->getResource($request->get('processing_id'), Processing::class);

$this->piaTransformer->setProcessing($processing);

try {
$pia = $this->piaTransformer->jsonToPia($data);
} catch (DataImportException $ex) {
return $this->view(unserialize($ex->getMessage()), Response::HTTP_PRECONDITION_FAILED);
}

$pia = $this->jsonToEntityTransformer->transform($importData);
$pia->setStructure($this->getUser()->getStructure());
$this->persist($pia);

return $this->view($pia, Response::HTTP_OK);
Expand Down Expand Up @@ -512,14 +516,12 @@ public function importAction(Request $request)
*/
public function exportAction(Request $request, $id)
{
$this->canAccessRouteOr403();

$pia = $this->getRepository()->find($id);
$pia = $this->getResource($id);
$this->canAccessResourceOr403($pia);

$serializedPia = $this->jsonToEntityTransformer->reverseTransform($pia);
$json = $this->piaTransformer->piaToJson($pia);

return new Response($serializedPia, Response::HTTP_OK);
return new Response($json, Response::HTTP_OK);
}

protected function getEntityClass()
Expand Down
61 changes: 54 additions & 7 deletions src/Controller/Pia/ProcessingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use PiaApi\Services\ProcessingService;
use PiaApi\Entity\Pia\Processing;
use PiaApi\Entity\Pia\Folder;
use PiaApi\DataExchange\Transformer\JsonToEntityTransformer;
use PiaApi\DataHandler\RequestDataHandler;
use PiaApi\Entity\Pia\ProcessingDataType;
use PiaApi\Entity\Pia\ProcessingComment;
Expand All @@ -26,6 +25,9 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use PiaApi\DataExchange\Transformer\ProcessingTransformer;
use PiaApi\Exception\DataImportException;
use PiaApi\DataExchange\Descriptor\ProcessingDescriptor;

class ProcessingController extends RestController
{
Expand All @@ -35,9 +37,9 @@ class ProcessingController extends RestController
protected $processingService;

/**
* @var jsonToEntityTransformer
* @var ProcessingTransformer
*/
protected $jsonToEntityTransformer;
protected $processingTransformer;

/**
* @var SerializerInterface
Expand All @@ -47,12 +49,12 @@ class ProcessingController extends RestController
public function __construct(
PropertyAccessorInterface $propertyAccessor,
ProcessingService $processingService,
JsonToEntityTransformer $jsonToEntityTransformer,
ProcessingTransformer $processingTransformer,
SerializerInterface $serializer
) {
parent::__construct($propertyAccessor);
$this->processingService = $processingService;
$this->jsonToEntityTransformer = $jsonToEntityTransformer;
$this->processingTransformer = $processingTransformer;
$this->serializer = $serializer;
}

Expand Down Expand Up @@ -404,8 +406,53 @@ public function exportAction(Request $request, $id)
$processing = $this->getResource($id);
$this->canAccessResourceOr403($processing);

$serializedPia = $this->jsonToEntityTransformer->entityToJson($processing);
$json = $this->processingTransformer->processingToJson($processing);

return new Response($serializedPia, Response::HTTP_OK);
return new Response($json, Response::HTTP_OK);
}

/**
* Imports a PROCESSING.
*
* @Swg\Tag(name="Processing")
*
* @FOSRest\Post("/processings/import")
*
* @Swg\Response(
* response=200,
* description="Returns the imported PROCESSING"
* )
*
* @Security("is_granted('CAN_IMPORT_PIA')")
*
* @return array
*/
public function importAction(Request $request)
{
$data = $request->get('processing');
$folder = $this->getResource($request->get('folder_id'), Folder::class);

$this->processingTransformer->setFolder($folder);

try {
$processing = $this->processingTransformer->jsonToProcessing($data);
$this->persist($processing);

$descriptor = $this->processingTransformer->fromJson($data, ProcessingDescriptor::class);

foreach ($descriptor->getPias() as $pia) {
$processing->addPia($this->processingTransformer->extractPia($processing, $pia));
}

foreach ($descriptor->getProcessingDataTypes() as $types) {
$processing->addProcessingDataType($this->processingTransformer->extractDataType($processing, $types));
}

$this->persist($processing);
} catch (DataImportException $ex) {
return $this->view(unserialize($ex->getMessage()), Response::HTTP_OK);
}

return $this->view($processing, Response::HTTP_OK);
}
}
50 changes: 50 additions & 0 deletions src/DataExchange/Descriptor/AbstractDescriptor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/*
* Copyright (C) 2015-2018 Libre Informatique
*
* This file is licensed under the GNU LGPL v3.
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace PiaApi\DataExchange\Descriptor;

class AbstractDescriptor
{
protected function checkProperty($mode, $method)
{
$attribute = false;
$reflect = new \ReflectionClass($this);

$name = lcfirst(str_replace($mode, '', $method));
$properties = array_column($reflect->getProperties(), 'name');

if (in_array($name, $properties)) {
$attribute = $name;
}

return $attribute;
}

public function __call($method, $args)
{
$value = false;

if (strpos($method, 'set') !== false) {
if ($name = $this->checkProperty('set', $method)) {
$this->$name = $args[0];
}

$value = true;
}

if (strpos($method, 'get') !== false) {
if ($name = $this->checkProperty('get', $method)) {
$value = $this->$name;
}
}

return $value;
}
}
63 changes: 63 additions & 0 deletions src/DataExchange/Descriptor/AnswerDescriptor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

/*
* Copyright (C) 2015-2018 Libre Informatique
*
* This file is licensed under the GNU LGPL v3.
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace PiaApi\DataExchange\Descriptor;

use JMS\Serializer\Annotation as JMS;
use Symfony\Component\Validator\Constraints as Assert;

class AnswerDescriptor extends AbstractDescriptor
{
/**
* @JMS\Type("string")
* @JMS\Groups({"Default", "Export"})
* @Assert\NotBlank
*
* @var string
*/
protected $referenceTo = '';

/**
* @JMS\Type("array")
* @JMS\Groups({"Default", "Export"})
* @Assert\NotBlank
*
* @var array
*/
protected $data = [];

/**
* @JMS\Type("DateTime")
* @JMS\Groups({"Export"})
*
* @var \DateTime|null
*/
protected $createdAt = '';

/**
* @JMS\Type("DateTime")
* @JMS\Groups({"Export"})
*
* @var \DateTime|null
*/
protected $updatedAt = '';

public function __construct(
string $referenceTo,
array $data,
\DateTime $createdAt,
\DateTime $updatedAt
) {
$this->referenceTo = $referenceTo;
$this->data = $data;
$this->createdAt = $createdAt;
$this->updatedAt = $updatedAt;
}
}
62 changes: 62 additions & 0 deletions src/DataExchange/Descriptor/DataTypeDescriptor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/*
* Copyright (C) 2015-2018 Libre Informatique
*
* This file is licensed under the GNU LGPL v3.
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace PiaApi\DataExchange\Descriptor;

use JMS\Serializer\Annotation as JMS;
use Symfony\Component\Validator\Constraints as Assert;

class DataTypeDescriptor extends AbstractDescriptor
{
/**
* @JMS\Type("string")
* @JMS\Groups({"Default", "Export"})
* @Assert\NotBlank
*
* @var string
*/
protected $reference = '';

/**
* @JMS\Type("string")
* @JMS\Groups({"Default", "Export"})
*
* @var string|null
*/
protected $data = '';

/**
* @JMS\Type("string")
* @JMS\Groups({"Default", "Export"})
*
* @var string|null
*/
protected $retentionPeriod = '';

/**
* @JMS\Type("boolean")
* @JMS\Groups({"Default", "Export"})
*
* @var bool
*/
protected $sensitive = false;

public function __construct(
string $reference,
string $data = null,
string $retentionPeriod = null,
bool $sensitive = false
) {
$this->reference = $reference;
$this->data = $data;
$this->retentionPeriod = $retentionPeriod;
$this->sensitive = $sensitive;
}
}
Loading

0 comments on commit 4b0ed39

Please sign in to comment.