Friday, December 30, 2011

Adding Javascripts or inline CSS to ADF page

The best way to add javascript or css to ADF page is use of af:resource. This component adds CSS or JavaScript resource to rendered HTML header. Tag af:resource is processed on the initial creation of the document.

You can point external js or css file, as shown bellow:

<af:document>
   <af:resource type="javascript" source="/js/MyJsCode.js"/>
...
</af:document>


or insert javascript / css code inside tag body:

<af:document>
   <af:resource type="javascript">

      function myJavascriptFunction(){
          alert('This is my function');
      }
   </af:resource>
...
</af:document>


The parent of a af:resource component should be af:document. There is not recommended use of af:resource inside other adf component.

Another way of adding scripts/css to ADF page is "standard" JSF way:
<af:document>
...

   <f:facet>
      <script type="text/javascript">
          function myJavascriptFunction(){
            alert('This is my function');

          }
      </script>
   </f:facet>
...
</af:document>


If you need put CSS into <f:facet> you can encounter problems when compiling. To avoid simillar problems you can wrap tags <style></style> section <![CDATA[ ]]>.

ADF: Custom Client Attributes

As Oracle say:
"The clientAttribute tag specifies the name/value for an attribute which will both be made available both on the server-side (Faces) component as well on on the client-side equivalent. This tag will be ignored for any server-rendered components, as it is only supported for the rich client. ClientAttributes are not synchronized to the server since unknown attributes from the client are not synchronized to the server."

Client attributes simply allows developers to add custom properties to an ADF Faces component instance. Next JavaScript can read custom attribute by calling the getProperty('attr_name') function on a component handle.

Use case:
  • passing to Javascript additional infromation, out of scope for the component.
  • passing to the client Context informations
  • alternative to Javascript callback

Thursday, December 29, 2011

How-to find ADF Faces Components on a Page using javascript

Search from AdfPage.PAGE
  • Search by the component ID - use findComponentByAbsoluteId function
  • Search in component that stamps its children - use findComponentByAbsoluteLocator function
 Relative Search
  •  Use findComponent function. This function searches the component clientId which can be renderer implementation specific.
 Ensure a component exists on the client before accessing it from JavaScript. To ensure client component exists (is rendered)  set clientComponent property to true.

AdfPage.PAGE.findComponentByAbsoluteLocator example:

<af:document title="Page1" id="d1">
  <af:form id="f1">
    <af:panelStretchLayout id="psl1">
      <f:facet name="center">
        <af:panelGroupLayout id="pgl1">
          <af:panelGroupLayout id="main" styleClass="AFStretchWidth" layout="vertical">
          ...
          </af:panelGroupLayout>
        </af:panelGroupLayout>
      </f:facet>
    </af:panelStretchLayout>


    <af:resource type="javascript">
      function f1(){
        var main = AdfPage.PAGE.findComponentByAbsoluteId('main');
         alert(main);
        if(main != null) {
          ActionEvent.queue(main, true);
        }
      }
    </af:resource>
    ...

Wednesday, December 28, 2011

ADF custom phase listeners

As we know developers can create and configure custom life cycle listeners to receive ADF lifecycle notifications at the beginning and the end of each phase to execute custom logic.

In Oracle ADF framework developer can use standard JSF listener or special ADF listener which supports additional ADF-specific page cycle extensions. Listeners can be used to customize the ADF Lifcycle.

Custom listener can beconfigured for the following scopes:
  • Global, when the page lifecycle listener is configured to execute for all lifecycle execution throughout the application
  • Page scope - the listener is configured in the page definition file and supports concrete page
  • Custom scope - a lifecycle listener is added programmatically from Java and remains active until it is removed or the user session is dismissed
 How to create listener?
  • Create an objects that should receive lifecycle notifications. The object must implement the PagePhaseListener interface
  • The method argument is an instance of PagePhaseEvent.
  • PhaseId returns phase identifier (int value)
  • ADF controller is requred
  • To create and configure global lifecycle listener use the adf-settings.xml configuration file. The adf-settings.xml file does not exist by default and needs to be created in the “.adf\META-INF” folder of the application. The adf-settings.xml file allows developers to define the order in which page phase listeners are invoked.
Example:
public class MyPageListener implements PagePhaseListener {

    public MyPageListener() {

        super();

    }

    public void afterPhase(PagePhaseEvent pagePhaseEvent) {

        String phaseId = pagePhaseEvent.getPhaseId();

        System.out.println("We are after "+ Lifecycle.getPhaseName(phaseId));

    }

    public void beforePhase(PagePhaseEvent pagePhaseEvent){

        String phaseId = pagePhaseEvent.getPhaseId();

        System.out.println("We are before "+ Lifecycle.getPhaseName(phaseId));

    }

}

<?xml version="1.0" encoding="US-ASCII" ?>
<adf-settings xmlns="http://xmlns.oracle.com/adf/settings">
  <adfc-controller-config xmlns="http://xmlns.oracle.com/adf/controller/config">
    <lifecycle>
      <phase-listener>
        <listener-id>MyPageListener1</listener-id>
        <class>mc.adf.listeners.MyPageListener</class>
        <before-id-set>
          <listener-id> MyPageListener2</listener-id>
        </before-id-set>
        <after-id-set>
          <listener-id>MyPageListener3</listener-id>
        </after-id-set>
     </phase-listener>
    </lifecycle>
  </adfc-controller-config>
