Monday, October 14, 2013

Disable or Enable Button/InputText in ADF by using Javascript

Disabling ADF Faces UI component on the client-side could be tricky sometimes.You basically need to set unsecure="disabled" and clientComponent="true". But if you use button.setProperty('disabled', true) on a commandButton, your button will still look like undisabled (though it won't invoke action event), proper style class and 'disabled' attribute will not be applied to the button element.
To properly disable button you can use script like this: 


<?xml version='1.0' encoding='UTF-8'?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1"
          xmlns:f="http://java.sun.com/jsf/core"
          xmlns:af="http://xmlns.oracle.com/adf/faces/rich">
  <jsp:directive.page contentType="text/html;charset=UTF-8"/>
  <f:view>
    <af:document id="d1">
      <af:resource type="javascript"> 

      function disableField(actionEvent) { 
  
        var nameInputText = actionEvent.getSource().findComponent("nameFld"); 
        nameInputText.setProperty("disabled", true); 
         
        //The following line is for partial refresh, in this case  
        //its taken care by framework 
        //AdfPage.PAGE.addPartialTargets(nameInputText); 
         
       
      } 
  
      function enableField(actionEvent) { 
  
        var nameInputText = actionEvent.getSource().findComponent("nameFld"); 
        nameInputText.setProperty("disabled", false); 
       
      } 
      function setButtonDisabled(actionEvent) {
        var button = actionEvent.getSource().findComponent("submitButton"); 
            button.setProperty('disabled', true);
            AdfDomUtils.addOrRemoveCSSClassName(true,
                AdfRichUIPeer.getDomElementForComponent(button),
                AdfRichUIPeer.DISABLED_STYLECLASS);
            var buttonDom = button.getPeer().getButtonElement(button);
           
                buttonDom.setAttribute('disabled', 'disabled');
                  
        }
   
   
      function setButtonEnabled(actionEvent) {
        var button = actionEvent.getSource().findComponent("submitButton"); 
            button.setProperty('disabled', false);
            AdfDomUtils.addOrRemoveCSSClassName(false,
                AdfRichUIPeer.getDomElementForComponent(button),
                AdfRichUIPeer.DISABLED_STYLECLASS);
            var buttonDom = button.getPeer().getButtonElement(button);
           
           
                buttonDom.removeAttribute('disabled');
                  
        }
   
   
     </af:resource> 
     <af:form id="f1"> 
       <af:panelGroupLayout id="pgl1"> 
         <af:commandButton text="Disable Name Field" id="disableBtn" clientComponent="true" unsecure="disabled"
                  > 
       <af:clientListener type="action" method="setButtonDisabled"/>     
         </af:commandButton> 
         <af:commandButton text="Enable Name Field" id="enableBtn" clientComponent="true" unsecure="disabled"
                  > 
           <af:clientListener type="action" method="setButtonEnabled"/> 
         </af:commandButton> 
      
       <af:inputText unsecure="disabled" clientComponent="true" label="Name" id="nameFld"/> 
       <af:commandButton disabled="true" text="Submit" id="submitButton" clientComponent="true" unsecure="disabled">
      
       </af:commandButton>
     </af:panelGroupLayout> 
     </af:form> 

    </af:document>
  </f:view>
</jsp:root>



unsecure:

 A whitespace separated list of attributes whose values ordinarily can be set only on the server, but need to be settable on the client. Currently, this is supported only for the "disabled" attribute. Note that when you are able to set a property on the client, you will be allowed to by using the the .setProperty('attribute', newValue) method, but not the .setXXXAttribute(newValue) method. For example, if you have unsecure="disabled", then on the client you can use the method .setProperty('disabled', false), while the method .setDisabled(false) will not work and will provide a javascript error that setDisabled is not a function.

clientComponent:

 whether a client-side component will be generated. A component may be generated whether or not this flag is set, but if client Javascript requires the component object, this must be set to true to guarantee the component's presence. Client component objects that are generated today by default may not be present in the future; setting this flag is the only way to guarantee a component's presence, and clients cannot rely on implicit behavior. However, there is a performance cost to setting this flag, so clients should avoid turning on client components unless absolutely necessary.

Friday, October 11, 2013

Reading Weather data from xml

      
 Reading Weather data from xml
    try {

                String url = "http://weather.yahooapis.com/forecastrss?w=2442047&u=f";
        System.out.println("::::Weather URL::::"+url);
              
                SAXParserFactory factory = SAXParserFactory.newInstance();
                SAXParser saxParser = factory.newSAXParser();

                DefaultHandler handler = new DefaultHandler() {
                    boolean flag;
                    public void startElement(String uri, String localName,
                                             String qName,
                                             org.xml.sax.Attributes attributes) throws SAXException {
                      
                        try {
                            // _logger.info("Start Element :" + qName);
                           
                            if (qName.equalsIgnoreCase("latitude")) {
                                flag=true;
                            }
                            if (qName.equalsIgnoreCase("description")) {
                                System.out.println(":::::::Start::::::Weather Condition:::::::::");
                                flag=true;
                            }
                            if (qName.equalsIgnoreCase("yweather:condition")) {
                                System.out.println(":::::::Start::::::Weather Condition:::::::::");
                                System.out.println(":::text:"+attributes.getValue("text"));
                                System.out.println(":::temp:"+attributes.getValue("temp"));
                                System.out.println(":::date:"+attributes.getValue("date"));
                                System.out.println(":::::::End::::::Weather Condition:::::::::");
                            }

                            if (qName.equals("yweather:forecast")) {
                                System.out.println(":::::::Start::::::Weather Forecast:::::::::");
                                System.out.println("Code:" + attributes.getValue("day"));
                                System.out.println(":::date:"+attributes.getValue("date"));
                                System.out.println(":::low:"+attributes.getValue("low"));
                                System.out.println(":::high:"+attributes.getValue("high"));
                                System.out.println(":::text:"+attributes.getValue("text"));
                                System.out.println(":::::::End::::::Weather Forecast:::::::::");
                            }
                           
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        System.out.println("::::::END Processes::::::;:::");
                    }


                    //                        public void endElement(String uri, String localName,
                    //                                        String qName) throws SAXException {
                    //                        }

                    public void characters(char[] ch, int start,
                                           int length) throws SAXException {
                    if(flag){
                        String s=new String(ch, start, length);
                           
                        System.out.println(":::new String(ch, start, length):::"+s.split("<br />")[0]);
                       
                    flag=false;
                    }
                    }

                };
                System.out.println("::::::::::::::::::::::::::::::::::::::::::::");

                saxParser.parse(url, handler);
                System.out.println(":::::::::::::::::::::::::::");
            } catch (Exception e) {
                e.printStackTrace();
            }


Adding Images to jspx page
<f:verbatim>
                <input type="image"
                       src="http://l.yimg.com/a/i/us/we/52/33.gif"/>
              </f:verbatim>
              <af:image source="http://l.yimg.com/a/i/us/we/52/26.gif" id="i1"/>
              <![CDATA[
<img src="http://l.yimg.com/a/i/us/we/52/33.gif"/>]]>
            </af:group>
           
         
      

Thursday, October 10, 2013

Setting Multiple Java Options or Time Zone in Jdeveloper

1) We can set multiple java options in Jdeveloper
     I. Goto the Project properties of Model or ViewController
    II.Go to Run/Debug/Profile on the Left Pane.
   III.If you are using Project Settings Click on Edit by selecting your Profile most of the cases it is  
         Default.
   IV.Select Launch Settings on the left pane and in Java option enter this value Note: you can  
        change according to your options by giving space between multiple java parameters . 

                            -Duser.timezone="+05:30" -Doracle.adfm.usemds=true
2) Error :oracle.jbo.JboException: JBO-29000: Unexpected exception caught:
               java.sql.SQLDataException, msg=ORA-01882: timezone region not found

    Solution : Set time zone in java options -Duser.timezone="+05:30"
                      This time zone is for India, you can set which ever time zone required.



