def create_fedora_metadata_object( self,
     FEDORA_ADMIN_URL, FEDORA_ADMIN_USERNAME, FEDORA_ADMIN_PASSWORD,
     COLLECTION_PID,
     item_data_dict, mods_schema_path, logger=None
     ):
     """ CONTROLLER
         Note: item_data_dict is a json entry from foundation/acc_num_to_data.py json """
     print 'starting try...'
     try:
         #Store logger if exists
         if logger:
             self.logger = logger
         #Store accession number
         self.accession_number = item_data_dict['calc_accession_id']  # for logging
         # print '- in fedora_metadata_only_builder.Task.create_fedora_metadata_object(); accession_number, %s' % self.accession_number
         #
         #Setup builders
         ir_builder = IRBuilder()
         mods_builder = ModsBuilder()
         rights_builder = RightsBuilder()
         #
         #Instantiate repo connection
         #Note: doesn't actually try to connect until it has to
         repo = Repository( root=FEDORA_ADMIN_URL, username=FEDORA_ADMIN_USERNAME, password=FEDORA_ADMIN_PASSWORD )
         print '- repo connection instantiated.'
         #
         #Instantiate new-obj
         #Purpose: this will be the object we'll build, then ingest
         new_obj = repo.get_object( type=CommonMetadataDO )
         print '- new_obj instantiated.'
         #
         #Instantiate collection object
         coll_obj = repo.get_object( pid=COLLECTION_PID )
         print '- coll_obj instantiated.'
         #
         #Get/reserve a pid
         #Note: after obtaining pid, new_obj.pid is a value, not a reference
         new_pid = new_obj.pid()
         new_obj.pid = new_pid
         print '- new_obj.pid obtained.'
         #
         #Assign collection object
         new_obj.owning_collection = coll_obj
         print '- collection object assigned'
         #
         #Build rights object
         #Note: builds via bdrxml
         rights_object = rights_builder.build_rights_object()
         print '- rights object built.'
         #
         #Assign rights object
         new_obj.rightsMD.content = rights_object
         print '- rights object assigned.'
         #
         #Build ir object
         ir_object = ir_builder.build_ir_object()
         print '- ir object built.'
         #
         #Assign ir object
         new_obj.irMD.content = ir_object
         print '- ir object assigned.'
         #
         #Build mods object
         #Example returned data: { 'data: mods_object, 'accession_number': accession_number }
         mods_object_dict = mods_builder.build_mods_object( item_data_dict, mods_schema_path, 'return_object' )  # or 'return_string'
         mods_object = mods_object_dict['data']
         print '- mods object built.'
         #
         #Assign mods object
         new_obj.mods.content = mods_object
         print '- mods object assigned.'
         #
         #Update default admin fields
         new_obj.label = mods_object.title
         new_obj.owner = 'Bell Gallery'
         print '- default admin fields updated.'
         #
         #Save to fedora
         self._save_to_fedora( new_obj )
         print '- saved to fedora.'
         #
         #Update logging
         print '- done.'
         self._update_task_tracker( message='new_pid:%s' % new_pid )
         #
         #Set next task
         task_manager.determine_next_task(
             unicode(sys._getframe().f_code.co_name),
             data={ 'item_data': item_data_dict, 'pid': new_pid },
             logger=logger
             )
         print '- next task set.'
     except Exception as e:
         error_message = 'in Task.create_fedora_metadata_object(); exception: %s' % unicode(repr(e))
         self.logger.info( error_message )
         raise Exception( error_message )
    def add_metadata_and_image(
        self,
        FEDORA_ADMIN_URL,
        FEDORA_ADMIN_USERNAME,
        FEDORA_ADMIN_PASSWORD,
        COLLECTION_PID,
        MASTER_IMAGES_DIR_PATH,
        MASTER_IMAGES_DIR_URL,
        JP2_IMAGES_DIR_PATH,
        JP2_IMAGES_DIR_URL,
        item_data_dict,
        mods_schema_path,
        logger=None,
    ):
        """ CONTROLLER
            Note: item_data_dict is a json entry from foundation/acc_num_to_data.py json """
        # print 'starting try...'
        # try:

        # Store logger if exists
        if logger:
            self.logger = logger
        # Store accession number
        self.accession_number = item_data_dict["calc_accession_id"]  # for logging
        #
        # Setup builders
        image_builder = ImageBuilder(logger)
        ir_builder = IRBuilder()
        mods_builder = ModsBuilder()
        rights_builder = RightsBuilder()
        #
        # Instantiate repo connection
        # Note: doesn't actually try to connect until it has to
        repo = Repository(root=FEDORA_ADMIN_URL, username=FEDORA_ADMIN_USERNAME, password=FEDORA_ADMIN_PASSWORD)
        print "- repo connection instantiated."
        #
        # Instantiate new-obj
        # Purpose: this will be the object we'll build, then ingest
        new_obj = repo.get_object(type=JP2Image)  # this image model _will_ handle either tiff or jpg
        print "- new_obj instantiated."
        #
        # Instantiate collection object
        coll_obj = repo.get_object(pid=COLLECTION_PID)
        print "- coll_obj instantiated."
        #
        # Get/reserve a pid
        # Note: after obtaining pid, new_obj.pid is a value, not a reference
        new_pid = new_obj.pid()
        new_obj.pid = new_pid
        print "- new_obj.pid obtained."
        #
        # Assign collection object
        new_obj.owning_collection = coll_obj
        print "- collection object assigned"
        #
        # Build rights object
        # Note: builds via bdrxml
        rights_object = rights_builder.build_rights_object()
        print "- rights object built."
        #
        # Assign rights object
        new_obj.rightsMD.content = rights_object
        print "- rights object assigned."
        #
        # Build ir object
        ir_object = ir_builder.build_ir_object()
        print "- ir object built."
        #
        # Assign ir object
        new_obj.irMD.content = ir_object
        print "- ir object assigned."
        #
        # Build mods object
        # Example returned data: { 'data: mods_object, 'accession_number': accession_number }
        mods_object_dict = mods_builder.build_mods_object(
            item_data_dict, mods_schema_path, "return_object"
        )  # or 'return_string'
        mods_object = mods_object_dict["data"]
        print "- mods object built."
        #
        # Assign mods object
        new_obj.mods.content = mods_object
        print "- mods object assigned."
        #
        # Update default admin fields
        new_obj.label = mods_object.title
        new_obj.owner = "Bell Gallery"
        print "- default admin fields updated."
        #
        #
        #
        # Update master datastream and rels-int
        master_filename = item_data_dict["object_image_scan_filename"]
        (file_url, dsID, mime_type) = image_builder.build_master_datastream_vars(
            filename=master_filename, image_dir_url=MASTER_IMAGES_DIR_URL
        )
        new_obj = image_builder.update_object_datastream(new_obj, dsID, file_url, mime_type)
        new_obj = image_builder.update_newobj_relsint(new_obj=new_obj, filename=master_filename, dsID=dsID)
        print "- master datastream and rels-int fields updated."
        #
        #
        #
        # Create jp2
        source_filepath = "%s/%s" % (MASTER_IMAGES_DIR_PATH, master_filename)
        self.logger.info(
            "in fedora_metadata_and_image_builder.add_metadata_and_image(); source_filepath, %s" % source_filepath
        )
        temp_jp2_filename = master_filename.replace(" ", "_")
        jp2_filename = temp_jp2_filename[0:-4] + ".jp2"
        destination_filepath = "%s/%s" % (JP2_IMAGES_DIR_PATH, jp2_filename)
        self.logger.info(
            "in fedora_metadata_and_image_builder.add_metadata_and_image(); destination_filepath, %s"
            % destination_filepath
        )
        image_builder.create_jp2(source_filepath, destination_filepath)
        print "- jp2 created."
        #
        #
        #
        # Update jp2 datastream and rels-int
        (file_url, dsID, mime_type) = image_builder.build_jp2_datastream_vars(
            filename=jp2_filename, image_dir_url=JP2_IMAGES_DIR_URL
        )
        new_obj = image_builder.update_object_datastream(new_obj, dsID, file_url, mime_type)
        new_obj = image_builder.update_newobj_relsint(new_obj=new_obj, filename=jp2_filename, dsID=dsID)
        print "- jp2 datastream and rels-int fields updated."
        #
        #
        #
        # Save to fedora
        self._save_to_fedora(new_obj)
        print "- saved to fedora."
        #
        # Update logging
        print "- done."
        self._update_task_tracker(message="new_pid:%s" % new_pid)
        #
        # Set next task
        task_manager.determine_next_task(
            unicode(sys._getframe().f_code.co_name), data={"item_data": item_data_dict, "pid": new_pid}, logger=logger
        )
        print "- next task set."
        return