Wednesday, April 21, 2010

Generating Random numbers in a SELECT statement using MS SQL Server

Generating random numbers in SQL statement is not as trivial as it may look. Simply invoking the rand() method in a query will not work. For example, if you have a table with 1000 rows, doing:

select rand() from my_table

Will not work.

The result of this query will be 1000 rows of the same random number. MS SQL optimizer evaluates the rand() function only once.

In order to override this problem, we can do the following steps:

1) Create a view named: random_view that selects rand() function:

create VIEW [dbo].[random_view]
AS
SELECT rand() 'rnd'   


2) Create a function that makes a select on the random_view:

CREATE FUNCTION [dbo].[random]()
RETURNS float
AS
BEGIN
return (select rnd from random_view)
END


Now we can use our new generated function in a any query:

select dbo.random() from my_table

Assuming my_table has has 1000 rows, we will get 1000 different random numbers.

Tuesday, April 20, 2010

Unable to delete files in Windows Vista/Windows 7

If you have reinstalled windows machine, without entirely erasing your disk you have probably encountered a problem in which you are unable to delete files or directories. These files belonged to the older windows machine (the previous user(s) of that machine), and therefore, your new operating system will allow you to access these files, but not delete them.

In order to delete these files, we first have to take control over them. This can be done by using 2 command line utilities. First open a command line as Administrator. This can be pressing the “Start” button and writing “cmd” on the search edit box. When the “cmd.exe” file is shown, right click on it and choose: “Run as administrator”. Another way of doing it, is writing “cmd” on the search box, and then pressing “Ctrl + Shift + Enter” instead of Enter.

After the the command line is open as administrator, we first run this command on the file we would like to delete:
takeown /f "file_name" /d y

For example:
takeown /f "c:\some dir\some undeletable file" /d y

If we would like to delete a directory instead of a file, we can use this command, which runs recursively on all the sub directories:
takeown /f "directory_name" /r /d y

For example:
takeown /f "c:\some undeletable directory" /r /d y

After running this command we should run this command on a single file:
icacls "file_name" /grant administrators:F

For example:
icacls "c:\some dir\some undeletable file" /grant administrators:F

And for a directory and it’s sub directories:
icacls "c:\some undeletable directory" /grant administrators:F /t

After running these 2 commands, simply delete the file or directory using the Windows explorer.

Sunday, April 11, 2010

Access Linux file system from Windows

Few months ago I had a server running Linux. It was running Apache Server connected with 2 Tomcat servers and mySQL database server. Each Tomcat was running several web applications. Everything was running smoothly, until one day I made some mistake and damaged the operating system to a point it was no longer running.


All my efforts to repair the problem were failed. Since my knowledge in Linux is not so good as my knowledge in Windows, I made a decision to move to Windows 2008 Server. But, unfortunately I needed lots of the data that was on my Linux machine. Since I was unable to repair the Linux machine, I decided to look for a way of a accessing directly to my Linux file system from Windows.


Removing the disk from the Linux machine and putting it on my Windows machine was an easy task. Finding a tool that allows access to my Linux file system was a real challenge. I downloaded several tools from the web and had no luck accessing my Linux file system with them. The tools simply didn’t work.


Finally after doing quite a search, I came across this project: http://www.ext2fsd.com/ which offers a software named: Ext2Fsd. This software is not so user friendly, and it took me quite a while understanding how to define a drive that will point to my Linux file system. But after connecting my Linux file system with a drive, I was able to see my entire Linux file system through Windows. I simply made a copy & paste to all the files I needed, and after few minutes I was able to save all my lost data.

Wednesday, March 24, 2010

Email validation in Java

You can write your own code to validate email, but why bother if you have it ready. The Apache Commons project has a sub project called: Commons Validator. This project contains all sort of validation methods. One of the validations is used for email. In order to check if email address is valid all you have to do is:
EmailValidator.getInstance().isValid("some.mail.address@gmail.com");

Make sure to add the Commons Validator jar file and it's dependencies to your project.

Sunday, March 21, 2010

Java interview question: Check array for duplicate values in one pass

This question should be asked more precisely: How can array of size N filled with numbers up to N (0 to N – 1) be scanned for duplicate values (or more) with one scan (O(n))?

The answer lies in the body of the question: The fact that the array is filled with numbers up to N only, implies that a helper array of size N can be used to store the number of times each number appears in the first array. The value of the number in the first array is used as an index in the helper array. For example, if the first value in the array is: 5, then the 5th element in the helper array is incremented by 1. Of course, before incrementing the value in the helper array, we check if it is larger than 0. If it is larger than 0, the value of the first array was already counted once, and this current value is a duplicate.

