Powered By Blogger

Saturday, October 17, 2015

Oracle function with dynamic SQL

   Following is an example of a function which dynamically creates an SQL statement using one of its input parameters and executes the same. And also, the query consists of place holders, which are replaced when executing (like in a prepared statement).

==============================================================

CREATE OR REPLACE
FUNCTION DASH_BOARD_ABW (YOP IN NUMBER, BU IN NUMBER, FIN_Y IN VARCHAR2, MON IN VARCHAR2)
  RETURN NUMBER AS
    query_str VARCHAR2(500) := NULL;
    abw NUMBER := 0;
  BEGIN
  query_str := 'select ' || MON || ' from ahl_fas_dash_board_abw where trim(fin_year) = :FIN_Y and bu_id = :BU and yop = :YOP and rownum = 1';
  EXECUTE IMMEDIATE query_str INTO abw
  USING FIN_Y, BU, YOP;
  RETURN abw;
END DASH_BOARD_ABW;


==============================================================

   The result of the execution is assigned to a variable 'abw' which is returned. 'EXECUTE IMMEDIATE' executes the query and 'USING' clause is to substitute placeholders of the query respectively. 

thanks,
Shyarmal.

Tuesday, December 3, 2013

Hibernate Composite Key Mapping (JPA)

   It is sometimes necessary to have composite keys in a database table. In such a scenario, it would be necessary to get hibernate to cater this requirement. This is an illustration of how it could be done.
   Consider an entity, BankFacilityAgent which has percentage and isKeyFacilitator as attributes and another attribute, key of type BankFacilityAgentKey. The class is annotated with @Table and @Entity. The class is bound to a database table, BANK_FAC_AGENT. Getters of attributes percentage and isKeyFacilitator are annotated with @javax.persistence.Column and @Basic. Note that getter of 'key' attribute is annotated with @Id.

===============================================================

package com.shyarmal.persistance.domain;

import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Table(name = "BANK_FAC_AGENT", catalog = "")
@Entity
public class BankFacilityAgent {

    private BankFacilityAgentKey key;
    private float percentage;
    private String isKeyFacilitator;

    @Id
    public BankFacilityAgentKey getKey() {
        return key;
    }

    public void setKey(BankFacilityAgentKey key) {
        this.key = key;
    }

    @javax.persistence.Column(name = "PERCENTAGE")
    @Basic
    public float getPercentage() {
        return percentage;
    }

    public void setPercentage(float percentage) {
        this.percentage = percentage;
    }

    @javax.persistence.Column(name = "KEY_FACILITATOR")
    @Basic
    public String getKeyFacilitator() {
        return isKeyFacilitator;
    }

    public void setKeyFacilitator(String keyFacilitator) {
        isKeyFacilitator = keyFacilitator;
    }
}


===============================================================


   BankFacilityAgentKey represents the composite key, which should implement Serializable and is annotated with @Embeddable. This class holds attributes that comprise the composite key (bankId, facilityAgentId). The getters of the attributes are annotated the same way as in the class above.

===============================================================

package com.shyarmal.persistance.domain;

import javax.persistence.Basic;
import javax.persistence.Embeddable;
import java.io.Serializable;

@Embeddable
public class BankFacilityAgentKey implements Serializable {

    private int bankId;
    private int facilityAgentId;

    public BankFacilityAgentKey(int bankId, int facilityAgentId) {
        this.bankId = bankId;
        this.facilityAgentId = facilityAgentId;
    }

    public BankFacilityAgentKey() {

    }

    @javax.persistence.Column(name = "BANK_ID")
    @Basic
    public int getBankId() {
        return bankId;
    }

    public void setBankId(int bankId) {
        this.bankId = bankId;
    }

    @javax.persistence.Column(name = "FACILITY_AGENT_ID")
    @Basic
    public int getFacilityAgentId() {
        return facilityAgentId;
    }

