Joshua Java

Archive for the ‘Codevaganza’ Category

Yesterday we were evaluating GWT to be used as a wrapper for the GMap API. And one thing we need in our apps is to be able to drop marker and follow where the last marker is dropped.
Read the rest of this entry »

Tags:

Okay, so here’s the problem what I faced recently. I’m doing a research on using GWT because we’re currently evaluating whether or not to use GWT in the future. I am trying to use GWT and stuck on using the Hyperlink widget. There were two problems that I faced:

  1. First I need to figure out how does Hyperlink changed the content of our view. Well in traditional web framework, you just fire out the page file with the href attribute. This is not the case with GWT. Because everything is done in Asynchronous way. Gee weez I always had problem thinking in Asynchronous way of thinking.
  2. Okay, when I have solved that, I came into the next problem. When user clicked the link and user hit the back button it should redirect me to the previous content. Remember when I said that with GWT you don’t actually fire out the file name?
     

Now you must be saying: “Yeach mate, there’s already an example in the GWT’s Showcase example complete with the source code.” Okay you’re right, but the problem is, the code is too complex, there’s already custom code in it which makes it really difficult to understand the niche of GWT itself. “But mate, there are already numerous blog entry that wrote about this”. Well all of them only writes on how to display the historyToken, but this is not what we’ll do in real application.
Read the rest of this entry »

In SQL Server 2000 you can not use the AVG() function on a datetime datatype field. So how do you get the average for a datetime datatype field?
This is the nasty way to do it:
convert(varchar, convert(datetime, avg(convert(real, end_time - start_time))), 108 ) avg_call_length

  1. In the example I subtracted two fields: start_time and end_time to get the length of how long user is using the system. You probably don’t need these part. If you don’t need it, you simply just put a datetime field in there instead.
  2. Now before using the avg() function I must convert the datetime values into real datatype.
  3. After having the averay in real datatype values, I would convert it back to the datetime datatype.
  4. You’re done upto this part if you’re happy with the datetime datatype. Unfortunately, the application require a time format to be displayed instead. SQL Server 2000 also don’t have the time datatype. So to get a time format I must convert it to a varchar datatype and then use the 108 type to get the time format values.

If you think you’ve got a better way to do this, please feel free to share with me. 🙂

Recently I’ve got a table with a field that the values needed to have an extra character infront of it. Each record must have extra zero infront of it and in the end the total character must be 10 characters long. So if I have a record with code: 12345, it should be transformed to 0000012345. In popular databases like PostgreSQL, MySQL or Oracle you would just call the LPAD() function. But unfortunately SQL Server 2000 hasn’t got it. Hopefully they already have it the latest version of SQL Server.

I couldn’t really find an elegant way to do this in SQL Server and so this is what I came up with:

select reverse(cast( reverse( cast(code as varchar) ) + '0000000000' as char(10) )) as productcode from product

  1. The first thing I did was to cast the record values as varchar using the cast() function to make sure that the data is in character data type.
  2. The next thing I did was to reverse the record values with the reverse() function.
  3. Then I add the extra zero and the end of the reversed record values.
  4. Then I cast the record as char(10) which would trim the un-needed zero behind the record values.
  5. After that I reverse the record values back with the reverse() function.

It’s a bit stupid, so if you think you’ve got a better way to do it in SQL Server, please feel free to share this with me. 🙂

Recently I’ve got a task to generate a random PIN number. Now the PIN must be in 6 digit number. Here’s the number generator:

public class RandomGenerator {
    private static final Logger logger = LoggerFactory.getLogger(RandomGenerator.class);

    final static int [] sizeTable = { 0, 9, 99, 999, 9999, 99999, 999999, 9999999,
                                      99999999, 999999999, Integer.MAX_VALUE };

    private static Random random = new Random();

    public static int generate(int digit){
        int highest = sizeTable[digit] + 1;
        int lowest = sizeTable[digit -1] + 1;

        int generated = random.nextInt(highest);

        logger.debug(“Entry {}”, generated);

        if( generated < lowest )             generated = generate(digit);         return generated;     } } [/sourcecode] And to use it is as follows: [sourcecode language='java'] public class TestRandom {     private static final Logger logger = LoggerFactory.getLogger(TestRandom.class);     @Test public void random(){         logger.debug("Random {}", RandomGenerator.generate(6));     } } [/sourcecode] The parameter is the number of digit of number I want to generate and the maximum digit number is 9.

Tags:

Wow, I just realized that I haven’t wrote anything in this blog for more than a month. Shame on me. Ok here’s another code that hopefully be beneficial. Recently I had a project where I need to display the enum values in local language. Thanks God for Seam I can easily do this.
In your model class (e.g Person.java)

public class Person {

	public enum Gender {
 	MALE,
 	FEMALE;

		@Override
 	public String toString() {
 		switch (this){
 			case MALE: return ResourceBundle.instance().getString("male");
 			case FEMALE: return ResourceBundle.instance().getString("female");
 			default: return super.toString();
 		}
 	}

	private Gender gender;
 public Gender getGender() {
 	return gender;
 }

	public void setGender(Gender gender) {
 	this.gender = gender;
 }
 };

I had this inner enum inside a Person class and in there I just override toString() method. Inside the method I just call the Seam’s ResourceBundle component and call the appropriate message from my resource bundles which is located in messages.properties.Now when I call the gender property from my view, it will be automatically localized.

        <h:panelGrid columns="2" columnClasses="form-column"
            rowClasses="form-row" styleClass="form-table">

            <h:outputLabel for="gender" value="#{messages&#91;'gender'&#93;}" />
            <h:outputText id="gender" value="#{client.gender}" />

        </h:panelGrid>

Recently on my project I’ve got an assignment at my office to have a function that will focus to first enabled field on an HTML form. I will post it here for my own archive, just in case I need it some other time :P. I hope it will be useful for you too.

Read the rest of this entry »