{@inheritdoc}

@param ProductTranslation $productTranslation
@param LifecycleEventArgs $event
487152
public function postPersist(ProductTranslation $productTranslation)
    {
        $this->eventDispatcher->dispatch(
            Events::PRODUCT_TRANSLATION_CREATED,
            new ProductTranslationEvent($productTranslation)
        );
    }
1136053
public function handle(ProductTypeRepository $productTypeRepository, Dispatcher $event)
    {
        $productType = ProductType::instantiate($this->description);

        $productTypeRepository->save($productType);

        $event->fire(new ProductTypeWasStored($productType));

        return $productType;
    }
536271
public function getTranslationData($entity, Property $property)
    {
        $metadata = $this->metadataCollection->getMetadata($entity);
        if($metadata === null) {
            return null;
        }

        $metaProperty = $metadata->getProperty($property->getName());

        if($metaProperty === null) {
            throw new TranslationException(
                sprintf('The property "%s" was used for translation, but no metadata was found. Maybe you forgot to define one for the entity "%s"',
                $property->getName(),
                get_class($entity)
            ));
        }

        $strategy = $this->strategyResolver->getStrategy($metaProperty->getStrategy());
        return $strategy->getTranslationData($entity, $metaProperty, $metadata);
    }
909668
public function copyAction(Product $product)
    {


        $productCopy = new Product();
        $copyName = $this->get('translator')->trans('Copy of') . " " . $product->getName();
        $productCopy->setName($copyName);

        $productCopy->setCostPrice($product->getCostPrice());
        $productCopy->setDescription($product->getDescription()??$copyName);
        $productCopy->setCode($product->getCode());
        $productCopy->setImage($product->getImage());
        $productCopy->setPlace($product->getPlace());
        $productCopy->setSalePrice($product->getSalePrice());
        $productCopy->setSupplier($product->getSupplier());
        $productCopy->setWarehouse($product->getWarehouse());

        // boleans
        $productCopy->setEnabled($product->getEnabled());
        $productCopy->setForSale($product->getForSale());
        $productCopy->setCompositionOnDemand($product->isCompositionOnDemand());
        $productCopy->setPack($product->isPack());
        $productCopy->setManualPackPricing($product->isManualPackPricing());
        $productCopy->setRawMaterial($product->getRawMaterial());

        $em = $this->getDoctrine()->getManager();
        $em->persist($productCopy);

        // relations

        /* @var ProductRawMaterial $rawMaterial */
        foreach ($product->getRawMaterials() as $rawMaterial) {
            $rawMaterialCopy = new ProductRawMaterial();
            $rawMaterialCopy->setRawMaterial($rawMaterial->getRawMaterial());
            $rawMaterialCopy->setProduct($productCopy);
            $rawMaterialCopy->setMeasureUnit($rawMaterial->getMeasureUnit());
            $rawMaterialCopy->setQuantity($rawMaterial->getQuantity());

            $em->persist($rawMaterialCopy);

            $productCopy->addRawMaterial($rawMaterialCopy);

        }

        /* @var ProductCategory $category */
        foreach ($product->getCategories() as $category) {
            $productCopy->addCategory($category);
        }

        /* @var ProductCustomField $customField */
        foreach ($product->getCustomFields() as $customField) {
            $customFieldCopy = new ProductCustomField();
            $customFieldCopy->setProduct($productCopy);
            $customFieldCopy->setSettingField($customField->getSettingField());
            $customFieldCopy->setValue($customField->getValue());

            $em->persist($customFieldCopy);

            $productCopy->addCustomField($customFieldCopy);
        }

        $em->flush();


        return $this->redirect($this->generateUrl('product_show', array("id" => $productCopy->getId())));
    }
774882
public function postUpdate(LifecycleEventArgs $args)
    {
        $entity = $args->getObject();
        if ($entity instanceof PackageVersionEntity) {
            $em = $args->getObjectManager();
            /* @var \Doctrine\ORM\EntityManager $em */
            $unitOfWork = $em->getUnitOfWork();
            $changeSet = $unitOfWork->getEntityChangeSet($entity);
            if (in_array('version', $changeSet)) {
                $this->refreshPackageLatestVersion($em, $entity->getPackage());
            }
        }
    }
766035
public function toggleVisibility(ProductToggleVisibilityEvent $event)
    {
        $product = $event->getProduct();

        $product
            ->setDispatcher($this->eventDispatcher)
            ->setVisible($product->getVisible() ? false : true)
            ->save()
            ;

        $event->setProduct($product);
    }
