Links

   Quran Explorer - Interactive Audio Recitations & Translations

Friday, December 20, 2013

Primefaces Chart Legend


Hi,
This is for those who generate Charts dynamicaly via java code.
Similar to the following;.....

   PieChart pie = new PieChart();

    pie.setId("pi_1");
    pie.setWidgetVar(pie.getId());
    pie.setDiameter(100);
    pie.setTitle("Test");
    pie.setDataFormat("value");            pie.setLegendToggle(true);
    pie.setLegendPosition("w");
 

it happened to my that whenever i set the legendPosition the chart never gets rendered.......

if u find urself in a similar position do what i did... (by the way this is not a hack !!!)

add the following code in the head section of ur XHTML file;-

<script language="javascript" type="text/javascript" src="./resources/js/jquery.jqplot.min.js">
<link  rel="stylesheet" type="text/css" href="./resources/css/jquery.jqplot.min.css"/>

....and let the magic continue....


Tested in primefaces 3.3


Thursday, November 21, 2013

Avoid 'SQL LIKE' - Use Full Text Search

Hello random googler,

Welcome back

Do u often find your self needing to do 'dirty' SQL LIKEs like the following;

SELECT member_id 
FROM vw_members
WHERE upper(member_name) LIKE 'JAY%' OR
upper(member_county) LIKE 'NAI%' OR
upper(staff_no) LIKE 'S01%' OR 
upper(mobile_no) LIKE '072%' OR 'i am tired of this stuff and this is only one permutation assuming the first 3 characters hav been filled...ouuuch'

? (yes question mark... this is where the question ends!!!)


If you answer is yes OR 'Y%'  ;-) ;-) then you are ready for FULL TEXT SEARCH

ONE TECHNIQUE...
Make your SQL VIEWS read for FTS by always having a column called 'ts_doc' that should contain the 'searchable' columns(of ur choice) ??????????


EG (postgres)

CREATE OR REPLACE VIEW vw_members AS
SELECT members.member_id, members.member_name, member.staff_no, members.mobile_no, andanyothercolumnyouwhishcoziassumeuknowsql,
 to_tsvector(COALESCE(members.member_name,'')||' '||COALESCE(member.staff_no,'') ||' '|| COALESCE(members.mobile_no,'')) as ts_doc


So that in your queries just become

SELECT blahblah
FROM vw_members
WHERE ts_doc @@ to_tsquery(' searchQuery:*') ";

NB: searchQuery is nothing but a cleanup to create a valid FTS query:-
searchQuery = searchQuery.replace(" ",":* & ") - in java, but of course u can use ur prefered technique... php, sql,

CONCLUSION
With the above u can use more than one search text(in whatever order or case) to narrow down the result..
for example with searchQuery like 'mik DS1 nai' it will search in all the columns (defined in ts_doc) for words starting with mik or DS1 or nai... thereby matching all entries for nairobi, all names starting with mik eg mike,mikel etc etc

NB: approach is 'begins with....'
(those who do LIKE '%xyz%' look for another evangelist !!!!)

example is in postgres

adios

Friday, November 15, 2013

Oracle and Postgres Cummulative Sum aka Running Totals


Hello Random Googler !!!

You landed here today... u r welcome.

Now consider the following output.

The 'interesting' column is the last one

NameDateAmountCumulative Sum
Mike01-JUN-1310001000
John01-JUN-1310002000
Omar01-JUL-1310003000
Shamim01-JUL-1310004000
Abdul01-JUL-1310005000
Jim01-AUG-1310006000
ZeGuru05-SEP-1310007000
Duli06-OCT-1310008000


Another example
(grouped by date... sorry partitioned by date)


NameDateAmountCumulative Sum
Mike01-JUN-1310001000
John01-JUN-1310002000
Omar01-JUL-1310001000
Shamim01-JUL-1310002000
Abdul01-JUL-1310003000
Jim01-AUG-1310001000
ZeGuru05-SEP-1310001000
Duli06-OCT-1310001000

The first one was achieved by;

SELECT name, date, amount, sum(amount) over(order by payment_id) cummulative_sum
FROM vw_payment

THE second was achieved by;

SELECT name, date, amount, sum(amount) over(partition by date, order by payment_id) cummulative_sum
FROM vw_payment

(the partition by clause was used to restart the running totals for each date)


do i need to say that payment_id is just the PK (incremental/serial integer) of the original table ?

By the way this works for both PostgreSQL and Oracle

Again u r welcome

Thursday, July 11, 2013

The LEGENDARY grep command


