Saturday, July 04, 2009

Woodstock Dropdown List Binding Examples

I am spending the morning answering a number of questions posted to the nbj2ee list. One of the questions was how to add separators to to drop down lists. I decided to write a little code example to show how this is done.

The idea is to retrieve the data values from a database. In this case, I use a built in travel database from NetBeans.

There are two examples. The first one is a simple database binding, and the second uses the Option class to control the output.

The Option class allows us to insert any "filler" into the items we display in the list. Here is the code.
    public Option[] getOptions() {
List<Option> options = new ArrayList<Option>();
personDataProvider.cursorFirst();
Option firstOption = new Option(personDataProvider.getValue("PERSON.PERSONID"), (String) personDataProvider.getValue("PERSON.NAME"));
options.add(firstOption);
Option filler = new Option(99999, "----------");
options.add(filler);
while (personDataProvider.cursorNext()) {
Option option = new Option(personDataProvider.getValue("PERSON.PERSONID"), (String) personDataProvider.getValue("PERSON.NAME"));
options.add(option);
}
return options.toArray(new Option[options.size()]);
}
Here is the source code DropDownListExample. The example was created using NetBeans 6.5.1 and Visual JSF plugin.

Wednesday, June 10, 2009

Suggestions for IT Professionals in a Down Economy

I just read an article from Computer World about how to remain positive in a down economy. I found the article to be spot on.

The first item was going back to your roots and doing the things that attracted you to a career in IT. I agree. If you are not doing something you love now, go back and reflect on why. Perhaps it is learning something new, or fixing something old. Get the flame back.

The second item referred to social networking. I am on Linked-In, Twitter, Facebook, Plaxo, etc. I know a number of people who have made this their personal addictions. I have not become addicted to it, but I find a lot of value. The ability to connect with friends, colleagues (current and former), peers, and associates is truly satisfying. It can help you in many ways, as long as, you are careful not to post anything that you would not be proud to show your mother.

They mention getting out into the real world. I would agree. I recommend joining your local user groups (Java Users Group), and attend free conferences like the Southeast Linux Fest. These groups are your peers, and contacts to potential jobs. Conferences can enhance your technical skills, and allow you to meet like minded individuals. It also can expose you to new technologies to make your IT life better.

The article also mentions getting technical certifications. I could not agree more. Here again, your local technical user group may be able to help by offering courses, certification boot camps, or technical deep dives. You can also enhance your knowledge by offering to give talks. You will really learn how much of a subject matter expert you are based on the questions the attendees ask.

Monday, May 04, 2009

How to Convert a File to a Byte Array

I wrote an EJB which performs faxing using gnu-hylafax for Java. The trick is that I needed to convert the file to bytes[] to pass to the EJB. Here is how I did it, but it should work for most files.


/**
* Converts a file into a byte[] array.
* @param file file to be converted
* @return file converted to {@code byte[]}
* @throws java.io.IOException
*/
public static byte[] getBytesFromFile(final File file) throws IOException {
InputStream is = new FileInputStream(file);

// Get the size of the file
long length = file.length();

if (length > Integer.MAX_VALUE) {
/* File is too large. If we are trying to determine the length of the byte[]
* array, it can only return an int, so we are limited by the file size.
*/
throw new IOException(file.getName() + " is too large.");
}

// Create the byte array to hold the data
byte[] data = new byte[(int) length];

// Read in the bytes
int offset = 0;
int bytesRead = 0;
while (offset < data.length &&
(bytesRead = is.read(data, offset, data.length - offset)) >= 0) {
offset += bytesRead;
}

// Ensure all the bytes have been read in
if (offset < data.length) {
throw new IOException("An exception occurred while trying to read + "
+ file.getName() + " into the byte array.");
}

// Close the input stream and return bytes
is.close();
return data;
}
}

Popular Posts