115892
public function update(ProductSaleElementUpdateEvent $event)
    {
        $salesElement = ProductSaleElementsQuery::create()->findPk($event->getProductSaleElementId());

        $con = Propel::getWriteConnection(ProductSaleElementsTableMap::DATABASE_NAME);

        $con->beginTransaction();

        try {
            // Update the product's tax rule
            $event->getProduct()->setTaxRuleId($event->getTaxRuleId())->save($con);

            // If product sale element is not defined, create it.
            if ($salesElement == null) {
                $salesElement = new ProductSaleElements();

                $salesElement->setProduct($event->getProduct());
            }

            $defaultStatus = $event->getIsDefault();

            // If this PSE will become the default one, be sure to have *only one* default for this product
            if ($defaultStatus) {
                ProductSaleElementsQuery::create()
                    ->filterByProduct($event->getProduct())
                    ->filterByIsDefault(true)
                    ->filterById($event->getProductSaleElementId(), Criteria::NOT_EQUAL)
                    ->update(['IsDefault' => false], $con)
                ;
            } else {
                // We will not allow the default PSE to become non default if no other default PSE exists for this product.
                if ($salesElement->getIsDefault() && ProductSaleElementsQuery::create()
                        ->filterByProduct($event->getProduct())
                        ->filterByIsDefault(true)
                        ->filterById($salesElement->getId(), Criteria::NOT_EQUAL)
                        ->count() === 0) {
                    // Prevent setting the only default PSE to non-default
                    $defaultStatus = true;
                }
            }

            // Update sale element
            $salesElement
                ->setRef($event->getReference())
                ->setQuantity($event->getQuantity())
                ->setPromo($event->getOnsale())
                ->setNewness($event->getIsnew())
                ->setWeight($event->getWeight())
                ->setIsDefault($defaultStatus)
                ->setEanCode($event->getEanCode())
                ->save()
            ;

            // Update/create price for current currency
            $productPrice = ProductPriceQuery::create()
                ->filterByCurrencyId($event->getCurrencyId())
                ->filterByProductSaleElementsId($salesElement->getId())
                ->findOne($con);

            // If price is not defined, create it.
            if ($productPrice == null) {
                $productPrice = new ProductPrice();

                $productPrice
                    ->setProductSaleElements($salesElement)
                    ->setCurrencyId($event->getCurrencyId())
                ;
            }

            // Check if we have to store the price
            $productPrice->setFromDefaultCurrency($event->getFromDefaultCurrency());

            if ($event->getFromDefaultCurrency() == 0) {
                // Store the price
                $productPrice
                    ->setPromoPrice($event->getSalePrice())
                    ->setPrice($event->getPrice())
                ;
            } else {
                // Do not store the price.
                $productPrice
                    ->setPromoPrice(0)
                    ->setPrice(0)
                ;
            }

            $productPrice->save($con);

            // Store all the stuff !
            $con->commit();
        } catch (\Exception $ex) {
            $con->rollback();

            throw $ex;
        }
    }
1037056
public function countErrorsForProcess(LifecycleEventArgs $event)
    {
        $entityManager = $event->getEntityManager();
        $repository    = $entityManager->getRepository(get_class($this));

        $repository->countByProcessIdAndStatus(
            $this->getProcess(),
            [self::STATUS_FAILED, self::STATUS_ABORTED],
            72,
            true
        );
    }
179620
public function postLoad(LifecycleEventArgs $args)
    {
        $entity = $args->getEntity();

        if (!$entity instanceof TranslatableInterface) {
            return;
        }

        $entity->initializeTranslations();
        $entity->setCurrentLocale($this->localeProvider->getCurrentLocale());
        $entity->setFallbackLocale($this->localeProvider->getFallbackLocale());
    }
436296
public function postLoad(LifecycleEventArgs $eventArgs)
    {
        $entity = $eventArgs->getEntity();

        if ($entity instanceof View) {
            $entity->setReferences([$entity->getCurrentLocale() => new ViewReference($entity->getId())]);
            $viewReferences = $this->viewReferenceRepository->getReferencesByParameters([
                'viewId'     => $entity->getId(),
                'templateId' => $entity->getId(),
            ], true, false, 'OR');
            foreach ($viewReferences as $viewReference) {
                if ($viewReference->getLocale() === $entity->getCurrentLocale()) {
                    if ($entity instanceof WebViewInterface && $viewReference instanceof ViewReference) {
                        $entity->setReference($viewReference, $viewReference->getLocale());
                        $entity->setUrl($viewReference->getUrl());
                    } elseif ($entity instanceof BusinessTemplate) {
                        $entity->setReferences([
                            $entity->getCurrentLocale() => $this->viewReferenceBuilder->buildViewReference($entity, $eventArgs->getEntityManager()),
                        ]);
                    }
                }
            }
        }
    }
49608
public function addTranslation(LayoutTranslation $translation)
    {
        $this->translations->add($translation);
        $translation->setLayout($this);
    }
