Commit a56dfd59 authored by François Agneray's avatar François Agneray
Browse files

Simplification metamodel => ok

parent a128b7e8
......@@ -16,6 +16,10 @@ use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use App\Utils\ActionTrait;
use App\Entity\Dataset;
use App\Entity\Attribute;
use App\Entity\CriteriaFamily;
use App\Entity\OutputCategory;
final class AttributeListAction
{
......@@ -23,28 +27,16 @@ final class AttributeListAction
private $logger;
private $em;
private $datasetCriteriaFamilyMaxDisplay;
private $datasetCriteriaFamilyList;
private $datasetOutputFamilyMaxDisplay;
private $datasetOutputFamilyList;
private $datasetOutputFamilyCategoryMaxDisplay;
private $datasetOutputFamilyCategoryList;
public function __construct(LoggerInterface $logger, EntityManagerInterface $em)
{
$this->logger = $logger;
$this->em = $em;
$this->datasetCriteriaFamilyMaxDisplay = 0;
$this->datasetCriteriaFamilyList = array();
$this->datasetOutputFamilyMaxDisplay = 0;
$this->datasetOutputFamilyList = array();
$this->datasetOutputFamilyCategoryMaxDisplay = array();
$this->datasetOutputFamilyCategoryList = array();
}
public function __invoke(Request $request, Response $response, array $args): Response
{
$this->logger->info('Category action dispatched');
$this->logger->info('Attribute list action dispatched');
if ($request->isOptions()) {
return $response->withHeader('Access-Control-Allow-Methods', 'GET, PUT, OPTIONS');
......@@ -69,12 +61,12 @@ final class AttributeListAction
}
if ($request->isPut()) {
$this->deleteDatasetCriteriaFamily($dataset);
$this->deleteDatasetOutputFamily($dataset);
$parsedBody = $request->getParsedBody();
foreach ($parsedBody as $datum) {
$attribute = $this->em->getRepository('App\Entity\Attribute')->findBy(array('id' => $datum['id'], 'dataset' => $dataset))[0];
$this->editAttribute($dataset, $attribute, $datum);
$criteriaFamily = $this->em->find('App\Entity\CriteriaFamily', $parsedBody['id_criteria_family']);
$outputCategory = $this->em->find('App\Entity\OutputCategory', $parsedBody['id_output_category']);
$this->editAttribute($dataset, $attribute, $datum, $criteriaFamily, $outputCategory);
}
$this->em->flush();
......@@ -85,7 +77,7 @@ final class AttributeListAction
return $newResponse;
}
private function editAttribute($dataset, $attribute, $datum): void
private function editAttribute(Dataset $dataset, Attribute $attribute, array $datum, CriteriaFamily $criteriaFamily, OutputCategory $outputCategory): void
{
$attribute->setLabel($datum['label']);
$attribute->setFormLabel($datum['form_label']);
......@@ -108,135 +100,14 @@ final class AttributeListAction
$attribute->setOrderDisplay($datum['order_display']);
$attribute->setDetail($datum['detail']);
$attribute->setRendererDetail($datum['renderer_detail']);
$attribute->setOptions($datum['options']);
$attribute->setVoUtype($datum['vo_utype']);
$attribute->setVoUcd($datum['vo_ucd']);
$attribute->setVoUnit($datum['vo_unit']);
$attribute->setVoDescription($datum['vo_description']);
$attribute->setVoDatatype($datum['vo_datatype']);
$attribute->setVoSize($datum['vo_size']);
$idCriteriaFamily = array_key_exists('id_criteria_family', $datum) ? $datum['id_criteria_family'] : null;
$this->editAttributeCriteriaFamily($dataset, $attribute, $idCriteriaFamily);
$idOutputFamily = array_key_exists('id_output_family', $datum) ? $datum['id_output_family'] : null;
$idCategory = array_key_exists('id_category', $datum) ? $datum['id_category'] : null;
$this->editAttributeOutputFamilyCategory($dataset, $attribute, $idOutputFamily, $idCategory);
$this->editOptions($attribute, $datum['options']);
}
private function deleteDatasetCriteriaFamily($dataset): void
{
foreach ($dataset->getAttributes() as $attribute) {
$attribute->setDatasetCriteriaFamily(null);
}
foreach ($dataset->getDatasetCriteriaFamilies() as $datasetCriteriaFamily) {
$this->em->remove($datasetCriteriaFamily);
}
$this->em->flush();
}
private function deleteDatasetOutputFamily($dataset): void
{
foreach ($dataset->getAttributes() as $attribute) {
$attribute->setDatasetOutputFamilyCategory(null);
}
foreach ($dataset->getDatasetOutputFamilies() as $datasetOutputFamily) {
$this->em->remove($datasetOutputFamily);
}
$this->em->flush();
}
private function editAttributeCriteriaFamily($dataset, $attribute, $idCriteriaFamily): void
{
if (is_null($idCriteriaFamily)) {
if ($attribute->getDatasetCriteriaFamily()) {
$attribute->setDatasetCriteriaFamily(null);
}
return ;
}
if (array_key_exists($idCriteriaFamily, $this->datasetCriteriaFamilyList)) {
$datasetCriteriaFamily = $this->datasetCriteriaFamilyList[$idCriteriaFamily];
} else {
$this->datasetCriteriaFamilyMaxDisplay += 10;
$criteriaFamily = $this->em->find('App\Entity\CriteriaFamily', $idCriteriaFamily);
$datasetCriteriaFamily = new \App\Entity\DatasetCriteriaFamily($dataset, $criteriaFamily);
$datasetCriteriaFamily->setDisplay($this->datasetCriteriaFamilyMaxDisplay);
$this->em->persist($datasetCriteriaFamily);
$this->datasetCriteriaFamilyList[$idCriteriaFamily] = $datasetCriteriaFamily;
}
$attribute->setDatasetCriteriaFamily($datasetCriteriaFamily);
}
private function getCriteriaFamilyMaxDisplay($dataset)
{
$qb = $this->em->createQueryBuilder();
$qb->select($qb->expr()->max('e.display'))
->from('App\Entity\DatasetCriteriaFamily', 'e')
->where('e.dataset = ?1')
->setParameter(1, $dataset);
$single = $qb->getQuery()->getSingleScalarResult();
if (is_null($single)) {
return 0;
} else {
return $single;
}
}
private function editAttributeOutputFamilyCategory($dataset, $attribute, $idOutputFamily, $idCategory): void
{
if (is_null($idOutputFamily) || is_null($idCategory)) {
if ($attribute->getDatasetOutputFamilyCategory()) {
$attribute->setDatasetOutputFamilyCategory(null);
}
return ;
}
if (array_key_exists($idOutputFamily, $this->datasetOutputFamilyList)) {
$datasetOutputFamily = $this->datasetOutputFamilyList[$idOutputFamily];
} else {
$this->datasetOutputFamilyMaxDisplay += 10;
$outputFamily = $this->em->find('App\Entity\OutputFamily', $idOutputFamily);
$datasetOutputFamily = new \App\Entity\DatasetOutputFamily($dataset, $outputFamily);
$datasetOutputFamily->setDisplay($this->datasetOutputFamilyMaxDisplay);
$this->em->persist($datasetOutputFamily);
$this->datasetOutputFamilyList[$idOutputFamily] = $datasetOutputFamily;
$this->datasetOutputFamilyCategoryList[$idOutputFamily] = array();
$this->datasetOutputFamilyCategoryMaxDisplay[$idOutputFamily] = 0;
}
if (array_key_exists($idCategory, $this->datasetOutputFamilyCategoryList[$idOutputFamily])) {
$datasetOutputFamilyCategory = $this->datasetOutputFamilyCategoryList[$idOutputFamily][$idCategory];
} else {
$this->datasetOutputFamilyCategoryMaxDisplay[$idOutputFamily] += 10;
$category = $this->em->find('App\Entity\Category', $idCategory);
$datasetOutputFamilyCategory = new \App\Entity\DatasetOutputFamilyCategory($datasetOutputFamily, $category);
$datasetOutputFamilyCategory->setDisplay($this->datasetOutputFamilyCategoryMaxDisplay[$idOutputFamily]);
$this->em->persist($datasetOutputFamilyCategory);
$this->datasetOutputFamilyCategoryList[$idOutputFamily][$idCategory] = $datasetOutputFamilyCategory;
}
$attribute->setDatasetOutputFamilyCategory($datasetOutputFamilyCategory);
}
private function editOptions($attribute, $options): void
{
if (empty($attribute->getOptions()) && empty($options)) {
return ;
}
foreach ($attribute->getOptions() as $option) {
$this->em->remove($option);
}
foreach ($options as $option) {
$newOption = new \App\Entity\Option($attribute);
$newOption->setLabel($option['label']);
$newOption->setValue($option['value']);
$newOption->setDisplay($option['display']);
$this->em->persist($newOption);
}
$attribute->setCriteriaFamily($criteriaFamily);
$attribute->setOutputCategory($outputCategory);
}
}
......@@ -17,7 +17,7 @@ use Psr\Http\Message\ResponseInterface as Response;
use App\Utils\ActionTrait;
final class DatasetCriteriaFamilyListAction
final class DatasetCriteriaListAction
{
use ActionTrait;
......@@ -32,7 +32,7 @@ final class DatasetCriteriaFamilyListAction
public function __invoke(Request $request, Response $response, array $args): Response
{
$this->logger->info('Dataset criteria family list action dispatched');
$this->logger->info('Dataset criteria list action dispatched');
if ($request->isOptions()) {
return $response->withHeader('Access-Control-Allow-Methods', 'GET, OPTIONS');
......@@ -49,8 +49,16 @@ final class DatasetCriteriaFamilyListAction
}
if ($request->isGet()) {
$datasetCriteriaFamilies = $dataset->getDatasetCriteriaFamilies()->getValues();
$newResponse = $response->withJson($datasetCriteriaFamilies);
$criteria = array();
$attributes = $dataset->getAttributes();
foreach ($attributes as $attribute) {
$criteriaFamily = $attribute->getCriteriaFamily();
if (!empty($criteriaFamily)) {
$idCriteriaFamily = $criteriaFamily->getId();
$criteria[$idCriteriaFamily][] = $attribute->getId();
}
}
$newResponse = $response->withJson($criteria);
}
return $newResponse;
......
......@@ -17,7 +17,7 @@ use Psr\Http\Message\ResponseInterface as Response;
use App\Utils\ActionTrait;
final class DatasetOutputFamilyListAction
final class DatasetOutputListAction
{
use ActionTrait;
......@@ -32,7 +32,7 @@ final class DatasetOutputFamilyListAction
public function __invoke(Request $request, Response $response, array $args): Response
{
$this->logger->info('Dataset output family list action dispatched');
$this->logger->info('Dataset output list action dispatched');
if ($request->isOptions()) {
return $response->withHeader('Access-Control-Allow-Methods', 'GET, OPTIONS');
......@@ -49,8 +49,18 @@ final class DatasetOutputFamilyListAction
}
if ($request->isGet()) {
$datasetOutputFamilies = $dataset->getDatasetOutputFamilies()->getValues();
$newResponse = $response->withJson($datasetOutputFamilies);
$output = array();
$attributes = $dataset->getAttributes();
foreach ($attributes as $attribute) {
$outputCategory = $attribute->getOutputCategory();
if (!empty($outputCategory)) {
$idOutputFamily = $outputCategory->getOutputFamily()->getId();
$idOutputCategory = $outputCategory->getId();
$criteria[$idOutputFamily][$idOutputCategory][] = $attribute->getId();
}
}
$newResponse = $response->withJson($output);
}
return $newResponse;
......
......@@ -67,10 +67,7 @@ final class FamilyAction
$parsedBody = $request->getParsedBody();
// Vérification des champs vides
$fields = array('label');
if ($type === 'dataset') {
$fields = array_merge(array('description', 'display'), $fields);
}
$fields = array('label', 'display');
foreach ($fields as $a) {
if ($this->isEmptyField($a, $parsedBody)) {
return $this->dispatchHttpError(
......@@ -127,10 +124,7 @@ final class FamilyAction
private function editFamily(Object $family, array $parsedBody): void
{
$family->setLabel($parsedBody['label']);
if ($family instanceof \App\Entity\DatasetFamily) {
$family->setDescription($parsedBody['description']);
$family->setDisplay($parsedBody['display']);
}
$family->setDisplay($parsedBody['display']);
$this->em->flush();
}
}
......@@ -61,10 +61,7 @@ final class FamilyListAction
$parsedBody = $request->getParsedBody();
// Vérification des champs vides
$fields = array('label');
if ($type === 'dataset') {
$fields = array_merge(array('description', 'display'), $fields);
}
$fields = array('label', 'display');
foreach ($fields as $a) {
if ($this->isEmptyField($a, $parsedBody)) {
return $this->dispatchHttpError(
......@@ -102,10 +99,7 @@ final class FamilyListAction
{
$family = new $class;
$family->setLabel($parsedBody['label']);
if ($family instanceof \App\Entity\DatasetFamily) {
$family->setDescription($parsedBody['description']);
$family->setDisplay($parsedBody['display']);
}
$family->setDisplay($parsedBody['display']);
$this->em->persist($family);
$this->em->flush();
......
......@@ -16,9 +16,10 @@ use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use App\Utils\ActionTrait;
use App\Entity\Category;
use App\Entity\OutputCategory;
use App\Entity\OutputFamily;
final class CategoryAction
final class OutputCategoryAction
{
use ActionTrait;
......@@ -33,57 +34,69 @@ final class CategoryAction
public function __invoke(Request $request, Response $response, $args): Response
{
$this->logger->info('Category action dispatched');
$this->logger->info('Output category action dispatched');
if ($request->isOptions()) {
return $response->withHeader('Access-Control-Allow-Methods', 'GET, PUT, DELETE, OPTIONS');
}
$category = $this->em->find('App\Entity\Category', $args['id']);
$outputCategory = $this->em->find('App\Entity\OutputCategory', $args['id']);
if (is_null($category)) {
if (is_null($outputCategory)) {
return $this->dispatchHttpError(
$response,
'Invalid request',
'Category with id ' . $args['id'] . ' is not found'
'Output category with id ' . $args['id'] . ' is not found'
)->withStatus(404);
}
if ($request->isGet()) {
$newResponse = $response->withJson($category);
$newResponse = $response->withJson($outputCategory);
}
if ($request->isPut()) {
$parsedBody = $request->getParsedBody();
// Vérification des champs vides
foreach (array('label') as $a) {
foreach (array('label', 'display', 'id_output_family') as $a) {
if ($this->isEmptyField($a, $parsedBody)) {
return $this->dispatchHttpError(
$response,
'Invalid request',
'Param ' . $a . ' needed to edit the category'
'Param ' . $a . ' needed to edit the output category'
);
}
}
$this->editCategory($category, $parsedBody);
$newResponse = $response->withJson($category);
// Vérification de l'existence de la output family
$outputFamily = $this->em->find('App\Entity\OutputFamily', $parsedBody['id_output_family']);
if (is_null($outputFamily)) {
return $this->dispatchHttpError(
$response,
'Invalid request',
'Output family with id ' . $parsedBody['id_output_family'] . ' is not found'
)->withStatus(404);
}
$this->editOutputCategory($outputCategory, $parsedBody, $outputFamily);
$newResponse = $response->withJson($outputCategory);
}
if ($request->isDelete()) {
$id = $category->getId();
$this->em->remove($category);
$id = $outputCategory->getId();
$this->em->remove($outputCategory);
$this->em->flush();
$newResponse = $response->withJson(array('message' => 'Category with id ' . $id . ' is removed!'));
$newResponse = $response->withJson(array('message' => 'Output category with id ' . $id . ' is removed!'));
}
return $newResponse;
}
private function editCategory(Category $category, array $parsedBody): void
private function editOutputCategory(OutputCategory $outputCategory, array $parsedBody, OutputFamily $outputFamily): void
{
$category->setLabel($parsedBody['label']);
$outputCategory->setLabel($parsedBody['label']);
$outputCategory->setDisplay($parsedBody['display']);
$outputCategory->setOutputFamily($outputFamily);
$this->em->flush();
}
}
......@@ -16,9 +16,10 @@ use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use App\Utils\ActionTrait;
use App\Entity\Category;
use App\Entity\OutputCategory;
use App\Entity\OutputFamily;
final class CategoryListAction
final class OutputCategoryListAction
{
use ActionTrait;
......@@ -33,46 +34,58 @@ final class CategoryListAction
public function __invoke(Request $request, Response $response, array $args): Response
{
$this->logger->info('Category list action dispatched');
$this->logger->info('Output category list action dispatched');
if ($request->isOptions()) {
return $response->withHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
}
if ($request->isGet()) {
$categories = $this->em->getRepository('App\Entity\Category')->findAll();
$newResponse = $response->withJson($categories);
$outputCategories = $this->em->getRepository('App\Entity\OutputCategory')->findAll();
$newResponse = $response->withJson($outputCategories);
}
if ($request->isPost()) {
$parsedBody = $request->getParsedBody();
// Vérification des champs vides
foreach (array('label') as $a) {
foreach (array('label', 'display', 'id_output_family') as $a) {
if ($this->isEmptyField($a, $parsedBody)) {
return $this->dispatchHttpError(
$response,
'Invalid request',
'Param ' . $a . ' needed to add a new category'
'Param ' . $a . ' needed to add a new output category'
);
}
}
$category = $this->postCategory($parsedBody);
$newResponse = $response->withJson($category, 201);
// Vérification de l'existence de la output family
$outputFamily = $this->em->find('App\Entity\OutputFamily', $parsedBody['id_output_family']);
if (is_null($outputFamily)) {
return $this->dispatchHttpError(
$response,
'Invalid request',
'Output family with id ' . $parsedBody['id_output_family'] . ' is not found'
)->withStatus(404);
}
$outputCategory = $this->postOutputCategory($parsedBody, $outputFamily);
$newResponse = $response->withJson($outputCategory, 201);
}
return $newResponse;
}
private function postCategory(array $parsedBody): Category
private function postOutputCategory(array $parsedBody, OutputFamily $outputFamily): OutputCategory
{
$category = new Category();
$category->setLabel($parsedBody['label']);
$outputCategory = new OutputCategory();
$outputCategory->setLabel($parsedBody['label']);
$outputCategory->setDisplay($parsedBody['display']);
$outputCategory->setOutputFamily($outputFamily);
$this->em->persist($category);
$this->em->persist($outputCategory);
$this->em->flush();
return $category;
return $outputCategory;
}
}
......@@ -582,11 +582,21 @@ class Attribute implements \JsonSerializable
return $this->criteriaFamily;
}
public function setCriteriaFamily($criteriaFamily)
{
$this->criteriaFamily = $criteriaFamily;
}
public function getOutputCategory()
{
return $this->outputCategory;
}
public function setOutputCategory($outputCategory)
{
$this->outputCategory = $outputCategory;
}
public function jsonSerialize()
{
return [
......@@ -621,8 +631,8 @@ class Attribute implements \JsonSerializable
'vo_description' => $this->getVoDescription(),
'vo_datatype' => $this->getVoDatatype(),
'vo_size' => $this->getVoSize(),
'id_criteria_family' => $this->getCriteriaFamily(),
'id_output_category' => $this->getOutputCategory()
'id_criteria_family' => $this->getCriteriaFamily()->getId(),
'id_output_category' => $this->getOutputCategory()->getId()
];
}
}
......@@ -34,13 +34,6 @@ class DatasetFamily implements \JsonSerializable
*/
protected $label;
/**
* @var string
*
* @Column(type="text", nullable=true)
*/
protected $description;
/**
* @var integer
*
......@@ -76,16 +69,6 @@ class DatasetFamily implements \JsonSerializable
$this->label = $label;
}
public function getDescription()
{
return $this->description;
}
public function setDescription($description)
{
$this->description = $description;
}
public function getDisplay()
{
return $this->display;
......@@ -110,7 +93,6 @@ class DatasetFamily implements \JsonSerializable
return [
'id' => $this->getId(),
'label' => $this->getLabel(),
'description' => $this->getDescription(),
'display' => $this->getDisplay(),
'type' => 'dataset',
'datasets' => $datasets
......
......@@ -155,12 +155,12 @@ $container['App\Action\Meta\FamilyAction'] = function ($c) {
return new App\Action\Meta\FamilyAction($c->get('logger'), $c->get('em'));
};
$container['App\Action\Meta\CategoryListAction'] = function ($c) {
return new App\Action\Meta\CategoryListAction($c->get('logger'), $c->get('em'));
$container['App\Action\Meta\OutputCategoryListAction'] = function ($c) {
return new App\Action\Meta\OutputCategoryListAction($c->get('logger'), $c->get('em'));
};
$container['App\Action\Meta\CategoryAction'] = function ($c) {
return new App\Action\Meta\CategoryAction($c->get('logger'), $c->get('em'));
$container['App\Action\Meta\OutputCategoryAction'] = function ($c) {
return new App\Action\Meta\OutputCategoryAction($c->get('logger'), $c->get('em'));
};
$container['App\Action\Meta\DatasetListAction'] = function ($c) {
......@@ -176,12 +176,12 @@ $container['App\Action\Meta\AttributeListAction'] = function ($c) {