prompt --application/pages/page_00019
begin
--   Manifest
--     PAGE: 00019
--   Manifest End
wwv_flow_imp.component_begin (
 p_version_yyyy_mm_dd=>'2023.10.31'
,p_release=>'23.2.0'
,p_default_workspace_id=>16017191443360494
,p_default_application_id=>10001
,p_default_id_offset=>0
,p_default_owner=>'STUDENT'
);
wwv_flow_imp_page.create_page(
 p_id=>19
,p_name=>'Step 1 - Upload Image File'
,p_alias=>'STEP-1-UPLOAD-IMAGE-FILE'
,p_step_title=>'Step 1 - Upload Image File'
,p_autocomplete_on_off=>'OFF'
,p_page_template_options=>'#DEFAULT#'
,p_protection_level=>'C'
,p_page_component_map=>'16'
,p_last_updated_by=>'STUDENT_DEV'
,p_last_upd_yyyymmddhh24miss=>'20231220175842'
);
wwv_flow_imp_page.create_page_plug(
 p_id=>wwv_flow_imp.id(4622790727289198)
,p_plug_name=>'Step 1 - Upload Image File'
,p_region_template_options=>'#DEFAULT#:t-Wizard--hideStepsXSmall'
,p_plug_template=>wwv_flow_imp.id(16133604317514901)
,p_plug_display_sequence=>10
,p_list_id=>wwv_flow_imp.id(4621855985289182)
,p_plug_source_type=>'NATIVE_LIST'
,p_list_template_id=>wwv_flow_imp.id(16164979301515009)
);
wwv_flow_imp_page.create_page_plug(
 p_id=>wwv_flow_imp.id(4622822913289198)
,p_plug_name=>'Step 1 - Upload Image File'
,p_parent_plug_id=>wwv_flow_imp.id(4622790727289198)
,p_region_template_options=>'#DEFAULT#'
,p_plug_template=>wwv_flow_imp.id(16062384265514656)
,p_plug_display_sequence=>10
,p_plug_display_point=>'SUB_REGIONS'
,p_attribute_01=>'N'
,p_attribute_02=>'HTML'
,p_attribute_03=>'Y'
);
wwv_flow_imp_page.create_page_button(
 p_id=>wwv_flow_imp.id(4624132207289203)
,p_button_sequence=>10
,p_button_plug_id=>wwv_flow_imp.id(4622790727289198)
,p_button_name=>'CANCEL'
,p_button_action=>'REDIRECT_PAGE'
,p_button_template_options=>'#DEFAULT#'
,p_button_template_id=>wwv_flow_imp.id(16189347253515098)
,p_button_image_alt=>'Cancel'
,p_button_position=>'CLOSE'
,p_button_redirect_url=>'f?p=&APP_ID.:7:&APP_SESSION.::&DEBUG.:::'
);
wwv_flow_imp_page.create_page_button(
 p_id=>wwv_flow_imp.id(4624485300289203)
,p_button_sequence=>30
,p_button_plug_id=>wwv_flow_imp.id(4622790727289198)
,p_button_name=>'NEXT'
,p_button_action=>'SUBMIT'
,p_button_template_options=>'t-Button--iconRight'
,p_button_template_id=>wwv_flow_imp.id(16189438884515099)
,p_button_is_hot=>'Y'
,p_button_image_alt=>'Next'
,p_button_position=>'NEXT'
,p_icon_css_classes=>'fa-chevron-right'
);
wwv_flow_imp_page.create_page_branch(
 p_id=>wwv_flow_imp.id(4625118890289206)
,p_branch_name=>'Go To Page 20'
,p_branch_action=>'f?p=&APP_ID.:20:&SESSION.::&DEBUG.::P20_FILE_PICKER:&P19_FILE_PICKER.&success_msg=#SUCCESS_MSG#'
,p_branch_point=>'AFTER_PROCESSING'
,p_branch_type=>'REDIRECT_URL'
,p_branch_when_button_id=>wwv_flow_imp.id(4624485300289203)
,p_branch_sequence=>20
);
wwv_flow_imp_page.create_page_item(
 p_id=>wwv_flow_imp.id(4615308275271402)
,p_name=>'P19_FILE_PICKER'
,p_is_required=>true
,p_item_sequence=>10
,p_item_plug_id=>wwv_flow_imp.id(4622822913289198)
,p_prompt=>'Pick File'
,p_display_as=>'NATIVE_FILE'
,p_cSize=>30
,p_field_template=>wwv_flow_imp.id(16186811375515083)
,p_item_template_options=>'#DEFAULT#'
,p_attribute_01=>'APEX_APPLICATION_TEMP_FILES'
,p_attribute_09=>'SESSION'
,p_attribute_10=>'N'
,p_attribute_12=>'DROPZONE_INLINE'
);
wwv_flow_imp_page.create_page_process(
 p_id=>wwv_flow_imp.id(4615570033271404)
,p_process_sequence=>10
,p_process_point=>'AFTER_SUBMIT'
,p_process_type=>'NATIVE_PLSQL'
,p_process_name=>'Read File'
,p_process_sql_clob=>wwv_flow_string.join(wwv_flow_t_varchar2(
'DECLARE',
'    ',
'    -- For debug',
'    CURSOR cur_get_files IS',
'        SELECT',
'            f.name,',
'            f.filename,',
'            f.mime_type,',
'            f.blob_content',
'        FROM',
'            apex_application_temp_files f;',
'',
'    lv_filename     apex_application_temp_files.filename%TYPE;',
'    lv_mime_type    apex_application_temp_files.mime_type%TYPE;',
'    lb_blob         apex_application_temp_files.blob_content%TYPE;',
'',
'    lv_db_dir       VARCHAR2(4000) := ''APEX_COS'';',
'    lv_os_dir       VARCHAR2(4000) := ''/home/oracle/python/apex/tmp'';',
'',
'    lv_retcode      VARCHAR2(1);',
'    lv_errmsg       VARCHAR2(32767);',
'',
'    -- Call File Upload Service',
'    lv_method       VARCHAR2(30)  := ''POST'';',
'    lc_req_body     CLOB;',
'    lv_url          VARCHAR(4000) := ''http://localhost:8000/get_detected_text/'';',
'    lc_res_body     CLOB;',
'  ',
'    lj_object           JSON_OBJECT_T;',
'    lv_result           VARCHAR2(4000);',
'    lv_result_message   VARCHAR2(4000);',
'    lj_detected_text    JSON_ARRAY_T;',
'    lv_detected_text    VARCHAR2(4000);',
'',
'    -- Debug',
'    lv_result_type          VARCHAR2(4000);',
'    lv_result_message_type  VARCHAR2(4000);',
'    lv_detected_text_type   VARCHAR2(4000);',
'',
'BEGIN',
'',
'    apex_debug.log_message(',
'        p_message   => ''----- Read File Start -----'',',
'        p_level     => apex_debug.c_log_level_info',
'    );',
'',
'    apex_debug.log_message(',
'        p_message   => ''session => '' || APEX_CUSTOM_AUTH.GET_SESSION_ID,',
'        p_level     => apex_debug.c_log_level_info',
'    );',
'',
'    FOR rec_get_files IN cur_get_files LOOP',
'',
'        apex_debug.log_message(',
'            p_message   => ''name => '' || rec_get_files.name,',
'            p_level     => apex_debug.c_log_level_info',
'        );',
'',
'        apex_debug.log_message(',
'            p_message   => ''filename => '' || rec_get_files.filename,',
'            p_level     => apex_debug.c_log_level_info',
'        );',
'',
'        apex_debug.log_message(',
'            p_message   => ''mime_type => '' || rec_get_files.mime_type,',
'            p_level     => apex_debug.c_log_level_info',
'        );',
'',
'    END LOOP;',
'',
'    apex_debug.log_message(',
'        p_message   => ''----- Read File End -----'',',
'        p_level     => apex_debug.c_log_level_info',
'    );',
'',
'    -- Purge temp table',
'    DELETE FROM stdt_tasks_temp;',
'',
'    -- Read file from file picker',
'    BEGIN',
'',
'        SELECT',
'            f.filename,',
'            f.mime_type,',
'            f.blob_content',
'        INTO',
'            lv_filename,',
'            lv_mime_type,',
'            lb_blob',
'        FROM',
'            apex_application_temp_files f',
'        WHERE',
'            f.name = :P19_FILE_PICKER;',
'',
'    EXCEPTION',
'        WHEN OTHERS THEN',
'            -- Exception handling',
'            APEX_ERROR.ADD_ERROR(',
'                p_message      => ''Error happened during pick upload!'' || SQLERRM,',
'                p_display_location => APEX_ERROR.C_INLINE_IN_NOTIFICATION',
'            );',
'    END;',
'',
'    -- Check MIME type',
'    IF (lv_mime_type NOT LIKE ''image%'') THEN',
'',
'        -- Exception handling',
'        APEX_ERROR.ADD_ERROR(',
'            p_message      => ''Uploaded file is NOT an image file!'',',
'            p_display_location => APEX_ERROR.C_INLINE_IN_NOTIFICATION',
'            -- p_display_location => APEX_ERROR.c_on_error_page',
'        );',
'',
'    END IF;',
'',
'    -- Write file to server',
'    proc_blob_to_file(',
'        ov_retcode      =>  lv_retcode,',
'        ov_errmsg       =>  lv_errmsg,',
'        ib_blob         =>  lb_blob,',
'        iv_dir          =>  lv_db_dir,',
'        iv_file_name    =>  lv_filename',
'    );',
'',
'    IF (lv_retcode = ''0'') THEN',
'        NULL;',
'    ELSE',
'        APEX_ERROR.ADD_ERROR(',
'            p_message      => ''Failed to write file ['' || lv_filename || ''] to folder ['' || lv_db_dir || '']'',',
'            p_display_location => APEX_ERROR.C_INLINE_IN_NOTIFICATION',
'        );',
'    END IF;',
'',
'    ',
'    -- Call OCR Service',
'    apex_web_service.g_request_headers(1).name := ''Content-Type'';',
'    apex_web_service.g_request_headers(1).Value := ''application/json; charset=utf-8'';',
' ',
'',
'    lc_req_body := ''{',
'                        "dir": "'' || lv_os_dir || ''",',
'                        "file_name": "'' || lv_filename || ''"',
'                    }'';',
'',
'    lc_res_body := APEX_WEB_SERVICE.make_rest_request(',
'        p_url         => lv_url,',
'        p_http_method => lv_method,',
'        p_body        => lc_req_body',
'    );',
'',
'    -- Parse Service Result',
'    lj_object := JSON_OBJECT_T.parse(lc_res_body);',
'    ',
'    ',
'    /*',
'    lj_object := JSON_OBJECT_T.parse(''',
'    {',
'        "result": 0,',
'        "result_message": "",',
'        "detected_text": [',
'            "Kitty and her friends are planning to visit a place in Garden city.",',
'            "Peter: Its my birthday on Saturday .Lets visit a place in Garden City,",',
'            "Kitty; Thats a good idea . Which place shall we visit?",',
'            "Alice: Lets go to Ocean Park in Spring Bay ,",',
'            "Kitty; All right .What time on Ssturday?",',
'            "Alice: How about ten oclock in the morning?",',
'            "Kitty: Yes, thats a good time . How are we going to get there?",',
'            "Peter: Lets go by bus,",',
'            "Joe : How much does it cost?",',
'            "Peter: Five yuan.",',
'            "Kitty: When are we going to come back?",',
'            "Alice: We are going to come back at six oclock",',
unistr('            "Kitty\548C\5979\7684\670B\53CB\4EEC\8BA1\5212\53BB\82B1\56ED\57CE\7684\4E00\4E2A\5730\65B9\73A9\3002",'),
unistr('            "Peter:\661F\671F\516D\662F\6211\7684\751F\65E5\3002 \8BA9\6211\4EEC\53C2\89C2\82B1\56ED\57CE\7684\4E00\4E2A\5730\65B9\3002",'),
unistr('            "Ki++y:\8FD9\662F\4E2A\597D\4E3B\610F\3002\6211\4EEC\5E94\8BE5\53BB\54EA\4E2A\5730\65B9\FF1F",'),
unistr('            "Alice:\6211\4EEC\53BB\65AF\666E\6797\6E7E\7684\6D77\6D0B\516C\56ED\5427\3002",'),
unistr('            "K++y:\597D\7684\3002\661F\671F\56DB\51E0\70B9\FF1F",'),
unistr('            "Alice:\4E0A\5348\5341\70B9\600E\4E48\6837\FF1F",'),
unistr('            "Kitty:\662F\7684\FF0C\90A3\662F\4E2A\597D\65F6\673A\3002\6211\4EEC\600E\4E48\53BB\90A3\91CC\FF1F",'),
unistr('            "Peter:\6211\4EEC\5750\516C\5171\6C7D\8F66\53BB\5427\3002",'),
unistr('            "Joe:\591A\5C11\94B1\FF1F",'),
unistr('            "Peter:\4E94\5143\94B1\3002",'),
unistr('            "Kit+y:\6211\4EEC\4EC0\4E48\65F6\5019\56DE\6765\FF1F",'),
unistr('            "Alice:\6211\4EEC\516D\70B9\949F\56DE\6765"'),
'        ]',
'    }'');',
'    */',
'    ',
'',
'    lv_result := lj_object.get_string(''result'');',
'    lv_result_message := lj_object.get_string(''result_message'');',
'    lv_result_message := lj_object.get_string(''result_message'');',
'',
'    lv_result_type          := lj_object.get_type(''result'');',
'    lv_result_message_type  := lj_object.get_type(''result_message'');',
'    lv_detected_text_type   := lj_object.get_type(''detected_text'');',
'    lj_detected_text        := lj_object.get_array(''detected_text'');',
'    ',
'    -- Debug Start',
'    apex_debug.log_message(',
'        p_message   => ''----- Read File Start -----'',',
'        p_level     => apex_debug.c_log_level_info',
'    );',
'',
'    apex_debug.log_message(',
'        p_message   => ''lc_res_body => '' || lc_res_body,',
'        p_level     => apex_debug.c_log_level_info',
'    );',
'',
'    apex_debug.log_message(',
'        p_message   => ''lv_result => '' || lv_result,',
'        p_level     => apex_debug.c_log_level_info',
'    );',
'',
'    apex_debug.log_message(',
'        p_message   => ''lv_result_message => '' || lv_result_message,',
'        p_level     => apex_debug.c_log_level_info',
'    );',
'',
'    apex_debug.log_message(',
'        p_message   => ''lv_result_type => '' || lv_result_type,',
'        p_level     => apex_debug.c_log_level_info',
'    );',
'',
'    apex_debug.log_message(',
'        p_message   => ''lv_result_message_type => '' || lv_result_message_type,',
'        p_level     => apex_debug.c_log_level_info',
'    );',
'',
'    apex_debug.log_message(',
'        p_message   => ''lv_detected_text_type => '' || lv_detected_text_type,',
'        p_level     => apex_debug.c_log_level_info',
'    );',
'',
'    FOR i IN 0 .. lj_detected_text.get_size - 1 LOOP',
'',
'        lv_detected_text := lj_detected_text.get_string(i);',
'',
'        INSERT INTO stdt_tasks_temp(',
'            "TASK_NAME",',
'        	"TASK_DESCRIPTION",',
'            "STUDENT_ID",',
'        	"TERM_ID",',
'        	"SUBJECT_ID",',
'        	"START_DATE",',
'        	"END_DATE",',
'            "SELECTED_FLAG"',
'        )',
'        VALUES (',
'            lv_detected_text,   -- TASK_NAME, ',
'        	NULL,               -- TASK_DESCRIPTION',
'            NULL,               -- STUDENT_ID',
'        	NULL,               -- "TERM_ID" NUMBER,  ',
'        	NULL,               -- "SUBJECT_ID" NUMBER(15,0), ',
'        	SYSDATE,            -- START_DATE" SYSDATE, ',
'        	SYSDATE,            -- END_DATE',
'            ''N''                 -- SELECTED_FLAG',
'        );',
'',
'        apex_debug.log_message(',
'            p_message   => ''lv_detected_text => '' || lv_detected_text,',
'            p_level     => apex_debug.c_log_level_info',
'        );',
'    END LOOP;',
'',
'    apex_debug.log_message(',
'        p_message   => ''----- Read File End -----'',',
'        p_level     => apex_debug.c_log_level_info',
'    );',
'    -- Debug End',
'',
'EXCEPTION',
'    WHEN OTHERS THEN',
'        -- Exception handling',
'        APEX_ERROR.ADD_ERROR(',
'            p_message      => ''Error happened!'' || SQLERRM,',
'            p_display_location => APEX_ERROR.C_INLINE_IN_NOTIFICATION',
'        );',
'END;'))
,p_process_clob_language=>'PLSQL'
,p_error_display_location=>'INLINE_IN_NOTIFICATION'
,p_internal_uid=>4615570033271404
);
wwv_flow_imp.component_end;
end;
/
