martedì 29 gennaio 2008

ordinare una collection @ManyToOne

Ecco l'annotazione di Hibernate (non EJB3 compliant) per ordinare una lista collegata ad un oggetto:
@OrderBy

Ad esempio:
@Entity
public class City {

private List streets;
......


@OneToMany(mappedBy="city")
@OrderBy("streetName")
public List getStreets() {
return streets;
}
...
}

domenica 27 gennaio 2008

Apache Lucene...quanta poca documentazione in giro!

Alcuni link utili:

http://www.bitworm.com/search/2007/simple-lucene-example/
http://www.chorrosoftware.com/docs/A%20Quick%20and%20Dirty%20Lucene%20HOWTO.pdf
http://www.darksleep.com/lucene/

L'immancabile Manning:
http://www.manning.com/hatcher2/

come aggiungere proprietà a @Id

@Id
@Column(name="PARTID", primaryKey=true, nullable=false, updatable=false, insertable=false)

tratto da:
EJB 3 Annotations for mapping composite primary keys that are part of foreign key

http://www.jroller.com/raghukodali/entry/ejb_3_annotations_for_mapping

sabato 26 gennaio 2008

Leggere o scrivere foglie di Excel

There are many solutions to read or write Excel spreadsheets from Java. This HowTo is only about OpenSource (and free) solutions.

JDBC-ODBC Excel driver

This solution lets you access your Excel worksheet with SQL SELECT statement. The required ODBC driver is included in a regular Windows installation and the JDBC-ODBC bridge is used to access the Excel DSN.

See this HowTo for an example.

JExcel

Java Excel API is a java API enabling developers to read, write, and modify Excel spreadsheets dynamically. Any operating system which can run a Java virtual machine can both process and deliver Excel spreadsheets. One nice thing about JExcelApi is that it has no dependencies on any third party libraries.

See http://jexcelapi.sourceforge.net/

POI

The POI project consists of APIs for manipulating various file formats based upon Microsoft's OLE 2 Compound Document format using pure Java. POI is your Java Excel solution as well as your Java Word solution.

HSSF is the POI Project's pure Java implementation of the Excel '97(-2002) file format and it provides a way to read spreadsheets create, modify, read and write XLS spreadsheets.

Since it's Jakarta project, POI has a dependencies with other JARs (commons,log4j,etc...).



The name was originally an acronym for "Poor Obfuscation Implementation" (ref: Wikipedia).

See http://jakarta.apache.org/poi/

JXLS

jXLS is a project that allows creation of extremely complex Excel reports just in several lines of code. It is based on Jakarta POI.

With jXLS, all you need is to create XLS template file with all required formatting, formulas etc using specific notation to indicate placement of data and then write a couple lines of code to invoke jXLS engine passing XLS template and the exported data as parameters.

See http://jxls.sourceforge.net/

xlSQL

xlSQL is a JDBC Driver for Excel and CSV data sources. Documents can be read and written with SQL as if they were tables in a database.

You can export XLS to XML or SQL INSERT statements. xlSQL includes its own "zero-admin" mySQL database. The documentation is minimal at this time.

See http://xlsql.sourceforge.net/

JCOM

JCOM is a Java to COM bridge library. With JCOM you can call a COM object from Java as if it were a Java object without having to deal with the internals of JNI. The documentation is minimal (in Japanese!).

See http://sourceforge.net/projects/jcom

See also this HowTo for an alternative package to access a COM package from Java.

OpenXLS Java Spreadsheet SDK

OpenXLS claims that it has the best compatibility with complex Excel files and able to handle any kind of Excel file out there without corrupting it. This open source effort is the result of over 6 years of development into it.

See http://www.extentech.com/estore/product_detail.jsp?product_group_id=228

See also this HowTo for a way to create a simple XLS without any additional library.

Quando la data non si comporta come ti aspetti!

When displaying dates, the java.util package automatically adjusts for the local timezone. This can cause problems when displaying dates within an application, as the dates look as if they are exactly one day previous to that which is stored in the Excel spreadsheet, although this is not in fact the case.

Excel stores dates as a numerical value, and the conversion process for transforming this into a java.util.Date consists of converting the Excel number into a UTC value and then using the UTC number to create the java Date. Say the number in Excel represents 20 August 2003, then the UTC equivalent of this number is used to create a java.util.Date object.

The problem occurs if you are operating in a timezone other than GMT. As soon as you try and perform any user IO on that java Date object (eg. System.out.print(date)) the JVM will perform timezone adjustment calculations. If you are located in EST zone (which is GMT - 5 hours) java will subtract 5 hours from the date - so the Date object instead of being 00:00 20/08/2003 becomes 19:00 19/08/2003. Because java recognizes you only want a date and not a date time, it truncates the hours/minutes/seconds and presents 19/08/2003 - so it appears that the day is one day less than was stored in Excel, whereas it is really only a few hours (the timezone offset) less. Needless to say, this is a very annoying feature.

The easiest way to work around this (and the method used internally by the getContents() method of a jxl.DateCell) is to force the timezone of the date format as follows:
TimeZone gmtZone = TimeZone.getTimeZone("GMT");
SimpleDateFormat format = new SimpleDateFormat("dd MMM yyyy");
format.setTimeZone(gmtZone);

DateCell dateCell = ....
String dateString = format.format(dateCell.getDate());