Wednesday, August 15, 2012

How to Create an APEX Session in PL/SQL

Debugging a function or procedure which references APEX items using the V/NV functions or trying to debug APEX collections can be frustrating when the only way to set, and view, the values is to actually do it through the application. Thankfully, there's a way to create an APEX session from a PL/SQL session to test out your code.

They're various examples on the APEX forums on how to create an APEX session in PL/SQL. Here's one version of it.

CREATE OR REPLACE PROCEDURE sp_create_apex_session(
  p_app_id IN apex_applications.application_id%TYPE,
  p_app_user IN apex_workspace_activity_log.apex_user%TYPE,
  p_app_page_id IN apex_application_pages.page_id%TYPE DEFAULT 1) 
AS
  l_workspace_id apex_applications.workspace_id%TYPE;
  l_cgivar_name  owa.vc_arr;
  l_cgivar_val   owa.vc_arr;
BEGIN

  htp.init; 
  
  l_cgivar_name(1) := 'REQUEST_PROTOCOL';
  l_cgivar_val(1) := 'HTTP';
  
  owa.init_cgi_env( 
    num_params => 1, 
    param_name => l_cgivar_name, 
    param_val => l_cgivar_val ); 
    
  SELECT workspace_id
  INTO l_workspace_id
  FROM apex_applications
  WHERE application_id = p_app_id;

  wwv_flow_api.set_security_group_id(l_workspace_id); 

  apex_application.g_instance := 1; 
  apex_application.g_flow_id := p_app_id; 
  apex_application.g_flow_step_id := p_app_page_id; 

  apex_custom_auth.post_login( 
    p_uname => p_app_user, 
    p_session_id => null, -- could use APEX_CUSTOM_AUTH.GET_NEXT_SESSION_ID
    p_app_page => apex_application.g_flow_id||':'||p_app_page_id); 
END;
To create an APEX session (in PL/SQL) to mimic some tests I can do the following:
SQL> BEGIN
  2    sp_create_apex_session(
  3      p_app_id => 106,
  4      p_app_user => 'MARTIN',
  5      p_app_page_id => 10);
  6  END;
  7  /

PL/SQL procedure successfully completed.
View some APEX session state variables:
SQL> SELECT v('APP_USER') app_user, v('APP_SESSION') app_session,
  2    v('APP_PAGE_ID') page_id, v('P1_X') p1_x
  3  FROM dual;

APP_USER   APP_SESSION     PAGE_ID    P1_X
---------- --------------- ---------- ----------
MARTIN     374363229560201 10

SQL> -- Set P1_X
SQL> exec apex_util.set_session_state('P1_X', 'abc');

PL/SQL procedure successfully completed.

SQL> SELECT v('APP_USER') app_user, v('APP_SESSION') app_session,
  2    v('APP_PAGE_ID') page_id, v('P1_X') p1_x
  3  FROM dual;

APP_USER   APP_SESSION     PAGE_ID    P1_X
---------- --------------- ---------- ----------
MARTIN     374363229560201 10         abc

SQL> -- Clear APEX Session State
SQL> exec APEX_UTIL.CLEAR_APP_CACHE(p_app_id => 106);

PL/SQL procedure successfully completed.
You can also create and view collections using the APEX_COLLECTION APIs and the APEX_COLLECTION view.

Monday, July 30, 2012

ODTUG APEXposed 2012


ODTUG is hosting it's APEX focused APEXposed conference in Montreal this fall from September 11-12. The speaker list is loaded with experts from around the world who will present on a wide variety of APEX and SQL Developer topics. The list includes 3 Oracle Product Managers (APEX / SQL Developer), 6 ACE and ACE Directors, and some other excellent presenters. To put some perspective on this list of presenters, there's only 14 ACE/ACE Directors in Canada and 375 world wide. Having 6 ACE/ACEDs coming to Montreal is a great opportunity to learn from them.

On a personal note, I'll be giving two talks on APEX + HTML5, and APEX Plugins. I gave the HTML 5 presentation this summer at Kscope 12 but I'll be adding some new parts to it. The plugins presentation will go over how to create a plugin from scratch to help you and your team build your own APEX plugins.

For more information and to register go to http://odtugapextraining.com. If you register before August 15th you can take advantage of the early bird rate for only $399! This is a steal for the amount of information that you'll learn and the opportunity to ask the experts any of your APEX questions. To make things even more compelling if you use the code CLARIFIT you will save an additional $50 off your registration cost.

For those that are coming from out of town (Toronto, Ottawa, Quebec City, Southern US, etc) ODTUG has secured a preferred rate at Le St.Martin. For more information go to http://odtugapextraining.com/Location.html and scroll to the bottom.

I really hope that the Oracle community in Canada takes advantage of this opportunity. I'm not sure how often we'll get such an amazing list of presenters and presentations in our own back yard. See you there!

Friday, July 6, 2012

APEX 4 + HTML5 = Awesome II - Slides

I have uploaded the slides from the my talk on HTML5 that I gave at ODTUG Kscope 12 last week. The slides include comments and links to various demos. I haven't had the opportunity to clean up the drag & drop file upload demo and I will post that separately when I have some free time.

You can download the slides or go to the Presentations page to see full abstract and download link. If you have any questions, comments, or feedback please feel free to send me an email (email address in slides).

Tuesday, July 3, 2012

Building a Better Team - Slides

A lot of people have emailed me about the slides from my talk on Building a Better Team that I gave at ODTUG Kscope 12 last week. I updated the slides to include a lot of comments about the main points from the talk.

You can download the slides or go to the Presentations page to see full abstract and download link. If you have any questions, comments, or want to let me know how implementing some of the ideas go please feel free to send me an email (email address in slides).

Friday, June 22, 2012

ODTUG Kscope 12: Things To Look Out For

With Kscope 12 around the corner (literally) I thought I'd post somethings that I'm really looking forward to at the conference.

Cary Millsap's Keynote at the General Session: I've heard a lot of great things about Cary's talk. Everyone that has seen it has only had rave reviews about it so I'm really excited to see it myself. Besides Cary's talk there will be a lot of other funny and exciting things from ODTUG at the general session.

Red Gate's booth: Red Gate will be doing something I've never seen before at a conference. They'll be having a Rapid Development Lab to develop a new Database Version Control system. I strongly encourage you to check it out and bring some ideas on how to manage changes coming in from multiple developers and how a tool could help with it.

Open Mic Night: Immediately following the general session on Monday there will be the annual APEX Open mic night where anyone can demo anything with APEX. Only rule is no powerpoints and a fixed time limit. I'm always amazed at all the neat things that people are doing in their organization.

Lots of APEX presentations: Obviously I'm biased on this topic, but I think there will be a lot of great presentations on the newly announced 4.2 EA 1 and mobile applications. I'm giving two talks this year that I'm really excited about: 
  • APEX 4 + HTML 5 = Awesome II: Mon  11:30 @ Cibolo Canyon 5 - This talk is a sequel to last year's award winning talk on HTML 5. I have some very cool new features to showcase.
  • Building a Better Team: Mon  11:30 @ Cibolo Canyon 5 - This talk will cover how to help improve your development team. Though it's not APEX specific it'll cover some very key points.
  • In both of my presentations I'll be giving copies of my APEX books away!

I look forward to seeing everyone over the next week and seeing a lot of APEX presentations.