</adf-settings> 

Friday, December 23, 2011

Java Applet java.lang.InternalError: couldn't create component peer

Sometimes our customers notice the following exception in Java console when they try to open the page with Scanner Applet and load it:

 Exception in thread "AWT-EventQueue-2" java.lang.InternalError: couldn't create component peer
    at sun.awt.windows.WComponentPeer.checkCreation(Unknown Source)
    at sun.awt.windows.WComponentPeer.<init>(Unknown Source)
    at sun.awt.windows.WCanvasPeer.<init>(Unknown Source)
    at sun.awt.windows.WPanelPeer.<init>(Unknown Source)
    at sun.awt.windows.WWindowPeer.<init>(Unknown Source)
    at sun.awt.windows.WFramePeer.<init>(Unknown Source)
    at sun.awt.windows.WEmbeddedFramePeer.<init>(Unknown Source)
    at sun.awt.windows.WToolkit.createEmbeddedFrame(Unknown Source)
    at sun.awt.windows.WEmbeddedFrame.addNotify(Unknown Source)
    at sun.plugin2.main.client.PluginEmbeddedFrame.addNotify(Unknown Source)
    at sun.awt.windows.WEmbeddedFrame.<init>(Unknown Source)
    at sun.plugin2.main.client.PluginEmbeddedFrame.<init>(Unknown Source)
    at sun.plugin2.main.client.PluginMain$StartAppletRunner.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)




Actually, it is a know problem for Oracle. Anyway, exception appears before the applet starts. Sometimes it has not an influence on the applet and it works without problem. Sometimes the applet is not loaded at all. In this case to resolve the issue you need to do the following steps:
  1. Open Java Control Panel
  2. Open Advanced tab
  3. Expand Java Plug-in item
  4. Uncheck Enable the next-generation Java Plug-in
  5. Click Apply
BUT!... Sometimes Enable the next-generation Java Plug-in checkbox is grayed out. In this situation follow the steps below to disable new java plugin via registy and see how it goes.
  1. Go to start, type regedit.exe and press Enter.
  2. Navigate to the location below:
  3. HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Plug-in
  4. Navigate to the subfolder of Java Plug-in. It should be something like:1.6.0_29
  5. Set the dword value of  "UseNewJavaPlugin" to 00000000 to disable new java plugin.
  6. Close the registry editor.
Free Web Directory - Add Your Link

Merry Christmas Everyone

Christmas greetings and all good wishes for your health and happiness in the coming year !

Friday, December 16, 2011

How to: array inversion count

Inversion Count for an array indicates – how far (or close) the array is from being sorted. If array is already sorted then inversion count is 0. If array is sorted in reverse order that inversion count is the maximum. 

Formal solution to solve is:
Given is an array A of N integers. Inversion is a pair of indexes (P, Q) such that P < Q and A[Q] < A[P].

Write a function

class Solution { public int array_inversion_count(int[] A); }

which computes the number of inversions in A, or returns -1 if such number exceeds 1,000,000,000.

Assume that:

        N is an integer within the range [0..100,000];
        each element of array A is an integer within the range [-2,147,483,648..2,147,483,647].

For example, in the following array

    A[0] = -1 A[1] = 6 A[2] = 3
    A[3] =  4 A[4] = 7 A[5] = 4

there are four inversions:

      (1,2)  (1,3)  (1,5)  (4,5)

so the function should return 4.

Complexity:

        expected worst-case time complexity is O(N*log(N));
        expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).


import java.math.*;
class Solution {
public int array_inversion_count ( int[] A ) {
// write your code here

int n = A.length;
int counter = 0;
for(int p = 0; p < n - 1; p++) 

   { 
      for(int q = p+1; q < n; q++) 
      { 
         if( A[p] > A[q] ) counter++;
      }
   }
   return counter;
  }
}

Monday, December 12, 2011

Configuring an SSL Certificate for Microsoft Active Directory

See more here: http://confluence.atlassian.com/display/CROWD/Configuring+an+SSL+Certificate+for+Microsoft+Active+Directory#ConfiguringanSSLCertificateforMicrosoftActiveDirectory-Step1InstalltheActiveDirectoryCertificateServices

Thursday, December 8, 2011

How to: creating Private Database Link to database on remote server (Oracle DB)

If you was somewhat confused about the problems with the 'ad hoc' data link to a remote database, I propose tested solution. Sometimes you can't edit TNSNAMES and you need directly put the TNSNAME entry information directly in the database link connection string. There is my example:


CREATE DATABASE LINK my_link
CONNECT TO my_schema_user IDENTIFIED BY my_password
USING '(DESCRIPTION =(ADDRESS = (PROTOCOL=TCP)(Host=my_host_ip)(Port=1521))(CONNECT_DATA=(SERVICE_NAME=my_service_name)))';