Monday, September 14, 2009

Future Yard Announces Android Auto Reply Application -- FY Receptionist

Dear Android Community,
I am pleased to announce that the FY Receptionist V1.0 release is now available in Android Market.
Please visit https://sites.google.com/site/futureyard/products/fy-receptionist to view the detail information.
If you want to download this app, go to the market on your GPhone, choose 'tools' category, and enjoy it!

Who have benefits from using this applicatioin?
  • Anyone who wants to auto reply any or some missed calls.
Features We Implemented
  • Different auto reply mode.
  • Auto answers all cell phone calls or just answer calls from some contacts.
  • Custom answering message.
  • I18N support.
Features Coming Soon...
  • Better UI experience.
  • Any suggestion from you.

Thursday, August 20, 2009

Future Yard Announces Android GPS & Map Application -- FY GPS Tracker

Dear Android Community,
I am pleased to announce that the FY Tracker V1.0 release is now available in Android Market.
Please visit https://sites.google.com/site/futureyard/products/fy-tracker to view the detail information.
If you want to download this app, go to the market on your GPhone, choose 'tools' category, and enjoy it!

Who have benefits from using this applicatioin?
  • Those who like hiking, biking, camping, workout...
  • Those who need to be cared or inspected...
  • Those who are negligent or scrupulous.
  • Those who like or need team play...
Features We Implemented
  • I18N support.
  • Track the GPS route and send to Google Clouds.
  • Tag the start point and stop point.
  • Follow the route on the map with compass on your phone.
  • Integared with weather report.
  • Display the detail location information such as speed, altitude, accuracy, bearing...
  • Long press the information text and copy/paste it to anywhere you want, SMS, EMail, Chat for instance.
  • Different unit for the convenient.
  • Change map mode via shaking.
  • Share with the friends via SMS, EMail on real-time.
  • Speeding alarm.
  • Emergent mode managed by Google Clouds.
  • Statistics & Analysis with Google FY CRM Console Clouds.
  • Integated with Future Yard product series.
Features Coming Soon...
  • Much mode support.
  • Much alarm support.
  • Real-time tracking on Google Clouds.
  • Better UI experience.
  • Any suggestion from you.

Saturday, August 1, 2009

Future Yard Announces Android Contacts & Logs Application -- FY Contacts & Logs Sync

Dear Android Community,
I am pleased to announce that the FY Sync V1.0 release is now available in Android Market.
Please visit https://sites.google.com/site/futureyard/products/fy-sync to view the detail information.
If you want to download this app, go to market on your GPhone, choose 'tools' category, and enjoy it!

Who have benefits from using this applicatioin?
  • Anyone.
Features We Implemented
  • Backup contact to google clouds
  • Restore contact from google clouds
  • Backup call logs to google clouds
  • Backup call logs to google clouds
  • Backup & restore automatically.
  • Share the contacts with friend via sms, email...
  • Share the backup items with friend via sms, email...
  • I18N support.
  • Add contact information in google clouds and restore to the phone.
  • Edit contact information in goole clouds and restore to the phone.
  • Delete contact information in google clouds and restore to the phone.
  • Statistic for contacts.
  • Statistic for call logs.
  • Statistic for sms logs.
  • System security is handled by google gmail account and google clouds.
Features Coming Soon...
  • Backup to email.
  • Bakcup bookmarks, mms, application data...
  • More restoring functionality.
  • Import & export.
  • More statistic chart.
  • Better UI experience.
  • Any suggestion from you.

Wednesday, May 27, 2009

HTTP GET and POST Thread Service on Android

HttpService.java
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;

public class HttpService implements Runnable {

private static final String TAG = "HttpService";
public static final int GET = 0;
public static final int POST = 1;
public static final String HTTP_RESPONSE = "HTTP_RESPONSE";

private Activity activity;
private Intent intent;
private int requestCode;
private String url;
private int getOrPost = 0;
private List<NameValuePair> nameValuePairs;
private boolean handleByMe;

/*
* for example:
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("aaa", "bbb"));
nameValuePairs.add(new BasicNameValuePair("ccc", "ddd"));
*/
public HttpService(Activity activity, Intent intent, int requestCode, String url, int getOrPost, List<NameValuePair> nameValuePairs, boolean handleByMe){
this.activity = activity;
this.intent = intent;
this.requestCode = requestCode;
this.url = url;
this.getOrPost = getOrPost;
this.nameValuePairs = nameValuePairs;
this.handleByMe = handleByMe;
}

public void start(){
Thread thread = new Thread(this);
try{
thread.start();
}catch(IllegalThreadStateException itse){
Log.e(TAG, "The Thread has been started before.", itse);
}
}

public void run() {
doRequest();
}

public void doRequest(){
HttpClient httpclient = new DefaultHttpClient();
HttpRequestBase httpRequest = null;
HttpResponse httpResponse = null;
InputStream inputStream = null;
String response = "";
StringBuffer buffer = new StringBuffer();

if(POST == getOrPost){
httpRequest = new HttpPost(url);
try {
((HttpPost)httpRequest).setEntity(new UrlEncodedFormEntity(nameValuePairs));
} catch (UnsupportedEncodingException usee) {
Log.e(TAG, "Could not encode the nameVaulePairs.", usee);
}
}else{
httpRequest = new HttpGet(url);
}
if(httpRequest != null){
try{
httpResponse = httpclient.execute(httpRequest);
inputStream = httpResponse.getEntity().getContent();
int contentLength = (int) httpResponse.getEntity().getContentLength();
if (contentLength < 0){
Log.e(TAG, "The HTTP response is too long.");
}
byte[] data = new byte[256];
int len = 0;
while (-1 != (len = inputStream.read(data)) )
{
buffer.append(new String(data, 0, len));
}
inputStream.close();
}catch (ClientProtocolException cpe) {
Log.e(TAG, "Http protocol error occured.", cpe);
}catch (IllegalStateException ise) {
Log.e(TAG, "Could not get a HTTP response from the server.", ise);
}catch (IOException ioe) {
Log.e(TAG, "Could not establish a HTTP connection to the server or could not get a response properly from the server.", ioe);
}
}
response = buffer.toString();
intent.putExtra(HTTP_RESPONSE, response);

if(handleByMe){
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
activity.startActivity(intent);
}else{
activity.startActivityForResult(intent, requestCode);
}
}

}



