# -*- coding: utf-8 -*-
import ch.systemsx.cisd.openbis.generic.server.jython.api.v1.DataType as DataType

print("Importing Microscopy Core Technology Master Data...")

tr = service.transaction()

# ==============================================================================
#
# FILE FORMATS
#
# ==============================================================================

# JPG file format
file_type_JPG = tr.getOrCreateNewFileFormatType('JPG')
file_type_JPG.setDescription(None)

# PNG file format
file_type_PNG = tr.getOrCreateNewFileFormatType('PNG')
file_type_PNG.setDescription(None)

# UNKNOWN file format
file_type_UNKNOWN = tr.getOrCreateNewFileFormatType('UNKNOWN')
file_type_UNKNOWN.setDescription('Unknown file format')

# ==============================================================================
#
# EXPERIMENT/COLLECTION TYPES
#
# ==============================================================================

# COLLECTION
exp_type_COLLECTION = tr.getOrCreateNewExperimentType('COLLECTION')
exp_type_COLLECTION.setDescription('Used as a folder for things.')

# ==============================================================================
#
# SAMPLE TYPES
#
# ==============================================================================

# MICROSCOPY_EXPERIMENT
samp_type_MICROSCOPY_EXPERIMENT = tr.getOrCreateNewSampleType('MICROSCOPY_EXPERIMENT')
samp_type_MICROSCOPY_EXPERIMENT.setDescription('Generic microscopy experiment.')
samp_type_MICROSCOPY_EXPERIMENT.setListable(True)
samp_type_MICROSCOPY_EXPERIMENT.setShowContainer(False)
samp_type_MICROSCOPY_EXPERIMENT.setShowParents(True)
samp_type_MICROSCOPY_EXPERIMENT.setSubcodeUnique(False)
samp_type_MICROSCOPY_EXPERIMENT.setAutoGeneratedCode(False)
samp_type_MICROSCOPY_EXPERIMENT.setShowParentMetadata(False)
samp_type_MICROSCOPY_EXPERIMENT.setGeneratedCodePrefix('MICROSCOPY_EXPERIMENT.')

# MICROSCOPY_SAMPLE_TYPE
samp_type_MICROSCOPY_SAMPLE_TYPE = tr.getOrCreateNewSampleType('MICROSCOPY_SAMPLE_TYPE')
samp_type_MICROSCOPY_SAMPLE_TYPE.setDescription('Sample type for microscopy data sets.')
samp_type_MICROSCOPY_SAMPLE_TYPE.setListable(True)
samp_type_MICROSCOPY_SAMPLE_TYPE.setShowContainer(False)
samp_type_MICROSCOPY_SAMPLE_TYPE.setShowParents(True)
samp_type_MICROSCOPY_SAMPLE_TYPE.setSubcodeUnique(False)
samp_type_MICROSCOPY_SAMPLE_TYPE.setAutoGeneratedCode(True)
samp_type_MICROSCOPY_SAMPLE_TYPE.setShowParentMetadata(False)
samp_type_MICROSCOPY_SAMPLE_TYPE.setGeneratedCodePrefix('M')

# ORGANIZATION_UNIT
samp_type_ORGANIZATION_UNIT = tr.getOrCreateNewSampleType('ORGANIZATION_UNIT')
samp_type_ORGANIZATION_UNIT.setDescription(
    'Used to create different organisations for samples since they can\'t belong to more than one experiment.')
samp_type_ORGANIZATION_UNIT.setListable(True)
samp_type_ORGANIZATION_UNIT.setShowContainer(False)
samp_type_ORGANIZATION_UNIT.setShowParents(True)
samp_type_ORGANIZATION_UNIT.setSubcodeUnique(False)
samp_type_ORGANIZATION_UNIT.setAutoGeneratedCode(True)
samp_type_ORGANIZATION_UNIT.setShowParentMetadata(False)
samp_type_ORGANIZATION_UNIT.setGeneratedCodePrefix('OU.')

# ==============================================================================
#
# DATASET TYPES
#
# ==============================================================================

# ATTACHMENT
data_set_type_ATTACHMENT = tr.getOrCreateNewDataSetType('ATTACHMENT')
data_set_type_ATTACHMENT.setDescription('Used to attach files to entities.')
data_set_type_ATTACHMENT.setMainDataSetPattern(None)
data_set_type_ATTACHMENT.setMainDataSetPath(None)
data_set_type_ATTACHMENT.setDeletionDisallowed(False)

# MICROSCOPY_ACCESSORY_FILE
data_set_type_MICROSCOPY_ACCESSORY_FILE = tr.getOrCreateNewDataSetType('MICROSCOPY_ACCESSORY_FILE')
data_set_type_MICROSCOPY_ACCESSORY_FILE.setDescription(
    'An accessory dataset file associated with a microscopy experiment.')
