2019-04-11 22:41:21 +02:00
|
|
|
<?php
|
2020-02-22 18:14:36 +01:00
|
|
|
/**
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
2022-11-29 22:28:53 +01:00
|
|
|
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
|
2020-02-22 18:14:36 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-01-05 15:46:58 +01:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2022-12-18 17:28:42 +01:00
|
|
|
namespace App\Services\ImportExportSystem;
|
2019-04-11 22:41:21 +02:00
|
|
|
|
2023-01-28 23:24:45 +01:00
|
|
|
use App\Entity\Base\AbstractNamedDBElement;
|
2020-02-01 19:48:07 +01:00
|
|
|
use App\Entity\Base\AbstractStructuralDBElement;
|
2023-03-12 19:53:55 +01:00
|
|
|
use App\Entity\Parts\Category;
|
|
|
|
use App\Entity\Parts\Part;
|
2023-03-12 19:16:49 +01:00
|
|
|
use Symplify\EasyCodingStandard\ValueObject\Option;
|
2020-01-05 22:49:00 +01:00
|
|
|
use function count;
|
2019-04-11 22:41:21 +02:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2020-01-05 22:49:00 +01:00
|
|
|
use InvalidArgumentException;
|
|
|
|
use function is_array;
|
2019-04-11 22:41:21 +02:00
|
|
|
use Symfony\Component\HttpFoundation\File\File;
|
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver;
|
|
|
|
use Symfony\Component\Serializer\SerializerInterface;
|
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
|
|
|
|
2023-06-11 15:02:59 +02:00
|
|
|
/**
|
|
|
|
* @see \App\Tests\Services\ImportExportSystem\EntityImporterTest
|
|
|
|
*/
|
2019-04-11 22:41:21 +02:00
|
|
|
class EntityImporter
|
|
|
|
{
|
2023-06-11 14:15:46 +02:00
|
|
|
public function __construct(protected SerializerInterface $serializer, protected EntityManagerInterface $em, protected ValidatorInterface $validator)
|
2019-04-11 22:41:21 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-08-12 22:41:58 +02:00
|
|
|
/**
|
2019-11-09 16:14:57 +01:00
|
|
|
* Creates many entries at once, based on a (text) list of name.
|
2023-03-12 19:16:49 +01:00
|
|
|
* The created entities are not persisted to database yet, so you have to do it yourself.
|
2019-08-12 22:41:58 +02:00
|
|
|
*
|
2020-03-15 13:56:31 +01:00
|
|
|
* @param string $lines The list of names seperated by \n
|
|
|
|
* @param string $class_name The name of the class for which the entities should be created
|
2020-02-01 19:48:07 +01:00
|
|
|
* @param AbstractStructuralDBElement|null $parent the element which will be used as parent element for new elements
|
2020-03-15 13:56:31 +01:00
|
|
|
* @param array $errors an associative array containing all validation errors
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2020-02-01 19:48:07 +01:00
|
|
|
* @return AbstractStructuralDBElement[] An array containing all valid imported entities (with the type $class_name)
|
2019-08-12 22:41:58 +02:00
|
|
|
*/
|
2020-02-01 19:48:07 +01:00
|
|
|
public function massCreation(string $lines, string $class_name, ?AbstractStructuralDBElement $parent = null, array &$errors = []): array
|
2019-08-12 22:41:58 +02:00
|
|
|
{
|
|
|
|
//Expand every line to a single entry:
|
|
|
|
$names = explode("\n", $lines);
|
|
|
|
|
2023-01-28 23:24:45 +01:00
|
|
|
if (!is_a($class_name, AbstractNamedDBElement::class, true)) {
|
2020-01-05 22:49:00 +01:00
|
|
|
throw new InvalidArgumentException('$class_name must be a StructuralDBElement type!');
|
2019-11-09 16:14:57 +01:00
|
|
|
}
|
2023-06-11 14:55:06 +02:00
|
|
|
if ($parent instanceof AbstractStructuralDBElement && !$parent instanceof $class_name) {
|
2020-01-05 22:49:00 +01:00
|
|
|
throw new InvalidArgumentException('$parent must have the same type as specified in $class_name!');
|
2019-11-09 16:14:57 +01:00
|
|
|
}
|
|
|
|
|
2019-11-09 00:47:20 +01:00
|
|
|
$errors = [];
|
2019-11-09 16:14:57 +01:00
|
|
|
$valid_entities = [];
|
2019-08-12 22:41:58 +02:00
|
|
|
|
2023-01-28 23:24:45 +01:00
|
|
|
$current_parent = $parent;
|
|
|
|
$last_element = $parent;
|
|
|
|
//We use this array to store all levels of indentation as a stack.
|
|
|
|
$indentations = [0];
|
|
|
|
|
2019-08-12 22:41:58 +02:00
|
|
|
foreach ($names as $name) {
|
2023-01-28 23:24:45 +01:00
|
|
|
//Count intendation level (whitespace characters at the beginning of the line)
|
|
|
|
$identSize = strlen($name)-strlen(ltrim($name));
|
|
|
|
|
|
|
|
//If the line is intendet more than the last line, we have a new parent element
|
|
|
|
if ($identSize > end($indentations)) {
|
|
|
|
$current_parent = $last_element;
|
|
|
|
//Add the new indentation level to the stack
|
|
|
|
$indentations[] = $identSize;
|
|
|
|
}
|
|
|
|
while ($identSize < end($indentations)) {
|
|
|
|
//If the line is intendet less than the last line, we have to go up in the tree
|
2023-06-11 14:55:06 +02:00
|
|
|
$current_parent = $current_parent instanceof AbstractStructuralDBElement ? $current_parent->getParent() : null;
|
2023-01-28 23:24:45 +01:00
|
|
|
array_pop($indentations);
|
|
|
|
}
|
|
|
|
|
2019-08-12 22:41:58 +02:00
|
|
|
$name = trim($name);
|
2020-01-04 20:24:09 +01:00
|
|
|
if ('' === $name) {
|
2019-11-09 16:14:57 +01:00
|
|
|
//Skip empty lines (StrucuralDBElements must have a name)
|
|
|
|
continue;
|
|
|
|
}
|
2020-02-01 19:48:07 +01:00
|
|
|
/** @var AbstractStructuralDBElement $entity */
|
2019-08-12 22:41:58 +02:00
|
|
|
//Create new element with given name
|
|
|
|
$entity = new $class_name();
|
|
|
|
$entity->setName($name);
|
2023-01-28 23:24:45 +01:00
|
|
|
//Only set the parent if the entity is a StructuralDBElement
|
|
|
|
if ($entity instanceof AbstractStructuralDBElement) {
|
|
|
|
$entity->setParent($current_parent);
|
|
|
|
}
|
2019-08-12 22:41:58 +02:00
|
|
|
|
|
|
|
//Validate entity
|
|
|
|
$tmp = $this->validator->validate($entity);
|
|
|
|
//If no error occured, write entry to DB:
|
2020-01-05 22:49:00 +01:00
|
|
|
if (0 === count($tmp)) {
|
2019-11-09 16:14:57 +01:00
|
|
|
$valid_entities[] = $entity;
|
2019-08-12 22:41:58 +02:00
|
|
|
} else { //Otherwise log error
|
2020-01-05 22:49:00 +01:00
|
|
|
$errors[] = [
|
|
|
|
'entity' => $entity,
|
|
|
|
'violations' => $tmp,
|
|
|
|
];
|
2019-08-12 22:41:58 +02:00
|
|
|
}
|
2023-01-28 23:24:45 +01:00
|
|
|
|
|
|
|
$last_element = $entity;
|
2019-08-12 22:41:58 +02:00
|
|
|
}
|
|
|
|
|
2019-11-09 16:14:57 +01:00
|
|
|
return $valid_entities;
|
2019-08-12 22:41:58 +02:00
|
|
|
}
|
|
|
|
|
2019-04-11 22:41:21 +02:00
|
|
|
/**
|
2023-03-12 19:16:49 +01:00
|
|
|
* Import data from a string.
|
|
|
|
* @param string $data The serialized data which should be imported
|
|
|
|
* @param array $options The options for the import process
|
|
|
|
* @param array $errors An array which will be filled with the validation errors, if any occurs during import
|
|
|
|
* @return array An array containing all valid imported entities
|
2019-04-11 22:41:21 +02:00
|
|
|
*/
|
2023-03-12 19:16:49 +01:00
|
|
|
public function importString(string $data, array $options = [], array &$errors = []): array
|
2019-04-11 22:41:21 +02:00
|
|
|
{
|
|
|
|
$resolver = new OptionsResolver();
|
|
|
|
$this->configureOptions($resolver);
|
|
|
|
$options = $resolver->resolve($options);
|
|
|
|
|
2023-03-12 19:16:49 +01:00
|
|
|
if (!is_a($options['class'], AbstractNamedDBElement::class, true)) {
|
|
|
|
throw new InvalidArgumentException('$class_name must be an AbstractNamedDBElement type!');
|
|
|
|
}
|
|
|
|
|
|
|
|
$groups = ['import']; //We can only import data, that is marked with the group "import"
|
|
|
|
//Add group when the children should be preserved
|
|
|
|
if ($options['preserve_children']) {
|
|
|
|
$groups[] = 'include_children';
|
|
|
|
}
|
|
|
|
|
|
|
|
//The [] behind class_name denotes that we expect an array.
|
|
|
|
$entities = $this->serializer->deserialize($data, $options['class'].'[]', $options['format'],
|
|
|
|
[
|
|
|
|
'groups' => $groups,
|
2023-03-12 19:53:55 +01:00
|
|
|
'csv_delimiter' => $options['csv_delimiter'],
|
2023-03-12 21:10:48 +01:00
|
|
|
'create_unknown_datastructures' => $options['create_unknown_datastructures'],
|
|
|
|
'path_delimiter' => $options['path_delimiter'],
|
2023-03-12 19:16:49 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
//Ensure we have an array of entity elements.
|
|
|
|
if (!is_array($entities)) {
|
|
|
|
$entities = [$entities];
|
|
|
|
}
|
|
|
|
|
|
|
|
//The serializer has only set the children attributes. We also have to change the parent value (the real value in DB)
|
|
|
|
if ($entities[0] instanceof AbstractStructuralDBElement) {
|
|
|
|
$this->correctParentEntites($entities, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Set the parent of the imported elements to the given options
|
|
|
|
foreach ($entities as $entity) {
|
|
|
|
if ($entity instanceof AbstractStructuralDBElement) {
|
|
|
|
$entity->setParent($options['parent']);
|
|
|
|
}
|
2023-03-15 21:46:14 +01:00
|
|
|
if ($entity instanceof Part) {
|
|
|
|
if ($options['part_category']) {
|
|
|
|
$entity->setCategory($options['part_category']);
|
|
|
|
}
|
|
|
|
if ($options['part_needs_review']) {
|
|
|
|
$entity->setNeedsReview(true);
|
|
|
|
}
|
2023-03-12 19:53:55 +01:00
|
|
|
}
|
2023-03-12 19:16:49 +01:00
|
|
|
}
|
2019-04-11 22:41:21 +02:00
|
|
|
|
2023-03-12 19:16:49 +01:00
|
|
|
//Validate the entities
|
2019-11-09 00:47:20 +01:00
|
|
|
$errors = [];
|
2019-04-11 22:41:21 +02:00
|
|
|
|
|
|
|
//Iterate over each $entity write it to DB.
|
2023-03-12 19:53:55 +01:00
|
|
|
foreach ($entities as $key => $entity) {
|
2019-04-11 22:41:21 +02:00
|
|
|
//Validate entity
|
|
|
|
$tmp = $this->validator->validate($entity);
|
|
|
|
|
2023-03-12 19:16:49 +01:00
|
|
|
if (count($tmp) > 0) { //Log validation errors to global log.
|
|
|
|
$name = $entity instanceof AbstractStructuralDBElement ? $entity->getFullPath() : $entity->getName();
|
|
|
|
|
|
|
|
$errors[$name] = [
|
|
|
|
'violations' => $tmp,
|
|
|
|
'entity' => $entity,
|
|
|
|
];
|
2023-03-12 19:53:55 +01:00
|
|
|
|
|
|
|
//Remove the invalid entity from the array
|
|
|
|
unset($entities[$key]);
|
2019-04-11 22:41:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-12 19:16:49 +01:00
|
|
|
return $entities;
|
|
|
|
}
|
2019-04-11 22:41:21 +02:00
|
|
|
|
2023-03-12 19:16:49 +01:00
|
|
|
protected function configureOptions(OptionsResolver $resolver): OptionsResolver
|
|
|
|
{
|
|
|
|
$resolver->setDefaults([
|
2023-03-12 19:53:55 +01:00
|
|
|
'csv_delimiter' => ';', //The separator to use when importing csv files
|
|
|
|
'format' => 'json', //The format of the file that should be imported
|
2023-03-12 19:16:49 +01:00
|
|
|
'class' => AbstractNamedDBElement::class,
|
|
|
|
'preserve_children' => true,
|
|
|
|
'parent' => null, //The parent element to which the imported elements should be added
|
|
|
|
'abort_on_validation_error' => true,
|
2023-03-12 21:10:48 +01:00
|
|
|
'part_category' => null,
|
2023-03-15 21:46:14 +01:00
|
|
|
'part_needs_review' => false, //If true, the imported parts will be marked as "needs review", otherwise the value from the file will be used
|
2023-03-12 21:10:48 +01:00
|
|
|
'create_unknown_datastructures' => true, //If true, unknown datastructures (categories, footprints, etc.) will be created on the fly
|
|
|
|
'path_delimiter' => '->', //The delimiter used to separate the path elements in the name of a structural element
|
2023-03-12 19:16:49 +01:00
|
|
|
]);
|
|
|
|
|
2023-03-12 19:53:55 +01:00
|
|
|
$resolver->setAllowedValues('format', ['csv', 'json', 'xml', 'yaml']);
|
|
|
|
$resolver->setAllowedTypes('csv_delimiter', 'string');
|
|
|
|
$resolver->setAllowedTypes('preserve_children', 'bool');
|
|
|
|
$resolver->setAllowedTypes('class', 'string');
|
|
|
|
$resolver->setAllowedTypes('part_category', [Category::class, 'null']);
|
2023-03-15 21:46:14 +01:00
|
|
|
$resolver->setAllowedTypes('part_needs_review', 'bool');
|
2023-03-12 19:53:55 +01:00
|
|
|
|
2023-03-12 19:16:49 +01:00
|
|
|
return $resolver;
|
2019-04-11 22:41:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-03-12 19:16:49 +01:00
|
|
|
* This method deserializes the given file and writes the entities to the database (and flush the db).
|
|
|
|
* The imported elements will be checked (validated) before written to database.
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2020-01-04 20:24:09 +01:00
|
|
|
* @param File $file the file that should be used for importing
|
|
|
|
* @param array $options options for the import process
|
2023-03-12 19:53:55 +01:00
|
|
|
* @param AbstractNamedDBElement[] $entities The imported entities are returned in this array
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2023-03-12 19:16:49 +01:00
|
|
|
* @return array An associative array containing an ConstraintViolationList and the entity name as key are returned,
|
|
|
|
* if an error happened during validation. When everything was successfully, the array should be empty.
|
2019-04-11 22:41:21 +02:00
|
|
|
*/
|
2023-03-12 19:53:55 +01:00
|
|
|
public function importFileAndPersistToDB(File $file, array $options = [], array &$entities = []): array
|
2019-04-11 22:41:21 +02:00
|
|
|
{
|
2023-03-12 19:16:49 +01:00
|
|
|
$options = $this->configureOptions(new OptionsResolver())->resolve($options);
|
2019-04-11 22:41:21 +02:00
|
|
|
|
2023-03-12 19:16:49 +01:00
|
|
|
$errors = [];
|
|
|
|
$entities = $this->importFile($file, $options, $errors);
|
2019-04-11 22:41:21 +02:00
|
|
|
|
2023-03-12 19:16:49 +01:00
|
|
|
//When we should abort on validation error, do nothing and return the errors
|
|
|
|
if (!empty($errors) && $options['abort_on_validation_error']) {
|
|
|
|
return $errors;
|
2019-04-11 22:41:21 +02:00
|
|
|
}
|
|
|
|
|
2023-03-12 19:53:55 +01:00
|
|
|
//Iterate over each $entity write it to DB (the invalid entities were already filtered out).
|
2023-03-12 19:16:49 +01:00
|
|
|
foreach ($entities as $entity) {
|
2023-03-12 19:53:55 +01:00
|
|
|
$this->em->persist($entity);
|
2019-04-11 22:41:21 +02:00
|
|
|
}
|
|
|
|
|
2023-03-12 19:16:49 +01:00
|
|
|
//Save changes to database, when no error happened, or we should continue on error.
|
|
|
|
$this->em->flush();
|
2019-04-11 22:41:21 +02:00
|
|
|
|
2023-03-12 19:16:49 +01:00
|
|
|
return $errors;
|
2019-04-11 22:41:21 +02:00
|
|
|
}
|
|
|
|
|
2023-03-12 19:16:49 +01:00
|
|
|
/**
|
|
|
|
* This method converts (deserialize) a (uploaded) file to an array of entities with the given class.
|
|
|
|
* The imported elements are not persisted to database yet, so you have to do it yourself.
|
|
|
|
*
|
|
|
|
* @param File $file the file that should be used for importing
|
|
|
|
* @param array $options options for the import process
|
|
|
|
*
|
|
|
|
* @return array an array containing the deserialized elements
|
|
|
|
*/
|
|
|
|
public function importFile(File $file, array $options = [], array &$errors = []): array
|
2020-01-05 15:46:58 +01:00
|
|
|
{
|
2023-03-12 19:16:49 +01:00
|
|
|
return $this->importString($file->getContent(), $options, $errors);
|
2020-01-05 15:46:58 +01:00
|
|
|
}
|
|
|
|
|
2023-03-12 21:43:40 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines the format to import based on the file extension.
|
|
|
|
* @param string $extension The file extension to use
|
|
|
|
* @return string The format to use (json, xml, csv, yaml), or null if the extension is unknown
|
|
|
|
*/
|
|
|
|
public function determineFormat(string $extension): ?string
|
|
|
|
{
|
|
|
|
//Convert the extension to lower case
|
|
|
|
$extension = strtolower($extension);
|
|
|
|
|
2023-06-11 14:15:46 +02:00
|
|
|
return match ($extension) {
|
|
|
|
'json' => 'json',
|
|
|
|
'xml' => 'xml',
|
|
|
|
'csv', 'tsv' => 'csv',
|
|
|
|
'yaml', 'yml' => 'yaml',
|
|
|
|
default => null,
|
|
|
|
};
|
2023-03-12 21:43:40 +01:00
|
|
|
}
|
|
|
|
|
2019-04-11 22:41:21 +02:00
|
|
|
/**
|
|
|
|
* This functions corrects the parent setting based on the children value of the parent.
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2020-03-15 13:56:31 +01:00
|
|
|
* @param iterable $entities the list of entities that should be fixed
|
2022-08-14 19:09:07 +02:00
|
|
|
* @param AbstractStructuralDBElement|null $parent the parent, to which the entity should be set
|
2019-04-11 22:41:21 +02:00
|
|
|
*/
|
2022-08-14 19:09:07 +02:00
|
|
|
protected function correctParentEntites(iterable $entities, AbstractStructuralDBElement $parent = null): void
|
2019-04-11 22:41:21 +02:00
|
|
|
{
|
|
|
|
foreach ($entities as $entity) {
|
2020-02-01 19:48:07 +01:00
|
|
|
/** @var AbstractStructuralDBElement $entity */
|
2019-04-11 22:41:21 +02:00
|
|
|
$entity->setParent($parent);
|
|
|
|
//Do the same for the children of entity
|
|
|
|
$this->correctParentEntites($entity->getChildren(), $entity);
|
|
|
|
}
|
|
|
|
}
|
2019-11-09 00:47:20 +01:00
|
|
|
}
|