The code of such method is very simple, lets have a look:

public class ArrayDuplicates {
  public static boolean isDuplicate(int[] arr)
  {
    int size = arr.length;
    int[] helperArr = new int[size];
    for (int i = 0; i < size; i++)
    {
      if (helperArr[arr[i]]++ > 0)
      {
        return true;
      }
    }
    return false;
  }
  public static void main(String[] args)
  {
    System.out.println(isDuplicate(new int[] { 2, 1, 0 }));
    System.out.println(isDuplicate(new int[] { 2, 0, 2 }));
  }
}


Note, that incrementing the index of the helper array (according to the value from the first array) and checking if we have already counted this number is all done the “if” sentence.

Friday, March 19, 2010

Java interview question: List “contains” method

This question verifies that you know how the “contains” method of List works.
Let’s have a look at this small piece of code:
Person person = new Person("guy");
List<Person> persons = new ArrayList<Person>();
persons.add(person);
System.out.println(persons.contains(person));

The question: Is it possible, that the output of this little piece of code returns: “false”.

At first look, the answer seems to be: NO, meaning, this code will always return “true”. So how can a “Person” object that was just created and added to a list, not be contained in it?

The answer, is in the way the “contains” method works. The “contains” method, is using the “equals” method in order to check if an object is contained in the list. Therefore, the code above can return “false” if the writer of the “Person” object overridden the equals method.

Java interview question: Count the number of “1” bits in a byte

Frankly, I think it is quite an idiotic question. Not because it is too easy or too hard question, but because I think this question doesn’t really imply on the practical programming capabilities of the person being interviewed. Anyway, a friend of mine was asked to solve this question, so I though it might be an interest for more developers looking for a job and having to handle all sort of weird questions.
In general, there are 2 solutions for this question:
  • First solution is iterating 8 times (a byte contains 8 bits). Each time the right most bit is extracted from the byte. and then the byte is shifted right one bit.
  • Second solution, is a little bit less straight forward, but more efficient: Divide the number by 2, if there is a modulo, count it. Repeat this operation as long as the number we are dividing is greater than zero.
