/**
  * Import applications.
  */
 protected function processApplications()
 {
     $allApplciations = $this->getFormattedApplications();
     if (empty($allApplciations)) {
         return;
     }
     $applicationsToCreate = array();
     foreach ($allApplciations as $host => $applications) {
         if (!$this->referencer->isProcessedHost($host)) {
             continue;
         }
         $hostid = $this->referencer->resolveHostOrTemplate($host);
         foreach ($applications as $application) {
             $application['hostid'] = $hostid;
             $appId = $this->referencer->resolveApplication($hostid, $application['name']);
             if (!$appId) {
                 $applicationsToCreate[] = $application;
             }
         }
     }
     // create the applications and create a hash hostid->name->applicationid
     if (!empty($applicationsToCreate)) {
         $newApplicationsIds = API::Application()->create($applicationsToCreate);
         foreach ($newApplicationsIds['applicationids'] as $anum => $applicationId) {
             $application = $applicationsToCreate[$anum];
             $this->referencer->addApplicationRef($application['hostid'], $application['name'], $applicationId);
         }
     }
     // refresh applications because templated ones can be inherited to host and used in items
     $this->referencer->refreshApplications();
 }
 /**
  * Deletes applications from DB that are missing in XML.
  *
  * @return null
  */
 protected function deleteMissingApplications()
 {
     if (!$this->options['applications']['deleteMissing']) {
         return;
     }
     $processedHostIds = $this->importedObjectContainer->getHostIds();
     $processedTemplateIds = $this->importedObjectContainer->getTemplateIds();
     $processedHostIds = array_merge($processedHostIds, $processedTemplateIds);
     // no hosts or templates have been processed
     if (!$processedHostIds) {
         return;
     }
     $applicationIdsXML = array();
     $allApplications = $this->getFormattedApplications();
     if ($allApplications) {
         foreach ($allApplications as $host => $applications) {
             $hostId = $this->referencer->resolveHostOrTemplate($host);
             foreach ($applications as $application) {
                 $applicationId = $this->referencer->resolveApplication($hostId, $application['name']);
                 if ($applicationId) {
                     $applicationIdsXML[$applicationId] = $applicationId;
                 }
             }
         }
     }
     $dbApplicationIds = API::Application()->get(array('output' => array('applicationid'), 'hostids' => $processedHostIds, 'preservekeys' => true, 'nopermissions' => true, 'inherited' => false));
     $applicationsToDelete = array_diff_key($dbApplicationIds, $applicationIdsXML);
     if ($applicationsToDelete) {
         API::Application()->delete(array_keys($applicationsToDelete));
     }
     // refresh applications because templated ones can be inherited to host and used in items
     $this->referencer->refreshApplications();
 }