Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Model\AbortActionData;
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Util\Config;
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Util\EnvironmentVariables;
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Util\StepConfig;
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Util\Trait\HandlerProgressTrait;
use Pimcore\Bundle\StudioBackendBundle\Mercure\Service\PublishServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Mercure\Service\UserTopicServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Util\Trait\ElementProviderTrait;
use Pimcore\Model\Element\ElementDescriptor;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;

/**
Expand Down Expand Up @@ -57,7 +59,7 @@ public function __invoke(RewriteRefMessage $message): void
}

$jobRun = $this->getJobRun($message);
$validatedParameters = $this->validateFullParameters(
$validatedParameters = $this->validateJobParameters(
$message,
$jobRun,
$this->userResolver,
Expand All @@ -71,36 +73,70 @@ public function __invoke(RewriteRefMessage $message): void
$this->abort($validatedParameters);
}

$user = $validatedParameters->getUser();
$environmentVariables = $validatedParameters->getEnvironmentData();
$element = $this->getElementById(
$validatedParameters->getSubject(),
$validatedParameters->getUser(),
$this->elementService
$rewriteConfiguration = $environmentVariables[EnvironmentVariables::REWRITE_CONFIGURATION->value];
$rewriteParameters = $environmentVariables[EnvironmentVariables::REWRITE_PARAMETERS->value];

$elementIds = $this->extractConfigFieldFromJobStepConfig(
$message,
StepConfig::ELEMENTS_TO_REWRITE_REFERENCES->value
);
$elementType = $this->extractConfigFieldFromJobStepConfig(
$message,
StepConfig::ELEMENT_TYPE_TO_REWRITE_REFERENCES->value
);
$totalItems = count($elementIds);
$stepName = $this->getJobStep($message)->getName();

foreach ($elementIds as $elementId) {
$element = $this->getElementById(
new ElementDescriptor($elementType, $elementId),
$user,
$this->elementService,
);

try {
$this->elementReferenceService->rewriteElementReferences(
$validatedParameters->getUser(),
$element,
$environmentVariables[EnvironmentVariables::REWRITE_CONFIGURATION->value],
$environmentVariables[EnvironmentVariables::REWRITE_PARAMETERS->value],
try {
$this->elementReferenceService->rewriteElementReferences(
$user,
$element,
$rewriteConfiguration,
$rewriteParameters,
);
} catch (Exception $exception) {
$this->abort($this->getAbortData(
Config::ELEMENT_REWRITE_REFERENCES_FAILED_MESSAGE->value,
[
'type' => $element->getType(),
'id' => $element->getId(),
'message' => $exception->getMessage(),
],
));
}

$this->updateProgress(
$this->publishService,
$this->userTopicService,
$jobRun,
$stepName,
$totalItems,
100,
);
} catch (Exception $exception) {
$this->abort($this->getAbortData(
Config::ELEMENT_REWRITE_REFERENCES_FAILED_MESSAGE->value,
[
'type' => $element->getType(),
'id' => $element->getId(),
'message' => $exception->getMessage(),
],
));
}
}

$this->updateProgress(
$this->publishService,
$this->userTopicService,
$jobRun,
$this->getJobStep($message)->getName()
protected function configureStep(): void
{
$this->stepConfiguration->setRequired(StepConfig::ELEMENTS_TO_REWRITE_REFERENCES->value);
$this->stepConfiguration->setAllowedTypes(
StepConfig::ELEMENTS_TO_REWRITE_REFERENCES->value,
StepConfig::CONFIG_TYPE_ARRAY->value
);

$this->stepConfiguration->setRequired(StepConfig::ELEMENT_TYPE_TO_REWRITE_REFERENCES->value);
$this->stepConfiguration->setAllowedTypes(
StepConfig::ELEMENT_TYPE_TO_REWRITE_REFERENCES->value,
StepConfig::CONFIG_TYPE_STRING->value
);
}
}
36 changes: 20 additions & 16 deletions src/Element/Service/ExecutionEngine/ElementReferenceService.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Util\Config;
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Util\EnvironmentVariables;
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Util\Jobs;
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Util\StepConfig;
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Util\Trait\ChunkGeneratorTrait;
use Pimcore\Model\Asset;
use Pimcore\Model\DataObject\AbstractObject;
use Pimcore\Model\Document;
use Pimcore\Model\Element\ElementDescriptor;
use Pimcore\Model\Element\ElementInterface;
use Pimcore\Model\UserInterface;

Expand All @@ -38,6 +39,10 @@
*/
final readonly class ElementReferenceService implements ElementReferenceServiceInterface
{
use ChunkGeneratorTrait;

private const int REWRITE_REFERENCES_BATCH_SIZE = 500;

public function __construct(
private AssetServiceResolverInterface $assetServiceResolver,
private DataObjectServiceResolverInterface $dataObjectServiceResolver,
Expand Down Expand Up @@ -82,23 +87,22 @@ public function rewriteReferencesWithExecutionEngine(
array $ids,
string $type
): int {
$jobSteps = [];
foreach ($this->chunkGenerator($ids, self::REWRITE_REFERENCES_BATCH_SIZE) as $batch) {
$jobSteps[] = new JobStep(
JobSteps::ELEMENT_REWRITE_REFERENCE->value,
RewriteRefMessage::class,
'',
[
StepConfig::ELEMENTS_TO_REWRITE_REFERENCES->value => $batch,
StepConfig::ELEMENT_TYPE_TO_REWRITE_REFERENCES->value => $type,
],
);
}

$job = new Job(
name: Jobs::REWRITE_REFERENCES->value,
steps: [
new JobStep(
JobSteps::ELEMENT_REWRITE_REFERENCE->value,
RewriteRefMessage::class,
'',
[]
),
],
selectedElements: array_map(
static fn (int $id) => new ElementDescriptor(
type: $type,
id: $id
),
$ids
),
steps: $jobSteps,
environmentData: [
EnvironmentVariables::REWRITE_CONFIGURATION->value => [$type => $rewriteConfiguration],
EnvironmentVariables::REWRITE_PARAMETERS->value => [],
Expand Down
4 changes: 4 additions & 0 deletions src/ExecutionEngine/Util/StepConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ enum StepConfig: string
case CUSTOM_REPORT_TO_EXPORT = 'custom_report_to_export';
case ELEMENT_CLASS_ID = 'element_class_id';
case ELEMENTS_TO_EXPORT = 'elements_to_export';
case ELEMENTS_TO_REWRITE_REFERENCES = 'elements_to_rewrite_references';
case ELEMENTS_TO_TAG = 'elements_to_tag';
case ELEMENT_TYPE = 'element_type';
case ELEMENT_TYPE_TO_TAG = 'element_type_to_tag';
case ELEMENT_TYPE_TO_REWRITE_REFERENCES = 'element_type_to_rewrite_references';
case EXPORT_FORMAT = 'export_format';
case FOLDER_TO_EXPORT = 'folder_to_export';
case GRID_EXPORT_DATA = 'grid_export_data';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Model\AbortActionData;
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Util\Config;
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Util\EnvironmentVariables;
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Util\StepConfig;
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Util\Trait\HandlerProgressTrait;
use Pimcore\Bundle\StudioBackendBundle\Mercure\Service\PublishServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Mercure\Service\UserTopicServiceInterface;
Expand Down Expand Up @@ -56,7 +57,7 @@ public function __invoke(BatchTagOperationMessage $message): void
return;
}

$validatedParameters = $this->validateFullParameters(
$validatedParameters = $this->validateJobParameters(
$message,
$jobRun,
$this->userResolver,
Expand All @@ -71,49 +72,63 @@ public function __invoke(BatchTagOperationMessage $message): void
}

$user = $validatedParameters->getUser();
$element = $validatedParameters->getSubject();
$environmentVariables = $validatedParameters->getEnvironmentData();
$operation = $environmentVariables[EnvironmentVariables::BATCH_TAG_OPERATION->value];
$tagIds = $environmentVariables[EnvironmentVariables::TAG_IDS->value];
$elementIds = $this->extractConfigFieldFromJobStepConfig($message, StepConfig::ELEMENTS_TO_TAG->value);
$elementType = $this->extractConfigFieldFromJobStepConfig($message, StepConfig::ELEMENT_TYPE_TO_TAG->value);

try {
$parameters = new BatchCollectionParameters(
$element->getType(),
[$element->getId()],
$environmentVariables[EnvironmentVariables::TAG_IDS->value]
);
match ($operation) {
BatchOperations::ASSIGN->value => $this->tagService->batchAssignTagsToElements(
$parameters,
$user
),
BatchOperations::REPLACE->value => $this->tagService->batchReplaceTagsToElements(
$parameters,
$user
),
default => throw new Exception(
sprintf(
'Invalid batch operation %s',
$operation
)
),
};
} catch (Exception $exception) {
$this->abort($this->getAbortData(
Config::ELEMENT_TAG_OPERATION_FAILED_MESSAGE->value,
[
'id' => $element->getId(),
'type' => $element->getType(),
'operation' => $operation,
'message' => $exception->getMessage(),
],
));
$totalItems = count($elementIds);
$stepName = $this->getJobStep($message)->getName();

foreach ($elementIds as $elementId) {
$parameters = new BatchCollectionParameters($elementType, [$elementId], $tagIds);

try {
match ($operation) {
BatchOperations::ASSIGN->value => $this->tagService->batchAssignTagsToElements(
$parameters,
$user
),
BatchOperations::REPLACE->value => $this->tagService->batchReplaceTagsToElements(
$parameters,
$user
),
default => throw new Exception(
sprintf(
'Invalid batch operation %s',
$operation
)
),
};
} catch (Exception $exception) {
$this->abort($this->getAbortData(
Config::ELEMENT_TAG_OPERATION_FAILED_MESSAGE->value,
[
'id' => $elementId,
'type' => $elementType,
'operation' => $operation,
'message' => $exception->getMessage(),
],
));
}

$this->updateProgress($this->publishService, $this->userTopicService, $jobRun, $stepName, $totalItems, 100);
}
}

$this->updateProgress(
$this->publishService,
$this->userTopicService,
$jobRun,
$this->getJobStep($message)->getName()
protected function configureStep(): void
{
$this->stepConfiguration->setRequired(StepConfig::ELEMENTS_TO_TAG->value);
$this->stepConfiguration->setAllowedTypes(
StepConfig::ELEMENTS_TO_TAG->value,
StepConfig::CONFIG_TYPE_ARRAY->value
);

$this->stepConfiguration->setRequired(StepConfig::ELEMENT_TYPE_TO_TAG->value);
$this->stepConfiguration->setAllowedTypes(
StepConfig::ELEMENT_TYPE_TO_TAG->value,
StepConfig::CONFIG_TYPE_STRING->value
);
}
}
35 changes: 19 additions & 16 deletions src/Tag/Service/ExecutionEngine/BatchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Util\Config;
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Util\EnvironmentVariables;
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Util\Jobs;
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Util\StepConfig;
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Util\Trait\ChunkGeneratorTrait;
use Pimcore\Bundle\StudioBackendBundle\Security\Service\SecurityServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Tag\ExecutionEngine\AutomationAction\Messenger\Messages\BatchTagOperationMessage;
use Pimcore\Bundle\StudioBackendBundle\Tag\MappedParameter\BatchOperationParameters;
use Pimcore\Bundle\StudioBackendBundle\Tag\MappedParameter\ElementParameters;
use Pimcore\Bundle\StudioBackendBundle\Tag\Service\TagServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Tag\Util\Constant\BatchOperations;
use Pimcore\Bundle\StudioBackendBundle\Util\Trait\ElementProviderTrait;
use Pimcore\Model\Element\ElementDescriptor;
use function sprintf;

/**
Expand All @@ -41,6 +42,9 @@
final readonly class BatchService implements BatchServiceInterface
{
use ElementProviderTrait;
use ChunkGeneratorTrait;

private const int BATCH_TAG_SIZE = 500;

public function __construct(
private ElementSearchServiceInterface $elementSearchService,
Expand Down Expand Up @@ -69,23 +73,22 @@ public function createJobRunForBatchOperation(BatchOperationParameters $paramete
new ElementParameters($parameters->getType(), $parameters->getId())
);

$jobSteps = [];
foreach ($this->chunkGenerator($childrenIds, self::BATCH_TAG_SIZE) as $batch) {
$jobSteps[] = new JobStep(
$this->getJobStepName($parameters->getOperation()),
BatchTagOperationMessage::class,
'',
[
StepConfig::ELEMENTS_TO_TAG->value => $batch,
StepConfig::ELEMENT_TYPE_TO_TAG->value => $parameters->getType(),
],
);
}

$job = new Job(
name: $this->getJobName($parameters->getOperation()),
steps: [
new JobStep(
$this->getJobStepName($parameters->getOperation()),
BatchTagOperationMessage::class,
'',
[]
),
],
selectedElements: array_map(
static fn (int $id) => new ElementDescriptor(
$parameters->getType(),
$id
),
$childrenIds
),
steps: $jobSteps,
environmentData: [
EnvironmentVariables::BATCH_TAG_OPERATION->value => $parameters->getOperation(),
EnvironmentVariables::TAG_IDS->value => $tagIds,
Expand Down
Loading