How to use it?
Intent intent = new Intent(this, SkeletonActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
HttpService httpService = new HttpService(this, intent, 0, "http://www.google.com", 0, null, true);
httpService.start();


protected void onNewIntent(Intent intent) {
String message = intent.getStringExtra(HttpService.HTTP_RESPONSE);
editor.setText(message);
}

Tuesday, May 26, 2009

GenericDAO for Android SQLite

GenericDAO.java
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class GenericDAO extends SQLiteOpenHelper {

private static final String TAG = "GenericDAO";
private static SQLiteDatabase db;
private static String dName;
private static String tName;
private static String sql;
public static final String KEY_ID = "_id";

private static GenericDAO instance;

private GenericDAO(Context ctx, String dbName, String sql, String tableName, int ver){
super(ctx, dbName, null, ver);
Log.i(TAG, "Creating or opening database [ " + dbName + " ].");
GenericDAO.sql = sql;
dName = dbName;
tName = tableName;
}

public static GenericDAO getInstance(Context ctx, String dbName, String sql, String tableName, int ver){
if(instance == null){
instance = new GenericDAO(ctx, dbName, sql, tableName, ver);
try{
Log.i(TAG, "Creating or opening the database [ " + dbName + " ].");
db = instance.getWritableDatabase();
}catch(SQLiteException se){
Log.e(TAG, "Cound not create and/or open the database [ " + dbName + " ] that will be used for reading and writing.", se);
}
}
return instance;
}

public void close(){
if(instance != null){
Log.i(TAG, "Closing the database [ " + dName + " ].");
db.close();
instance = null;
}
}

@Override
public void onCreate(SQLiteDatabase db){
Log.i(TAG, "Trying to create database table if it isn't existed [ " + sql + " ].");
try{
db.execSQL(sql);
}catch(SQLException se){
Log.e(TAG, "Cound not create the database table according to the SQL statement [ " + sql + " ].", se);
}
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
Log.i(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data");
try{
db.execSQL("DROP TABLE IF EXISTS " + tName);
}catch(SQLException se){
Log.e(TAG, "Cound not drop the database table [ " + tName + " ].", se);
}
onCreate(db);
}

public long insert(String table, ContentValues values){
return db.insert(table, null, values);
}

public Cursor get(String table, String[] columns){
return db.query(table, columns, null, null, null, null, null);
}

public Cursor get(String table, String[] columns, long id){
Cursor cursor =db.query(true, table, columns, KEY_ID + "=" + id, null, null, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
}
return cursor;
}

public int delete(String table) {
return db.delete(table, "1", null);
}

public int delete(String table, long id) {
return db.delete(table, KEY_ID + "=" + id, null);
}

public int update(String table, long id, ContentValues values) {
return db.update(table, values, KEY_ID + "=" + id, null);
}
}



How to use it?
Notes.java
public class Notes {

public static final String DATABASE_NAME = "data";
public static final String DATABASE_TABLE = "notes";
public static final int DATABASE_VERSION = 1;
public static final String TABLE_CREATE =
"create table notes (_id integer primary key autoincrement, "
+ "title text not null, body text not null);";

public static final String COL_TITLE = "title";
public static final String COL_BODY = "body";

private int id;
private String title;
private String body;

public int getId() {
return id;
}
public String getTitle() {
return title;
}
public String getBody() {
return body;
}
public void setId(int id) {
this.id = id;
}
public void setTitle(String title) {
this.title = title;
}
public void setBody(String body) {
this.body = body;
}


}



    private String testCRUD(){

String result="";
Cursor cursor = null;
String[] columns = new String[] {GenericDAO.KEY_ID, Notes.COL_TITLE, Notes.COL_BODY};

GenericDAO dao = GenericDAO.getInstance(this, Notes.DATABASE_NAME, Notes.TABLE_CREATE, Notes.DATABASE_TABLE, Notes.DATABASE_VERSION);

if(dao != null){

ContentValues values = new ContentValues();
values.put(Notes.COL_TITLE, "aaa");
values.put(Notes.COL_BODY, "bbb");
dao.insert(Notes.DATABASE_TABLE, values);

values = new ContentValues();
values.put(Notes.COL_TITLE, "ccc");
values.put(Notes.COL_BODY, "ddd");
dao.insert(Notes.DATABASE_TABLE, values);

cursor = dao.get(Notes.DATABASE_TABLE, columns);


int idColumn = cursor.getColumnIndex(GenericDAO.KEY_ID);
int titleColumn = cursor.getColumnIndex(Notes.COL_TITLE);
int bodyColumn = cursor.getColumnIndex(Notes.COL_BODY);

if(cursor != null){
if(cursor.moveToFirst()){

int count = cursor.getCount();
result = "there are " + count + " records.";
List<Notes> list = new ArrayList<Notes>();

for(int i=0; i<count; i++){

int id = cursor.getInt(idColumn);
String title = cursor.getString(titleColumn);
String body = cursor.getString(bodyColumn);

Notes notes = new Notes();
notes.setId(id);
notes.setTitle(title);
notes.setBody(body);

list.add(notes);

result += " " + i + ": " + "id=" + id + ", title=" + title + ", body=" + body + ";";

cursor.moveToNext();
}
}
}

result += " now update the second record.";

values = new ContentValues();
values.put(Notes.COL_TITLE, "eee");
values.put(Notes.COL_BODY, "fff");
dao.update(Notes.DATABASE_TABLE, 2, values);

cursor.requery();
cursor.close();

result += " now delete first record.";

dao.delete(Notes.DATABASE_TABLE, 1);

result += " now delete all records.";

dao.delete(Notes.DATABASE_TABLE);

dao.close();
}

return result;
}


Sunday, May 17, 2009

How to Fix "Failed to find an AVD compatible with target" on Android

"Failed to find an AVD compatible with target"
cd ANDROID_HOME/tools/
android create avd -n android1.1 -t 1
android create avd -n android1.5 -t 2
android create avd -n goolgeapi -t 3

Date Between Syntax Bug from MS SQL Server 2005?

1. select TradeDate from test where TradeDate between '2009-05-13 00:00:00.000' and '2009-05-13 23:59:59.999'
result:
2009-05-14 00:00:00.000
2009-05-14 00:00:00.000
2009-05-14 00:00:00.000
2009-05-14 00:00:00.000
2009-05-14 00:00:00.000
(5 row(s) affected)


2. select TradeDate from test where TradeDate between '2009-05-13 00:00:00.000' and '2009-05-13 23:59:59.998'
result:
(0 row(s) affected)


3. select TradeDate from test where TradeDate between '2009-05-13 00:00:00.000' and '2009-05-14 00:00:00.000'
result:
2009-05-14 00:00:00.000
2009-05-14 00:00:00.000
2009-05-14 00:00:00.000
2009-05-14 00:00:00.000
2009-05-14 00:00:00.000
(5 row(s) affected)

Friday, May 15, 2009

A Solution for Equals and HashCode Method of Hibernate Domain Ojbect

Issue:

Solution:
every domain object extends the following object.
public abstract class BaseObject {

private String uuid = UUID.randomUUID().toString();

public String getUuid() {
return uuid;
}

public void setUuid(String uuid) {
this.uuid = uuid;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((uuid == null) ? 0 : uuid.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final BaseObject other = (BaseObject) obj;
if (uuid == null) {
if (other.uuid != null)
return false;
} else if (!uuid.equals(other.uuid))
return false;
return true;
}
}

Thursday, May 14, 2009

Sample of Hibernate Annotation on Entity Bean Associations/Relationships

1. one-one
@Entity
public class Body {
@Id
public Long getId() { return id; }

@OneToOne(cascade = CascadeType.ALL)
@PrimaryKeyJoinColumn
public Heart getHeart() {
return heart;
}
...
}

@Entity
public class Heart {
@Id
public Long getId() { ...}
}


@Entity
public class Customer implements Serializable {
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name="passport_fk")
public Passport getPassport() {
...
}

@Entity
public class Passport implements Serializable {
@OneToOne(mappedBy = "passport")
public Customer getOwner() {
...
}


2. many-one
    @ManyToOne(fetch = FetchType.EAGER)
@org.hibernate.annotations.Cascade(value={org.hibernate.annotations.CascadeType.SAVE_UPDATE})
@JoinColumn(name = "SecurityID")
public SecurityMaster getSecurity()
{
return this.security;
}



3. one-many
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "transactionId")
@OrderBy("transactionTaxlotId asc")
public Set<TransactionTaxlot> getTransactionTaxlots() {
return transactionTaxlots;
}



4. many-many
    @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "trelTrader",
joinColumns = {
@JoinColumn(name="traderId", unique = true)
},
inverseJoinColumns = {
@JoinColumn(name="fundId")
}
)
public Set<Fund> getFunds() {
return this.funds;
}

Wednesday, May 6, 2009

Cascade Attribute in Hibernate

none
ignore the association.
save-update
navigate the association when the transaction is committed and when an object is passed to save() or update() and save newly instantiated transient instances and persist changes to detached instances.
delete
navigate the association and delete persistent instances when an object is passed to delete().
all
cascade both save-update and delete as well as calls to evict and lock.
all-delete-orphan
the same as cascade "all" but in addition deletes any persistent entity instance that has been removed (dereferenced) from the association (for example from a collection).
delete-orphan
delete any persistent entity instance that has been removed (dereferenced) from the association (for example from a collection).

Tuesday, May 5, 2009

Performance Competition Within Hibernate Query Methods

Table or Object Struct:
    private Integer    historyId;
private Integer accountId;
private Date historyDate;
private Double units;
private Double sharePrice;
private Double amount;
private Integer createId;
private Date createDate;



Rows in the Table:
40797


Performance Competition (time unit is ms) :



conclusion:

findByNamedQuery(namedQuery) > createCriteria(clazz).list() > loadAll(clazz) > createSQLQuery(sqlString).addEntity(clazz).list(), but basically they are in the one level.

Thursday, April 30, 2009

Hibernate Object States and Actions



Transient
an object is transient if it has just been instantiated using the new operator, and it is not associated with a Hibernate Session. It has no persistent representation in the database and no identifier value has been assigned. Transient instances will be destroyed by the garbage collector if the application doesn't hold a reference anymore. Use the Hibernate Session to make an object persistent (and let Hibernate take care of the SQL statements that need to be executed for this transition).

Persistent
a persistent instance has a representation in the database and an identifier value. It might just have been saved or loaded, however, it is by definition in the scope of a Session. Hibernate will detect any changes made to an object in persistent state and synchronize the state with the database when the unit of work completes. Developers don't execute manual UPDATE statements, or DELETE statements when an object should be made transient.

Detached
a detached instance is an object that has been persistent, but its Session has been closed. The reference to the object is still valid, of course, and the detached instance might even be modified in this state. A detached instance can be reattached to a new Session at a later point in time, making it (and all the modifications) persistent again. This feature enables a programming model for long running units of work that require user think-time. We call them application transactions, i.e. a unit of work from the point of view of the user.

save
stores an object into the database. That means it insert an entry if the identifier doesn't exist, else it will throw error. If the primary key already present in the table, it cannot be inserted.

update
is used for updating the object using identifier. If the identifier is missing or doesn't exist, it will throw exception.

saveOrUpdate
calls save() or update() based on the operation. If the identifier exists, it will call update method else the save method will be called.

persist
does the same like session.save(). But session.save() return Serializable object but session.persist() return void.

lock
simply reattaches the object to the session without checking or updating the database on the assumption that the database in sync with the detached object. It is the best practice to use either session.update(..) or session.saveOrUpdate(). Use session.lock() only if you are absolutely sure that the detached object is in sync with your detached object or if it does not matter because you will be overwriting all the columns that would have changed later on within the same transaction.When you reattach detached objects you need to make sure that the dependent objects are reatched as well.

get and load
Both methods create a persistent object by loading the required object from the database. But if there was not such object in the database then the method load() throws an exception whereas get() returns null.

Wednesday, April 22, 2009

How to Use Hibernate Annotation

1. applicationContext.xml
    <bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.aais.model.Transaction</value>
<value>com.aais.model.Account</value>
<value>com.aais.model.Security</value>
<value>com.aais.model.TradeType</value>
<value>com.aais.model.JournalEntry</value>
<value>com.aais.model.SecurityType</value>
</list>
</property>
...


2. SecurityType.java
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.NamedQueries;
import org.hibernate.annotations.NamedQuery;

/**
* SecurityType entity.
*/
@SuppressWarnings("serial")
@Entity
@Table(name = "security_type")
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
@NamedQueries({
@NamedQuery(name = "SecurityType.FindAll", query = "from SecurityType order by securityTypeId asc") ,
@NamedQuery(name = "SecurityType.FindById", query = "from SecurityType where securityTypeId=:securityTypeId")
})
public class SecurityType implements java.io.Serializable {

// Fields

private Integer securityTypeId;
private String name;

// Constructors

/** default constructor */
public SecurityType() {
}

// Property accessors
@Id
@Column(name = "security_type_id", unique = true, nullable = false)
public Integer getSecurityTypeId() {
return this.securityTypeId;
}

public void setSecurityTypeId(Integer securityTypeId) {
this.securityTypeId = securityTypeId;
}

@Column(name = "name", length = 128)
public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((securityTypeId == null) ? 0 : securityTypeId.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
SecurityType other = (SecurityType) obj;
if (securityTypeId == null) {
if (other.securityTypeId != null)
return false;
} else if (!securityTypeId.equals(other.securityTypeId))
return false;
return true;
}

}


3. Security.java
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

/**
* Security entity.
*/
@SuppressWarnings("serial")
@Entity
@Table(name = "security")
public class Security implements java.io.Serializable {

// Fields

private Integer securityId;
private SecurityType securityType;
private String ticker;

// Constructors

/** default constructor */
public Security() {
}

// Property accessors
@Id
@Column(name = "security_id", unique = true, nullable = false)
public Integer getSecurityId() {
return this.securityId;
}

public void setSecurityId(Integer securityId) {
this.securityId = securityId;
}

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "security_type_id")
public SecurityType getSecurityType() {
return this.securityType;
}

public void setSecurityType(SecurityType securityType) {
this.securityType = securityType;
}

@Column(name = "ticker", length = 128)
public String getTicker() {
return this.ticker;
}

public void setTicker(String ticker) {
this.ticker = ticker;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((securityId == null) ? 0 : securityId.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Security other = (Security) obj;
if (securityId == null) {
if (other.securityId != null)
return false;
} else if (!securityId.equals(other.securityId))
return false;
return true;
}

}


4. JournalEntry.java
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

/**
* JournalEntry entity.
*/
@SuppressWarnings("serial")
@Entity
@Table(name = "journal_entry")
public class JournalEntry implements java.io.Serializable {

// Fields

private Integer journalEntryId;
private Transaction transaction;
private Account account;
private Double debit;
private Double credit;

// Constructors

/** default constructor */
public JournalEntry() {
}

// Property accessors
@Id
@Column(name = "journal_entry_id", unique = true, nullable = false)
public Integer getJournalEntryId() {
return this.journalEntryId;
}

public void setJournalEntryId(Integer journalEntryId) {
this.journalEntryId = journalEntryId;
}

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "transaction_id")
public Transaction getTransaction() {
return this.transaction;
}

public void setTransaction(Transaction transaction) {
this.transaction = transaction;
}

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "account_id")
public Account getAccount() {
return this.account;
}

public void setAccount(Account account) {
this.account = account;
}

@Column(name = "debit", precision = 18, scale = 4)
public Double getDebit() {
return this.debit;
}

public void setDebit(Double debit) {
this.debit = debit;
}

@Column(name = "credit", precision = 18, scale = 4)
public Double getCredit() {
return this.credit;
}

public void setCredit(Double credit) {
this.credit = credit;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((journalEntryId == null) ? 0 : journalEntryId.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
JournalEntry other = (JournalEntry) obj;
if (journalEntryId == null) {
if (other.journalEntryId != null)
return false;
} else if (!journalEntryId.equals(other.journalEntryId))
return false;
return true;
}

}


5. Transaction.java
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;

/**
* Transaction entity.
*/
@SuppressWarnings("serial")
@Entity
@Table(name = "transaction")
public class Transaction implements java.io.Serializable {

// Fields

private Integer transactionId;
private Security security;
private TradeType tradeType;
private Double price;
private Integer quantity;
private Set<JournalEntry> journalEntries = new HashSet<JournalEntry>(0);

// Constructors

/** default constructor */
public Transaction() {
}

// Property accessors
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "transaction_id", unique = true, nullable = false)
public Integer getTransactionId() {
return this.transactionId;
}

public void setTransactionId(Integer transactionId) {
this.transactionId = transactionId;
}

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "security_id")
public Security getSecurity() {
return this.security;
}