data_set_type_MICROSCOPY_ACCESSORY_FILE.setMainDataSetPattern(None)
data_set_type_MICROSCOPY_ACCESSORY_FILE.setMainDataSetPath(None)
data_set_type_MICROSCOPY_ACCESSORY_FILE.setDeletionDisallowed(False)

# MICROSCOPY_IMG
data_set_type_MICROSCOPY_IMG = tr.getOrCreateNewDataSetType('MICROSCOPY_IMG')
data_set_type_MICROSCOPY_IMG.setDescription('Generic Microscopy Image.')
data_set_type_MICROSCOPY_IMG.setMainDataSetPattern(None)
data_set_type_MICROSCOPY_IMG.setMainDataSetPath(None)
data_set_type_MICROSCOPY_IMG.setDeletionDisallowed(False)
if 'setDataSetKind' in dir(data_set_type_MICROSCOPY_IMG):
    data_set_type_MICROSCOPY_IMG.setDataSetKind('PHYSICAL')

# MICROSCOPY_IMG_CONTAINER
data_set_type_MICROSCOPY_IMG_CONTAINER = tr.getOrCreateNewDataSetType('MICROSCOPY_IMG_CONTAINER')
data_set_type_MICROSCOPY_IMG_CONTAINER.setDescription('Generic Microscopy Image Container.')
data_set_type_MICROSCOPY_IMG_CONTAINER.setMainDataSetPattern(None)
data_set_type_MICROSCOPY_IMG_CONTAINER.setMainDataSetPath(None)
data_set_type_MICROSCOPY_IMG_CONTAINER.setDeletionDisallowed(False)
if 'setDataSetKind' in dir(data_set_type_MICROSCOPY_IMG_CONTAINER):
    data_set_type_MICROSCOPY_IMG_CONTAINER.setDataSetKind('CONTAINER')

# MICROSCOPY_IMG_OVERVIEW
data_set_type_MICROSCOPY_IMG_OVERVIEW = tr.getOrCreateNewDataSetType('MICROSCOPY_IMG_OVERVIEW')
data_set_type_MICROSCOPY_IMG_OVERVIEW.setDescription('Overview Microscopy Images. Generated from raw images.')
data_set_type_MICROSCOPY_IMG_OVERVIEW.setMainDataSetPattern(None)
data_set_type_MICROSCOPY_IMG_OVERVIEW.setMainDataSetPath(None)
data_set_type_MICROSCOPY_IMG_OVERVIEW.setDeletionDisallowed(False)
if 'setDataSetKind' in dir(data_set_type_MICROSCOPY_IMG_OVERVIEW):
    data_set_type_MICROSCOPY_IMG_OVERVIEW.setDataSetKind('PHYSICAL')

# MICROSCOPY_IMG_THUMBNAIL
data_set_type_MICROSCOPY_IMG_THUMBNAIL = tr.getOrCreateNewDataSetType('MICROSCOPY_IMG_THUMBNAIL')
data_set_type_MICROSCOPY_IMG_THUMBNAIL.setDescription('Representative image for the whole dataset.')
data_set_type_MICROSCOPY_IMG_THUMBNAIL.setMainDataSetPattern(None)
data_set_type_MICROSCOPY_IMG_THUMBNAIL.setMainDataSetPath(None)
data_set_type_MICROSCOPY_IMG_THUMBNAIL.setDeletionDisallowed(False)
if 'setDataSetKind' in dir(data_set_type_MICROSCOPY_IMG_THUMBNAIL):
    data_set_type_MICROSCOPY_IMG_THUMBNAIL.setDataSetKind('PHYSICAL')

# ==============================================================================
#
# PROPERTY TYPES
#
# ==============================================================================

# ANNOTATIONS_STATE
prop_type_ANNOTATIONS_STATE = tr.getOrCreateNewPropertyType('ANNOTATIONS_STATE', DataType.XML)
prop_type_ANNOTATIONS_STATE.setLabel('Annotations State')
prop_type_ANNOTATIONS_STATE.setManagedInternally(False)
prop_type_ANNOTATIONS_STATE.setInternalNamespace(True)

# DEFAULT_OBJECT_TYPE
prop_type_DEFAULT_OBJECT_TYPE = tr.getOrCreateNewPropertyType('DEFAULT_OBJECT_TYPE', DataType.VARCHAR)
prop_type_DEFAULT_OBJECT_TYPE.setLabel('Default Object Type')
prop_type_DEFAULT_OBJECT_TYPE.setManagedInternally(False)
prop_type_DEFAULT_OBJECT_TYPE.setInternalNamespace(True)

