Monthly Archives: April 2013

ZK framework: AJAX without JavaScript

Recently came across ZK framework which can be used to create AJAX based UI without actual JavaScript code. It claims that you do not need to be a programmer to code using ZK framework which actually uses ZUML Markup language to do create the UI. I have not used it yet but look interesting and would like to give it a try.

http://en.wikipedia.org/wiki/ZK (framework)

http://www.zkoss.org/

Why rownum will nor work for greater or equal to checks

Someone asked a question on a forum that why rownum=2 does not work for his query, so i though of sharing some details here.

Using rownum is a tricky affair. Safest bet is to use it only when you want to limit the number of results to be shown. For example rownum<2 or rownum<=5.

Why rownum=2 or rownum>2 will not work?

Read here – http://www.oracle.com/technetwork/issue-archive/2006/06-sep/o56asktom-086197.html

In summary, this is how oracle execute a query

  1. The FROM/WHERE clause goes first.
  2. ROWNUM is assigned and incremented to each output row from the FROM/WHERE clause.
  3. SELECT is applied.
  4. GROUP BY is applied.
  5. HAVING is applied.
  6. ORDER BY is applied.

rownum<=2 clause will get converted to

ROWNUM = 1
for x in
( select * from emp )
loop
    exit when NOT(ROWNUM <= 2)
    OUTPUT record to temp
    ROWNUM = ROWNUM+1
end loop
SORT TEMP

if you change exit when NOT(ROWNUM <= 2) with rownnum=2, you can see it will fail in the first run itself

So if I cannot use rownum, what can I use. Try using row_number() http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions137.htm

It works something like

SELECT last_name FROM
   (SELECT last_name, ROW_NUMBER() OVER (ORDER BY last_name) R FROM employees)
   WHERE R BETWEEN 51 and 100;

Are my online transactions secured? HTTPS to rescue

You must have wondered at times that how can my data be secured over the internet when using a website that needs sensitive data like bank account details (or how secured is a banking site).

The protocol that comes to rescue is HTTPS. Whenever we are working on a website where we want ourselves to be secure, we should make sure the url starts with https://www.mysite.com instead of http. http is hyper text transfer protocol which just specifies some rules as to how the data needs to be transferred and understood on the internet. https is http + secure or http over SSL (Secured socket layer).

If I ask you, how can I secure the data? The obvious answer is encryption. The most common type of encryption is symmetric encryption which uses a single key to encrypt and decrypt the data. That is the same key that encrypts the data will decrypt it. The problem with this kind of encryption is that if someone gets access to this key, he can create or read the data using the key. To solve this problem, there is concept of 2 key encryption. In this we have a private and public key. the beauty of this approach is that data encrypted using one key can be decrypted using second key only. On the downside this approach is a bit slow because of added complexity.

Lets quickly talk about concept of certificates as well. There are various certification authorities (CA) which can provide a certificate of authentication to a website. Whenever you are on a https website, you will see a small icon of a lock (may vary from browser to browser), on which when you click, will show you the certificate details. This will guarantee that the website is indeed what it claims to be. So for example you misspelt a link or opened a link from a spam email, looking for https and certificate will be sufficient for you to ensure authenticity of the website.

Now lets go behind the scenes and see how my websites are secured because of https.

Step 1: You click a secured url or type into your browser.

Step 2: The server listens to your request and returns back a certificate and a public key (server will never share the private key for security reasons)

Step 3: Your browser will verify this certificate (shows error in case of some doubt)

Step 3a: After verifying certificate, browser will generate a symmetric key and encrypt it using “the public key” sent by server.

Step 3b: Browser then sends this encrypted symmetric key to web server.

Step 4: Web server decrypts the data sent by browser using its private key (as this was encrypted using public key). The decrypted data is symmetric key from browser.

Step 5: Now this symmetric key will be used by both parties (browser and web server) to transfer the data (remember public/ private key is time consuming, so it is used only to initially transfer the symmetric key).

The above steps ensure that the communication between server and browser is always secured and hence you can do the online shopping or banking without fear.

Want to Install Mac OS on Windows virtual Machine

If you are addicted to windows but still want to try your hands in Mac OS, you can do that on your windows machine using a virtual machine. Here are a few resources which will help

http://www.codeproject.com/Articles/335596/How-to-install-latest-Mac-OS-X-Lion-Virtual-Machin

http://www.sysprobs.com/working-method-install-mac-107-lion-vmware-windows-7-intel-pc

http://www.sysprobs.com/vmware-workstation-8-0-8-0-1-unlocker-to-run-mac-os-x-guest-in-windows-7

Downloading docx files as zip

Faced a weird issue with my new windows 7 laptop today. Whenever I downloaded a docx word document or pptx or xlsx it was getting saved as a zip file. The zip file had a lot of xml files. That reminded me how all the UI elements in windows are stored as xml files. So MS word is just a tool which will take these xml files and create the required document out of it.

But the question for me was how to open this zip file in MS word. The fluke I tried worked out for me that is, just change the extension. E.g. if you downloaded abc.docx, and while download the document got downloaded as abc.zip, you just need to change the name of downloaded file back to abc.docx.

Facade Pattern

Façade pattern is a simple design pattern which is used to hide unwanted details for a module. Chances are that you have followed this pattern without even knowing that you are using it.

The literal meaning of Façade is front; that explains a lot about pattern. I have a habit of over simplifying things, so I will do the same here using a simple example. Say you need to fill up some forms to get admission to a college or club. There is a procedure to do that, first you need to go to clerk 1 and fill general info form, second you need to go to clerk 2 and fill a form for specific interests / courses. Than you need to go to third one for background check form and finally you need to get to fourth clerk for filling fees details.

Will it not be good if we could have done all this at one place in one form. Say there is a receptionist at front desk who takes all these details from you and is responsible for contacting all the clerks and getting details filled (obviously you are not aware of the clerks’ existence as you are only interacting with receptionist). That saves you from a lot of trouble and also helps institution to keep the direct dependency low. Say if they are changing location of a clerk, in first case they would need to tell hundreds of users about the change so that they can go to correct location, but in second case they are only telling the receptionist.

In above solution we are following façade pattern (to solve a real life problem). In software term the same stands true. Say you are interacting with this college system, earlier you were to fetch data from multiple classes which can be done from one façade class. Secondly maintenance is easy as background changes (change clerk location) will need a change only in façade class and no change for users using the API.

Case Original:

GeneralInfo generalInfo=new GeneralInfo();

GeneralInfo.fillInfo(MyDetails);

CourseInfo courseInfo=new CourseInfo();

CourseInfo.fillInfo(MyDetails);

Fees fees=new Fees();

MyDetails.receipt=fees.fillFees(MyDetails);

MoreInfo moreInfo=new MoreInfo();

enrollmentNumber= moreInfo.finallyFill(MyDetails);

Case Facade Pattern:

CollegeFacade collegeFacade=new CollegeFacade();

enrollmentNumber=collegeFacade.fillInfo(MyDetails);

Facade Pattern

Using hosts file to redirect URLs locally

There are times when you want to access machines on LAN or internet using the IP address. Hosts files in various systems can help you auto redirect logical names to the IP address. For example

127.0.0.1 mysite.com

Now whenever you will call mysite.com, you will be pointing to localhost. Similarly you can use any IP address on LAN or Internet and redirect logical names to them.

More on usage of hosts file : en.wikipedia.org/wiki/Hosts_(file)