Example: Portal Object Event Logging

16-22 Oracle Fusion Middleware Developers Guide for Oracle Portal Example 16–11 Running the LOG_PORTAL_EVENT CMEF Subscriber begin log_portal_event; end;

16.6 Example: Item Notification

The item_notify subscriber in Example 16–12 sends an e-mail notification whenever a user adds, updates, or deletes an item on a specified page: Example 16–12 The CMEF_ITEM_NOTIFY Subscriber create or replace procedure item_notify as MIME_TYPE_TEXT constant varchar30 := textplain; CUSTOM_ATTRIBUTE_ID constant number := 1020; ADDED constant varchar220 := added; UPDATED constant varchar220 := updated; DELETED constant varchar220 := deleted; ITEM constant varchar210 := ITEM; agent_list dbms_aq.aq_agent_list_t; wait_time integer := 5; begin_time pls_integer; end_time pls_integer; agent_w_message sys.aq_agent; dequeue_options dbms_aq.dequeue_options_t; message_properties dbms_aq.message_properties_t; message_handle raw16; message portal.wwsbr_event; l_subscriber varchar230 := CMEF_ITEM_NOTIFY; l_queue varchar230 := PORTAL.WWSBR_EVENT_Q; l_page_id number := 33; -- Page ID of page. l_mode binary_integer := dbms_aq.REMOVE; l_rec varchar2256; l_body varchar24000 := null; l_from_user varchar230 := from-email-address; l_to_user varchar230 := to-email-address; l_event varchar230 := ; l_portal_user_name varchar230 := portal; l_portal_password varchar230 := portal password; l_display_name varchar2256 := ; begin begin_time := dbms_utility.get_time; agent_list1 := sys.aq_agentl_subscriber, l_queue, null; loop -- Wait for messages. dbms_aq.listen agent_list = agent_list, wait = wait_time, agent = agent_w_message ; if agent_w_message.name = l_subscriber then dequeue_options.wait := DBMS_AQ.NO_WAIT; dequeue_options.consumer_name := l_subscriber; dequeue_options.navigation := dbms_aq.FIRST_MESSAGE; dequeue_options.dequeue_mode := l_mode; dbms_aq.dequeue Using the Content Management Event Framework 16-23 queue_name = l_queue, dequeue_options = dequeue_options, message_properties = message_properties, payload = message, msgid = message_handle ; if message.object_class = ITEM then -- Determine the type of event that occurred. if message.raw_event = portal.wwsbr_event_q_access.EVENT_INSERT then l_event := ADDED; elsif message.raw_event = portal.wwsbr_event_q_access.EVENT_UPDATE then l_event := UPDATED; elsif message.raw_event = portal.wwsbr_event_q_access.EVENT_DELETE then l_event := DELETED; end if; if l_event = ADDED or l_event = UPDATED or l_event = DELETED then -- Set the Portal Context. portal.wwctx_api.set_contextl_portal_user_name, l_portal_password; begin -- Get the Item Display name from the all items view. select display_name into l_display_name from portal.wwsbr_all_items where id=message.object_id and caid=message.object_site_id; exception when NO_DATA_FOUND then dbms_output.put_linesqlerrm; exit; end; -- Only send an e-mail if the item has a display name. if l_display_name is not null then -- Construct the body of the message. l_body := l_body || brAn item titled || CSBR_UTIL.file_viewer_hyperlink message.object_id, message.page_site_id, l_display_name || was || l_event || on || to_charmessage.events_date,dd-mon-yyyy hh12:mi pm || by || message.events_user || . ; -- Send the message. html_email p_from = l_from_user, p_to = l_to_user, p_subject = Item || l_event || Notification, p_text = Text, p_html = l_body ; end if; end if; end if; end if; end_time := dbms_utility.get_time; if end_time - begin_time 3000 then exit; end if; 16-24 Oracle Fusion Middleware Developers Guide for Oracle Portal end loop; end; The ITEM_NOTIFY CMEF subscriber removes the actual events from the queue so that multiple e-mails are not sent to the user. For further information on the performance and timing of AQ events, refer to the Streams Advanced Queuing page on OTN: http:www.oracle.comtechnologyproductsaq For an example of how to add a subscriber to the WWSBR_EVENT_Q queue, refer to Example 16–10 . For an example of how to run a subscriber, refer to Example 16–11 . The ITEM_NOTIFY CMEF subscriber runs for 3 seconds. This ensures that the subscriber does not continuously run and consume system resources. Alternatively, you could modify the wait_time value used in the subscriber to a higher value, say 60 seconds, so that the subscriber listens for new events every minute. You could also use a DBMS_JOB to have the subscriber run at a specified interval, for example, 30 minutes, as shown in Example 16–13 . Example 16–13 Using DMBS_JOB declare v_job number; begin dbms_job.submit job = v_job, what = item_notify, interval = SYSDATE + 302460 ; end; Additional Code Example 16–14 shows the CSBR_UTIL package. Example 16–14 CSBR_UTIL Package create or replace package CSBR_UTIL as function file_viewer_hyperlink p_item_id in number, p_caid in number, p_text in varchar2 return varchar2; end; create or replace package body CSBR_UTIL as function file_viewer_url Note: For descriptions of the file_viewer_hyperlink function, which gets the URL of the item, and the html_email procedure, which handles the actual sending of the message, refer to Additional Code . Using the Content Management Event Framework 16-25 p_item_id in number, p_caid in number return varchar2 is l_return varchar210000; l_item_name varchar2100; l_folder_id number; l_folder_name varchar2100; l_portal_url varchar21000; begin select name, folder_id into l_return, l_folder_id from portal.wwsbr_all_items where id = p_item_id and caid = p_caid and active = 1; begin while 1=1 loop select parent_id, name into l_folder_id, l_folder_name from portal.wwsbr_all_folders where id = l_folder_id and caid = p_caid; l_return := l_folder_name||||l_return; end loop; exception when NO_DATA_FOUND then null; -- Exit loop at no rows found, this is expected. END; -- Set the Portal URL. l_portal_url := http:host:portportalpagedad; return l_portal_url||l_return; exception when OTHERS then dbms_output.put_linesqlerrm; return Error; end file_viewer_url; function file_viewer_hyperlink p_item_id in number, p_caid in number, p_text in varchar2 return varchar2 is begin return a href=||file_viewer_url p_item_id = p_item_id, p_caid = p_caid||||p_text||a; end file_viewer_hyperlink; end; Example 16–15 shows the html_email procedure. Example 16–15 The HTML E-mail Procedure create or replace procedure html_email p_to in varchar2, Note: For this procedure to work you will need to deploy the Oracle Database Sendmail package on your database.