def create_apps(book_id):
    """
    Celery queued task that creates tt_apps and tt1 tasks

    :arg book_id: Internet archive book id
    :returns: book indicating if the applications were created
    :rtype: bool

    """

    try:
        imgs = meb_util.get_tt_images(book_id)
        bookInfo = meb_util.get_archive_book_data(book_id)
        
        app_tt_select = Apptt_select(short_name=book_id + "_tt1", title=bookInfo['title'], book_info=bookInfo)
        app_tt_meta = Apptt_meta(short_name=book_id + "_tt2", title=bookInfo['title'])
        app_tt_struct = Apptt_struct(short_name=book_id + "_tt3", title=bookInfo['title'])
        app_tt_transcribe = Apptt_transcribe(short_name=book_id + "_tt4", title=bookInfo['title'])
        
        app_tt_select.add_app_infos(bookInfo)
        app_tt_meta.add_app_infos(bookInfo)
        app_tt_struct.add_app_infos(bookInfo)
        app_tt_transcribe.add_app_infos(bookInfo)
        
        if len(app_tt_select.get_tasks()) == 0:
            for img in imgs:
                task = app_tt_select.add_task(img)
                workflow_transaction_info = dict(task_id_1=task.id, task_id_2=None, task_id_3=None, task_id_4=None)
                data_mngr.record_workflow_transaction(workflow_transaction_info)

        return True

    except Exception as e:
        return False
    def add_next_task(self):

        # Get the list of task_runs
        task_runs = self.get_task_runs()
        task_run = task_runs[-1]  # Get only the last answer
        
        answer = task_run.info
        answer_info_json = json.loads(answer)
        
        if (answer != "0"): # page don't have table
            
            # record the page tables and its metadata on mbdb
            self.__record_page_table_and_metadata(answer_info_json)

            if (self.__checkIfNextTaskWasCreated()):
                logger.warn(Meb_exception_tt2(6, self.task.id))
                raise Meb_exception_tt2(6, self.task.id)
            
            tt3_app_short_name = self.app_short_name[:-1] + "3"
            tt3_app = ttapps.Apptt_struct(short_name=tt3_app_short_name)

            bookId = self.get_book_id()
            imgId = self.task.info["page"]
            rotate = answer_info_json[0]["text"]["girar"]
                        
            try:
                self.__downloadArchiveImages(bookId, imgId)
                self.__runLinesRecognition(bookId, imgId, rotate)
    
                # file with the lines recognized
                arch = open(
                    "%s/books/%s/metadados/saida/image%s_model%s.txt" % (
                    app.config['CV_MODULES'], bookId, imgId, "1"))
                
                # get the lines recognitions
                tables_coords = self.__splitFile(arch)
                t3_tasks = []
                
                for tableId in range(len(tables_coords)):
                    self.__runAreaSelection(bookId, imgId, tableId, rotate)

                    image_pieces = self.__getAreaSelection(bookId, imgId, tableId)
                    
                    table_subject_code = answer_info_json[tableId]['text']['assunto']

                    next_task_priority = priority_task_manager.get_priority(table_subject_code)
                    
                    if (len(image_pieces) > 0):
                        for image_piece in image_pieces:
                            info = dict(hasZoom=True, zoom=image_piece,
                                        coords=tables_coords[tableId],
                                        table_id=tableId,
                                        page=imgId, img_url=self.__url_table(
                                        bookId, imgId, tableId))
                            t3_tasks.append(tt3_app.add_task(info, priority=next_task_priority))  # add task to tt3_backend
                            
                    else:
                        info = dict(hasZoom=False,
                                    coords=tables_coords[tableId],
                                    table_id=tableId,
                                    page=imgId, img_url=self.__url_table(
                                    bookId, imgId, tableId))
                        t3_tasks.append(tt3_app.add_task(info, priority=next_task_priority))
                    
                    workflow_transaction = data_manager.get_workflow_transaction_by_task_id_2(self.task.id)
                    workflow_transaction_info = dict(task_id_1=workflow_transaction.task_id_1, task_id_2=workflow_transaction.task_id_2, task_id_3=workflow_transaction.task_id_3, task_id_4=workflow_transaction.task_id_4)
                    
                    for i in range(0, len(t3_tasks)):
                        workflow_transaction_info['task_id_3'] = t3_tasks[i].id
                        if i == 0:
                            data_manager.update_workflow_transaction_t3(workflow_transaction_info)
                        else:
                            data_manager.record_workflow_transaction(workflow_transaction_info)
                        
                return True
                
            except Meb_exception_tt2 as e:
                raise e
            
        return False
def add_workflow_transaction(info_workflow_transaction):
    data_mngr.record_workflow_transaction(info_workflow_transaction)