Presentation on theme: "The Power of the Oracle APEX Repository Patrick Wolf, Sphinx IT Consulting DOAG SIG – APEX for Advanced, 31-May-2007."— Presentation transcript:
1 The Power of the Oracle APEX Repository Patrick Wolf, Sphinx IT Consulting DOAG SIG – APEX for Advanced, 31-May-2007
2 About Sphinx IT Consulting Founded 1993Creates mission-critical individual softwareConsulting for Databases & Java MiddlewareSpecialized in OracleMore information atAbout Patrick Wolf13 years of Oracle experience with SQL, PL/SQL and Oracle FormsSolution Architect at Sphinx IT ConsultingRun an APEX Blog –Author of the ApexLib Frameworks –Author of the Oracle APEX Builder PluginsContact mail is
3 Agenda The Oracle APEX Repository Part 1 - The APEX Dictionary Views Simple queriesAutomatic documentation creationMonitoring APEX ApplikationsCreate generic code using the repositoryPart 2 – Directly manipulating the APEX RepositoryDo mass updates in the RepositoryCreate new Page Items, ...Q&A
4 The Oracle APEX Repository Oracle APEX stores all meta-data of an application in a repostiory in the databaseSchema FLOWS_xxx (eg.: FLOWS_030000)Table prefixed with WWV_FLOW_*This tables are not public.Starting with version 2.2 there is a view access layer.Prefix of the views is APEX_*Since 3.0 there is an user interface for simple queries. Can be found at Utilities/APEX Dictionary
5 The APEX Dictionary Views Returns a „readable“ form of the internal APEX Repository tablesHide the generic/old data modelLookups for Foreign-KeysTranslate interne Enumerations into EnglishContains statistic counters – eg. Number of child elementsChecks access privileges (you only see your applications)Connected as schema user: All applications of the workspaces where the schema is assigned to.Connected as SYS, SYSTEM, FLOWS_xxx: All applicationsOther users: nothingThe Views are public and can therefore be used in APEX applications or in external scripts.
11 The APEX Dictionary Views Don’t forget the quote!To execute press „Go“ button at the beginning of the page
12 The APEX Dictionary Views Divided into 3 areasApplication-Meta-DataAPEX_APPLICATION*Monitoring data about the applicationAPEX_WORKSPACE_ACCESS_LOGAPEX_WORKSPACE_ACTIVITY_LOGAPEX_WORKSPACE_LOG_SUMMARY*APEX_WORKSPACE_CLICKSAPEX_WORKSPACE_SESSIONSWorkspace-Meta-DataThe reminding APEX_WORKSPACE* views. For example APEX_WORKSPACE_APEX_USERS, ...
13 The APEX Dictionary Views What are usage examples for the views?A few examplesQuality checks a coding styleguides, …Dynamic generation of Site-MapsAutomatic documentation of the applicationAutomatic monitoring of the applicationSome nice page view chartsFor the creating of generic codeAnd so on...
14 Quality check 1Don’t use the „xxx Label with Help“ Label Template for Page Items.SELECT APPLICATION_NAME, PAGE_ID, ITEM_NAME, DISPLAY_AS, ITEM_LABEL_TEMPLATEFROM APEX_APPLICATION_PAGE_ITEMSWHERE ITEM_LABEL_TEMPLATE LIKE '%Label with Help'AND DISPLAY_AS <> 'Hidden'ORDER BYAPPLICATION_NAME;
15 Quality check 2 Are all labels left aligned? SELECT APPLICATION_NAME , PAGE_ID, ITEM_NAME, DISPLAY_AS, LABEL_ALIGNMENTFROM APEX_APPLICATION_PAGE_ITEMSWHERE LABEL_ALIGNMENT <> 'Left'AND DISPLAY_AS <> 'Hidden'ORDER BYAPPLICATION_NAME;
16 Quality check 3Has a help text been entered for all Page Items where the „xxx Label with Help“ Template has been used?SELECT APPLICATION_NAME, PAGE_ID, ITEM_NAME, DISPLAY_ASFROM APEX_APPLICATION_PAGE_ITEMSWHERE ITEM_LABEL_TEMPLATE LIKE '%Label with Help'AND DISPLAY_AS <> 'Hidden'AND ITEM_HELP_TEXT IS NULLORDER BYAPPLICATION_NAME;
17 Dynamic Site-MapsCreate a automatically generated Site-Map for your application.Tag relevant pages with $SITEMAP$ in the page comment.SELECT PAGE_ID, PAGE_TITLE, PAGE_GROUPFROM APEX_APPLICATION_PAGESWHERE APPLICATION_ID = :APP_IDAND INSTR(PAGE_COMMENT, '$SITEMAP$') > 0ORDER BY PAGE_TITLE;Create SQL Report with above SQL statement.For column „PAGE_TITLE“, change Link Text to #PAGE_TITLE# and Page to #PAGE_ID#Set column „PAGE_ID“ to „Hidden“.Page Groups could be used to get a hierarchical representation in a tree view…
18 Automatic Documentation Wouldn’t it be nice to have a diagram of the Page Flow of an application? Which page calls which page…Like JDeveloper, where there is a Page Flow editor for JSF applications, but which has be manually mantained…Not like other development environments where the necessary data is stored somewhere in XML files or in Java/… code, we have all the necessary data accessible with simple SQL queries!With the help of Graphviz an open source tool, the graphical diagram generation is quite easy.
19 Page Flow GeneratorAnalyses the meta-data of an application, like Pages, Branches, Buttons, Links- in Report Columns, HTML Regions and SQL Statements.Creates a dependency model in the memory andcreates for each Page Group (to keep them small) it’s own diagram.The complicated part was the Graphviz syntax.The Page Flow Generator is part of the ApexLib Frameworks.
21 Monitoring APEX Applications 1 Wouldn’t it be cool if the administrator automatically gets notified if an error occurs in the application?APEX provides the view APEX_WORKSPACE_ACTIVITY_LOG and the column ERROR_MESSAGE for such a purpose.A DBMS_JOB can be used to check it periodically.See blog entryAnd with you can set it up to send a SMS.
22 Monitoring APEX Applications 2 How to detect hackers trying to break into your application?The view APEX_WORKSPACE_ACCESS_LOG logs each login attempt with the following dataTimestampUsernamenIP addressResult – Successful/Failed
23 Monitoring APEX Applications 3 How can you detect if the response time of you application decreases?The view APEX_WORKSPACE_ACTIVITY_LOG logs each page access and the column ELAPSED_TIME tells you how long APEX needed to generate the page.Combined with historical data and the number of users currently accessing the system you can estimate a trend of your response time.The view can also be used to analyze the navigation behavior of your users. Also see column THINK_TIME for the time the user waited till opened another page.Or create a query which returns the pages which are never called.And so on…
24 Create Generic Code 1Ever had the problem that you had a page where the Page Items are very similar and number VALUE1, VALUE2, … and where you had to create the same validation for all of them? Boring job, isn’t it?There is also a way to do it with less effort!BEGINFOR rITEM IN( SELECT ITEM_NAMEFROM APEX_APPLICATION_PAGE_ITEMSWHERE APPLICATION_ID = :APP_IDAND PAGE_ID = :PAGE_IDAND ITEM_NAME LIKE 'P'||:PAGE_ID||'_VALUE%')LOOPIF V(rITEM.ITEM_NAME) IS NOT NULLTHENAPEX_Util.set_session_state( p_name => rITEM.ITEM_NAME, p_value => 1);END IF;END;
25 Create Generic Code 2 Ever needed a cascading/hierarchical LOV? Have you implemented Carl Backstom’s (http://carlback.blogspot.com/) AJAX example for each LOV?Isn’t all required information available in the meta-data of the Page Item? The WHERE clause of the LOV contains all dependencies!Why don’t we use it and stop duplicating code and information about the dependencies?That’s what the Open Source ApexLib Framework (http://apexlib.sourceforge.net/) does!Short demonstration of the framework.
26 APEX Dictionary Views - Conclusio Are a powerful tool which you can use to enhance your application of your development process.There a lot of possible scenarios where you can use them, you just have to come up with some good ideas.Use the APEX Dictionary View Browser to get used to them!Available since Oracle APEX Version 2.2.
27 Manipulating the APEX Repository Ever had the requirement to make a bulk update in your application? For example to change the Label Template to “without Help” for all Page Items.Or create with your own code Page Items, … Your own wizard.Oracle APEX doesn’t offer an official tool to fulfill above requirements.But if already all the meta-data of the application are in the database…But the result of doing a direct update on the repository tables can really be surprising. The application isn’t accessible anymore in the builder!So how do it the right way?
28 Manipulating the APEX Repository BACKUP, BACKUP, BACKUP!!!Make a backup/export of the FLOWS_XXX schema before you do any manipulation of the repository tables! At least do an export of your application!Use the following tips at your own risk !
29 Manipulating the APEX Repository The manipulation is down connected as FLOWS_xxx (eg. FLOWS_030000).The tables are prefixed with WWV_FLOW*The best way to identify the tables and columns is to have a look at the APEX Dictionary Views.Create a reference record. Eg. With the new Label TemplateSet APEX context with WWV_Flow_API.set_security_group_idWhat is the Security Group ID?Is equal to the Workspace ID and can be get with Apex_Util.get_security_group_id(`Workspacename`)
30 Manipulating the APEX Repository Example to initialize the APEX context.DECLAREvSecurityGroupId NUMBER;BEGINvSecurityGroupId := Apex_Util.find_security_group_id( p_workspace => 'SPHINX_SXSD');IF vSecurityGroupId = 0THENRAISE_APPLICATION_ERROR(, 'Workspace not found! Do not issue updates'END IF;--WWV_Flow_Api.set_security_group_id(vSecurityGroupId);WWV_Flow.g_user := 'PWOLF';END;
31 Manipulating the APEX Repository Example which sets the Label Template from „Optional Label with Help“ to „Optional Label“ on all Page Items on page 4.UPDATE WWV_FLOW_STEP_ITEMSSET ITEM_FIELD_TEMPLATE = E18WHERE FLOW_ID = 106AND FLOW_STEP_ID = 4AND ITEM_FIELD_TEMPLATE = E18;FLOW_STEP_ID equal to Page IdFLOW_ID equals to Application Id
32 Manipulating the APEX Repository What can I do if my application isn’t accessible after an update anymore?Check the SECURITY_GROUP_ID in WWV_FLOWS.If it’s 0, initialize the APEX context and set the SECURITY_GROUP_ID in WWV_FLOWS again.UPDATE WWV_FLOWSSET SECURITY_GROUP_ID = NULLWHERE FLOW_ID = 106;NULL is used by intention!
33 Manipulating the APEX Repository How can we create new Page Items, …?APEX provides the public(!) but undocumented package WWV_Flow_ApiCan be called from a package in the application schema or from FLOWS_XXX.There are a lot of examples, just view the export file of an application.WWV_Flow_Api.g_id_offset is used during import to get unique IDs -> not required in our case.The next free ID is returned by WWV_Flow_Id.next_val.
34 Manipulating the APEX Repository The following session initialization steps are required if you are executing the code outside of an APEX session. For example when you are connected to FLOWS_XXX.Initialize APEX context as done for an update + execute the following code.BEGINSELECT VALUEINTO WWV_Flow_Api.g_nls_numeric_charsFROM NLS_SESSION_PARAMETERSWHERE PARAMETER = 'NLS_NUMERIC_CHARACTERS';EXECUTE IMMEDIATE'ALTER SESSION SET NLS_NUMERIC_CHARACTERS=''.,''';--WWV_FLOW.g_browser_language := 'en';WWV_Flow_Api.g_id_offset := 0;END;
35 Manipulating the APEX Repository Just create a new Page Item with the following example code.DECLAREvPageItemId NUMBER;BEGINWWV_Flow_Api.set_version( WWV_Flow_Api.g_compatable_from_version);WWV_Flow.g_flow_id := 100; -- Application Id--vPageItemId := WWV_Flow_Id.next_val;WWV_Flow_Api.create_page_item( p_id => vPageItemId, p_flow_id => WWV_Flow.g_flow_id, p_flow_step_id => 4, p_name => 'P4_TESTIT'...END;
36 Manipulating the APEX Repository - Conclusio BACKUP, BACKUP, BACKUP!!!For updates, do a direct update on the tables.Always create a Reference record.Do inserts with the WWV_Flow_Api package.Application export file has good example.