the man page starts as follows...
 
SYNOPSIS
       grep [OPTIONS] PATTERN [FILE...]
       grep [OPTIONS] [-e PATTERN | -f FILE] [FILE...]

DESCRIPTION
       grep  searches  the named input FILEs (or standard input if no files are named, or if a single hyphen-minus (-) is given as file name) for lines containing a match to the given PATTERN.  By default, grep prints the matching lines.

       In addition, two variant programs egrep and fgrep are available.  egrep is the same as  grep -E.   fgrep  is  the  same  as grep -F.   Direct  invocation as either egrep or fgrep is deprecated, but is provided to allow historical applications that rely on them to run unmodified.

..
..
..
..till the end of the page

The following are some practical uses of grep from my own experience  and some sourced from the net.

1. Search file contents

Search for the word PAGE (case sensitive) in the file application.xml;
$grep "PAGE" /opt/tomcat7/webapps.labs/primetest/WEB-INF/configs/application.xml


For case insensitive match use the -i switch as follows;

$grep -i "PAGE" /opt/tomcat7/webapps.labs/primetest/WEB-INF/configs/application.xml

2. Search inside a directory

Search for the word drvierClassName inside the directory META-INF

$ grep -R "driverClassName" META-INF/

You will see result on a separate line preceded by the name of the file in which it was found.

3. Find whole words only

When you search for class, grep will match class, className etc.

$ grep -w "class" WEB-INF/context.xml

4. Multiple word search

Use the egrep command as follows:
$ egrep -w 'word1|word2' /path/to/file

5. Count occurences

The grep can report the number of times that the pattern has been matched for each file using -c (count) switch:

$ grep -R -c "driverClassName" META-INF/

6. Show line numbers

Pass the -n switch to precede each line of output with the number of the line in the text file from which it was obtained:

$ grep -R -n "driverClassName" META-INF/

7. Names of matching files only

Use the -l (list) switch as follows;

$ grep -R -l "driverClassName" META-INF/


8. Grep invert match

You can use -v option to print inverts the match; that is, it matches only those lines that do not contain the given word. For example print all line that do not contain the word bar:

$ grep -v bar /path/to/file

NB: for coloured output (if not on by default) you can add --color=auto to your grep command

Other options:
  • -A x or -B x (where x is a number) --- display “x” lines After or Before the section where the particular word is found.

Wednesday, July 3, 2013

Programaticaly Creating Primefaces DataExporter


The following is a utility method that creates a single HtmlCommandLink(h:commandLink NOT p:commandLink) that exports to PDF.

NB: please not that DataExporter is an ActionListener and not a UIComponent !

Modify the id and target variables accordingly

public static HtmlCommandLink createExportCommand(XMLElement wd){

    FacesContext fc = FacesContext.getCurrentInstance();
    Application application = fc.getApplication();
    ExpressionFactory ef = application.getExpressionFactory();
    ELContext elc = fc.getELContext();

    String table_id = "dt_mn_" + wd.getAttribute("key","x") + "_"  +    wd.getAttribute("keyfield","kf"); //id of parent datatable


          HtmlCommandLink command = new HtmlCommandLink();
          command.setId("cmd_pdf_" + table_id);
          command.setValue("PDF");
          command.setTitle("Click to Export");
          //command.addActionListener(new DataExporter(table.getId(),"pdf","Export","false","false","","utf-8",null,null));

          ValueExpression target = ef.createValueExpression(elc, ("mainForm:"+table_id), String.class);
          ValueExpression type = ef.createValueExpression(elc, "pdf", String.class);
          ValueExpression fileName = ef.createValueExpression(elc, "Export", String.class);
          ValueExpression pageOnly = ef.createValueExpression(elc, "true", String.class);
          ValueExpression selectionOnly = ef.createValueExpression(elc, "false", String.class);
          ValueExpression exludeColumns = ef.createValueExpression(elc, "", String.class);
          ValueExpression encoding = ef.createValueExpression(elc, "CP1252", String.class);
          MethodExpression preProcessor = FacesAccessor.createMethodExpression("#{eventManager.preProcessor}",Void.class, new Class[2]);
          MethodExpression postProcessor = FacesAccessor.createMethodExpression("#{eventManager.postProcessor}",Void.class, new Class[2]);

          DataExporter exporter = new DataExporter(target,type,fileName,pageOnly,selectionOnly,exludeColumns,encoding,null,null);

          command.addActionListener(exporter);
       // }

    return command;
      }


===============
Stack
===============
Primefaces 3.3
Tomcat 7
JSF 2.0