/**
  * {@inheritdoc}
  */
 public function save(\Magento\Catalog\Api\Data\CategoryInterface $category)
 {
     $existingData = $category->toFlatArray();
     /** 'available_sort_by' should be set separately because fields of array type are destroyed by toFlatArray() */
     $existingData['available_sort_by'] = $category->getAvailableSortBy();
     if ($category->getId()) {
         $existingCategory = $this->get($category->getId());
         if (isset($existingData['image']) && is_array($existingData['image'])) {
             $existingData['image_additional_data'] = $existingData['image'];
             unset($existingData['image']);
         }
         $existingData['id'] = $existingCategory->getId();
         $existingData['parent_id'] = $existingCategory->getParentId();
         $existingData['path'] = $existingCategory->getPath();
         $existingData['is_active'] = $existingCategory->getIsActive();
         $existingData['include_in_menu'] = isset($existingData['include_in_menu']) ? (bool) $existingData['include_in_menu'] : false;
         $category->setData($existingData);
     } else {
         $parentId = $category->getParentId() ?: $this->storeManager->getStore()->getRootCategoryId();
         $parentCategory = $this->get($parentId);
         $existingData['include_in_menu'] = isset($existingData['include_in_menu']) ? (bool) $existingData['include_in_menu'] : false;
         /** @var  $category Category */
         $category->setData($existingData);
         $category->setPath($parentCategory->getPath());
     }
     try {
         $this->validateCategory($category);
         $this->categoryResource->save($category);
     } catch (\Exception $e) {
         throw new CouldNotSaveException(__('Could not save category: %1', $e->getMessage()), $e);
     }
     unset($this->instances[$category->getId()]);
     return $category;
 }
 /**
  * {@inheritdoc}
  */
 public function save(\Magento\Catalog\Api\Data\CategoryInterface $category)
 {
     $storeId = (int) $this->storeManager->getStore()->getId();
     $existingData = $this->getExtensibleDataObjectConverter()->toNestedArray($category, [], 'Magento\\Catalog\\Api\\Data\\CategoryInterface');
     $existingData = array_diff_key($existingData, array_flip(['path', 'level', 'parent_id']));
     $existingData['store_id'] = $storeId;
     if ($category->getId()) {
         $metadata = $this->getMetadataPool()->getMetadata(CategoryInterface::class);
         $category = $this->get($category->getId(), $storeId);
         $existingData[$metadata->getLinkField()] = $category->getData($metadata->getLinkField());
         if (isset($existingData['image']) && is_array($existingData['image'])) {
             $existingData['image_additional_data'] = $existingData['image'];
             unset($existingData['image']);
         }
     } else {
         $parentId = $category->getParentId() ?: $this->storeManager->getStore()->getRootCategoryId();
         $parentCategory = $this->get($parentId, $storeId);
         $existingData['path'] = $parentCategory->getPath();
         $existingData['parent_id'] = $parentId;
     }
     $category->addData($existingData);
     try {
         $this->validateCategory($category);
         $this->categoryResource->save($category);
     } catch (\Exception $e) {
         throw new CouldNotSaveException(__('Could not save category: %1', $e->getMessage()), $e);
     }
     unset($this->instances[$category->getId()]);
     return $this->get($category->getId(), $storeId);
 }
 /**
  * {@inheritdoc}
  */
 public function save(\Magento\Framework\Model\AbstractModel $object)
 {
     $pluginInfo = $this->pluginList->getNext($this->subjectType, 'save');
     if (!$pluginInfo) {
         return parent::save($object);
     } else {
         return $this->___callPlugins('save', func_get_args(), $pluginInfo);
     }
 }