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.




Monday, October 22, 2012

Flex - Changing Cursor Icon

Mouse icon can be changed in flex. This example changes the cursor icon to a configured image when mouse is moved over it and then resets it to system cursor on mouse moves out. I have posted the action script code here.

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

    [Embed(source="assets/images/tree_move.png")]
    private var treeCursorIcon: Class;  //define an icon

    public function changeToTreeCursor (e: flash.events.MouseEvent) : void { 
            CursorManager.setCursor(treeCursorIcon); // change the cursor here
    }

    private function changeToDefaultCursor() : void {
        CursorManager.removeAllCursors(); // reset the cursor here
    }

==================================================================
First an your icon has to be defined, providing the relative path to the image. The function, changeToTreeCursor(MouseEvent) sets that icon as the mouse cursor and changeToDefaultCursor() sets it back to system cursor.

You can have your own custom component as follows, and define rollOut and rollOver attributes of it to fire changeToTreeCursor(MouseEvent) and changeToDefaultCursor() methods respectively for the corresponding events to fire.

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

<xxx:My_Component id="my_comp" x="0" y="39" title="@Resource(key='comp_label', bundle='draw')" rollOut="changeToDefaultCursor()" rollOver="changeToTreeCursor(event)">
    </xxx:My_Component>

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

thanks,
Shyarmal.

Saturday, October 20, 2012

Customizing Flash Right- Click Menu


   The menu which appears on right click of a flash object can be customized to include items of your choice. The following code segment adds options, "Expand", "Hide Outcomes", "Hide Inputs", "Hide Outputs", "Hide Privileges" and "Keys Only" to the menu. Further it removes/ hides some existing items of the menu. Code segment to do this is posted below.

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

            var contextMenu: ContextMenu = new ContextMenu();
            contextMenu.hideBuiltInItems();
            contextMenu.clipboardMenu = false;   
            var showAll : ContextMenuItem = new ContextMenuItem("Expand");
            var hideOutcomes : ContextMenuItem = new ContextMenuItem("Hide Outcomes");
            var hideInputs : ContextMenuItem = new ContextMenuItem("Hide Inputs");
            var hideOutputs : ContextMenuItem = new ContextMenuItem("Hide Outputs");
            var hidePrivileges : ContextMenuItem = new ContextMenuItem("Hide Privileges");
            var hideAll : ContextMenuItem = new ContextMenuItem("Keys Only");
            showAll.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, handleShowAll);
            hideOutcomes.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, handleHideOutcomes);
            hideInputs.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, handleHideInput);
            hideOutputs.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, handleHideOutput);
            hidePrivileges.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, handleHidePrivileges);
            hideAll.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,      handleHideAll);
            contextMenu.customItems.push(showAll);
            contextMenu.customItems.push(hideOutcomes);
            contextMenu.customItems.push(hideInputs);
            contextMenu.customItems.push(hideOutputs);
            contextMenu.customItems.push(hidePrivileges);
            contextMenu.customItems.push(hideAll);

            component.contextMenu = contextMenu;  
            // component is of type mx.core.UIComponent

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

   First a 'flash.ui.ContextMenu' instance is created. Subsequently, its inbuilt and clip board items are removed [contextMenu.hideBuiltInItems(); contextMenu.clipboardMenu = false;]. Then new menu items are created and added to the menu. Each menu items is registered with an event listener to be fired when the same is selected.
  For an example, it "Hide Outputs" item is selected the following method will be triggered.

private function handleHideOutcomes (event: ContextMenuEvent) : void {
        // operational code

}

Finally, the menu created this way is assigned as the context menu of the respective UI component (mx.core.UIComponent). Following is the figure of the right click menu.


thanks,
Shyarmal.

Friday, September 28, 2012

Flex Popup Window


 This is about how a popup window can be created to appear to the right side, with a mouse click. I have used a button bar in this code sample which would be positioned vertically and contain an image icon. Further, the button bar will dispatch an item clicked event when any of it's items is clicked. What appears subsequently is a tiled window, that would dispatch an event to hide itself when exit (x) is clicked.

 The methods to be executed when events are fired are written in a separate action script, my_action_script.as which is included in my mxml file with the line, <mx:Script source="my_action_script.as"/>. 