public void setSecurity(Security security) {
this.security = security;
}

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "trade_type_id")
public TradeType getTradeType() {
return this.tradeType;
}

public void setTradeType(TradeType tradeType) {
this.tradeType = tradeType;
}

@Column(name = "price", precision = 8, scale = 4)
public Double getPrice() {
return this.price;
}

public void setPrice(Double price) {
this.price = price;
}

@Column(name = "quantity")
public Integer getQuantity() {
return this.quantity;
}

public void setQuantity(Integer quantity) {
this.quantity = quantity;
}

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "transaction")
@OrderBy("journalEntryId asc")
public Set<JournalEntry> getJournalEntries() {
return this.journalEntries;
}

public void setJournalEntries(Set<JournalEntry> journalEntries) {
this.journalEntries = journalEntries;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((transactionId == null) ? 0 : transactionId.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Transaction other = (Transaction) obj;
if (transactionId == null) {
if (other.transactionId != null)
return false;
} else if (!transactionId.equals(other.transactionId))
return false;
return true;
}

}

Friday, April 3, 2009

Generic Data Access Objects with Pagnation under Spring and Hibernate Framework

Page.java
import java.util.List;

public class Page<T> {
private List<T> rowsInPage;
private int pageSize;
private int pageNo;
private int totalRows;

/**
*
* @param pageNo
* @param pageSize
* @param rowsInPage
* @param totalRows
*/
public Page(int pageNo, int pageSize, List<T> rowsInPage, int totalRows) {
if(pageSize > 0){
this.pageNo = (pageNo > 0) ? pageNo : 1;
this.pageSize = pageSize;
this.rowsInPage = rowsInPage;
this.totalRows = totalRows;
}else if(pageSize == 0){
this.pageNo = 0;
this.pageSize = 0;
this.rowsInPage = null;
this.totalRows = 0;
}else{
this.pageNo = 1;
this.pageSize = -1;
this.rowsInPage = rowsInPage;
this.totalRows = -1;
}
}

/**
*
* @return
*/
public boolean hasNext() {
return getPages() > pageNo;
}

/**
*
* @return
*/
public boolean hasPrevious() {
return pageNo > 1;
}

/**
*
* @return
*/
public List<T> getRowsInPage() {
return rowsInPage;
}

/**
*
* @return
*/
public int getPageSize() {
return pageSize;
}

/**
*
* @return
*/
public int getPageNo() {
return pageNo;
}

/**
*
* @return
*/
public int getTotalRows() {
return totalRows;
}

/**
*
* @return
*/
public int getPages() {
if(totalRows > 0)
return (totalRows%pageSize == 0) ? totalRows / pageSize : totalRows / pageSize + 1;
else if(totalRows == 0)
return 0;
else
return 1;
}
}


