Wednesday, July 23, 2014

Auto tab JSF 2.1 phone fields custom component using Jquery


This blog will describe how to create a custom UI for auto-tab component and server side component in JSF2.1 and jquery

Wouldn't it be cool to have your own custom components and just have one line of code in your jsf page which generates a whole block of code?
Well thats what custom components is all about.


Prerequisites

1. Existing JSF 2.1 project - Easy to create in Netbeans if you are a beginner
2. HTML knowledge


There are 2 ways of creating custom components

a. Server Side
b. UI Component


a. Server Side

This does not show how to do the auto tab but gives a start on how to create custom blogs on the server side.

i. Create a java class say PhoneComponent.java

   



ii. Create an xml file custom.taglib.xml under WEB-INF folder.

   

iii. Now we have to tell the application what this xml is
    Add an entry in web.xml to do this
    web.xml

    

iv. The jsf page say test.xhtml

     

    Thats it!! Now you can create as many custom columns as you want that willl do stuff that will reduce coding time in half... Have fun..

b. UI Custom Components

Now we can see how to create a custom UI component. No server side code here.

i. Create your component which will be a xhtml page with whatever feature or block of code.

   In this example i am creating an autotab feature for phones using jquery. 

   phoneComponent.xhtml

   


ii. Create a custom.taglib.xml under WEB-INF.

   


iii. Now we have to tell the application what this xml is
    Add an entry in web.xml to do this just like above server side config.
    web.xml
    

iv. Use it in your jsf page.. As easy as that..

    You will have to include the jquery libraries in your path 

    test.xhtml

   


That was easy right!
















Thursday, March 15, 2012

Scan through lines Java

If you want to delete a line in a paragraph in java you can use java.util.Scanner;

In the below example, I scan through each line and check if a particular pattern occurs as the start of that line and delete that line.


import java.util.Scanner;
public class PatternFinder
{
    /**
     * @param args
     */
    public static void main(String[] args)
    {
        String a = "TEST/Line1" +
                   "\nTEST/Line3  TESTER/123456" +
                   "\nthis is TESTER/Line4" +
                   "\nCON/Line2  TESTER/123456 " +
                   "\nTEST/Line3  TESTER/123456";
        System.out.println("Input String :\n****************************\n" + a +"\n***************************");
        Scanner scanner = new Scanner(a);
        String b = "";
        try{
            while(scanner.hasNextLine())
            {
                String line = scanner.nextLine();
                if(!containsElmntWithStartPtrn(line,"TEST/" ,"TESTER/")){
                    b= b + line +"\n";
                }
            }
            System.out.println("Output String :\n****************************\n" + b.trim() +"\n***************************");
        }catch (Exception e){
            System.out.println("Error in PatternFinder" + e);
        }finally {
            scanner.close();
        }
      
    }
  
    public static boolean containsElmntWithStartPtrn(String line, String startStr,String elementStr)
    {
        boolean contains = false;
        if((line.indexOf(startStr) != -1) && line.indexOf(startStr) < startStr.length()){
            if(line.contains(elementStr)){
                contains = true;
            }
        }
        return contains;

    }

}

Monday, February 27, 2012

Start ipp printer

To start an ipp printer use


RESUME_PRINTER constant raw2) := '0011';



function process_request(operation in raw,
uri in ipp_attr,
attributes in ipp_attributes := null,
data in clob := null) return integer is
req Utl_Http.Req;
resp Utl_Http.Resp;
ipp_request raw(2000);
ipp_response raw(32000);
result integer;
i pls_integer;
BEGINUtl_Http.Set_Response_Error_Check(enable => true);

ipp_request := ipp_request_header(operation, uri, attributes);req := Utl_Http.Begin_Request(url => utl_raw.cast_to_varchar2(uri.val), method => 'POST');Utl_Http.set_authentication(req,username,password);
Utl_Http.Set_Header(r => req, name => 'Content-Type', value => 'application/ipp');
Utl_Http.Set_Header(r => req,
name => 'Content-Length',
value => to_char(utl_raw.length(ipp_request) + nvl(length(data), 0)));
Utl_Http.Write_raw(req, ipp_request);
-- write out data in chunks
i := 1;
loop
exit when data is null or(i > length(data));
Utl_Http.Write_Text(req, substr(data, i, 4000));
i := i + 4000;
end loop;