# DESCRIPTION
prop_type_DESCRIPTION = tr.getOrCreateNewPropertyType('DESCRIPTION', DataType.VARCHAR)
prop_type_DESCRIPTION.setLabel('Description')
prop_type_DESCRIPTION.setManagedInternally(False)
prop_type_DESCRIPTION.setInternalNamespace(False)

# NAME
prop_type_NAME = tr.getOrCreateNewPropertyType('NAME', DataType.VARCHAR)
prop_type_NAME.setLabel('Name')
prop_type_NAME.setManagedInternally(False)
prop_type_NAME.setInternalNamespace(True)

# NAME
prop_type_NOTES = tr.getOrCreateNewPropertyType('NOTES', DataType.VARCHAR)
prop_type_NOTES.setLabel('Notes')
prop_type_NOTES.setManagedInternally(False)
prop_type_NOTES.setInternalNamespace(False)

# RESOLUTION
prop_type_RESOLUTION = tr.getOrCreateNewPropertyType('RESOLUTION', DataType.VARCHAR)
prop_type_RESOLUTION.setLabel('Resolution')
prop_type_RESOLUTION.setManagedInternally(False)
prop_type_RESOLUTION.setInternalNamespace(True)

# XMLCOMMENTS
prop_type_XMLCOMMENTS = tr.getOrCreateNewPropertyType('XMLCOMMENTS', DataType.XML)
prop_type_XMLCOMMENTS.setLabel('XML Comments')
prop_type_XMLCOMMENTS.setManagedInternally(False)
prop_type_XMLCOMMENTS.setInternalNamespace(True)

# MICROSCOPY_EXPERIMENT_ACQ_HARDWARE_FRIENDLY_NAME
prop_type_MICROSCOPY_EXPERIMENT_ACQ_HARDWARE_FRIENDLY_NAME = tr.getOrCreateNewPropertyType(
    'MICROSCOPY_EXPERIMENT_ACQ_HARDWARE_FRIENDLY_NAME', DataType.VARCHAR)
prop_type_MICROSCOPY_EXPERIMENT_ACQ_HARDWARE_FRIENDLY_NAME.setLabel('Acquisition station name')
prop_type_MICROSCOPY_EXPERIMENT_ACQ_HARDWARE_FRIENDLY_NAME.setManagedInternally(False)
prop_type_MICROSCOPY_EXPERIMENT_ACQ_HARDWARE_FRIENDLY_NAME.setInternalNamespace(False)

# MICROSCOPY_EXPERIMENT_DESCRIPTION
prop_type_MICROSCOPY_EXPERIMENT_DESCRIPTION = tr.getOrCreateNewPropertyType('MICROSCOPY_EXPERIMENT_DESCRIPTION',
                                                                            DataType.MULTILINE_VARCHAR)
prop_type_MICROSCOPY_EXPERIMENT_DESCRIPTION.setLabel('Description')
prop_type_MICROSCOPY_EXPERIMENT_DESCRIPTION.setManagedInternally(False)
prop_type_MICROSCOPY_EXPERIMENT_DESCRIPTION.setInternalNamespace(False)

# MICROSCOPY_EXPERIMENT_NAME
prop_type_MICROSCOPY_EXPERIMENT_NAME = tr.getOrCreateNewPropertyType('MICROSCOPY_EXPERIMENT_NAME', DataType.VARCHAR)
prop_type_MICROSCOPY_EXPERIMENT_NAME.setLabel('Experiment name')
prop_type_MICROSCOPY_EXPERIMENT_NAME.setManagedInternally(False)
prop_type_MICROSCOPY_EXPERIMENT_NAME.setInternalNamespace(False)

# MICROSCOPY_EXPERIMENT_VERSION
prop_type_MICROSCOPY_EXPERIMENT_VERSION = tr.getOrCreateNewPropertyType('MICROSCOPY_EXPERIMENT_VERSION',
                                                                        DataType.INTEGER)
prop_type_MICROSCOPY_EXPERIMENT_VERSION.setLabel('Version')
prop_type_MICROSCOPY_EXPERIMENT_VERSION.setManagedInternally(False)
prop_type_MICROSCOPY_EXPERIMENT_VERSION.setInternalNamespace(False)

# MICROSCOPY_IMG_CONTAINER_METADATA
prop_type_MICROSCOPY_IMG_CONTAINER_METADATA = tr.getOrCreateNewPropertyType('MICROSCOPY_IMG_CONTAINER_METADATA',
                                                                            DataType.XML)