GenericDAO.java
import java.io.Serializable;
import org.hibernate.criterion.Order;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.aais.core.utils.Page;

@Transactional(rollbackFor=Exception.class)
public interface GenericDAO<T, ID extends Serializable> {

/**
*
* @param id
* @param lock
* @return
*/
@Transactional(readOnly=true)
public T findById(ID id, boolean lock);

/**
*
* @param pageNo
* @param pageSize
* @param order
* @return
*/
@Transactional(readOnly=true)
public Page<T> findAll(int pageNo, int pageSize, Order order);

/**
*
* @param pageNo
* @param pageSize
* @param exampleInstance
* @param order
* @param excludeProperty
* @return
*/
@Transactional(readOnly=true)
public Page<T> findByExample(int pageNo, int pageSize, T exampleInstance, Order order, String[] excludeProperty);

/**
*
* @param pageNo
* @param pageSize
* @param queryName
* @param paramNames
* @param values
* @return
*/
@Transactional(readOnly=true)
public Page<T> findByHQL(int pageNo, int pageSize, String queryName, String[] paramNames, Object[] values);

/**
*
* @param entity
* @return
*/
@Transactional(propagation=Propagation.REQUIRED)
public T saveOrUpdate(T entity);

/**
*
* @param entity
*/
@Transactional(propagation=Propagation.REQUIRED)
public void delete(T entity);
}