94247
public function onMoveContent(EventInterface $event)
    {
        $translator = $this->getTranslator();
        $mapper = $this->getLayoutMapper();
        if (is_null($event->getParam('tid'))) {
            throw new InvalidArgumentException(
                $translator->translate(
                    "Unknown transaction identifier. Missing event param 'tid'."
                )
            );
        }
        if (is_null($event->getParam('content'))) {
            throw new InvalidArgumentException(sprintf(
                $translator->translate(
                    "Missing event param '%s'."
                ),
                'content'
            ));
        }
        $mapper->unregisterContent(
            $event->getParam('content'),
            false,
            $event->getParam('tid')
        );
    }
281081
private function updateViewReference(LifecycleEventArgs $eventArgs)
    {
        $entity = $eventArgs->getEntity();
        $view = null;
        $currentLocale = null;
        $translations = [];
        if ($entity instanceof WebViewInterface) {
            $translations = $entity->getTranslations();
            $view = $entity;
            $currentLocale = $view->getCurrentLocale();
        } elseif ($entity instanceof ViewTranslation && $entity->getTranslatable() instanceof WebViewInterface) {
            $translations = [$entity];
            $view = $entity->getTranslatable();
            $currentLocale = $view->getCurrentLocale();
        }

        // if a page is persisted we rebuild his viewRef
        foreach ($translations as $translation) {
            $view->setCurrentLocale($translation->getLocale());
            if ($view->getParent() && $view->getParent()->getCurrentLocale() != $translation->getLocale()) {
                $view->getParent()->translate($translation->getLocale());
                $view->getParent()->setCurrentLocale($translation->getLocale());
            }
            $event = new ViewReferenceEvent($view);
            $this->dispatcher->dispatch(ViewReferenceEvents::UPDATE_VIEW_REFERENCE, $event);
        }
        if ($view) {
            $view->setCurrentLocale($currentLocale);
        }
    }
817252
public function postRemoveEntity(LifecycleEventArgs $eventArgs)
    {
        $file = $eventArgs->getDocument();
        if (!$file instanceof Attachment) {
            return;
        }
        
        $fileId = new \MongoId($file->getId());
        $dm = $eventArgs->getDocumentManager();
        $dm->createQueryBuilder('Cv\Entity\Cv')
           ->update()->multiple(true)
           ->field('attachments')->equals($fileId)->pull($fileId)
           ->getQuery()->execute();
    }
989024
public function update(ProductUpdateEvent $event)
    {
        if (null !== $product = ProductQuery::create()->findPk($event->getProductId())) {
            $con = Propel::getWriteConnection(ProductTableMap::DATABASE_NAME);
            $con->beginTransaction();

            try {
                $prevRef = $product->getRef();

                $product
                    ->setDispatcher($this->eventDispatcher)
                    ->setRef($event->getRef())
                    ->setLocale($event->getLocale())
                    ->setTitle($event->getTitle())
                    ->setDescription($event->getDescription())
                    ->setChapo($event->getChapo())
                    ->setPostscriptum($event->getPostscriptum())
                    ->setVisible($event->getVisible() ? 1 : 0)
                    ->setVirtual($event->getVirtual() ? 1 : 0)
                    ->setBrandId($event->getBrandId() <= 0 ? null : $event->getBrandId())

                    ->save($con)
                ;

                // Update default PSE (if product has no attributes and the product's ref change)
                $defaultPseRefChange = $prevRef !== $product->getRef()
                    && 0 === $product->getDefaultSaleElements()->countAttributeCombinations();
                if ($defaultPseRefChange) {
                    $defaultPse = $product->getDefaultSaleElements();
                    $defaultPse->setRef($product->getRef())->save();
                }

                // Update default category (if required)
                $product->setDefaultCategory($event->getDefaultCategory());

                $event->setProduct($product);
                $con->commit();
            } catch (PropelException $e) {
                $con->rollBack();
                throw $e;
            }
        }
    }
