/**
  * Function to create the items.
  *
  * @param int $collectionId
  * @param int $parentFolderid
  */
 private function _createFolderForItem($collectionId, $parentFolderid)
 {
     /** @var FolderModel $Folder */
     $Folder = MidasLoader::loadModel('Folder');
     /** @var ItemModel $Item */
     $Item = MidasLoader::loadModel('Item');
     /** @var ItemRevisionModel $ItemRevision */
     $ItemRevision = MidasLoader::loadModel('ItemRevision');
     /** @var GroupModel $Group */
     $Group = MidasLoader::loadModel('Group');
     /** @var AssetstoreModel $Assetstore */
     $Assetstore = MidasLoader::loadModel('Assetstore');
     /** @var FolderpolicygroupModel $Folderpolicygroup */
     $Folderpolicygroup = MidasLoader::loadModel('Folderpolicygroup');
     /** @var FolderpolicyuserModel $Folderpolicyuser */
     $Folderpolicyuser = MidasLoader::loadModel('Folderpolicyuser');
     /** @var ItempolicygroupModel $Itempolicygroup */
     $Itempolicygroup = MidasLoader::loadModel('Itempolicygroup');
     /** @var ItempolicyuserModel $Itempolicyuser */
     $Itempolicyuser = MidasLoader::loadModel('Itempolicyuser');
     /** @var UserModel $User */
     $User = MidasLoader::loadModel('User');
     $colquery = pg_query('SELECT i.item_id, mtitle.text_value AS title, mabstract.text_value AS abstract ' . 'FROM item AS i ' . 'LEFT JOIN metadatavalue AS mtitle ON (i.item_id = mtitle.item_id AND mtitle.metadata_field_id = 64) ' . 'LEFT JOIN metadatavalue AS mabstract ON (i.item_id = mabstract.item_id AND mabstract.metadata_field_id = 27) ' . 'WHERE i.owning_collection=' . $collectionId);
     while ($colquery_array = pg_fetch_array($colquery)) {
         $item_id = $colquery_array['item_id'];
         $title = $colquery_array['title'];
         // If title is empty we skip this item
         if (empty($title)) {
             continue;
         }
         $abstract = $colquery_array['abstract'];
         $folderDao = false;
         try {
             // Create the folder for the item
             $folderDao = $Folder->createFolder($title, $abstract, $parentFolderid);
             // Assign the policies to the folder as the same as the parent folder
             $folder = $Folder->load($parentFolderid);
             $policyGroup = $folder->getFolderpolicygroup();
             $policyUser = $folder->getFolderpolicyuser();
             foreach ($policyGroup as $policy) {
                 $group = $policy->getGroup();
                 $policyValue = $policy->getPolicy();
                 $Folderpolicygroup->createPolicy($group, $folderDao, $policyValue);
             }
             foreach ($policyUser as $policy) {
                 $user = $policy->getUser();
                 $policyValue = $policy->getPolicy();
                 $Folderpolicyuser->createPolicy($user, $folderDao, $policyValue);
             }
             // Add specific policies for users (not dealing with groups)
             $policyquery = pg_query('SELECT max(action_id) AS actionid, eperson.eperson_id, eperson.email
                             FROM resourcepolicy
                             LEFT JOIN eperson ON (eperson.eperson_id=resourcepolicy.eperson_id)
                              WHERE epersongroup_id IS NULL AND resource_type_id=' . MIDAS2_RESOURCE_ITEM . ' AND resource_id=' . $item_id . ' GROUP BY eperson.eperson_id, email');
             while ($policyquery_array = pg_fetch_array($policyquery)) {
                 $actionid = $policyquery_array['actionid'];
                 $email = $policyquery_array['email'];
                 if ($actionid > 1) {
                     $policyValue = MIDAS_POLICY_ADMIN;
                 } elseif ($actionid == 1) {
                     $policyValue = MIDAS_POLICY_WRITE;
                 } else {
                     $policyValue = MIDAS_POLICY_READ;
                 }
                 $userDao = $User->getByEmail($email);
                 $Folderpolicyuser->createPolicy($userDao, $folderDao, $policyValue);
             }
         } catch (Zend_Exception $e) {
             $this->getLogger()->info($e->getMessage());
             Zend_Debug::dump($e);
             // we continue
         }
         if ($folderDao) {
             // Create the item from the bitstreams
             $bitquery = pg_query('SELECT   b.bitstream_id, b.name, b.description, b.internal_id FROM bitstream AS b, item2bitstream AS i2b ' . 'WHERE i2b.bitstream_id = b.bitstream_id AND i2b.item_id=' . $item_id);
             while ($bitquery_array = pg_fetch_array($bitquery)) {
                 $filename = $bitquery_array['name'];
                 $itemdao = new ItemDao();
                 $itemdao->setName($filename);
                 // Get the number of downloads and set it
                 $itemstatsquery = pg_query('SELECT downloads from midas_resourcelog WHERE
                                   resource_id_type=' . MIDAS2_RESOURCE_ITEM . ' AND resource_id=' . $item_id);
                 $itemstats_array = pg_fetch_array($itemstatsquery);
                 if ($itemstats_array) {
                     $itemdao->setView($itemstats_array['downloads']);
                     $itemdao->setDownload($itemstats_array['downloads']);
                 }
                 $Item->save($itemdao);
                 // Just check if the group anonymous can access the item
                 $policyquery = pg_query('SELECT policy_id FROM resourcepolicy WHERE resource_type_id=' . MIDAS2_RESOURCE_ITEM . ' AND resource_id=' . $item_id . ' AND epersongroup_id=0');
                 if (pg_num_rows($policyquery) > 0) {
                     $anonymousGroup = $Group->load(MIDAS_GROUP_ANONYMOUS_KEY);
                     $Itempolicygroup->createPolicy($anonymousGroup, $itemdao, MIDAS_POLICY_READ);
                 }
                 // Add specific policies for users
                 $policyquery = pg_query('SELECT max(action_id) AS actionid, eperson.eperson_id, eperson.email
                               FROM resourcepolicy
                               LEFT JOIN eperson ON (eperson.eperson_id=resourcepolicy.eperson_id)
                                WHERE epersongroup_id IS NULL AND resource_type_id=' . MIDAS2_RESOURCE_ITEM . ' AND resource_id=' . $item_id . ' GROUP BY eperson.eperson_id, email');
                 while ($policyquery_array = pg_fetch_array($policyquery)) {
                     $actionid = $policyquery_array['actionid'];
                     $email = $policyquery_array['email'];
                     if ($actionid > 1) {
                         $policyValue = MIDAS_POLICY_ADMIN;
                     } elseif ($actionid == 1) {
                         $policyValue = MIDAS_POLICY_WRITE;
                     } else {
                         $policyValue = MIDAS_POLICY_READ;
                     }
                     $userDao = $User->getByEmail($email);
                     // Set the policy of the item
                     $Itempolicyuser->createPolicy($userDao, $itemdao, $policyValue);
                 }
                 // Add the item to the current directory
                 $Folder->addItem($folderDao, $itemdao);
                 // Create a revision for the item
                 $itemRevisionDao = new ItemRevisionDao();
                 $itemRevisionDao->setChanges('Initial revision');
                 $itemRevisionDao->setUser_id($this->userId);
                 $Item->addRevision($itemdao, $itemRevisionDao);
                 // Add the metadata
                 /** @var MetadataModel $MetadataModel */
                 $MetadataModel = MidasLoader::loadModel('Metadata');
                 //
                 $metadataquery = pg_query('SELECT metadata_field_id, text_value FROM metadatavalue WHERE item_id=' . $item_id);
                 while ($metadata_array = pg_fetch_array($metadataquery)) {
                     $text_value = $metadata_array['text_value'];
                     $metadata_field_id = $metadata_array['metadata_field_id'];
                     // Do not check 64 and 27 because they are stored as field and not metadata
                     // in MIDAS3
                     switch ($metadata_field_id) {
                         case 3:
                             $element = 'contributor';
                             $qualifier = 'author';
                             break;
                         case 11:
                             $element = 'date';
                             $qualifier = 'uploaded';
                             break;
                         case 14:
                             $element = 'date';
                             $qualifier = 'created';
                             break;
                         case 15:
                             $element = 'date';
                             $qualifier = 'issued';
                             break;
                         case 18:
                             $element = 'identifier';
                             $qualifier = 'citation';
                             break;
                         case 25:
                             $element = 'identifier';
                             $qualifier = 'uri';
                             break;
                         case 26:
                             $element = 'description';
                             $qualifier = 'general';
                             break;
                         case 28:
                             $element = 'description';
                             $qualifier = 'provenance';
                             break;
                         case 29:
                             $element = 'description';
                             $qualifier = 'sponsorship';
                             break;
                         case 39:
                             $element = 'description';
                             $qualifier = 'publisher';
                             break;
                         case 57:
                             $element = 'subject';
                             $qualifier = 'keyword';
                             break;
                         case 68:
                             $element = 'subject';
                             $qualifier = 'ocis';
                             break;
                         case 75:
                             $element = 'identifier';
                             $qualifier = 'pubmed';
                             break;
                         case 74:
                             $element = 'identifier';
                             $qualifier = 'doi';
                             break;
                         default:
                             $element = '';
                             $qualifier = '';
                     }
                     if ($element != '') {
                         $MetadataModel->addMetadataValue($itemRevisionDao, MIDAS_METADATA_TEXT, $element, $qualifier, $text_value);
                     }
                 }
                 // Add bitstreams to the revision
                 $bitstreamDao = new BitstreamDao();
                 $bitstreamDao->setName($filename);
                 // Compute the path from the internalid
                 // We are assuming only one assetstore
                 $internal_id = $bitquery_array['internal_id'];
                 $filepath = $this->midas2Assetstore . '/';
                 $filepath .= substr($internal_id, 0, 2) . '/';
                 $filepath .= substr($internal_id, 2, 2) . '/';
                 $filepath .= substr($internal_id, 4, 2) . '/';
                 $filepath .= $internal_id;
                 // Check that the file exists
                 if (file_exists($filepath)) {
                     // Upload the bitstream
                     $assetstoreDao = $Assetstore->load($this->assetstoreId);
                     $bitstreamDao->setPath($filepath);
                     $bitstreamDao->fillPropertiesFromPath();
                     $bitstreamDao->setAssetstoreId($this->assetstoreId);
                     $UploadComponent = new UploadComponent();
                     $UploadComponent->uploadBitstream($bitstreamDao, $assetstoreDao);
                     // Upload the bitstream if necessary (based on the assetstore type)
                     $ItemRevision->addBitstream($itemRevisionDao, $bitstreamDao);
                     unset($UploadComponent);
                 }
             }
         } else {
             echo 'Cannot create Folder for item: ' . $title . '<br>';
         }
     }
 }