===================================================================

 <!--
     Code that is not relevant is omitted.
-->
<mx:Script source="my_action_script.as"/>

<mx:Array id="buttons">

        <mx:Object icon="@Embed(source='images/my_image.png')" desc="test"/>   
 </mx:Array>

 <!--
     Code that is not relevant is omitted.
-->
<mx:ButtonBar height="100%" width="2%" verticalGap="2" direction="vertical" id="buttonBar" dataProvider="{buttons}" itemClick="buttonBar_itemClick(event);" paddingTop="0" paddingBottom="{this.height - 75}" />
               
            <s:PopUpAnchor height="{buttonBar.height}" width="0"
                       popUpPosition="right" id="popAnc">
                  <s:TitleWindow id="title_window"                                                              title="XXX"   close="popUpAnchorCloseHandler(event);">
                    <mx:Panel id="my_panel" height="{buttonBar.height - acb.height}" 
                              width="{acb.width/4}" title="my panel" alpha="1">
                        <!-- add items to the panel here -->
                    </mx:Panel>
                </s:TitleWindow>
            </s:PopUpAnchor>

 <!--

     Code that is not relevant is omitted.
-->

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

 First, an array with one object, having an icon is defined. This array is the data provider of the button bar. The direction of the button bar is vertical. If an item of the button bar is clicked (in this case there is only one item) the method, buttonBar_itemClick(e:ItemClickEvent) will be called (since it is defined as itemClicked attribute).
 Popup anchor is the component used for popup. It can be customized by introducing where to popup with height, width and popUpPosition attributes. The window will appear to the right as stated in popUpPosition. This UI component engulfs a title window containing a panel. The title window will trigger popUpAnchorCloseHandler(e: CloseEvent) method as stated for it's close attribute.

 Actionscript code segments pertinent to the example are posted below.
===================================================================


/**
     * This is called when an item of the button bar is clicked.
     */
    private function buttonBar_itemClick(e:ItemClickEvent):void {
        var message:String = ObjectUtil.toString(e.item);
        if (message.indexOf("test1") != -1) {
            // open the pop-up anchor.
            FlexGlobals.topLevelApplication.popAnc.displayPopUp = true;
        }
    }

    /**
     * This is called when the title window's close event is fired.
     * Pop up anchor is closed here.
     */
    private function popUpAnchorCloseHandler(e: CloseEvent): void {
        FlexGlobals.topLevelApplication.popAnc.displayPopUp = false;
    }

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

 The first method, which is invoked when the item is clicked, converts the item into a string format and checks if "test1" is available ("test1" is the value of desc attribute of the item). If so, popup anchor is made visible. The second method will be invoked when the 'x' sign of the title window is clicked and it will make the anchor invisible.

thanks,
Shyarmal.

Monday, September 24, 2012

Flex Datagrid Example

This is a simple implementation of a data grid in Flex. It would have three columns and 2 rows. Data to be contained in the grid is initialized at application start up and bound to the same. The method, initData(), which initialized the data for the grid, is called at application start up as it is defined in the "initialize" attribute of "s:Application".

============================================================= 
<?xml version="1.0" encoding="utf-8"?>

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" 
               minWidth="955" minHeight="600" 
               initialize="initData();">

    <fx:Declarations>
    </fx:Declarations>

    <fx:Script>
        <![CDATA[
            import mx.collections.*;

            private var dataArray: Array = [
                {Artist:'Danuka', Album:'storm', Price:17.97}, 
                {Artist:'Shyarmal', Album:'gloomy', Price:96.29}
            ];  

            [Bindable]
            public var gridData: ArrayCollection;
            public function initData(): void {
                gridData = new ArrayCollection(dataArray);
            }

        ]]>
    </fx:Script>
     

    <s:DataGrid id="my_grid" width="500" 
         height="500" dataProvider="{gridData}">
    </s:DataGrid>