Here is how the first solution looks in Java code:
public class GetBits {
public static int countBits(byte num)
{
  int count = 0;
  for (int i = 0; i < 8; i++)
{
    if ((num & 1) == 1) // check if right most bit is 1
{
count++;
}
num = (byte)(num >>> 1); // shit right 1 bit, including the sign bit
}
  return count;    
}


Here is how the second solution looks like:

public static int countBits(byte num)
{
  int count = 0;
  while (num > 0)
{
    if (num % 2 == 1) // check if number have modulo
{
count++;
}
num /= 2; // divide the number by 2
}
  return count;    
}

Note, that the first solution is a bit less efficient, because it always iterates 8 times. But, it handles both positive and negative values. The second solution will simply won’t work with negative numbers.

Monday, March 15, 2010

Excel Pivot Tables and Dynamic Range

Excel Pivot Table is a great tool for viewing and aggregating data. The data shown in a Pivot Table is taken from some range in one of the sheets of the excel file. The problem with Pivot Tables, is when you add new data. After refreshing the Pivot Table the new data is not shown. To solve this issue we can use a dynamic range. A dynamic range knows to grow automatically according to the rows we add. Suppose we have an excel file that contains 2 sheets: “data” and “pivot table”. We would like to create from the raw data in the sheet: “data” a dynamic range. This will allow us to add more rows to the “data” sheet, and just refresh the pivot table to see our new rows.
This is how our “data” sheet looks like:
1_pivot_data

And this is how our “pivot_table” sheet looks like:


2_pivot_table

In order to create a dynamic range from our raw data, we should go to the “Formulas” tab on excel and choose the “Define Name” option. A pop up window will be opened. In the window we should name the range. For example: “pivot_data_source”. In the “Refers to” field we define the dynamic range. The value that should be put there look like:
=OFFSET(data!$A$1,0,0, COUNTA(data!$A:$A), COUNTA(data!$1:$1))

Where the “data” is the name of the sheet in which the dynamic range is located.
The popup window looks like this:

image

After defining the dynamic data source, we should connect the pivot table with it. We do it by standing on the pivot table and selecting the “Options” tab from the main menu and choosing the “Change Data Source” option. A popup window is opened. In the field: “Table/Range” we should put the name of the dynamic range we defined: “pivot_data_source”.
The popup windows looks like:

5_pivot_data_source_change

Finally we would like the pivot table to be automatically refreshed every time our excel file is being opened. We do it by standing on the pivot table, then selecting “PivotTable Options…”. A popup window will be opened. On the “Data” tab we should check the option: “Refresh data when opening the file”. The popup window looks like:

7_pivot_table_refresh_on_load

Note that if you add new rows to the raw data, and you immediately want to see the changes in the pivot table, you should right click on the pivot table and choose the option “Refresh”. Press here to see an example of an excel file with pivot table and dynamic range defined.

Wednesday, March 3, 2010

Seam and Quartz integration

Seam is a great framework for easily building Internet applications in Java. It integrates with lots of well known existing java technologies and serves as a complement to JSF (Java Server Faces).
Quartz is a job scheduling framework allowing to easily add the capability of running scheduled jobs in your applications.
Quartz can be easily integrated with Seam. By integrating both technologies you will be able to enjoy Seam approach to managing beans on your web application.
Let’s take a look at a simple example:
First make sure you have all the jars needed to run Quartz and Seam.
Then go to your Seamcomponents.xml” file and add these lines:
<async:quartz-dispatcher/>
  <event type="org.jboss.seam.postInitialization">
  <action execute="#{quartzController.scheduleTimer}"/>
</event>
Now, put in your sources root directory (usually the “src”) the file: “seam.quartz.properties” file. A typical file may look like this:
org.quartz.scheduler.instanceName = Sched1
org.quartz.scheduler.instanceId = 1
org.quartz.scheduler.rmi.export = false
org.quartz.scheduler.rmi.proxy = false
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 3
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
Now, to the Java code. The code is constructed from 2 Seam beans. The first bean contains the Quartz trigger class. I looks like:
package com.bashan.blog.job;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.async.QuartzTriggerHandle;
import java.io.Serializable;
import java.util.Date;
/**
* @author Bashan
*/
@Name("quartzController")
@AutoCreate
public class QuartzController implements Serializable {
@In
ScheduleProcessor processor;
private QuartzTriggerHandle quartzTriggerHandleDoJob;
public void scheduleTimer() {
quartzTriggerHandleDoJob = processor.doJob(new Date(), "0 0/1 * * * ?");
}
}
Note, that when calling to “doJob” method, a cron expression is passed. This expression describes the frequency in which the job will run (event minute in the example).

The second class contains the actual task we would like to do:
package com.bashan.blog.job;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.async.Asynchronous;
import org.jboss.seam.annotations.async.Expiration;
import org.jboss.seam.annotations.async.IntervalCron;
import org.jboss.seam.async.QuartzTriggerHandle;
import org.jboss.seam.log.Log;
import java.util.Date;
/**
* @author Bashan
*/
@Name("processor")
@AutoCreate
@Scope(ScopeType.APPLICATION)
public class ScheduleProcessor {
 
  @Logger
  static Log log;
 
  @Asynchronous
  public QuartzTriggerHandle doJob(@Expiration Date when, @IntervalCron String interval) {
    System.out.println("I am a Quartz job.");
    log.debug("Pass some information to the log");
    return null;
  }
}
The method “doJob” is a Seam asynchronous method. This method will be executed according to the data of the cron expression given in the class: QuartzController.

Sunday, February 28, 2010

Fixing Facelets/XHTML corrupted page issues in Google Chrome

I had a problem for quite some time on a web site being shown corrupted in Google Chrome. On the original HTML page everything was working great. On the resulted JSF/Seam/Facelets produced page, things were looking corrupted. The problem appeared only on Google Chrome. In other web browsers things look good. When I made a diff on the original working page and the page generated by JSF/Seam/Facelets, there was no difference. But still, page was corrupted on Google Chrome. After some investigation I noticed that Google Chrome was showing the page corrupted, simply because the generated page was not setting content type of: text/html. Google Chrome, unlike the other browsers, was probably sensitive to this issue. The solution to this problem is very simple. Just add to your JSF view tag the property: contentType. If your IDE (for example, IntelliJ) marks the contentType as unrecognized, ignore it. It runs ok. Here is an example of how the contentType is used:
<f:view contentType="text/html" />
Note, that you don’t have to wrap the view tag on all of your page, if you are using Facelets (the view tag is not mandatory in Facelets), so just putting the view tag is enough (like in the example).