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.