prop_type_MICROSCOPY_IMG_CONTAINER_METADATA.setLabel('Series metadata')
prop_type_MICROSCOPY_IMG_CONTAINER_METADATA.setManagedInternally(False)
prop_type_MICROSCOPY_IMG_CONTAINER_METADATA.setInternalNamespace(False)

# MICROSCOPY_SAMPLE_DESCRIPTION
prop_type_MICROSCOPY_SAMPLE_DESCRIPTION = tr.getOrCreateNewPropertyType('MICROSCOPY_SAMPLE_DESCRIPTION',
                                                                        DataType.MULTILINE_VARCHAR)
prop_type_MICROSCOPY_SAMPLE_DESCRIPTION.setLabel('Description')
prop_type_MICROSCOPY_SAMPLE_DESCRIPTION.setManagedInternally(False)
prop_type_MICROSCOPY_SAMPLE_DESCRIPTION.setInternalNamespace(False)

# MICROSCOPY_SAMPLE_SIZE_IN_BYTES
prop_type_MICROSCOPY_SAMPLE_SIZE_IN_BYTES = tr.getOrCreateNewPropertyType('MICROSCOPY_SAMPLE_SIZE_IN_BYTES',
                                                                          DataType.INTEGER)
prop_type_MICROSCOPY_SAMPLE_SIZE_IN_BYTES.setLabel('Size')
prop_type_MICROSCOPY_SAMPLE_SIZE_IN_BYTES.setManagedInternally(False)
prop_type_MICROSCOPY_SAMPLE_SIZE_IN_BYTES.setInternalNamespace(False)

# ==============================================================================
#
# PROPERTY ASSIGNMENTS
#
# ==============================================================================

# DATA_SET_ATTACHMENT_NAME
assignment_DATA_SET_ATTACHMENT_NAME = tr.assignPropertyType(data_set_type_ATTACHMENT, prop_type_NAME)
assignment_DATA_SET_ATTACHMENT_NAME.setMandatory(False)
assignment_DATA_SET_ATTACHMENT_NAME.setSection(None)
assignment_DATA_SET_ATTACHMENT_NAME.setPositionInForms(1)
assignment_DATA_SET_ATTACHMENT_NAME.setShownEdit(False)

# DATA_SET_ATTACHMENT_DESCRIPTION
assignment_DATA_SET_ATTACHMENT_DESCRIPTION = tr.assignPropertyType(data_set_type_ATTACHMENT, prop_type_DESCRIPTION)
assignment_DATA_SET_ATTACHMENT_DESCRIPTION.setMandatory(False)
assignment_DATA_SET_ATTACHMENT_DESCRIPTION.setSection(None)
assignment_DATA_SET_ATTACHMENT_DESCRIPTION.setPositionInForms(2)
assignment_DATA_SET_ATTACHMENT_DESCRIPTION.setShownEdit(False)

# DATA_SET_ATTACHMENT_NOTES
assignment_DATA_SET_ATTACHMENT_NOTES = tr.assignPropertyType(data_set_type_ATTACHMENT, prop_type_NOTES)
assignment_DATA_SET_ATTACHMENT_NOTES.setMandatory(False)
assignment_DATA_SET_ATTACHMENT_NOTES.setSection(None)
assignment_DATA_SET_ATTACHMENT_NOTES.setPositionInForms(3)
assignment_DATA_SET_ATTACHMENT_NOTES.setShownEdit(False)

# DATA_SET_ATTACHMENT_XMLCOMMENTS
assignment_DATA_SET_ATTACHMENT_XMLCOMMENTS = tr.assignPropertyType(data_set_type_ATTACHMENT, prop_type_XMLCOMMENTS)
assignment_DATA_SET_ATTACHMENT_XMLCOMMENTS.setMandatory(False)
assignment_DATA_SET_ATTACHMENT_XMLCOMMENTS.setSection(None)
assignment_DATA_SET_ATTACHMENT_XMLCOMMENTS.setPositionInForms(4)
assignment_DATA_SET_ATTACHMENT_XMLCOMMENTS.setShownEdit(False)

# EXPERIMENT_COLLECTION_NAME
assignment_EXPERIMENT_COLLECTION_NAME = tr.assignPropertyType(exp_type_COLLECTION, prop_type_NAME)
assignment_EXPERIMENT_COLLECTION_NAME.setMandatory(False)
assignment_EXPERIMENT_COLLECTION_NAME.setSection(None)
assignment_EXPERIMENT_COLLECTION_NAME.setPositionInForms(1)
assignment_EXPERIMENT_COLLECTION_NAME.setShownEdit(False)

