Delimiter //
/**
    In batchnum Int : num du lot
*/

Drop Procedure if exists LOADER_EDMINDEX_COPY//

Create Procedure LOADER_EDMINDEX_COPY (
    In batchnum int
)
Begin
--     Set @idEntities = GET_LOADER_INFO('ID_ENTITIES', batchnum);

	updateQuerys: Begin
        call LOADER_EDMINDEX_DOCUMENTS_UPDATE(batchnum);
        call LOADER_EDMINDEX_METADATA_UPDATE(batchnum);
    end ;

    insertQuerys: Begin
        call CASH_AUTOINCREMENT_VALUE('DOCUMENTS', @documentsLastid);
		call CASH_AUTOINCREMENT_VALUE('ATTACHED_DOCUMENTS', @attachedDocslastid);

        call LOADER_EDMINDEX_DOCUMENTS_INSERT(batchnum, @documentsLastid);

        call LOADER_EDMINDEX_METADATA_INSERT(batchnum, @documentsLastid);

        call LOADER_EDMINDEX_ATTACHED_DOCUMENTS_INSERT(batchnum, @documentsLastid, @attachedDocslastid);

        call LOADER_EDMINDEX_ACCOUNTS_HAS_DOCUMENTS_INSERT(batchnum, @attachedDocslastid); 
        call LOADER_EDMINDEX_ENTRIES_HAS_DOCUMENTS_INSERT(batchnum, @attachedDocslastid); 

		call LOADER_EDMINDEX_ENTRIES_KEYWORDS_INSERT(batchnum, @attachedDocslastid);

        call LOADER_EDMINDEX_SUMMARIES_INSERT(batchnum); 
-- 
    end ;

-- 	Set @max_batch = GET_LOADER_INFO('MAX_BATCH', batchnum);
-- 	if batchnum = @max_batch Then
	if batchnum > 0 Then
        set @entiies = GET_LOADER_INFO('ID_ENTITIES', batchnum);
        -- la table LOADER_EDMINDEX_ENTRIES_HAS_DOCUMENTS est purgé dans chaque lot
        -- il faut donc lancer la procédure de calcule dans chaque lot
		call BUSINESS_COMPUTE_ENTRIES_DATA_NB_DOCS(null, null, @entiies, 'LOADER_EDMINDEX_ENTRIES_HAS_DOCUMENTS');
	End if;
    
End //

Delimiter ;