</s:Application>

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

<s:DataGrid id="my_grid" width="500" height="500" dataProvider="{gridData}"></s:DataGrid> introduces a flex grid. The attribute dataProvider binds data to the grid. In this example, "gridData" is an ArrayCollection holding that data, which is initialized through initData() method. In the method, gridData is initialized with a predefined array (dataArray). The symbol, [Bindable] above gridData makes it a source to be bound to a component (This enables gridData to be used as the source of data of the grid).

Output:



thanks,
Shyarmal.

Flex "Hello World" Application

This is my first mxml script. Here I have used a label to display "Hello World". This is a flex web application.

==================================================================
<?xml version="1.0" encoding="utf-8"?>

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" 
               minWidth="955" minHeight="600">

    <fx:Declarations>
    </fx:Declarations>

    <s:Label id="lb" x="350" y="350" text="hello world" />

</s:Application>

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

     <s:Label id="lb" x="350" y="350" text="hello world" /> - introduces a label component to the application. [id is the attribute by which this component will be referred, x and y define the coordinates and text holds what's to display. ]

This mxml is further enhanced, adding a button which will change the text of the label.

==================================================================
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" 
               minWidth="955" minHeight="600">

    <fx:Declarations>

    </fx:Declarations>

    <s:Label id="lb" x="350" y="350" text="hello world" />
    <s:Button id="btn" x="350" y="400" 
        label="click" click="changeLabel();"/>
    <fx:Script>
        <![CDATA[
             private function changeLabel() : void {
                if (lb.text == "hello world")
                    lb.text = "good bye ..";
                else 
                    lb.text = "hello world";
            }
         ]]>
    </fx:Script>

</s:Application>

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

Here,     <s:Button id="btn" x="350" y="400" label="click" click="changeLabel();"/>  adds a button component to the application. [id is the attribute by which this component will be referred, x and y define the coordinates and label is the caption and click triggers a function that is to be called when the button is clicked. ]

fx:Script encloses actionscript code in an mxml script. Here a function, changeLabel() is defined, which is to be called when a user clicks on the button (The function is bound to "on click" event of the button). This function changes the text of the label to "good bye .." if it is "hello world" and visa-verse.


thanks,
Shyarmal.

Saturday, September 8, 2012

Configure remote repository for Maven

Remote repository can be changed by configuring settings.xml in conf folder of maven. 

<?xml version="1.0" encoding="UTF-8"?>


<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">

  <!--
  Maven local repository defaults to ~/.m2/repository.
To change the path to the local repository, have the following tag.
  -->
 <localRepository>/path/to/local/repo</localRepository>


  <pluginGroups>
  </pluginGroups>
  <proxies>
    <proxy>
      <id>optional</id>
      <active>true</active>
      <protocol>http</protocol>
      <username>proxyuser</username>
      <password>proxypass</password>
      <host>proxy.host.net</host>
      <port>80</port>
      <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
    </proxy>
  </proxies>

  <servers>

    <!-- 
To configure your credentials to access the remote repository
    -->
    <server>
      <id>xxxxx</id>
      <username>your_username</username>
      <password>your_password</password>
    </server>

  </servers>


  <mirrors>

    <!--
Remote repository can be configured here.
     -->
    <mirror>
      <id>artifactory</id>
      <mirrorOf>*</mirrorOf>
      <name>my repo name</name>
      <url>https://www.location_of_my_repo/repo</url>
    </mirror>

  </mirrors>

  <profiles>
    <!-- profile
    <profile>
      <id>jdk-1.4</id>
      <activation>
        <jdk>1.4</jdk>
      </activation>
      <repositories>
        <repository>
          <id>jdk14</id>
          <name>Repository for JDK 1.4 builds</name>
          <url>http://www.myhost.com/maven/jdk14</url>
          <layout>default</layout>
          <snapshotPolicy>always</snapshotPolicy>
        </repository>
      </repositories>
    </profile>
    -->
  </profiles>
</settings>

thanks,
Shyarmal.