# EXPERIMENT_COLLECTION_DEFAULT_OBJECT_TYPE
assignment_EXPERIMENT_COLLECTION_DEFAULT_OBJECT_TYPE = tr.assignPropertyType(exp_type_COLLECTION,
                                                                             prop_type_DEFAULT_OBJECT_TYPE)
assignment_EXPERIMENT_COLLECTION_DEFAULT_OBJECT_TYPE.setMandatory(False)
assignment_EXPERIMENT_COLLECTION_DEFAULT_OBJECT_TYPE.setSection(None)
assignment_EXPERIMENT_COLLECTION_DEFAULT_OBJECT_TYPE.setPositionInForms(2)
assignment_EXPERIMENT_COLLECTION_DEFAULT_OBJECT_TYPE.setShownEdit(False)

# DATA_SET_MICROSCOPY_ACCESSORY_FILE_NAME
assignment_DATA_SET_MICROSCOPY_ACCESSORY_FILE_NAME = tr.assignPropertyType(data_set_type_MICROSCOPY_ACCESSORY_FILE,
    prop_type_NAME)
assignment_DATA_SET_MICROSCOPY_ACCESSORY_FILE_NAME.setMandatory(False)
assignment_DATA_SET_MICROSCOPY_ACCESSORY_FILE_NAME.setSection(None)
assignment_DATA_SET_MICROSCOPY_ACCESSORY_FILE_NAME.setPositionInForms(1)
assignment_DATA_SET_MICROSCOPY_ACCESSORY_FILE_NAME.setShownEdit(False)

# SAMPLE_MICROSCOPY_EXPERIMENT_NAME
assignment_SAMPLE_MICROSCOPY_EXPERIMENT_NAME = tr.assignPropertyType(samp_type_MICROSCOPY_EXPERIMENT, prop_type_NAME)
assignment_SAMPLE_MICROSCOPY_EXPERIMENT_NAME.setMandatory(False)
assignment_SAMPLE_MICROSCOPY_EXPERIMENT_NAME.setSection('General Info')
assignment_SAMPLE_MICROSCOPY_EXPERIMENT_NAME.setPositionInForms(1)
assignment_SAMPLE_MICROSCOPY_EXPERIMENT_NAME.setShownEdit(True)

# SAMPLE_MICROSCOPY_EXPERIMENT_ANNOTATIONS_STATE
assignment_SAMPLE_MICROSCOPY_EXPERIMENT_ANNOTATIONS_STATE = tr.assignPropertyType(samp_type_MICROSCOPY_EXPERIMENT,
                                                                                  prop_type_ANNOTATIONS_STATE)
assignment_SAMPLE_MICROSCOPY_EXPERIMENT_ANNOTATIONS_STATE.setMandatory(False)
assignment_SAMPLE_MICROSCOPY_EXPERIMENT_ANNOTATIONS_STATE.setSection('General Info')
assignment_SAMPLE_MICROSCOPY_EXPERIMENT_ANNOTATIONS_STATE.setPositionInForms(2)
assignment_SAMPLE_MICROSCOPY_EXPERIMENT_ANNOTATIONS_STATE.setShownEdit(False)

# SAMPLE_MICROSCOPY_EXPERIMENT_MICROSCOPY_EXPERIMENT_NAME
assignment_SAMPLE_MICROSCOPY_EXPERIMENT_MICROSCOPY_EXPERIMENT_NAME = tr.assignPropertyType(
    samp_type_MICROSCOPY_EXPERIMENT, prop_type_MICROSCOPY_EXPERIMENT_NAME)
assignment_SAMPLE_MICROSCOPY_EXPERIMENT_MICROSCOPY_EXPERIMENT_NAME.setMandatory(False)
assignment_SAMPLE_MICROSCOPY_EXPERIMENT_MICROSCOPY_EXPERIMENT_NAME.setSection(None)
assignment_SAMPLE_MICROSCOPY_EXPERIMENT_MICROSCOPY_EXPERIMENT_NAME.setPositionInForms(3)
assignment_SAMPLE_MICROSCOPY_EXPERIMENT_MICROSCOPY_EXPERIMENT_NAME.setShownEdit(True)

# SAMPLE_MICROSCOPY_EXPERIMENT_MICROSCOPY_EXPERIMENT_DESCRIPTION
assignment_SAMPLE_MICROSCOPY_EXPERIMENT_MICROSCOPY_EXPERIMENT_DESCRIPTION = tr.assignPropertyType(
    samp_type_MICROSCOPY_EXPERIMENT, prop_type_MICROSCOPY_EXPERIMENT_DESCRIPTION)