    public void setFacilityAgentId(int facilityAgentId) {
        this.facilityAgentId = facilityAgentId;
    }
}

===============================================================

   The database table mapped is BANK_FAC_AGENT with columns PERCENTAGE, KEY_FACILITATOR, BANK_ID and FACILITY_AGENT_ID where BANK_ID and FACILITY_AGENT_ID are the composite key.


thanks,
Shyarmal.

Friday, November 29, 2013

Enable/ Disable Link Using jQuery


Consider a link by the id, 'link1'.
<a href="abc.htm" id="link1">

It can be disabled using jquery .
$("#link1").css({'pointer-events':'none', 'cursor':'default'});

Disabled link can be enabled by removing the css properties. 
$("#link1").css({'pointer-events':'', 'cursor':''});

thanks,
Shyarmal

Thursday, November 28, 2013

jQuery Popup Dialog



    This example shows how to prompt a pop-up with jQeury when an icon is clicked. Following is the HTML code required for this exercise. An image link is created with the id, 'hint' (, which does not link to any page). Then a div element is defined with the 'dialog' as the id. This is the dialog that would be displayed when the image icon is clicked. The dialog will have a title, 'Bank Code' and a text, 'Abbreviation of the bank name.'. Its dimensions are defined in the style attribute (height and width). Since the div should not be displayed on the html page, it has been made invisible by setting the 'display' property to 'none' in style attribute.

================================================================
<a href="#" id="hint"><img src="images/information-icon.gif" align="top"> </a>


<div id="dialog" title="Bank Code" style="display:none; height: 75mm; width: 300mm;">
    <p>Abbreviation of the bank name.</p>
</div>

================================================================

Binding of click event of the link (referred to by 'hint') should be done in jQuery ready function. Click event of the link is captured and the display property of the div, 'dialog' is set to 'block'. Then prompting of the dialog is done by calling '$( "#dialog" ).dialog();'.


================================================================
$(function() {
     $("#hint").click(function () {
        $( "#dialog").css("display", "block");
        $( "#dialog" ).dialog();
    });
});

================================================================


screen shot of the prompt:


thanks, 
Shyarmal

Saturday, December 22, 2012

Flex: Displayable Image from an Image Resource

  Following is a class which can be used to convert and an image resource (here a png file) to a displayable image (mx.controls.Image) instance in Flex. Here a static function 'displayableImageFromClass(Image, int, int): void ' does the work. The function's height and width parameters are optional and they specify the height and width of the displayable image that is returned.

================================================================

package com.shyarmal.drawing.utils {
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.utils.ByteArray;
   
    import mx.controls.Image;
    import mx.graphics.codec.PNGEncoder;
   

    public class ImageUtils {
       
        /**
         * png image encoder.
         */
        private static var encoder:PNGEncoder = new PNGEncoder();
       
        public function ImageUtils() {
        }
       
        /**
         * Given a class (with an embeded png image), 
         * creates a corresponding displayable image,
         * if imageClass is not null.
         *
         * @param imageClass - The image class from 
         *            which the image has to be created.
         * @param height - Height of the final image (optional).
         * @param width - Width of the final image (optional).
         * @return - mx.controls.Image produced.
         */
        public static function displayableImageFromClass( 
                   imageClass: Class, height: int = 25, 
                                   width: int = 25): Image {
            var i:Image = new Image();
            if (imageClass != null) {
                var bitMap: Bitmap = new imageClass();
                var bitmapData: BitmapData = bitMap.bitmapData;
                bitmapData.draw(bitMap);
                var imageToDisplay:ByteArray = 
                         encoder.encode(bitmapData);
                i.source = imageToDisplay;
                i.height = height;
                i.width = width;
            }
            return i;
        }
    }
}