resp := Utl_Http.Get_Response(r => req);
utl_http.read_raw(r => resp, data => ipp_response);Utl_Http.End_Response(r => resp);

result := parse_response(ipp_response);

return result;

END;



Friday, January 27, 2012

Using jquery Datatable with struts2 action class using ajax

Using the jquery datatable is a really awesome thing when you have to display search results in an already sorted and paginated and searchable and..... the features just keep going on.


I spent some hours figuring using struts2 action class to return the values using ajax json object. Thought of sharing it..


I will start with the requirements:
1. struts2 framework application - Assuming you already have an application up and running. You can get a struts framework which has the dataTable jquery being used with a servlet here
2. struts2-json-plugin-2.1.8.jar - You can get the jar from the link. Put it in the lib folder of your struts application or add it to your project build path.
3. jquery dataTable plugin from here - JQuery DataTable

After downloading the dataTable zip file you just need few of the files if you are just using a basic version of the datatable.
Here are the files you would need from the zip extract:
i. \media\css\demo_page.css
ii. \media\css\demo_table.css
iii. \media\css\demo_table_jui.css
iv. \media\js\jquery.dataTables.min.js
v. \media\js\jquery.js
vi. /examples_support/themes/smoothness/jquery-ui-1.8.4.custom.css - Just for a theme. This is not required for a basic table.


search.jsp







struts.xml

       

SearchItemAction.java



BaseWebAction - To get the request which is required by the datatable



Company.java




DataRepository.java




DataTablesParamUtility.java



JQueryDataTableParamModel.java




And that would be enough. PS: The last 4 classes are from the sample workspace provided in the link above. 

Start giving your page the stylish datatable in your struts framework!!!

Sample : DataTable_Struts2 - Maven based. Just run the pom and it will get the jar files required.





Friday, December 2, 2011

First TO DO List in US


Well for those who come as an alien in US you need alot of things to be done and no one tells you these things for free!!
All these are my personal experiences and i fell into each and every trap listed.

1. Apply your SSN after 2 weeks only. Your name gets into the system only after 2 weeks.
2. Apply for medical insurance as soon as possible(within 30 days). Employer usually covers it but you have to follow up.
3. Take a credit card. In US we need credit! that controls your life in US. But there is a catch to it. Do not apply lots of credit cards. Since each time you apply a credit card it will hit your credit score. Take this very seriously and do some research before applying in stores. Every store will try to sell their cards, but make sure you don't fall in their trap. There is lot of information online that will help you build your credit score.
4. Buy a car as soon as you have money. That will make you mobile and since many places are not well connected to travel by public transport, it might not be that easy. Also one catch, car loans for first buyers are very high due to high interest rate(As of 2011, 7.99% for first time and 2.99 for successive car loans), so don't spend too much money on buying your first car, or even if you do, pay it off fast.
5. Regarding apartments: Before moving into an apartment, it is important to check the crime report of the neighborhood.
check crimemapping.com to know the crime rate. Check the damages before you move into the apartment, because when you leave the apartment, you will be charged for the preexisting damages.
6. Its is very easy to save few bucks in US. If your neighbor bought an iPhone for $200, you can very well get it for $20-$30 less, provided you spend few minutes googling for coupons or online deals. This applies to anything you want to buy. There are always deals around. Also always look out for used stuff from craigslist.com,goodwill stores,salvation army stores or garage sales. Craigslist also has services like workmanship, movers, repairs, room mates.....




Tuesday, November 22, 2011

Always Apply h1B premium when you are transfering your visa


It is always advisable to apply a premium H1B visa when you are transfering your visa from one company to another. The normal visa application would be about 1500USD cheaper but not worth the wait. The consultancies will always make you do a normal one because thats where they get their profit. One of my friends has been waiting for 3 months now and still hasnt got a reply from USCIS.
Even if you have to pay extra for the premium visa, i would go ahead and do it.