/**
  * @Route("/collection/{id}/glossary_importation", name="glossary_importation")
  * @Method("post")
  * @Template("PlantnetDataBundle:Backend\Glossary:glossary_import_glossarydata.html.twig")
  */
 public function glossary_importationAction($id)
 {
     $user = $this->container->get('security.context')->getToken()->getUser();
     $dm = $this->get('doctrine.odm.mongodb.document_manager');
     $dm->getConfiguration()->setDefaultDB($this->getDataBase($user, $dm));
     $configuration = $dm->getConnection()->getConfiguration();
     $configuration->setLoggerCallable(null);
     $request = $this->container->get('request');
     set_time_limit(0);
     if ($request->isXmlHttpRequest()) {
         $collection = $dm->getRepository('PlantnetDataBundle:Collection')->findOneBy(array('id' => $id));
         if (!$collection) {
             throw $this->createNotFoundException('Unable to find Collection entity.');
         }
         $glossary = $collection->getGlossary();
         if (!$glossary) {
             throw $this->createNotFoundException('Unable to find Glossary entity.');
         }
         /*
          * Open the uploaded csv
          */
         $csvfile = __DIR__ . '/../../Resources/uploads/' . $collection->getAlias() . '/glossary.csv';
         $handle = fopen($csvfile, "r");
         /*
          * Get the glossary properties
          */
         $columns = fgetcsv($handle, 0, ";");
         $fields = array();
         $attributes = $glossary->getProperties();
         foreach ($attributes as $field) {
             $fields[] = $field;
         }
         /*
          * Initialise the metrics
          */
         //echo "Memory usage before: " . (memory_get_usage() / 1024) . " KB" . PHP_EOL;
         $s = microtime(true);
         $batchSize = 500;
         $rowCount = '';
         while (($data = fgetcsv($handle, 0, ';')) !== FALSE) {
             $num = count($data);
             $rowCount++;
             $definition = new Definition();
             $definition->setGlossary($glossary);
             $attributes = array();
             $def_error = false;
             for ($c = 0; $c < $num; $c++) {
                 $value = trim($this->data_encode($data[$c]));
                 $attributes[$fields[$c]->getId()] = $value;
                 switch ($fields[$c]->getType()) {
                     case 'keyword':
                         if (empty($value)) {
                             $def_error = true;
                         }
                         $definition->setName($value);
                         $definition->setDisplayedname($value);
                         break;
                     case 'definition':
                         $definition->setDefinition($value);
                         break;
                     case 'file':
                         $definition->setPath($value);
                         break;
                 }
             }
             if (!$def_error) {
                 $definition->setHaschildren(false);
                 $dm->persist($definition);
             }
             if ($rowCount % $batchSize == 0) {
                 $dm->flush();
                 $dm->clear();
                 $collection = $dm->getRepository('PlantnetDataBundle:Collection')->findOneBy(array('id' => $id));
                 $glossary = $collection->getGlossary();
             }
         }
         $dm->persist($glossary);
         $dm->flush();
         $dm->clear();
         //echo "Memory usage after: " . (memory_get_usage() / 1024) . " KB" . PHP_EOL;
         $e = microtime(true);
         echo ' Inserted ' . $rowCount . ' objects in ' . ($e - $s) . ' seconds' . PHP_EOL;
         fclose($handle);
         if (file_exists($csvfile)) {
             unlink($csvfile);
         }
         return $this->container->get('templating')->renderResponse('PlantnetDataBundle:Backend\\Glossary:glossary_import_glossarydata.html.twig', array('importCount' => 'Importation Success: ' . $rowCount . ' objects imported'));
     } else {
         return $this->glossary_import_dataAction($id);
     }
 }