================================================================

  If the 'imageClass' parameter is null, simply a new instance of mx.controls.Image is formed and returned. If not a 'Bitmap' is formed by instantiating the 'imageClass' passed in. Then its 'BitmapData' is derived. A byte array is constructed with the help of PNGEncoder and assigned to the source of the image instance. Image is returned after setting its dimensions.

Invocation of the method can be done as below;

[Embed (source="/assets/images/your_png.png" )] 
var resource: Class; // define a class with the actual resource.

// height and width can be passed if necessary.
var i:Image = ImageUtils.displayableImageFromClas(resource);



thanks,
Shyarmal.

Thursday, December 13, 2012

Distinguish 'double click' from 'click' in Flex

   Defining behavior for both 'click' and 'double click' events in Flex is tricky, as when a double click is done, the click event gets fired. Therefore there needs to be a workaround to solve this issue. First of all add event listeners for 'click' and 'double click' (this can be done either in mxml or actionscrips which I have chosen the later).


//Adding click listener
component.addEventListener(MouseEvent.CLICK, localMouseClickEvent, false, 0, true);

//Adding double click listener
component.addEventListener(MouseEvent.DOUBLE_CLICK, localMouseDoubleClickedEvent, false, 0, true);

//n.b. here component refers to any mx UIComponent 


   According to the code above if a mouse click is issued, 'localMouseClickEvent(MouseEvent)' method is called and for a double click, 'localMouseDoubleClickedEvent(MouseEvent)' should be called. However, when a double click is done, 'localMouseClickEvent(MouseEvent)' triggers.Both functions are defined below. 


/** reference to the deferred click operation */
private var deferredClickReference: Number = 0;


protected function localMouseClickEvent (event: MouseEvent) : void {    
          clearInterval(deferredClickReference); // clearing previous schedule
         
          // defer operation                   
          deferredClickReference = setInterval(deferredMouseClickEvent, 300); 
}
   
protected function deferredMouseClickEvent() : void {
        clearInterval(deferredClickReference); // clearing previous schedule  
        
       // code to execute for 'click'
}

protected function localMouseDoubleClickedEvent(event:MouseEvent):void {
         // revoking deferred click operation
         clearInterval(deferredClickReference);

        // code to execute for 'double click'
}


   The workaround is to defer the single click operations to see if it is actually followed by another click. Another function, 'deferredMouseClickEvent()' is defined, and invoked from localMouseClickEvent(event: MouseEvent0)' scheduled to be triggered after 300 ms. This scheduling is done by 'setInterval(deferredMouseClickEvent, 300)' , which returns a reference to the schedule (this is assigned to 'deferredClickReference'). So if a click is followed by another within 300 ms, double click event operations will be run, otherwise that of click
   'clearInterval(deferredClickReference)' resets the reference, which revokes the scheduled single click operation. This is done before scheduling the deferred operation, after execution of the same and in double click event's function (to prevent deferredMouseClickEvent() run).


 thanks,
Shyarmal.

Wednesday, December 5, 2012

Delete XML nodes in Flex

This is about deleting a node from XML in Flex. Here our XML is assigned to a variable, treeXml.

var treeXml: XML = <root>
                       <node label="abc">
                             <node label="Page"></node>
                             <node label="Business"></node>
                             <node label="Application"></node>
                             <node label="Loyalty"></node>
                        </node>
                        <node label="def">
                             <node label="Page"></node>
                             <node label="Business"></node>
                             <node label="Application"></node>
                             <node label="Loyalty"></node>
                        </node> 
                     </root>

Suppose we need to delete the node with label 'abc'. The way to do it is;

delete treeXml.node.(@label=="abc")[0];

Above code deletes the child (node) which is having the attribute label equal to 'abc'. Notice '[0]' at the end of the statement. After this, treeXml would be;

                    <root>
                         <node label="def">
                              <node label="Page"></node>
                              <node label="Business"></node>
                              <node label="Application"></node>
                              <node label="Loyalty"></node>
                         </node> 
                    </root>

Thanks,
Danuka.