Sunday, September 8, 2013

Page Definition Variables to Store Temporary Page Values

Back to the basics. In most of the cases data is coming from Model layer, from ADF BC or EJB. When user is changing data on the screen, frameworks takes care and preserves temporary data. However, what about such screens where we have temporary fields, without any relation with the Model layer - transient data fields. What if there is no corresponding Model implementation, and still we need to store field data between requests - where should we store it? I believer, one of the best techniques is to use Page Definition variables, this is old approach back from ADF 10g times - but it still works very well. Main advantage - we are able to store transient temporary data between requests and there is no need to define session scope bean.

It happened to see scary things - developers are defining managed beans in Page Flow Scope, just because they want to make sure temporary UI values are preserved between requests :)

This sample implements two input components - inputText and inputNumberSlider. Both components are enabled with autoSubmit, as well as value change listener is defined for both. Once value is changed for one of the components, we recalculate total sum of both fields and display as total:


Both input components exist only in UI, there is no Model layer representation - user types values, total is recalculated immediately. However, we still need to store entered values, otherwise once we select number slider - partial request will be invoked and value entered for income will be lost. ADF developer would try to store input value inside Backing Bean:

It would be wrong to store temporary value inside Backing Bean - this bean lifetime is as long as request, in other words - once we will enter second input value, first will be always lost. You may think to define Session or even Page Flow Scope bean - that would make it work, but why to waste server memory - only if you want to make your ADF applications slow.

We can use Page Definition variables to store temporary values, these values will be preserved between requests. Open Page Definition, associated with the page, expand executables and select to insert new variable:

Give a name and specify a type for new variable:

In my example, I will define all three variables in the same way - for each of the fields:


 Once variables are defined, we are not done yet with Page Definition. For every defined variable we need to define corresponding attribute (will be accessed from the page). Insert new item under bindings - Attribute Value:



When creating new Attribute Value, make sure to select variables as Data Source and map related variable (defined one step above) name:


Once all done, we should have following picture - each variable is assigned with attribute value:


Go back to UI now, input component should have its value property mapped with attribute value (not with variable directly, because data will not be stored/retrieved) from Page Definition. For example: bindings.incomeVarAttr.inputValue:


Once again - UI component value you should map with attribute value, not with variable directly:


Finally, you may ask question - how to access attribute values defined in Page Definition programmatically? Easy - use ADFUtils wrapper class. There are two methods available for your convenience - getBoundAttributeValue("name") and setBoundAttributeValue("name", value):

Its how it looks on UI:




This is HA compatible, In cluster this will work fine.

Thursday, September 5, 2013

ADF Mobile Links

Sequence number generation in EO

 To generate Primary key sequence generation in EO , Best practice is generate Data Manipulation Methods and in that we have to check condition if DML_INSERT?

 If we call createInsert in Updataable VO and while commit time it will call doDML() method,



 While insertion time if you want Primary key generation then we need to keep condition
if(operration==this.DML_INSERT)
and we have to set sequence number to primary key column (EmployeeId).