GenericDAOHibernateImpl.java
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.aais.core.dao.GenericDAO;
import com.aais.core.utils.Page;

public abstract class GenericDAOHibernateImpl<T, ID extends Serializable> extends HibernateDaoSupport implements GenericDAO<T, ID> {

private Class<T> persistentClass;
private final static Log logger = LogFactory.getLog(GenericDAOHibernateImpl.class);

/**
*
*/
@SuppressWarnings("unchecked")
public GenericDAOHibernateImpl() {
this.persistentClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}

/**
*
* @return
*/
public Class<T> getPersistentClass() {
return persistentClass;
}

/**
* (pageNo > 0 && pageSize > 0) paginate
* (pageNo < 0 && pageSize < 0) don't paginate
*/
public Page<T> findAll(int pageNo, int pageSize, Order order) {
return findByCriteria(pageNo, pageSize, order, (Criterion[])null);
}

/**
* (pageNo > 0 && pageSize > 0) paginate
* (pageNo < 0 && pageSize < 0) don't paginate
*/
@SuppressWarnings("unchecked")
protected Page<T> findByCriteria(int pageNo, int pageSize, Order order, Criterion...criterion) {
Criteria crit = getCriteria();
if(criterion != null){
for (Criterion c : criterion) {
crit.add(c);
}
}
if(order != null)
crit.addOrder(order);
int rowCount = 0;
if(pageSize > 0){
rowCount = (Integer) ((Criteria) crit.setProjection(Projections.rowCount())).uniqueResult();
crit.setProjection(null);
if(pageNo < 1)
pageNo = 1;
((Criteria) crit).setFirstResult((pageNo - 1) * pageSize);
((Criteria) crit).setMaxResults(pageSize);
if(pageSize > rowCount)
pageSize = rowCount;
}
crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
List<T> list = crit.list();
return new Page<T>(pageNo, pageSize, list, rowCount);
}

/**
*
* @return
*/
protected Criteria getCriteria(){
Criteria crit = this.getSession().createCriteria(getPersistentClass());
return crit;
}

/**
* (pageNo > 0 && pageSize > 0) paginate
* (pageNo < 0 && pageSize < 0) don't paginate
*/
@SuppressWarnings("unchecked")
protected Page<T> findByCriteria(int pageNo, int pageSize, Criteria crit, Order order){
if(order != null)
crit.addOrder(order);
int rowCount = 0;
if(pageSize > 0){
rowCount = (Integer) ((Criteria) crit.setProjection(Projections.rowCount())).uniqueResult();
crit.setProjection(null);
if(pageNo < 1)
pageNo = 1;
((Criteria) crit).setFirstResult((pageNo - 1) * pageSize);
((Criteria) crit).setMaxResults(pageSize);
if(pageSize > rowCount)
pageSize = rowCount;
}
crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
List<T> list = crit.list();
return new Page<T>(pageNo, pageSize, list, rowCount);
}

/**
* (pageNo > 0 && pageSize > 0) paginate
* (pageNo < 0 && pageSize < 0) don't paginate
*/
@SuppressWarnings("unchecked")
public Page<T> findByExample(int pageNo, int pageSize, T exampleInstance, Order order, String[] excludeProperty) {
Criteria crit = getCriteria();
Example example = Example.create(exampleInstance).enableLike();
if(excludeProperty != null){
for (String exclude : excludeProperty) {
example.excludeProperty(exclude);
}
}
crit.add(example);
if(order != null)
crit.addOrder(order);
int rowCount = 0;
if(pageSize > 0){
rowCount = (Integer) ((Criteria) crit.setProjection(Projections.rowCount())).uniqueResult();
crit.setProjection(null);
if(pageNo < 1)
pageNo = 1;
((Criteria) crit).setFirstResult((pageNo - 1) * pageSize);
((Criteria) crit).setMaxResults(pageSize);
if(pageSize > rowCount)
pageSize = rowCount;
}
crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
List<T> list = crit.list();
return new Page<T>(pageNo, pageSize, list, rowCount);
}

/**
*
*/
public T findById(ID id, boolean lock) {
T entity = null;
if (lock)
entity = (T) getHibernateTemplate().get(getPersistentClass(), id, LockMode.UPGRADE);
else
entity = (T) getHibernateTemplate().get(getPersistentClass(), id);
if(entity == null){
logger.warn("Could not find out " + getPersistentClass().getName() + " instance with id=" + id);
}else{
if(logger.isDebugEnabled())
logger.debug("Getting "+getPersistentClass().getName()+" instance with id: " + id);
}
return entity;
}

/**
*
* @param queryName
* @param paramNames
* @param values
* @return
*/
private Query createQuery(String queryName, String[] paramNames, Object[] values) {
Query query = getSession().getNamedQuery(queryName);
query.setCacheable(true);
if ((paramNames != null) && (values != null)) {
for (int i = 0; i < paramNames.length; i++) {
query.setParameter(paramNames[i], values[i]);
}
}
return query;
}

/**
* (pageNo > 0 && pageSize > 0) paginate
* (pageNo < 0 && pageSize < 0) don't paginate
* don't use for full table paginate, that would be low efficency
*/
@SuppressWarnings("unchecked")
public Page<T> findByHQL(int pageNo, int pageSize, String queryName, String[] paramNames, Object[] values){
long rowCount = 0;
Query query = createQuery(queryName, paramNames, values);
if(pageSize > 0){
rowCount = ((Long)getRowCountByHQL(appendRowCountHQL(queryName), paramNames, values)).longValue();
if(pageNo < 1)
pageNo = 1;
if(pageSize > rowCount)
pageSize = (int)rowCount;
query.setFirstResult((pageNo - 1) * pageSize);
query.setMaxResults(pageSize);
}
List<T> list = query.list();
return new Page<T>(pageNo, pageSize, list, (int)rowCount);
}

/**
*
* @param queryName
* @return
*/
private String appendRowCountHQL(String queryName){
return queryName + ".Count";
}

/**
*
* @param queryName
* @param paramNames
* @param values
* @return
*/
@SuppressWarnings("unchecked")
private Object getRowCountByHQL(final String queryName, final String[] paramNames, final Object[] values) {
return ((Object) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Query query = createQuery(queryName, paramNames, values);
if (paramNames != null && values != null && paramNames.length == values.length) {
for (int i = 0, max = paramNames.length; i < max; i++) {
query.setParameter(paramNames[i], values[i]);
}
}
return query.uniqueResult();
}
}));
}