assignment_SAMPLE_MICROSCOPY_EXPERIMENT_MICROSCOPY_EXPERIMENT_DESCRIPTION.setMandatory(False)
assignment_SAMPLE_MICROSCOPY_EXPERIMENT_MICROSCOPY_EXPERIMENT_DESCRIPTION.setSection(None)
assignment_SAMPLE_MICROSCOPY_EXPERIMENT_MICROSCOPY_EXPERIMENT_DESCRIPTION.setPositionInForms(4)
assignment_SAMPLE_MICROSCOPY_EXPERIMENT_MICROSCOPY_EXPERIMENT_DESCRIPTION.setShownEdit(True)

# SAMPLE_MICROSCOPY_EXPERIMENT_MICROSCOPY_EXPERIMENT_VERSION
assignment_SAMPLE_MICROSCOPY_EXPERIMENT_MICROSCOPY_EXPERIMENT_VERSION = tr.assignPropertyType(
    samp_type_MICROSCOPY_EXPERIMENT, prop_type_MICROSCOPY_EXPERIMENT_VERSION)
assignment_SAMPLE_MICROSCOPY_EXPERIMENT_MICROSCOPY_EXPERIMENT_VERSION.setMandatory(False)
assignment_SAMPLE_MICROSCOPY_EXPERIMENT_MICROSCOPY_EXPERIMENT_VERSION.setSection(None)
assignment_SAMPLE_MICROSCOPY_EXPERIMENT_MICROSCOPY_EXPERIMENT_VERSION.setPositionInForms(5)
assignment_SAMPLE_MICROSCOPY_EXPERIMENT_MICROSCOPY_EXPERIMENT_VERSION.setShownEdit(False)

# SAMPLE_MICROSCOPY_EXPERIMENT_MICROSCOPY_EXPERIMENT_ACQ_HARDWARE_FRIENDLY_NAME
assignment_SAMPLE_MICROSCOPY_EXPERIMENT_MICROSCOPY_EXPERIMENT_ACQ_HARDWARE_FRIENDLY_NAME = tr.assignPropertyType(
    samp_type_MICROSCOPY_EXPERIMENT, prop_type_MICROSCOPY_EXPERIMENT_ACQ_HARDWARE_FRIENDLY_NAME)
assignment_SAMPLE_MICROSCOPY_EXPERIMENT_MICROSCOPY_EXPERIMENT_ACQ_HARDWARE_FRIENDLY_NAME.setMandatory(False)
assignment_SAMPLE_MICROSCOPY_EXPERIMENT_MICROSCOPY_EXPERIMENT_ACQ_HARDWARE_FRIENDLY_NAME.setSection(None)
assignment_SAMPLE_MICROSCOPY_EXPERIMENT_MICROSCOPY_EXPERIMENT_ACQ_HARDWARE_FRIENDLY_NAME.setPositionInForms(6)
assignment_SAMPLE_MICROSCOPY_EXPERIMENT_MICROSCOPY_EXPERIMENT_ACQ_HARDWARE_FRIENDLY_NAME.setShownEdit(True)

# DATA_SET_MICROSCOPY_IMG_CONTAINER_MICROSCOPY_IMG_CONTAINER_METADATA
assignment_DATA_SET_MICROSCOPY_IMG_CONTAINER_MICROSCOPY_IMG_CONTAINER_METADATA = tr.assignPropertyType(
    data_set_type_MICROSCOPY_IMG_CONTAINER, prop_type_MICROSCOPY_IMG_CONTAINER_METADATA)
assignment_DATA_SET_MICROSCOPY_IMG_CONTAINER_MICROSCOPY_IMG_CONTAINER_METADATA.setMandatory(False)
assignment_DATA_SET_MICROSCOPY_IMG_CONTAINER_MICROSCOPY_IMG_CONTAINER_METADATA.setSection(None)
assignment_DATA_SET_MICROSCOPY_IMG_CONTAINER_MICROSCOPY_IMG_CONTAINER_METADATA.setPositionInForms(1)
assignment_DATA_SET_MICROSCOPY_IMG_CONTAINER_MICROSCOPY_IMG_CONTAINER_METADATA.setScriptName(
    'MICROSCOPY_SERIES_METADATA_EDITOR')
assignment_DATA_SET_MICROSCOPY_IMG_CONTAINER_MICROSCOPY_IMG_CONTAINER_METADATA.setDynamic(False)
assignment_DATA_SET_MICROSCOPY_IMG_CONTAINER_MICROSCOPY_IMG_CONTAINER_METADATA.setManaged(True)
assignment_DATA_SET_MICROSCOPY_IMG_CONTAINER_MICROSCOPY_IMG_CONTAINER_METADATA.setShownEdit(False)

