/**
  * Set the item's properties
  */
 protected function setProperties()
 {
     // No properties
     if (is_null($this->ItemBase->ItemProperties)) {
         return;
     }
     $groups = array();
     /** @var PlentySoapObject_ItemProperty $ItemProperty */
     foreach ($this->ItemBase->ItemProperties->item as $ItemProperty) {
         if (is_null($ItemProperty->PropertyGroupID)) {
             PlentymarketsLogger::getInstance()->error('Sync:Item', 'The property »' . $ItemProperty->PropertyName . '« will not be assigned to the item »' . $this->data['name'] . '« with the id »' . $this->ItemBase->ItemID . '« (not assigned to any group)', 3410);
         } else {
             if (!$ItemProperty->ShowOnItemPageInWebshop) {
                 PlentymarketsLogger::getInstance()->error('Sync:Item', 'The property »' . $ItemProperty->PropertyName . '« will not be assigned to the item »' . $this->data['name'] . '« with the id »' . $this->ItemBase->ItemID . '« (may not be shown on item page)', 3420);
             } else {
                 $groups[$ItemProperty->PropertyGroupID][] = $ItemProperty;
             }
         }
     }
     if (empty($groups)) {
         PlentymarketsLogger::getInstance()->error('Sync:Item', 'No property group will be assigned to the item »' . $this->data['name'] . '« with the id »' . $this->ItemBase->ItemID . '«', 3430);
         return;
     }
     $groupId = -1;
     $numberOfValuesMax = 0;
     foreach ($groups as $groupIdx => $values) {
         if (count($values) > $numberOfValuesMax) {
             $groupId = $groupIdx;
             $numberOfValuesMax = count($values);
         }
     }
     // Check for filterId
     try {
         $filterGroupId = PlentymarketsMappingController::getPropertyGroupByPlentyID($groupId);
     } catch (PlentymarketsMappingExceptionNotExistant $E) {
         // Create the group
         $GroupAdded = Shopware\Components\Api\Manager::getResource('PropertyGroup')->create(array('name' => $ItemProperty->PropertyGroupFrontendName));
         // Get the new id
         $filterGroupId = $GroupAdded->getId();
         // Write the mapping and the log
         PlentymarketsMappingController::addPropertyGroup($filterGroupId, $groupId);
         PlentymarketsLogger::getInstance()->message('Sync:Item', 'The property group »' . $ItemProperty->PropertyGroupFrontendName . '« has been created');
     }
     // Load the group consistently
     /** @var Shopware\Models\Property\Group $Group */
     $Group = Shopware()->Models()->find('Shopware\\Models\\Property\\Group', $filterGroupId);
     if (!$Group) {
         throw new PlentymarketsImportItemException('The property group with the id »' . $filterGroupId . '« could not be loaded', 3441);
     }
     // Update the data of this item
     $this->data['filterGroupId'] = $filterGroupId;
     $this->data['propertyValues'] = array();
     // Properties
     /** @var PlentySoapObject_ItemProperty $ItemProperty */
     foreach ($groups[$groupId] as $ItemProperty) {
         // import only property values in German language
         if ($ItemProperty->PropertyValueLang != 'de') {
             continue;
         }
         // Mapping GroupId;ValueId -> ValueId
         try {
             $property = PlentymarketsMappingController::getPropertyByPlentyID($ItemProperty->PropertyID);
             $propertyParts = explode(';', $property);
             $optionId = $propertyParts[1];
         } catch (PlentymarketsMappingExceptionNotExistant $E) {
             // import the property
             $Option = new Shopware\Models\Property\Option();
             $Option->fromArray(array('name' => $ItemProperty->PropertyName, 'filterable' => 1));
             // Persist the objects
             Shopware()->Models()->persist($Group);
             Shopware()->Models()->persist($Option);
             // Add the option to the group
             $Group->addOption($Option);
             try {
                 // And flush everything
                 Shopware()->Models()->flush();
                 PlentymarketsLogger::getInstance()->message('Sync:Item', 'The property »' . $ItemProperty->PropertyName . '« has been created and added to the group »' . $Group->getName() . '«');
             } catch (Exception $E) {
                 throw new PlentymarketsImportItemException('The property »' . $ItemProperty->PropertyName . '« could not be created (' . $E->getMessage() . ')', 3440);
             }
             //
             $optionId = $Option->getId();
             // Save the mapping
             PlentymarketsMappingController::addProperty($filterGroupId . ';' . $optionId, $ItemProperty->PropertyID);
         }
         // Use SelectionName as PropertyValue for Merkmale-Typ "Auswahl"
         if (empty($ItemProperty->PropertyValue) && !empty($ItemProperty->PropertySelectionName)) {
             $ItemProperty->PropertyValue = $ItemProperty->PropertySelectionName;
         }
         // Shopware cannot handle empty values
         if (!empty($ItemProperty->PropertyValue)) {
             $this->data['propertyValues'][] = array('option' => array('id' => $optionId), 'value' => $ItemProperty->PropertyValue);
         }
     }
 }
 /**
  * Export the missing properties
  */
 protected function doExport()
 {
     $propertyGroupRepository = Shopware()->Models()->getRepository('Shopware\\Models\\Property\\Group');
     /** @var Shopware\Models\Property\Group $PropertyGroup */
     foreach ($propertyGroupRepository->findAll() as $PropertyGroup) {
         try {
             $groupIdAdded = PlentymarketsMappingController::getPropertyGroupByShopwareID($PropertyGroup->getId());
         } catch (PlentymarketsMappingExceptionNotExistant $E) {
             if (array_key_exists($PropertyGroup->getName(), $this->PLENTY_groupName2ID)) {
                 $groupIdAdded = $this->PLENTY_groupName2ID[$PropertyGroup->getName()];
             } else {
                 $Request_SetPropertyGroups = new PlentySoapRequest_SetPropertyGroups();
                 $Request_SetPropertyGroups->Properties = array();
                 $Object_SetPropertyGroup = new PlentySoapObject_SetPropertyGroup();
                 $Object_SetPropertyGroup->BackendName = $PropertyGroup->getName();
                 $Object_SetPropertyGroup->FrontendName = $PropertyGroup->getName();
                 $Object_SetPropertyGroup->Lang = 'de';
                 $Object_SetPropertyGroup->PropertyGroupID = null;
                 $Request_SetPropertyGroups->PropertyGroups[] = $Object_SetPropertyGroup;
                 $Response = PlentymarketsSoapClient::getInstance()->SetPropertyGroups($Request_SetPropertyGroups);
                 if (!$Response->Success) {
                     throw new PlentymarketsExportException('The item property group »' . $Object_SetPropertyGroup->BackendName . '« could not be exported', 2941);
                 }
                 $groupIdAdded = (int) $Response->ResponseMessages->item[0]->SuccessMessages->item[0]->Value;
             }
             PlentymarketsMappingController::addPropertyGroup($PropertyGroup->getId(), $groupIdAdded);
             // do export for property group translation
             $this->exportPropertyGroupTranslations($PropertyGroup->getId(), $groupIdAdded);
         }
         if (!isset($this->PLENTY_groupIDValueName2ID[$groupIdAdded])) {
             $this->PLENTY_groupIDValueName2ID[$groupIdAdded] = array();
         }
         /** @var Shopware\Models\Property\Option $Property */
         foreach ($PropertyGroup->getOptions() as $Property) {
             $Request_SetProperties = new PlentySoapRequest_SetProperties();
             $Request_SetProperties->Properties = array();
             $Object_SetProperty = new PlentySoapObject_SetProperty();
             $Object_SetProperty->PropertyGroupID = $groupIdAdded;
             $Object_SetProperty->PropertyID = null;
             $Object_SetProperty->Lang = 'de';
             $shopwareID = $PropertyGroup->getId() . ';' . $Property->getId();
             try {
                 PlentymarketsMappingController::getPropertyByShopwareID($shopwareID);
             } catch (PlentymarketsMappingExceptionNotExistant $E) {
                 if (array_key_exists($Property->getName(), $this->PLENTY_groupIDValueName2ID[$groupIdAdded])) {
                     $propertyIdAdded = $this->PLENTY_groupIDValueName2ID[$groupIdAdded][$Property->getName()];
                 } else {
                     $Object_SetProperty->PropertyFrontendName = $Property->getName();
                     $Object_SetProperty->PropertyBackendName = $Property->getName();
                     $Object_SetProperty->ShowOnItemPage = 1;
                     $Object_SetProperty->ShowInItemList = 1;
                     $Object_SetProperty->PropertyType = 'text';
                     $Request_SetProperties->Properties[] = $Object_SetProperty;
                     $Response_SetProperties = PlentymarketsSoapClient::getInstance()->SetProperties($Request_SetProperties);
                     if (!$Response_SetProperties->Success) {
                         throw new PlentymarketsExportException('The item property »' . $Object_SetProperty->PropertyBackendName . '« could not be created', 2942);
                     }
                     $propertyIdAdded = (int) $Response_SetProperties->ResponseMessages->item[0]->SuccessMessages->item[0]->Value;
                 }
                 PlentymarketsMappingController::addProperty($shopwareID, $propertyIdAdded);
                 $this->exportPropertyTranslations($groupIdAdded, $Property->getId(), $propertyIdAdded);
             }
         }
     }
 }