/**
*
*/
public T saveOrUpdate(T entity) throws DataIntegrityViolationException{
getHibernateTemplate().saveOrUpdate(entity);
return entity;
}

/**
*
*/
public void delete(T entity) {
getHibernateTemplate().delete(entity);
}
}

Thursday, April 2, 2009

Unit Testing Sample with Spring and JUnit Annotations

package test.com.one.dao.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import junit.framework.Assert;

import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.one.dao.CustomerDAO;
import com.one.model.Customer;

@ContextConfiguration(locations={"/applicationContext.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class CustomerDAOHibernateImplTests extends AbstractTransactionalJUnit4SpringContextTests{

@Autowired
private CustomerDAO customerDAO;

@Before
public void setup(){

}

@After
public void clean(){

}

@Test
public void testCRUD(){
Customer customer = new Customer();
customer.setCustomerFirstName("firstName");
customer.setCustomerLastName("lastName");
customer.setEmailAddr("email");
customer.setCreatedOn(new Date());
customer.setUpdatedOn(new Date());
customerDAO.saveOrUpdateCustomer(customer);
Assert.assertNotNull(customer.getCustomerId());
Assert.assertTrue(customer.getCustomerId().intValue() > 0);
customer = (Customer) customerDAO.getCustomerByCustomerId(customer.getCustomerId());
Assert.assertNotNull(customer);
Assert.assertEquals(customer.getCustomerFirstName(), "firstName");
Assert.assertEquals(customer.getCustomerLastName(), "lastName");
Assert.assertEquals(customer.getEmailAddr(), "email");
customerDAO.deleteCustomer(customer);
customer = (Customer) customerDAO.getCustomerByCustomerId(customer.getCustomerId());
Assert.assertNull(customer);
}

@Test
public void testGetCustomersByDateRange(){
Customer customer = new Customer();
customer.setCustomerFirstName("firstName");
customer.setCustomerLastName("lastName");
customer.setEmailAddr("email");
customer.setCreatedOn(new Date());
customer.setUpdatedOn(new Date());
customerDAO.saveOrUpdateCustomer(customer);
Assert.assertNotNull(customer.getCustomerId());
Assert.assertTrue(customer.getCustomerId().intValue() > 0);
Calendar c = Calendar.getInstance();
c.add(Calendar.DAY_OF_YEAR, -1);
Date from = c.getTime();
c.add(Calendar.DAY_OF_YEAR, +2);
Date to = c.getTime();
List list = customerDAO.getCustomersByDateRange(from, to);
Assert.assertNotNull(list);
Assert.assertTrue(list.size() > 0);
}

@Ignore
public void testGetCustomerByFirstnameAndLastnameAndEmail(){
Customer customer = new Customer();
customer.setCustomerFirstName("firstName");
customer.setCustomerLastName("lastName");
customer.setEmailAddr("email");
customer.setCreatedOn(new Date());
customer.setUpdatedOn(new Date());
customerDAO.saveOrUpdateCustomer(customer);
Assert.assertNotNull(customer.getCustomerId());
Assert.assertTrue(customer.getCustomerId().intValue() > 0);
customer = (Customer) customerDAO.getCustomerByFirstnameAndLastnameAndEmail("firstName", "lastName", "email");
Assert.assertNotNull(customer);
Assert.assertEquals(customer.getCustomerFirstName(), "firstName");
Assert.assertEquals(customer.getCustomerLastName(), "lastName");
Assert.assertEquals(customer.getEmailAddr(), "email");
}

}

Tuesday, March 31, 2009

Using Spring BlazeDS Integration 1.0.0.M1

1. Download Spring and BlazeDS.
2. Create a java project, remember to push blazeds configuration files to the properly folder.
3. Add this part to your web.xml:
    <!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<!-- Map all /messagbroker requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<url-pattern>/messagebroker/*</url-pattern>
</servlet-mapping>


4. Write your applicationContext.xml like this:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:flex="http://www.springframework.org/schema/flex"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/flex http://www.springframework.org/schema/flex/spring-flex-1.0.xsd">

<!-- Bootstraps and exposes the BlazeDS MessageBroker -->
<bean id="_messageBroker" class="org.springframework.flex.messaging.MessageBrokerFactoryBean" />

<!-- Maps request paths at /* to the BlazeDS MessageBroker -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>
/*=_messageBroker
</value>
</property>
</bean>

<!-- Dispatches requests mapped to a MessageBroker -->
<bean class="org.springframework.flex.messaging.servlet.MessageBrokerHandlerAdapter"/>

<bean id="testService"
class="com.one.service.impl.TestServiceImpl">
<flex:remote-service />
</bean>

</beans>


5. Enjoy it.

Sunday, March 22, 2009

Best Practices for Exception Handling and Logging

The Nature of Exceptions
Broadly speaking, there are three different situations that cause exceptions to be thrown:
Exceptions due to programming errors: In this category, exceptions are generated due to programming errors (e.g., NullPointerException and IllegalArgumentException). The client code usually cannot do anything about programming errors.
Exceptions due to client code errors: Client code attempts something not allowed by the API, and thereby violates its contract. The client can take some alternative course of action, if there is useful information provided in the exception. For example: an exception is thrown while parsing an XML document that is not well-formed. The exception contains useful information about the location in the XML document that causes the problem. The client can use this information to take recovery steps.
Exceptions due to resource failures: Exceptions that get generated when resources fail. For example: the system runs out of memory or a network connection fails. The client's response to resource failures is context-driven. The client can retry the operation after some time or just log the resource failure and bring the application to a halt.
Best Practices for Exception Handling
1. When deciding on checked exceptions vs. unchecked exceptions, ask yourself, "What action can the client code take when the exception occurs?"
If the client can take some alternate action to recover from the exception, make it a checked exception. If the client cannot do anything useful, then make the exception unchecked. By useful, I mean taking steps to recover from the exception and not just logging the exception.
Moreover, prefer unchecked exceptions for all programming errors: unchecked exceptions have the benefit of not forcing the client API to explicitly deal with them. They propagate to where you want to catch them, or they go all the way out and get reported. The Java API has many unchecked exceptions, such as NullPointerException, IllegalArgumentException, and IllegalStateException. I prefer working with standard exceptions provided in Java rather than creating my own. They make my code easy to understand and avoid increasing the memory footprint of code.
2. Preserve encapsulation.
Never let implementation-specific checked exceptions escalate to the higher layers. For example, do not propagate SQLException from data access code to the business objects layer. Business objects layer do not need to know about SQLException. You have two options:
1) Convert SQLException into another checked exception, if the client code is expected to recuperate from the exception.
2) Convert SQLException into an unchecked exception, if the client code cannot do anything about it.
3. Try not to create new custom exceptions if they do not have useful information for client code.
4. Do not use your base exception class for "unkown" exception cases.
actually a follow-up from the first advice above. If you model your own exception hierarchy, you will typically have an exception base class (eg. MyAPIException) and several specific ones that inherit from that one (eg. MyAPIPathNotFoundException). Now it is tempting to throw the base exception class whenever you don't really know what else to throw, because the error case is not clear or very seldom. It's probably a Fault and thus you would start mixing it with your Contingency exception class hierarchy, which is obviously a bad thing.
One of the advantages of an exception base class is that the client has the choice to catch the base class if he does not want to handle the specific cases (although that's probably not the most robust code). But if that exception is also thrown in faulty situations, the client can no longer make a distinction between one-of-those-contingency-cases and all-those-unexcpected-fault-cases. And it obviously brakes your explicit exception design: there are those specific error cases you state and let the client know about, but then there is this generic exception thrown where the client cannot know what it means and is not able to handle it as a consequence.
5. When wrapping or logging exceptions, add your specific data to the message.
6. Don't throw exceptions in methods that are likely to be used for the exception handling itself.
This follows straight from the two previous advices: if you throw or log exceptions, you are typically in exception handling code, because you wrap a lower-level exception. If you add dynamic data to your exceptions, you might access methods from the underlying API. But if those methods throw exceptions, your code becomes ugly.
7. Document exceptions.

Best Practices for Using Exceptions
1. Always clean up after yourself
If you are using resources like database connections or network connections, make sure you clean them up. If the API you are invoking uses only unchecked exceptions, you should still clean up resources after use, with try - finally blocks.
2. Never use exceptions for flow control
Generating stack traces is expensive and the value of a stack trace is in debugging. In a flow-control situation, the stack trace would be ignored, since the client just wants to know how to proceed.
3. Do not suppress or ignore exceptions
When a method from an API throws a checked exception, it is trying to tell you that you should take some counter action. If the checked exception does not make sense to you, do not hesitate to convert it into an unchecked exception and throw it again, but do not ignore it by catching it with {} and then continue as if nothing had happened.
4. Do not catch top-level exceptions
5. Log exceptions just once
Logging the same exception stack trace more than once can confuse the programmer examining the stack trace about the original source of exception. So just log it once.

Logging
When your code encounters an exception, it must either handle it, let it bubble up, wrap it, or log it. If your code can programmatically handle an exception (e.g., retry in the case of a network failure), then it should. If it can't, it should generally either let it bubble up (for unchecked exceptions) or wrap it (for checked exceptions). However, it is ultimately going to be someone's responsibility to log the fact that this exception occurred if nobody in the calling stack was able to handle it programmatically. This code should typically live as high in the execution stack as it can. Some examples are the onMessage() method of an MDB, and the main() method of a class. Once you catch the exception, you should log it appropriately.
The JDK has a java.util.logging package built in, although the Log4j project from Apache continues to be a commonly-used alternative. Apache also offers the Commons Logging project, which acts as a thin layer that allows you to swap out different logging implementations underneath in a pluggable fashion. All of these logging frameworks that I've mentioned have basically equivalent levels:
1) FATAL: Should be used in extreme cases, where immediate attention is needed. This level can be useful to trigger a support engineer's pager.
2) ERROR: Indicates a bug, or a general error condition, but not necessarily one that brings the system to a halt. This level can be useful to trigger email to an alerts list, where it can be filed as a bug by a support engineer.
3) WARN: Not necessarily a bug, but something someone will probably want to know about. If someone is reading a log file, they will typically want to see any warnings that arise.
4) INFO: Used for basic, high-level diagnostic information. Most often good to stick immediately before and after relatively long-running sections of code to answer the question "What is the app doing?" Messages at this level should avoid being very chatty.
5) DEBUG: Used for low-level debugging assistance.
If you are using commons-logging or Log4j, watch out for a common gotcha. The error, warn, info, and debug methods are overloaded with one version that takes only a message parameter, and one that also takes a Throwable as the second parameter. Make sure that if you are trying to log the fact that an exception was thrown, you pass both a message and the exception. If you call the version that accepts a single parameter, and pass it the exception, it hides the stack trace of the exception.
When calling log.debug(), it's good practice to always surround the call with a check for log.isDebugEnabled(). This is purely for optimization. It's simply a good habit to get into, and once you do it for a few days, it will just become automatic.
Do not use System.out or System.err. You should always use a logger. Loggers are extremely configurable and flexible, and each appender can decide which level of severity it wants to report/act on, on a package-by-package basis. Printing a message to System.out is just sloppy and generally unforgivable.

Thanks for these following articles:
Best Practices for Exception Handling
Exception Handling Best Practices Part 1
Exception Handling Best Practices Part 2: Control flow in data oriented APIs
Exception Handling Best Practices Part 3
Exception-Handling Antipatterns
Effective Java Exceptions
Three Rules for Effective Exception Handling

Thursday, March 5, 2009

Sample of Java Pattern -- Adapter

LegacyModel.java
package adapter;

public class LegacyModel {
public void ins(String sku){
System.out.println("call ins method.");
}
}


ModernModel.java
package adapter;

public interface ModernModel {
public void save(Order order);
}


ModernModelAdapter.java
package adapter;

public class ModernModelAdapter implements ModernModel{
private LegacyModel legacyModel;

public ModernModelAdapter(LegacyModel legacyModel) {
super();
this.legacyModel = legacyModel;
}

public void save(Order order){
String sku = order.getSku();
legacyModel = new LegacyModel();
legacyModel.ins(sku);
}
}


Order.java
package adapter;

public class Order {
private String sku;

public String getSku() {
return sku;
}

public void setSku(String sku) {
this.sku = sku;
}

}


AdapterTest.java
package adapter;

public class AdapterTest {
public static void main(String[] args) {
LegacyModel legacy = new LegacyModel();
Order order = new Order();
order.setSku("sku");
ModernModelAdapter adapter = new ModernModelAdapter(legacy);
adapter.save(order);
}
}


you may download the source code.

Friday, February 27, 2009

Sample of Java Pattern -- Proxy

Order.java
package proxy;

import java.io.Serializable;
import java.util.Date;

public class Order implements Serializable {

private int orderId;
private String sku;
private Date createdOn;
private Date updatedOn;
private int status;

public int getOrderId() {
return orderId;
}
public void setOrderId(int orderId) {
this.orderId = orderId;
}
public String getSku() {
return sku;
}
public void setSku(String sku) {
this.sku = sku;
}
public Date getCreatedOn() {
return createdOn;
}
public void setCreatedOn(Date createdOn) {
this.createdOn = createdOn;
}
public Date getUpdatedOn() {
return updatedOn;
}
public void setUpdatedOn(Date updatedOn) {
this.updatedOn = updatedOn;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((sku == null) ? 0 : sku.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Order other = (Order) obj;
if (sku == null) {
if (other.sku != null)
return false;
} else if (!sku.equals(other.sku))
return false;
return true;
}
}

OrderDAO.java
package proxy;

import java.util.List;

public interface OrderDAO {
public List<Order> getAllOrders();
}


OrderDAOImpl.java
package proxy;

import java.util.ArrayList;
import java.util.List;

public class OrderDAOImpl implements OrderDAO{
public List<Order> getAllOrders(){
List<Order> list = new ArrayList<Order>();
for(int i =0; i<10; i++){
Order order = new Order();
order.setSku("sku"+i);
list.add(order);
}
return list;
}
}


OrderDAOProxy.java
package proxy;

import java.util.List;

public class OrderDAOProxy implements OrderDAO{
public List<Order> getAllOrders(){
long stTime = System.currentTimeMillis();
OrderDAO orderDAOImpl = new OrderDAOImpl();
List<Order> list = orderDAOImpl.getAllOrders();
long endTime = System.currentTimeMillis();
System.out.println("took " + (endTime - stTime) + " milliseconds.");
return list;
}
}


ProxyTest.java
package proxy;

public class ProxyTest {

public static void main(String[] args) {
OrderDAO orderDAOProxy = new OrderDAOProxy();
orderDAOProxy.getAllOrders();
}

}


you may download the source code.

Saturday, February 21, 2009

Sample of Java Pattern -- State

OrderState.java
package state;

public interface OrderState {
public void handleOrder(String orderNo);
public void cancelOrder(String orderNo);
public void completeOrder(String orderNo);
}



OrderOpendState.java
package state;

public class OrderOpendState implements OrderState {

OrderStateMgmt orderStateMgmt;

public OrderOpendState(OrderStateMgmt orderStateMgmt){
this.orderStateMgmt = orderStateMgmt;
}

public void handleOrder(String orderNo){
System.out.println("handle this order, orderNo=" + orderNo);
orderStateMgmt.setState(orderStateMgmt.getOrderInProgressState());
}

public void cancelOrder(String orderNo){
System.out.println("cancel this order, orderNo=" + orderNo);
orderStateMgmt.setState(orderStateMgmt.getOrderCancelledState());
}

public void completeOrder(String orderNo){
System.out.println("could not complete this order under opend status, orderNo=" + orderNo);
}
}



OrderInProgressState.java
package state;

public class OrderInProgressState implements OrderState {

OrderStateMgmt orderStateMgmt;

public OrderInProgressState(OrderStateMgmt orderStateMgmt){
this.orderStateMgmt = orderStateMgmt;
}

public void handleOrder(String orderNo){
System.out.println("could not handle this order under in progress status, orderNo=" + orderNo);
}

public void cancelOrder(String orderNo){
System.out.println("cancel this order, orderNo=" + orderNo);
orderStateMgmt.setState(orderStateMgmt.getOrderCancelledState());
}

public void completeOrder(String orderNo){
System.out.println("complete this order, orderNo=" + orderNo);
orderStateMgmt.setState(orderStateMgmt.getOrderClosedState());
}
}



OrderCancelledState.java
package state;

public class OrderCancelledState implements OrderState {

OrderStateMgmt orderStateMgmt;

public OrderCancelledState(OrderStateMgmt orderStateMgmt){
this.orderStateMgmt = orderStateMgmt;
}

public void handleOrder(String orderNo){
System.out.println("could not handle this order under cancelled status, orderNo=" + orderNo);
}

public void cancelOrder(String orderNo){
System.out.println("could not cancel this order under cancelled status, orderNo=" + orderNo);
}

public void completeOrder(String orderNo){
System.out.println("could not complete this order under cancelled status, orderNo=" + orderNo);
}
}



OrderClosedState.java
package state;

public class OrderClosedState implements OrderState {

OrderStateMgmt orderStateMgmt;

public OrderClosedState(OrderStateMgmt orderStateMgmt){
this.orderStateMgmt = orderStateMgmt;
}

public void handleOrder(String orderNo){
System.out.println("could not handle this order under closed status, orderNo=" + orderNo);
}

public void cancelOrder(String orderNo){
System.out.println("could not cancel this order under closed status, orderNo=" + orderNo);
}

public void completeOrder(String orderNo){
System.out.println("could not complete this order under closed status, orderNo=" + orderNo);
}
}



OrderStateMgmt.java
package state;

public class OrderStateMgmt {

private OrderOpendState orderOpendState;
private OrderInProgressState orderInProgressState;
private OrderCancelledState orderCancelledState;
private OrderClosedState orderClosedState;

private OrderState orderState;

public OrderStateMgmt(){
this.orderOpendState = new OrderOpendState(this);
this.orderInProgressState = new OrderInProgressState(this);
this.orderCancelledState = new OrderCancelledState(this);
this.orderClosedState = new OrderClosedState(this);
this.orderState = this.orderOpendState;
System.out.println("The order inital status is: opend.");
}

public void setState(OrderState orderState){
this.orderState = orderState;
}

public OrderOpendState getOrderOpendState() {
return orderOpendState;
}

public OrderInProgressState getOrderInProgressState() {
return orderInProgressState;
}

public OrderCancelledState getOrderCancelledState() {
return orderCancelledState;
}

public OrderClosedState getOrderClosedState() {
return orderClosedState;
}

public void handleOrder(String orderNo){
orderState.handleOrder(orderNo);
}

public void cancelOrder(String orderNo){
orderState.cancelOrder(orderNo);
}

public void completeOrder(String orderNo){
orderState.completeOrder(orderNo);
}
}



OrderStateTest.java
package state;

public class OrderStateTest {

public static void main(String[] args) {
OrderStateMgmt orderStateMgmtA = new OrderStateMgmt();

orderStateMgmtA.handleOrder("01");
orderStateMgmtA.completeOrder("01");

OrderStateMgmt orderStateMgmtB = new OrderStateMgmt();

orderStateMgmtB.handleOrder("02");
orderStateMgmtB.cancelOrder("02");

OrderStateMgmt orderStateMgmtC = new OrderStateMgmt();

orderStateMgmtC.completeOrder("03");
}

}



you may download the source code.