# DATA_SET_MICROSCOPY_IMG_CONTAINER_NAME
assignment_DATA_SET_MICROSCOPY_IMG_CONTAINER_NAME = tr.assignPropertyType(data_set_type_MICROSCOPY_IMG_CONTAINER,
                                                                          prop_type_NAME)
assignment_DATA_SET_MICROSCOPY_IMG_CONTAINER_NAME.setMandatory(False)
assignment_DATA_SET_MICROSCOPY_IMG_CONTAINER_NAME.setSection(None)
assignment_DATA_SET_MICROSCOPY_IMG_CONTAINER_NAME.setPositionInForms(3)
assignment_DATA_SET_MICROSCOPY_IMG_CONTAINER_NAME.setShownEdit(False)

# DATA_SET_MICROSCOPY_IMG_OVERVIEW_RESOLUTION
assignment_DATA_SET_MICROSCOPY_IMG_OVERVIEW_RESOLUTION = tr.assignPropertyType(data_set_type_MICROSCOPY_IMG_OVERVIEW,
                                                                               prop_type_RESOLUTION)
assignment_DATA_SET_MICROSCOPY_IMG_OVERVIEW_RESOLUTION.setMandatory(False)
assignment_DATA_SET_MICROSCOPY_IMG_OVERVIEW_RESOLUTION.setSection(None)
assignment_DATA_SET_MICROSCOPY_IMG_OVERVIEW_RESOLUTION.setPositionInForms(2)
assignment_DATA_SET_MICROSCOPY_IMG_OVERVIEW_RESOLUTION.setShownEdit(False)

# SAMPLE_MICROSCOPY_SAMPLE_TYPE_NAME
assignment_SAMPLE_MICROSCOPY_SAMPLE_TYPE_NAME = tr.assignPropertyType(samp_type_MICROSCOPY_SAMPLE_TYPE, prop_type_NAME)
assignment_SAMPLE_MICROSCOPY_SAMPLE_TYPE_NAME.setMandatory(False)
assignment_SAMPLE_MICROSCOPY_SAMPLE_TYPE_NAME.setSection(None)
assignment_SAMPLE_MICROSCOPY_SAMPLE_TYPE_NAME.setPositionInForms(1)
assignment_SAMPLE_MICROSCOPY_SAMPLE_TYPE_NAME.setShownEdit(False)

# SAMPLE_MICROSCOPY_SAMPLE_TYPE_MICROSCOPY_SAMPLE_DESCRIPTION
assignment_SAMPLE_MICROSCOPY_SAMPLE_TYPE_MICROSCOPY_SAMPLE_DESCRIPTION = tr.assignPropertyType(
    samp_type_MICROSCOPY_SAMPLE_TYPE, prop_type_MICROSCOPY_SAMPLE_DESCRIPTION)
assignment_SAMPLE_MICROSCOPY_SAMPLE_TYPE_MICROSCOPY_SAMPLE_DESCRIPTION.setMandatory(False)
assignment_SAMPLE_MICROSCOPY_SAMPLE_TYPE_MICROSCOPY_SAMPLE_DESCRIPTION.setSection(None)
assignment_SAMPLE_MICROSCOPY_SAMPLE_TYPE_MICROSCOPY_SAMPLE_DESCRIPTION.setPositionInForms(2)
assignment_SAMPLE_MICROSCOPY_SAMPLE_TYPE_MICROSCOPY_SAMPLE_DESCRIPTION.setShownEdit(True)

# SAMPLE_MICROSCOPY_SAMPLE_TYPE_MICROSCOPY_SAMPLE_SIZE_IN_BYTES
assignment_SAMPLE_MICROSCOPY_SAMPLE_TYPE_MICROSCOPY_SAMPLE_SIZE_IN_BYTES = tr.assignPropertyType(
    samp_type_MICROSCOPY_SAMPLE_TYPE, prop_type_MICROSCOPY_SAMPLE_SIZE_IN_BYTES)
assignment_SAMPLE_MICROSCOPY_SAMPLE_TYPE_MICROSCOPY_SAMPLE_SIZE_IN_BYTES.setMandatory(False)
assignment_SAMPLE_MICROSCOPY_SAMPLE_TYPE_MICROSCOPY_SAMPLE_SIZE_IN_BYTES.setSection(None)
assignment_SAMPLE_MICROSCOPY_SAMPLE_TYPE_MICROSCOPY_SAMPLE_SIZE_IN_BYTES.setPositionInForms(3)
assignment_SAMPLE_MICROSCOPY_SAMPLE_TYPE_MICROSCOPY_SAMPLE_SIZE_IN_BYTES.setShownEdit(False)

