public static function buildFieldFromRecord($rec)
 {
     $fields_cache =& self::$fields_cache;
     if (isset($fields_cache[$rec["id"]])) {
         return $fields_cache[$rec["id"]];
     }
     $field = new ilDataCollectionField();
     $field->setId($rec["id"]);
     $field->setTableId($rec["table_id"]);
     $field->setTitle($rec["title"]);
     $field->setDescription($rec["description"]);
     $field->setDatatypeId($rec["datatype_id"]);
     $field->setRequired($rec["required"]);
     $field->setUnique($rec["is_unique"]);
     $field->setLocked($rec["is_locked"]);
     $fields_cache[$rec["id"]] = $field;
     return $field;
 }
 /**
  * @param string          $a_entity
  * @param                 $a_types
  * @param array           $a_rec
  * @param ilImportMapping $a_mapping
  * @param string          $a_schema_version
  */
 public function importRecord($a_entity, $a_types, $a_rec, $a_mapping, $a_schema_version)
 {
     switch ($a_entity) {
         case 'dcl':
             // Calling new_obj->create() will create the main table for us
             $new_obj = new ilObjDataCollection();
             $new_obj->setTitle($a_rec['title']);
             $new_obj->setDescription($a_rec['description']);
             $new_obj->setApproval($a_rec['approval']);
             $new_obj->setPublicNotes($a_rec['public_notes']);
             $new_obj->setNotification($a_rec['notification']);
             $new_obj->setPublicNotes($a_rec['public_notes']);
             $new_obj->setOnline(false);
             $new_obj->setRating($a_rec['rating']);
             $new_obj->create();
             $this->import_dc_object = $new_obj;
             $a_mapping->addMapping('Modules/DataCollection', 'dcl', $a_rec['id'], $new_obj->getId());
             break;
         case 'il_dcl_table':
             // If maintable, update. Other tables must be created as well
             $table = $this->count_imported_tables > 0 ? new ilDataCollectionTable() : ilDataCollectionCache::getTableCache($this->import_dc_object->getMainTableId());
             $table->setTitle($a_rec['title']);
             $table->setObjId($this->import_dc_object->getId());
             $table->setDescription($a_rec['description']);
             $table->setAddPerm($a_rec['add_perm']);
             $table->setEditPerm($a_rec['edit_perm']);
             $table->setDeletePerm($a_rec['delete_perm']);
             $table->setEditByOwner($a_rec['edit_by_owner']);
             $table->setLimited($a_rec['limited']);
             $table->setLimitStart($a_rec['limit_start']);
             $table->setLimitEnd($a_rec['limit_end']);
             $table->setIsVisible($a_rec['is_visible']);
             $table->setExportEnabled($a_rec['export_enabled']);
             $table->setDefaultSortField($a_rec['default_sort_field_id']);
             $table->setDefaultSortFieldOrder($a_rec['default_sort_field_order']);
             $table->setPublicCommentsEnabled($a_rec['public_comments']);
             $table->setViewOwnRecordsPerm($a_rec['view_own_records_perm']);
             if ($this->count_imported_tables > 0) {
                 $table->doCreate(false);
                 // false => Do not create views! They are imported later
             } else {
                 $table->doUpdate();
                 $this->count_imported_tables++;
                 // Delete views from maintable because we want to import them from the xml data
                 $set = $this->db->query('SELECT * FROM il_dcl_view WHERE table_id = ' . $this->db->quote($table->getId(), 'integer'));
                 $view_ids = array();
                 while ($row = $this->db->fetchObject($set)) {
                     $view_ids[] = $row->id;
                 }
                 if (count($view_ids)) {
                     $this->db->manipulate("DELETE FROM il_dcl_viewdefinition WHERE view_id IN (" . implode(',', $view_ids) . ")");
                 }
                 $this->db->manipulate("DELETE FROM il_dcl_view WHERE table_id = " . $this->db->quote($table->getId(), 'integer'));
             }
             $a_mapping->addMapping('Modules/DataCollection', 'il_dcl_table', $a_rec['id'], $table->getId());
             break;
         case 'il_dcl_field':
             $new_table_id = $a_mapping->getMapping('Modules/DataCollection', 'il_dcl_table', $a_rec['table_id']);
             if ($new_table_id) {
                 $field = new ilDataCollectionField();
                 $field->setTableId($new_table_id);
                 $field->setDatatypeId($a_rec['datatype_id']);
                 $field->setTitle($a_rec['title']);
                 $field->setDescription($a_rec['description']);
                 $field->setRequired($a_rec['required']);
                 $field->setUnique($a_rec['is_unique']);
                 $field->setLocked($a_rec['is_locked']);
                 $field->doCreate();
                 $a_mapping->addMapping('Modules/DataCollection', 'il_dcl_field', $a_rec['id'], $field->getId());
                 // Check if this field was used as default order by, if so, update to new id
                 $table = ilDataCollectionCache::getTableCache($new_table_id);
                 if ($table && $table->getDefaultSortField() == $a_rec['id']) {
                     $table->setDefaultSortField($field->getId());
                     $table->doUpdate();
                 }
             }
             break;
         case 'il_dcl_record':
             $new_table_id = $a_mapping->getMapping('Modules/DataCollection', 'il_dcl_table', $a_rec['table_id']);
             if ($new_table_id) {
                 $record = new ilDataCollectionRecord();
                 $record->setTableId($new_table_id);
                 $datetime = new ilDateTime(time(), IL_CAL_UNIX);
                 $record->setCreateDate($datetime);
                 $record->setLastUpdate($datetime);
                 $record->setOwner($this->user->getId());
                 $record->setLastEditBy($this->user->getId());
                 $record->doCreate();
                 $a_mapping->addMapping('Modules/DataCollection', 'il_dcl_record', $a_rec['id'], $record->getId());
             }
             break;
         case 'il_dcl_view':
             $new_table_id = $a_mapping->getMapping('Modules/DataCollection', 'il_dcl_table', $a_rec['table_id']);
             if ($new_table_id) {
                 if ($a_rec['type'] == 0 && $a_rec['formtype'] == 0) {
                     // RecordViewViewDefinition: Create a new RecordViewViewdefinition. Note that the associated Page object is NOT created.
                     // Creation of the Page object is handled by the import of Services/COPage
                     $definition = new ilDataCollectionRecordViewViewdefinition();
                     $definition->setTableId($new_table_id);
                     $definition->create(true);
                     // DO not create DB entries for page object
                     // This mapping is needed for the import handled by Services/COPage
                     $a_mapping->addMapping('Services/COPage', 'pg', 'dclf:' . $a_rec['id'], 'dclf:' . $definition->getId());
                     $a_mapping->addMapping('Modules/DataCollection', 'il_dcl_view', $a_rec['id'], $definition->getId());
                 } else {
                     // Other definitions - grab next ID from il_dcl_view
                     $view_id = $this->db->nextId("il_dcl_view");
                     $sql = "INSERT INTO il_dcl_view (id, table_id, type, formtype) VALUES (" . $this->db->quote($view_id, "integer") . ", " . $this->db->quote($new_table_id, "integer") . ", " . $this->db->quote($a_rec['type'], "integer") . ", " . $this->db->quote($a_rec['formtype'], "integer") . ")";
                     $this->db->manipulate($sql);
                     $a_mapping->addMapping('Modules/DataCollection', 'il_dcl_view', $a_rec['id'], $view_id);
                 }
             }
             break;
         case 'il_dcl_viewdefinition':
             $new_view_id = $a_mapping->getMapping('Modules/DataCollection', 'il_dcl_view', $a_rec['view_id']);
             $new_field_id = $a_mapping->getMapping('Modules/DataCollection', 'il_dcl_field', $a_rec['field']);
             $field = $new_field_id ? $new_field_id : $a_rec['field'];
             if ($new_view_id) {
                 $sql = 'INSERT INTO il_dcl_viewdefinition (view_id, field, field_order, is_set) VALUES (' . $this->db->quote($new_view_id, 'integer') . ', ' . $this->db->quote($field, 'text') . ', ' . $this->db->quote($a_rec['field_order'], 'integer') . ', ' . $this->db->quote($a_rec['is_set'], 'integer') . ')';
                 $this->db->manipulate($sql);
             }
             break;
         case 'il_dcl_field_prop':
             $new_field_id = $a_mapping->getMapping('Modules/DataCollection', 'il_dcl_field', $a_rec['field_id']);
             if ($new_field_id) {
                 $prop = new ilDataCollectionFieldProp();
                 $prop->setFieldId($new_field_id);
                 $prop->setDatatypePropertyId($a_rec['datatype_prop_id']);
                 // For field references, we need to get the new field id of the referenced field
                 // If the field_id does not yet exist (e.g. referenced table not yet created), store temp info and fix before finishing import
                 $value = $a_rec['value'];
                 $refs = array(ilDataCollectionField::PROPERTYID_REFERENCE, ilDataCollectionField::PROPERTYID_N_REFERENCE);
                 $fix_refs = false;
                 if (in_array($prop->getDatatypePropertyId(), $refs)) {
                     $new_field_id = $a_mapping->getMapping('Modules/DataCollection', 'il_dcl_field', $a_rec['value']);
                     if ($new_field_id === false) {
                         $value = NULL;
                         $fix_refs = true;
                     }
                 }
                 $prop->setValue($value);
                 $prop->doCreate();
                 $a_mapping->addMapping('Modules/DataCollection', 'il_dcl_field_prop', $a_rec['id'], $prop->getId());
                 if ($fix_refs) {
                     $this->import_temp_refs_props[$prop->getId()] = $a_rec['value'];
                 }
             }
             break;
         case 'il_dcl_record_field':
             $record_id = $a_mapping->getMapping('Modules/DataCollection', 'il_dcl_record', $a_rec['record_id']);
             $field_id = $a_mapping->getMapping('Modules/DataCollection', 'il_dcl_field', $a_rec['field_id']);
             if ($record_id && $field_id) {
                 $record = ilDataCollectionCache::getRecordCache($record_id);
                 $field = ilDataCollectionCache::getFieldCache($field_id);
                 $record_field = new ilDataCollectionRecordField($record, $field);
                 // Created in constructor if not existing
                 $a_mapping->addMapping('Modules/DataCollection', 'il_dcl_record_field', $a_rec['id'], $record_field->getId());
                 $this->import_record_field_cache[$record_field->getId()] = $record_field;
             }
             break;
         case 'il_dcl_stloc1_value':
         case 'il_dcl_stloc2_value':
         case 'il_dcl_stloc3_value':
             $new_record_field_id = $a_mapping->getMapping('Modules/DataCollection', 'il_dcl_record_field', $a_rec['record_field_id']);
             if ($new_record_field_id) {
                 /** @var ilDataCollectionRecordField $record_field */
                 $record_field = $this->import_record_field_cache[$new_record_field_id];
                 if (is_object($record_field)) {
                     // Need to rewrite internal references and lookup new objects if MOB or File
                     // For some fieldtypes it's better to reset the value, e.g. ILIAS_REF
                     switch ($record_field->getField()->getDatatypeId()) {
                         case ilDataCollectionDatatype::INPUTFORMAT_MOB:
                             // Check if we got a mapping from old object
                             $new_mob_id = $a_mapping->getMapping('Services/MediaObjects', 'mob', $a_rec['value']);
                             $value = $new_mob_id ? (int) $new_mob_id : NULL;
                             $this->import_temp_new_mob_ids[] = $new_mob_id;
                             break;
                         case ilDataCollectionDatatype::INPUTFORMAT_FILE:
                             $new_file_id = $a_mapping->getMapping('Modules/File', 'file', $a_rec['value']);
                             $value = $new_file_id ? (int) $new_file_id : NULL;
                             break;
                         case ilDataCollectionDatatype::INPUTFORMAT_REFERENCE:
                         case ilDataCollectioNDatatype::INPUTFORMAT_REFERENCELIST:
                             // If we are referencing to a record from a table that is not yet created, return value is always false because the record does exist neither
                             // Solution: Temporary store all references and fix them before finishing the import.
                             $new_record_id = $a_mapping->getMapping('Modules/DataCollection', 'il_dcl_record', $a_rec['value']);
                             if ($new_record_id === false) {
                                 $this->import_temp_refs[$new_record_field_id] = $a_rec['value'];
                             }
                             $value = $new_record_id ? (int) $new_record_id : NULL;
                             break;
                         case ilDataCollectionDatatype::INPUTFORMAT_ILIAS_REF:
                             $value = NULL;
                             break;
                         default:
                             $value = $a_rec['value'];
                             if ($a_entity == 'il_dcl_stloc3_value' && (is_null($value) || empty($value))) {
                                 $value = '0000-00-00 00:00:00';
                             }
                     }
                     $record_field->setValue($value, true);
                     $record_field->doUpdate();
                 }
             }
             break;
     }
 }