217990
private function convertProduct(ProductInterface $productEntity, ProductInterface $publishedProductEntity)
    {
        // Move prices
        foreach ($publishedProductEntity->getPrices() as $data) {
            $this->em->remove($data);
        }
        foreach ($productEntity->getPrices() as $data) {
            $publishedProductEntity->addPrice($data);
            $data->setProduct($publishedProductEntity);
        }

        // Move productAttributes
        foreach ($publishedProductEntity->getProductAttributes() as $data) {
            $this->em->remove($data);
        }
        foreach ($productEntity->getProductAttributes() as $data) {
            $publishedProductEntity->addProductAttribute($data);
            $data->setProduct($publishedProductEntity);
        }

        // Move translations
        foreach ($publishedProductEntity->getTranslations() as $data) {
            $this->em->remove($data);
        }
        foreach ($productEntity->getTranslations() as $data) {
            $publishedProductEntity->addTranslation($data);
            $data->setProduct($publishedProductEntity);
        }

        // Move addons
        foreach ($publishedProductEntity->getAddons() as $data) {
            $publishedProductEntity->removeAddon($data);
        }
        foreach ($productEntity->getAddons() as $data) {
            $publishedProductEntity->addAddon($data);
            $data->setProduct($publishedProductEntity);
        }

        // Move sets
        foreach ($publishedProductEntity->getSets() as $data) {
            $publishedProductEntity->removeSet($data);
        }
        foreach ($productEntity->getSets() as $data) {
            $publishedProductEntity->addSet($data);
            $data->setProduct($publishedProductEntity);
        }

        // Move relation
        foreach ($publishedProductEntity->getRelations() as $data) {
            $publishedProductEntity->removeRelation($data);
        }
        foreach ($productEntity->getRelations() as $data) {
            $publishedProductEntity->addRelation($data);
            $data->setProduct($publishedProductEntity);
        }

        // Move upsell
        foreach ($publishedProductEntity->getUpsells() as $data) {
            $publishedProductEntity->removeUpsell($data);
        }
        foreach ($productEntity->getUpsells() as $data) {
            $publishedProductEntity->addUpsell($data);
            $data->setProduct($publishedProductEntity);
        }

        // Move crossells
        foreach ($publishedProductEntity->getCrosssells() as $data) {
            $publishedProductEntity->removeCrosssell($data);
        }
        foreach ($productEntity->getCrosssells() as $data) {
            $publishedProductEntity->addCrosssell($data);
            $data->setProduct($publishedProductEntity);
        }

        // Move categories
        foreach ($publishedProductEntity->getCategories() as $data) {
            $publishedProductEntity->removeCategory($data);
        }
        foreach ($productEntity->getCategories() as $data) {
            $publishedProductEntity->addCategory($data);
        }

        // Move media
        foreach ($publishedProductEntity->getMedia() as $data) {
            $publishedProductEntity->removeMedia($data);
        }
        foreach ($productEntity->getMedia() as $data) {
            $publishedProductEntity->addMedia($data);
        }

        $publishedProductEntity->setNumber($productEntity->getNumber());
        $publishedProductEntity->setGlobalTradeItemNumber($productEntity->getGlobalTradeItemNumber());
        $publishedProductEntity->setInternalItemNumber($productEntity->getInternalItemNumber());
        $publishedProductEntity->setManufacturer($productEntity->getManufacturer());
        $publishedProductEntity->setCost($productEntity->getCost());
        $publishedProductEntity->setPriceInfo($productEntity->getPriceInfo());
        $publishedProductEntity->setChanged($productEntity->getChanged());
        $publishedProductEntity->setManufacturerCountry($productEntity->getManufacturerCountry());
        $publishedProductEntity->setType($productEntity->getType());
        $publishedProductEntity->setStatus($productEntity->getStatus());
        $publishedProductEntity->setDeliveryStatus($productEntity->getDeliveryStatus());
        $publishedProductEntity->setDeliveryTime($productEntity->getDeliveryTime());
        $publishedProductEntity->setSupplier($productEntity->getSupplier());
        $publishedProductEntity->setParent($productEntity->getParent());
        $publishedProductEntity->setContentUnit($productEntity->getContentUnit());
        $publishedProductEntity->setOrderUnit($productEntity->getOrderUnit());
        $publishedProductEntity->setOrderContentRatio($productEntity->getOrderContentRatio());
        $publishedProductEntity->setMinimumOrderQuantity($productEntity->getMinimumOrderQuantity());
        $publishedProductEntity->setRecommendedOrderQuantity($productEntity->getRecommendedOrderQuantity());
        $publishedProductEntity->setChanger($productEntity->getChanger());
        $publishedProductEntity->setSearchTerms($productEntity->getSearchTerms());
        $publishedProductEntity->setTaxClass($productEntity->getTaxClass());

        // Move children
        foreach ($publishedProductEntity->getChildren() as $data) {
            $this->em->remove($data);
        }
        foreach ($productEntity->getChildren() as $data) {
            $data->setParent($publishedProductEntity);
        }

        $this->em->remove($productEntity);
        $this->em->flush();

        return $publishedProductEntity;
    }
494676
public function expandWithProductListCompanyRelationTransfer(ProductListTransfer $productListTransfer): ProductListTransfer
    {
        $productListCompanyRelationTransfer = new ProductListCompanyRelationTransfer();

        $productListCompanyRelationTransfer->setIdProductList($productListTransfer->getIdProductList());

        $productListCompanyRelationTransfer = $this->productListCompanyRelationReader
            ->getProductListCompanyRelation($productListCompanyRelationTransfer);

        $productListTransfer->setProductListCompanyRelation($productListCompanyRelationTransfer);

        return $productListTransfer;
    }