# SAMPLE_ORGANIZATION_UNIT_NAME
assignment_SAMPLE_ORGANIZATION_UNIT_NAME = tr.assignPropertyType(samp_type_ORGANIZATION_UNIT, prop_type_NAME)
assignment_SAMPLE_ORGANIZATION_UNIT_NAME.setMandatory(False)
assignment_SAMPLE_ORGANIZATION_UNIT_NAME.setSection(None)
assignment_SAMPLE_ORGANIZATION_UNIT_NAME.setPositionInForms(1)
assignment_SAMPLE_ORGANIZATION_UNIT_NAME.setShownEdit(False)

# SAMPLE_ORGANIZATION_UNIT_DESCRIPTION
assignment_SAMPLE_ORGANIZATION_UNIT_DESCRIPTION = tr.assignPropertyType(samp_type_ORGANIZATION_UNIT,
                                                                        prop_type_DESCRIPTION)
assignment_SAMPLE_ORGANIZATION_UNIT_DESCRIPTION.setMandatory(False)
assignment_SAMPLE_ORGANIZATION_UNIT_DESCRIPTION.setSection(None)
assignment_SAMPLE_ORGANIZATION_UNIT_DESCRIPTION.setPositionInForms(2)
assignment_SAMPLE_ORGANIZATION_UNIT_DESCRIPTION.setShownEdit(False)

# SAMPLE_ORGANIZATION_UNIT_XMLCOMMENTS
assignment_SAMPLE_ORGANIZATION_UNIT_XMLCOMMENTS = tr.assignPropertyType(samp_type_ORGANIZATION_UNIT,
                                                                        prop_type_XMLCOMMENTS)
assignment_SAMPLE_ORGANIZATION_UNIT_XMLCOMMENTS.setMandatory(False)
assignment_SAMPLE_ORGANIZATION_UNIT_XMLCOMMENTS.setSection(None)
assignment_SAMPLE_ORGANIZATION_UNIT_XMLCOMMENTS.setPositionInForms(3)
assignment_SAMPLE_ORGANIZATION_UNIT_XMLCOMMENTS.setShownEdit(False)

# SAMPLE_ORGANIZATION_UNIT_ANNOTATIONS_STATE
assignment_SAMPLE_ORGANIZATION_UNIT_ANNOTATIONS_STATE = tr.assignPropertyType(samp_type_ORGANIZATION_UNIT,
                                                                              prop_type_ANNOTATIONS_STATE)
assignment_SAMPLE_ORGANIZATION_UNIT_ANNOTATIONS_STATE.setMandatory(False)
assignment_SAMPLE_ORGANIZATION_UNIT_ANNOTATIONS_STATE.setSection(None)
assignment_SAMPLE_ORGANIZATION_UNIT_ANNOTATIONS_STATE.setPositionInForms(4)
assignment_SAMPLE_ORGANIZATION_UNIT_ANNOTATIONS_STATE.setShownEdit(False)

# ==============================================================================
#
# SCRIPTS
#
# ==============================================================================

# MICROSCOPY_SERIES_METADATA_EDITOR
script_MICROSCOPY_SERIES_METADATA_EDITOR = tr.getOrCreateNewScript('MICROSCOPY_SERIES_METADATA_EDITOR')
script_MICROSCOPY_SERIES_METADATA_EDITOR.setName('MICROSCOPY_SERIES_METADATA_EDITOR')
script_MICROSCOPY_SERIES_METADATA_EDITOR.setDescription(
    'Plug-in for viewing and editing microscopy series metadata information.')
script_MICROSCOPY_SERIES_METADATA_EDITOR.setScript('''import xml.etree.ElementTree as ET

def configureUI():

    # Create a table builder
    tableBuilder = createTableBuilder()

    try:

        # Get the property value and create an XML tree
        root = ET.fromstring(property.getValue().encode('UTF-8'))

        # Extract and sort the metadata attributes
        keys = root.attrib.keys()
        keys.sort()

        # Create the header
        for key in keys: 
            tableBuilder.addHeader(key)

        # Fill in the values
        row = tableBuilder.addRow()
        for key in keys:
            row.setCell(key, root.attrib[key])

    except Exception:

        # Report an error
        tableBuilder.addHeader("Error")
        row = tableBuilder.addRow()
        row.setCell("Error", "Could not retrieve metadata information.")

    # Return the table
    property.setOwnTab(True)
    uiDesc = property.getUiDescription()
    uiDesc.useTableOutput(tableBuilder.getTableModel())
''')
script_MICROSCOPY_SERIES_METADATA_EDITOR.setEntityForScript('DATA_SET')
script_MICROSCOPY_SERIES_METADATA_EDITOR.setScriptType('MANAGED_PROPERTY')

print("Import of Microscopy Core Technology Master Data finished.")
