2025-08-02 17:56:46 +02:00
|
|
|
<?php
|
|
|
|
|
/*
|
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
|
|
|
|
|
*
|
|
|
|
|
* 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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace App\Controller;
|
|
|
|
|
|
2025-08-02 20:40:37 +02:00
|
|
|
use App\Entity\BulkInfoProviderImportJob;
|
2025-08-02 23:35:30 +02:00
|
|
|
use App\Entity\BulkInfoProviderImportJobPart;
|
2025-08-02 20:40:37 +02:00
|
|
|
use App\Entity\BulkImportJobStatus;
|
2025-08-02 17:56:46 +02:00
|
|
|
use App\Entity\Parts\Part;
|
|
|
|
|
use App\Entity\Parts\Supplier;
|
|
|
|
|
use App\Form\InfoProviderSystem\GlobalFieldMappingType;
|
2025-09-09 20:30:27 +02:00
|
|
|
use App\Services\InfoProviderSystem\BulkInfoProviderService;
|
2025-09-21 14:24:34 +02:00
|
|
|
use App\Services\InfoProviderSystem\DTOs\BulkSearchResponseDTO;
|
2025-09-21 17:49:00 +02:00
|
|
|
use App\Services\InfoProviderSystem\DTOs\BulkSearchFieldMappingDTO;
|
|
|
|
|
use App\Services\InfoProviderSystem\DTOs\BulkSearchPartResultsDTO;
|
2025-08-02 17:56:46 +02:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
2025-09-20 14:33:16 +02:00
|
|
|
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
2025-08-02 17:56:46 +02:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
2025-09-09 20:30:27 +02:00
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
2025-08-02 17:56:46 +02:00
|
|
|
use Symfony\Component\Routing\Attribute\Route;
|
2025-08-02 21:14:04 +02:00
|
|
|
use App\Entity\UserSystem\User;
|
2025-08-02 17:56:46 +02:00
|
|
|
|
|
|
|
|
#[Route('/tools/bulk-info-provider-import')]
|
|
|
|
|
class BulkInfoProviderImportController extends AbstractController
|
|
|
|
|
{
|
|
|
|
|
public function __construct(
|
2025-09-09 20:30:27 +02:00
|
|
|
private readonly BulkInfoProviderService $bulkService,
|
|
|
|
|
private readonly EntityManagerInterface $entityManager,
|
2025-09-19 16:28:40 +02:00
|
|
|
private readonly LoggerInterface $logger,
|
2025-09-20 14:33:16 +02:00
|
|
|
#[Autowire(param: 'partdb.bulk_import.batch_size')]
|
2025-09-19 16:28:40 +02:00
|
|
|
private readonly int $bulkImportBatchSize,
|
2025-09-20 14:33:16 +02:00
|
|
|
#[Autowire(param: 'partdb.bulk_import.max_parts_per_operation')]
|
2025-09-19 16:28:40 +02:00
|
|
|
private readonly int $bulkImportMaxParts
|
2025-08-02 17:56:46 +02:00
|
|
|
) {
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-19 16:28:40 +02:00
|
|
|
/**
|
|
|
|
|
* Convert field mappings from array format to FieldMappingDTO[].
|
|
|
|
|
*
|
|
|
|
|
* @param array $fieldMappings Array of field mapping arrays
|
2025-09-21 17:49:00 +02:00
|
|
|
* @return BulkSearchFieldMappingDTO[] Array of FieldMappingDTO objects
|
2025-09-19 16:28:40 +02:00
|
|
|
*/
|
|
|
|
|
private function convertFieldMappingsToDto(array $fieldMappings): array
|
|
|
|
|
{
|
|
|
|
|
$dtos = [];
|
|
|
|
|
foreach ($fieldMappings as $mapping) {
|
2025-09-21 17:49:00 +02:00
|
|
|
$dtos[] = new BulkSearchFieldMappingDTO(field: $mapping['field'], providers: $mapping['providers'], priority: $mapping['priority'] ?? 1);
|
2025-09-19 16:28:40 +02:00
|
|
|
}
|
|
|
|
|
return $dtos;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-09 20:30:27 +02:00
|
|
|
private function createErrorResponse(string $message, int $statusCode = 400, array $context = []): JsonResponse
|
|
|
|
|
{
|
|
|
|
|
$this->logger->warning('Bulk import operation failed', array_merge([
|
|
|
|
|
'error' => $message,
|
|
|
|
|
'user' => $this->getUser()?->getUserIdentifier(),
|
|
|
|
|
], $context));
|
|
|
|
|
|
|
|
|
|
return $this->json([
|
|
|
|
|
'success' => false,
|
|
|
|
|
'error' => $message
|
|
|
|
|
], $statusCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function validateJobAccess(int $jobId): ?BulkInfoProviderImportJob
|
|
|
|
|
{
|
2025-09-14 16:24:56 +02:00
|
|
|
$this->denyAccessUnlessGranted('@info_providers.create_parts');
|
|
|
|
|
|
2025-09-09 20:30:27 +02:00
|
|
|
$job = $this->entityManager->getRepository(BulkInfoProviderImportJob::class)->find($jobId);
|
|
|
|
|
|
|
|
|
|
if (!$job) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($job->getCreatedBy() !== $this->getUser()) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $job;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-21 17:49:00 +02:00
|
|
|
private function updatePartSearchResults(BulkInfoProviderImportJob $job, int $partId, ?BulkSearchPartResultsDTO $newResults): void
|
2025-09-09 20:30:27 +02:00
|
|
|
{
|
|
|
|
|
if ($newResults === null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Only deserialize and update if we have new results
|
2025-09-21 14:24:34 +02:00
|
|
|
$allResults = $job->getSearchResults($this->entityManager);
|
2025-09-09 20:30:27 +02:00
|
|
|
|
|
|
|
|
// Find and update the results for this specific part
|
2025-09-21 14:24:34 +02:00
|
|
|
$allResults = $allResults->replaceResultsForPart($partId, $newResults);
|
2025-09-09 20:30:27 +02:00
|
|
|
|
|
|
|
|
// Save updated results back to job
|
2025-09-21 14:24:34 +02:00
|
|
|
$job->setSearchResults($allResults);
|
2025-09-09 20:30:27 +02:00
|
|
|
}
|
|
|
|
|
|
2025-08-02 17:56:46 +02:00
|
|
|
#[Route('/step1', name: 'bulk_info_provider_step1')]
|
2025-09-09 20:30:27 +02:00
|
|
|
public function step1(Request $request): Response
|
2025-08-02 17:56:46 +02:00
|
|
|
{
|
|
|
|
|
$this->denyAccessUnlessGranted('@info_providers.create_parts');
|
2025-08-31 23:41:16 +02:00
|
|
|
|
2025-09-09 20:30:27 +02:00
|
|
|
set_time_limit(600);
|
2025-08-02 17:56:46 +02:00
|
|
|
|
|
|
|
|
$ids = $request->query->get('ids');
|
|
|
|
|
if (!$ids) {
|
|
|
|
|
$this->addFlash('error', 'No parts selected for bulk import');
|
|
|
|
|
return $this->redirectToRoute('homepage');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$partIds = explode(',', $ids);
|
|
|
|
|
$partRepository = $this->entityManager->getRepository(Part::class);
|
|
|
|
|
$parts = $partRepository->getElementsFromIDArray($partIds);
|
|
|
|
|
|
|
|
|
|
if (empty($parts)) {
|
|
|
|
|
$this->addFlash('error', 'No valid parts found for bulk import');
|
|
|
|
|
return $this->redirectToRoute('homepage');
|
|
|
|
|
}
|
2025-08-31 23:41:16 +02:00
|
|
|
|
2025-09-19 16:28:40 +02:00
|
|
|
// Validate against configured maximum
|
|
|
|
|
if (count($parts) > $this->bulkImportMaxParts) {
|
|
|
|
|
$this->addFlash('error', sprintf(
|
|
|
|
|
'Too many parts selected (%d). Maximum allowed is %d parts per operation.',
|
|
|
|
|
count($parts),
|
|
|
|
|
$this->bulkImportMaxParts
|
|
|
|
|
));
|
|
|
|
|
return $this->redirectToRoute('homepage');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (count($parts) > ($this->bulkImportMaxParts / 2)) {
|
2025-08-04 23:34:20 +02:00
|
|
|
$this->addFlash('warning', 'Processing ' . count($parts) . ' parts may take several minutes and could timeout. Consider processing smaller batches.');
|
|
|
|
|
}
|
2025-08-02 17:56:46 +02:00
|
|
|
|
|
|
|
|
// Generate field choices
|
|
|
|
|
$fieldChoices = [
|
|
|
|
|
'info_providers.bulk_search.field.mpn' => 'mpn',
|
|
|
|
|
'info_providers.bulk_search.field.name' => 'name',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// Add dynamic supplier fields
|
|
|
|
|
$suppliers = $this->entityManager->getRepository(Supplier::class)->findAll();
|
|
|
|
|
foreach ($suppliers as $supplier) {
|
|
|
|
|
$supplierKey = strtolower(str_replace([' ', '-', '_'], '_', $supplier->getName()));
|
|
|
|
|
$fieldChoices["Supplier: " . $supplier->getName() . " (SPN)"] = $supplierKey . '_spn';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Initialize form with useful default mappings
|
|
|
|
|
$initialData = [
|
|
|
|
|
'field_mappings' => [
|
2025-08-04 23:34:20 +02:00
|
|
|
['field' => 'mpn', 'providers' => [], 'priority' => 1]
|
2025-08-02 20:40:37 +02:00
|
|
|
],
|
|
|
|
|
'prefetch_details' => false
|
2025-08-02 17:56:46 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$form = $this->createForm(GlobalFieldMappingType::class, $initialData, [
|
|
|
|
|
'field_choices' => $fieldChoices
|
|
|
|
|
]);
|
|
|
|
|
$form->handleRequest($request);
|
|
|
|
|
|
|
|
|
|
$searchResults = null;
|
|
|
|
|
|
|
|
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
2025-08-02 20:40:37 +02:00
|
|
|
$formData = $form->getData();
|
2025-09-21 17:41:56 +02:00
|
|
|
$fieldMappingDtos = $this->convertFieldMappingsToDto($formData['field_mappings']);
|
2025-08-02 20:40:37 +02:00
|
|
|
$prefetchDetails = $formData['prefetch_details'] ?? false;
|
2025-08-31 23:41:16 +02:00
|
|
|
|
2025-09-09 20:30:27 +02:00
|
|
|
$user = $this->getUser();
|
|
|
|
|
if (!$user instanceof User) {
|
|
|
|
|
throw new \RuntimeException('User must be authenticated and of type User');
|
|
|
|
|
}
|
2025-08-02 20:40:37 +02:00
|
|
|
|
2025-09-19 16:28:40 +02:00
|
|
|
// Validate part count against configuration limit
|
|
|
|
|
if (count($parts) > $this->bulkImportMaxParts) {
|
|
|
|
|
$this->addFlash('error', "Too many parts selected. Maximum allowed: {$this->bulkImportMaxParts}");
|
|
|
|
|
$partIds = array_map(fn($part) => $part->getId(), $parts);
|
|
|
|
|
return $this->redirectToRoute('bulk_info_provider_step1', ['ids' => implode(',', $partIds)]);
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-02 20:40:37 +02:00
|
|
|
// Create and save the job
|
|
|
|
|
$job = new BulkInfoProviderImportJob();
|
2025-09-21 17:41:56 +02:00
|
|
|
$job->setFieldMappings($fieldMappingDtos);
|
2025-08-02 20:40:37 +02:00
|
|
|
$job->setPrefetchDetails($prefetchDetails);
|
2025-08-02 21:14:04 +02:00
|
|
|
$job->setCreatedBy($user);
|
2025-08-02 20:40:37 +02:00
|
|
|
|
2025-08-02 23:35:30 +02:00
|
|
|
foreach ($parts as $part) {
|
|
|
|
|
$jobPart = new BulkInfoProviderImportJobPart($job, $part);
|
|
|
|
|
$job->addJobPart($jobPart);
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-02 20:40:37 +02:00
|
|
|
$this->entityManager->persist($job);
|
|
|
|
|
$this->entityManager->flush();
|
|
|
|
|
|
2025-08-04 23:34:20 +02:00
|
|
|
try {
|
2025-09-19 16:28:40 +02:00
|
|
|
$searchResultsDto = $this->bulkService->performBulkSearch($parts, $fieldMappingDtos, $prefetchDetails);
|
2025-08-31 23:41:16 +02:00
|
|
|
|
2025-08-04 23:34:20 +02:00
|
|
|
// Save search results to job
|
2025-09-21 14:24:34 +02:00
|
|
|
$job->setSearchResults($searchResultsDto);
|
2025-08-04 23:34:20 +02:00
|
|
|
$job->markAsInProgress();
|
|
|
|
|
$this->entityManager->flush();
|
2025-08-31 23:41:16 +02:00
|
|
|
|
2025-09-09 20:30:27 +02:00
|
|
|
// Prefetch details if requested
|
|
|
|
|
if ($prefetchDetails) {
|
2025-09-21 14:24:34 +02:00
|
|
|
$this->bulkService->prefetchDetailsForResults($searchResultsDto);
|
2025-09-09 20:30:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->redirectToRoute('bulk_info_provider_step2', ['jobId' => $job->getId()]);
|
|
|
|
|
|
2025-08-04 23:34:20 +02:00
|
|
|
} catch (\Exception $e) {
|
2025-09-09 20:30:27 +02:00
|
|
|
$this->logger->error('Critical error during bulk import search', [
|
2025-08-04 23:34:20 +02:00
|
|
|
'job_id' => $job->getId(),
|
|
|
|
|
'error' => $e->getMessage(),
|
|
|
|
|
'exception' => $e
|
|
|
|
|
]);
|
2025-08-31 23:41:16 +02:00
|
|
|
|
2025-08-04 23:34:20 +02:00
|
|
|
$this->entityManager->remove($job);
|
|
|
|
|
$this->entityManager->flush();
|
2025-08-31 23:41:16 +02:00
|
|
|
|
2025-08-04 23:34:20 +02:00
|
|
|
$this->addFlash('error', 'Search failed due to an error: ' . $e->getMessage());
|
2025-09-19 16:28:40 +02:00
|
|
|
$partIds = array_map(fn($part) => $part->getId(), $parts);
|
2025-08-04 23:34:20 +02:00
|
|
|
return $this->redirectToRoute('bulk_info_provider_step1', ['ids' => implode(',', $partIds)]);
|
2025-08-02 17:56:46 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-02 20:40:37 +02:00
|
|
|
// Get existing in-progress jobs for current user
|
|
|
|
|
$existingJobs = $this->entityManager->getRepository(BulkInfoProviderImportJob::class)
|
|
|
|
|
->findBy(['createdBy' => $this->getUser(), 'status' => BulkImportJobStatus::IN_PROGRESS], ['createdAt' => 'DESC'], 10);
|
|
|
|
|
|
2025-08-02 17:56:46 +02:00
|
|
|
return $this->render('info_providers/bulk_import/step1.html.twig', [
|
|
|
|
|
'form' => $form,
|
|
|
|
|
'parts' => $parts,
|
|
|
|
|
'search_results' => $searchResults,
|
2025-08-02 20:40:37 +02:00
|
|
|
'existing_jobs' => $existingJobs,
|
2025-08-02 17:56:46 +02:00
|
|
|
'fieldChoices' => $fieldChoices
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-02 20:40:37 +02:00
|
|
|
#[Route('/manage', name: 'bulk_info_provider_manage')]
|
|
|
|
|
public function manageBulkJobs(): Response
|
|
|
|
|
{
|
2025-09-14 16:24:56 +02:00
|
|
|
$this->denyAccessUnlessGranted('@info_providers.create_parts');
|
|
|
|
|
|
2025-08-02 20:40:37 +02:00
|
|
|
// Get all jobs for current user
|
|
|
|
|
$allJobs = $this->entityManager->getRepository(BulkInfoProviderImportJob::class)
|
|
|
|
|
->findBy([], ['createdAt' => 'DESC']);
|
|
|
|
|
|
|
|
|
|
// Check and auto-complete jobs that should be completed
|
2025-08-04 23:34:20 +02:00
|
|
|
// Also clean up jobs with no results (failed searches)
|
2025-08-02 20:40:37 +02:00
|
|
|
$updatedJobs = false;
|
2025-08-04 23:34:20 +02:00
|
|
|
$jobsToDelete = [];
|
2025-08-31 23:41:16 +02:00
|
|
|
|
2025-08-02 20:40:37 +02:00
|
|
|
foreach ($allJobs as $job) {
|
|
|
|
|
if ($job->isAllPartsCompleted() && !$job->isCompleted()) {
|
|
|
|
|
$job->markAsCompleted();
|
|
|
|
|
$updatedJobs = true;
|
|
|
|
|
}
|
2025-08-31 23:41:16 +02:00
|
|
|
|
2025-08-04 23:34:20 +02:00
|
|
|
// Mark jobs with no results for deletion (failed searches)
|
|
|
|
|
if ($job->getResultCount() === 0 && $job->isInProgress()) {
|
|
|
|
|
$jobsToDelete[] = $job;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-31 23:41:16 +02:00
|
|
|
|
2025-08-04 23:34:20 +02:00
|
|
|
// Delete failed jobs
|
|
|
|
|
foreach ($jobsToDelete as $job) {
|
|
|
|
|
$this->entityManager->remove($job);
|
|
|
|
|
$updatedJobs = true;
|
2025-08-02 20:40:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Flush changes if any jobs were updated
|
|
|
|
|
if ($updatedJobs) {
|
|
|
|
|
$this->entityManager->flush();
|
2025-08-31 23:41:16 +02:00
|
|
|
|
2025-08-04 23:34:20 +02:00
|
|
|
if (!empty($jobsToDelete)) {
|
|
|
|
|
$this->addFlash('info', 'Cleaned up ' . count($jobsToDelete) . ' failed job(s) with no results.');
|
|
|
|
|
}
|
2025-08-02 20:40:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->render('info_providers/bulk_import/manage.html.twig', [
|
2025-08-04 23:34:20 +02:00
|
|
|
'jobs' => $this->entityManager->getRepository(BulkInfoProviderImportJob::class)
|
|
|
|
|
->findBy([], ['createdAt' => 'DESC']) // Refetch after cleanup
|
2025-08-02 20:40:37 +02:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[Route('/job/{jobId}/delete', name: 'bulk_info_provider_delete', methods: ['DELETE'])]
|
|
|
|
|
public function deleteJob(int $jobId): Response
|
|
|
|
|
{
|
2025-09-14 16:24:56 +02:00
|
|
|
$job = $this->validateJobAccess($jobId);
|
|
|
|
|
if (!$job) {
|
|
|
|
|
return $this->createErrorResponse('Job not found or access denied', 404, ['job_id' => $jobId]);
|
2025-08-02 20:40:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Only allow deletion of completed, failed, or stopped jobs
|
|
|
|
|
if (!$job->isCompleted() && !$job->isFailed() && !$job->isStopped()) {
|
|
|
|
|
return $this->json(['error' => 'Cannot delete active job'], 400);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->entityManager->remove($job);
|
|
|
|
|
$this->entityManager->flush();
|
|
|
|
|
|
|
|
|
|
return $this->json(['success' => true]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[Route('/job/{jobId}/stop', name: 'bulk_info_provider_stop', methods: ['POST'])]
|
|
|
|
|
public function stopJob(int $jobId): Response
|
|
|
|
|
{
|
2025-09-14 16:24:56 +02:00
|
|
|
$job = $this->validateJobAccess($jobId);
|
|
|
|
|
if (!$job) {
|
|
|
|
|
return $this->createErrorResponse('Job not found or access denied', 404, ['job_id' => $jobId]);
|
2025-08-02 20:40:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Only allow stopping of pending or in-progress jobs
|
|
|
|
|
if (!$job->canBeStopped()) {
|
|
|
|
|
return $this->json(['error' => 'Cannot stop job in current status'], 400);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$job->markAsStopped();
|
|
|
|
|
$this->entityManager->flush();
|
|
|
|
|
|
|
|
|
|
return $this->json(['success' => true]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[Route('/step2/{jobId}', name: 'bulk_info_provider_step2')]
|
|
|
|
|
public function step2(int $jobId): Response
|
|
|
|
|
{
|
2025-09-14 16:24:56 +02:00
|
|
|
$this->denyAccessUnlessGranted('@info_providers.create_parts');
|
|
|
|
|
|
2025-08-02 20:40:37 +02:00
|
|
|
$job = $this->entityManager->getRepository(BulkInfoProviderImportJob::class)->find($jobId);
|
|
|
|
|
|
|
|
|
|
if (!$job) {
|
|
|
|
|
$this->addFlash('error', 'Bulk import job not found');
|
|
|
|
|
return $this->redirectToRoute('bulk_info_provider_step1');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if user owns this job
|
|
|
|
|
if ($job->getCreatedBy() !== $this->getUser()) {
|
|
|
|
|
$this->addFlash('error', 'Access denied to this bulk import job');
|
|
|
|
|
return $this->redirectToRoute('bulk_info_provider_step1');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get the parts and deserialize search results
|
2025-08-02 23:35:30 +02:00
|
|
|
$parts = $job->getJobParts()->map(fn($jobPart) => $jobPart->getPart())->toArray();
|
2025-09-21 14:24:34 +02:00
|
|
|
$searchResults = $job->getSearchResults($this->entityManager);
|
2025-08-02 20:40:37 +02:00
|
|
|
|
|
|
|
|
return $this->render('info_providers/bulk_import/step2.html.twig', [
|
|
|
|
|
'job' => $job,
|
|
|
|
|
'parts' => $parts,
|
|
|
|
|
'search_results' => $searchResults,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[Route('/job/{jobId}/part/{partId}/mark-completed', name: 'bulk_info_provider_mark_completed', methods: ['POST'])]
|
|
|
|
|
public function markPartCompleted(int $jobId, int $partId): Response
|
|
|
|
|
{
|
2025-09-14 16:24:56 +02:00
|
|
|
$job = $this->validateJobAccess($jobId);
|
|
|
|
|
if (!$job) {
|
|
|
|
|
return $this->createErrorResponse('Job not found or access denied', 404, ['job_id' => $jobId]);
|
2025-08-02 20:40:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$job->markPartAsCompleted($partId);
|
|
|
|
|
|
|
|
|
|
// Auto-complete job if all parts are done
|
|
|
|
|
if ($job->isAllPartsCompleted() && !$job->isCompleted()) {
|
|
|
|
|
$job->markAsCompleted();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->entityManager->flush();
|
|
|
|
|
|
|
|
|
|
return $this->json([
|
|
|
|
|
'success' => true,
|
|
|
|
|
'progress' => $job->getProgressPercentage(),
|
|
|
|
|
'completed_count' => $job->getCompletedPartsCount(),
|
|
|
|
|
'total_count' => $job->getPartCount(),
|
|
|
|
|
'job_completed' => $job->isCompleted()
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[Route('/job/{jobId}/part/{partId}/mark-skipped', name: 'bulk_info_provider_mark_skipped', methods: ['POST'])]
|
|
|
|
|
public function markPartSkipped(int $jobId, int $partId, Request $request): Response
|
|
|
|
|
{
|
2025-09-14 16:24:56 +02:00
|
|
|
$job = $this->validateJobAccess($jobId);
|
|
|
|
|
if (!$job) {
|
|
|
|
|
return $this->createErrorResponse('Job not found or access denied', 404, ['job_id' => $jobId]);
|
2025-08-02 20:40:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$reason = $request->request->get('reason', '');
|
|
|
|
|
$job->markPartAsSkipped($partId, $reason);
|
|
|
|
|
|
|
|
|
|
// Auto-complete job if all parts are done
|
|
|
|
|
if ($job->isAllPartsCompleted() && !$job->isCompleted()) {
|
|
|
|
|
$job->markAsCompleted();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->entityManager->flush();
|
|
|
|
|
|
|
|
|
|
return $this->json([
|
|
|
|
|
'success' => true,
|
|
|
|
|
'progress' => $job->getProgressPercentage(),
|
|
|
|
|
'completed_count' => $job->getCompletedPartsCount(),
|
|
|
|
|
'skipped_count' => $job->getSkippedPartsCount(),
|
|
|
|
|
'total_count' => $job->getPartCount(),
|
|
|
|
|
'job_completed' => $job->isCompleted()
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[Route('/job/{jobId}/part/{partId}/mark-pending', name: 'bulk_info_provider_mark_pending', methods: ['POST'])]
|
|
|
|
|
public function markPartPending(int $jobId, int $partId): Response
|
|
|
|
|
{
|
2025-09-14 16:24:56 +02:00
|
|
|
$job = $this->validateJobAccess($jobId);
|
|
|
|
|
if (!$job) {
|
|
|
|
|
return $this->createErrorResponse('Job not found or access denied', 404, ['job_id' => $jobId]);
|
2025-08-02 20:40:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$job->markPartAsPending($partId);
|
|
|
|
|
$this->entityManager->flush();
|
|
|
|
|
|
|
|
|
|
return $this->json([
|
|
|
|
|
'success' => true,
|
|
|
|
|
'progress' => $job->getProgressPercentage(),
|
|
|
|
|
'completed_count' => $job->getCompletedPartsCount(),
|
|
|
|
|
'skipped_count' => $job->getSkippedPartsCount(),
|
|
|
|
|
'total_count' => $job->getPartCount(),
|
|
|
|
|
'job_completed' => $job->isCompleted()
|
|
|
|
|
]);
|
|
|
|
|
}
|
2025-09-09 20:30:27 +02:00
|
|
|
|
|
|
|
|
#[Route('/job/{jobId}/part/{partId}/research', name: 'bulk_info_provider_research_part', methods: ['POST'])]
|
|
|
|
|
public function researchPart(int $jobId, int $partId): JsonResponse
|
|
|
|
|
{
|
|
|
|
|
$job = $this->validateJobAccess($jobId);
|
|
|
|
|
if (!$job) {
|
|
|
|
|
return $this->createErrorResponse('Job not found or access denied', 404, ['job_id' => $jobId]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$part = $this->entityManager->getRepository(Part::class)->find($partId);
|
|
|
|
|
if (!$part) {
|
|
|
|
|
return $this->createErrorResponse('Part not found', 404, ['part_id' => $partId]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Only refresh if the entity might be stale (optional optimization)
|
|
|
|
|
if ($this->entityManager->getUnitOfWork()->isScheduledForUpdate($part)) {
|
|
|
|
|
$this->entityManager->refresh($part);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// Use the job's field mappings to perform the search
|
|
|
|
|
$fieldMappings = $job->getFieldMappings();
|
|
|
|
|
$prefetchDetails = $job->isPrefetchDetails();
|
|
|
|
|
|
2025-09-19 16:28:40 +02:00
|
|
|
$fieldMappingDtos = $this->convertFieldMappingsToDto($fieldMappings);
|
2025-09-09 20:30:27 +02:00
|
|
|
|
|
|
|
|
try {
|
2025-09-19 16:28:40 +02:00
|
|
|
$searchResultsDto = $this->bulkService->performBulkSearch([$part], $fieldMappingDtos, $prefetchDetails);
|
2025-09-09 20:30:27 +02:00
|
|
|
} catch (\Exception $searchException) {
|
|
|
|
|
// Handle "no search results found" as a normal case, not an error
|
|
|
|
|
if (str_contains($searchException->getMessage(), 'No search results found')) {
|
2025-09-21 14:24:34 +02:00
|
|
|
$searchResultsDto = null;
|
2025-09-09 20:30:27 +02:00
|
|
|
} else {
|
|
|
|
|
throw $searchException;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update the job's search results for this specific part efficiently
|
2025-09-21 14:24:34 +02:00
|
|
|
$this->updatePartSearchResults($job, $partId, $searchResultsDto[0] ?? null);
|
2025-09-09 20:30:27 +02:00
|
|
|
|
|
|
|
|
// Prefetch details if requested
|
2025-09-21 14:24:34 +02:00
|
|
|
if ($prefetchDetails && $searchResultsDto !== null) {
|
|
|
|
|
$this->bulkService->prefetchDetailsForResults($searchResultsDto);
|
2025-09-09 20:30:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->entityManager->flush();
|
|
|
|
|
|
|
|
|
|
// Return the new results for this part
|
2025-09-21 14:24:34 +02:00
|
|
|
$newResults = $searchResultsDto[0] ?? null;
|
2025-09-09 20:30:27 +02:00
|
|
|
|
|
|
|
|
return $this->json([
|
|
|
|
|
'success' => true,
|
|
|
|
|
'part_id' => $partId,
|
2025-09-21 14:24:34 +02:00
|
|
|
'results_count' => $newResults ? $newResults->getResultCount() : 0,
|
|
|
|
|
'errors_count' => $newResults ? $newResults->getErrorCount() : 0,
|
2025-09-09 20:30:27 +02:00
|
|
|
'message' => 'Part research completed successfully'
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
return $this->createErrorResponse(
|
|
|
|
|
'Research failed: ' . $e->getMessage(),
|
|
|
|
|
500,
|
|
|
|
|
[
|
|
|
|
|
'job_id' => $jobId,
|
|
|
|
|
'part_id' => $partId,
|
|
|
|
|
'exception' => $e->getMessage()
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[Route('/job/{jobId}/research-all', name: 'bulk_info_provider_research_all', methods: ['POST'])]
|
|
|
|
|
public function researchAllParts(int $jobId): JsonResponse
|
|
|
|
|
{
|
|
|
|
|
$job = $this->validateJobAccess($jobId);
|
|
|
|
|
if (!$job) {
|
|
|
|
|
return $this->createErrorResponse('Job not found or access denied', 404, ['job_id' => $jobId]);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-19 16:28:40 +02:00
|
|
|
// Get all parts that are not completed or skipped
|
2025-09-14 22:56:12 +02:00
|
|
|
$parts = [];
|
2025-09-09 20:30:27 +02:00
|
|
|
foreach ($job->getJobParts() as $jobPart) {
|
|
|
|
|
if (!$jobPart->isCompleted() && !$jobPart->isSkipped()) {
|
2025-09-14 22:56:12 +02:00
|
|
|
$parts[] = $jobPart->getPart();
|
2025-09-09 20:30:27 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-14 22:56:12 +02:00
|
|
|
if (empty($parts)) {
|
2025-09-09 20:30:27 +02:00
|
|
|
return $this->json([
|
|
|
|
|
'success' => true,
|
|
|
|
|
'message' => 'No parts to research',
|
|
|
|
|
'researched_count' => 0
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$fieldMappings = $job->getFieldMappings();
|
2025-09-19 16:28:40 +02:00
|
|
|
$fieldMappingDtos = $this->convertFieldMappingsToDto($fieldMappings);
|
2025-09-09 20:30:27 +02:00
|
|
|
$prefetchDetails = $job->isPrefetchDetails();
|
|
|
|
|
|
|
|
|
|
// Process in batches to reduce memory usage for large operations
|
2025-09-21 14:24:34 +02:00
|
|
|
$allResults = new BulkSearchResponseDTO(partResults: []);
|
2025-09-19 16:28:40 +02:00
|
|
|
$batches = array_chunk($parts, $this->bulkImportBatchSize);
|
2025-09-09 20:30:27 +02:00
|
|
|
|
|
|
|
|
foreach ($batches as $batch) {
|
2025-09-19 16:28:40 +02:00
|
|
|
$batchResultsDto = $this->bulkService->performBulkSearch($batch, $fieldMappingDtos, $prefetchDetails);
|
2025-09-21 14:24:34 +02:00
|
|
|
$allResults = BulkSearchResponseDTO::merge($allResults, $batchResultsDto);
|
2025-09-09 20:30:27 +02:00
|
|
|
|
2025-09-19 16:28:40 +02:00
|
|
|
// Properly manage entity manager memory without losing state
|
|
|
|
|
$jobId = $job->getId();
|
2025-09-09 20:30:27 +02:00
|
|
|
$this->entityManager->clear();
|
2025-09-19 16:28:40 +02:00
|
|
|
$job = $this->entityManager->find(BulkInfoProviderImportJob::class, $jobId);
|
2025-09-09 20:30:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update the job's search results
|
2025-09-21 14:24:34 +02:00
|
|
|
$job->setSearchResults($allResults);
|
2025-09-09 20:30:27 +02:00
|
|
|
|
|
|
|
|
// Prefetch details if requested
|
|
|
|
|
if ($prefetchDetails) {
|
|
|
|
|
$this->bulkService->prefetchDetailsForResults($allResults);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->entityManager->flush();
|
|
|
|
|
|
|
|
|
|
return $this->json([
|
|
|
|
|
'success' => true,
|
2025-09-14 22:56:12 +02:00
|
|
|
'researched_count' => count($parts),
|
|
|
|
|
'message' => sprintf('Successfully researched %d parts', count($parts))
|
2025-09-09 20:30:27 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
return $this->createErrorResponse(
|
|
|
|
|
'Bulk research failed: ' . $e->getMessage(),
|
|
|
|
|
500,
|
|
|
|
|
[
|
|
|
|
|
'job_id' => $jobId,
|
2025-09-19 16:28:40 +02:00
|
|
|
'part_count' => count($parts),
|
2025-09-09 20:30:27 +02:00
|
|
|
'exception' => $e->getMessage()
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-31 23:41:16 +02:00
|
|
|
}
|