Web2.0 profile DAO

BTT Web2.0 profile DAO provide a unified interface to manage Web2.0 user profile data.

You can read, add, and update user profile data using the Web2.0 profile DAO without knowing the implementation details. Following are the interfaces:

public interface BTTWeb2ProfileDAO {

	public String getUserProfile(String configType, String userID) throws Exception;

	public String getGlobalProfile(String configType) throws Exception;

	public int addUserProfile(String configType, String userID, String xmlContent) throws Exception;

	public int addGlobalProfile(String configType, String xmlContent) throws Exception;

	public int updateUserProfile(String configType, String userID,String xmlContent) throws Exception;

	public int updateGlobalProfile(String configType, String xmlContent) throws Exception;
	
}
Web2.0 profile DAO provides two kinds of implementation: file implementation and JDBC implementation. You can choose the implementation depend on your own scenario, or your can implement the DAO interface by yourself.
Note: JDBC implementation provided by BTT follows JDBC4 specification.

You can develop your own DAO for a specific database. Following is an example for Oracle:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import oracle.xdb.XMLType;


/*
 *  BTT Web2 Profile DAO implementation by JDBC for Oracle.
 *  @copyright(c) Copyright IBM Corporation 2010.
 */
public class BTTWeb2ProfileOracleDAOImpl implements BTTWeb2ProfileDAO {
	@SuppressWarnings("unused")
	private static final java.lang.String COPYRIGHT = "Licensed Materials - Property of IBM "//$NON-NLS-1$
		+ "Restricted Materials of IBM "//$NON-NLS-1$
		+ "5724-H82 "//$NON-NLS-1$
		+ "(C) Copyright IBM Corp. 2010 All Rights Reserved. "//$NON-NLS-1$
		+ "US Government Users Restricted Rights - Use, duplication or disclosure "//$NON-NLS-1$
		+ "restricted by GSA ADP Schedule Contract with IBM Corp ";//$NON-NLS-1$
	
	private String dataSource;
	
	private String dbDriverName;
	private String dbUrl;
	private String dbUserName;
	private String dbPassWord;
	
	javax.naming.InitialContext ctx=null;
	javax.sql.DataSource  ds=null;
	
	public String getDataSource() {
		return dataSource;
	}

	public void setDataSource(String dataSource) {
		this.dataSource = dataSource;
	}

	public String getDbDriverName() {
		return dbDriverName;
	}

	public void setDbDriverName(String dbDriverName) {
		this.dbDriverName = dbDriverName;
	}

	public String getDbUrl() {
		return dbUrl;
	}

	public void setDbUrl(String dbUrl) {
		this.dbUrl = dbUrl;
	}

	public String getDbUserName() {
		return dbUserName;
	}

	public void setDbUserName(String dbUserName) {
		this.dbUserName = dbUserName;
	}

	public String getDbPassWord() {
		return dbPassWord;
	}

	public void setDbPassWord(String dbPassWord) {
		this.dbPassWord = dbPassWord;
	}


	/*
	 * (non-Javadoc)
	 * @see com.ibm.btt.web2.profile.BTTWeb2ProfileDAO#getGlobalProfile(java.lang.String)
	 */
	public String getGlobalProfile(String configType) throws Exception{
		if (configType==null)
			throw new Exception("getGlobalProfile, input configType should not be null ");
		
			Connection con=getConnection( );
			
		
			Statement statement=null;
		
			try {
				statement = con.createStatement();
  
				ResultSet rs = statement.executeQuery("select * from BTTWeb2globalProfile where  configtype='"+configType+"'");
				if ( rs.next()){
					XMLType xml = (XMLType)rs.getObject("content");
					return xml.getStringVal();
				}
			}catch (Exception e){
				throw e;
			}finally{
				try{
					if (statement!=null) statement.close();
					if (con!=null) con.close();
					
				}catch (Exception e1){
					throw e1;
				}
				
			}
			
		    return null;
			
	}

	/*
	 * (non-Javadoc)
	 * @see com.ibm.btt.web2.profile.BTTWeb2ProfileDAO#getUserProfile(java.lang.String, java.lang.String)
	 */
	public String getUserProfile(String configType, String userID) throws Exception{
		if (configType==null || userID==null )
			throw new Exception("getUserProfile, input configType or userID should not be null !");
		
		
		Connection con=getConnection( );
		
		
		Statement statement=null;
		try{
			statement = con.createStatement();

			ResultSet rs = statement.executeQuery("select * from BTTWeb2UserProfile where  configtype='"+configType+"' and userID='"+userID+"'" );
			if ( rs.next()){	
				XMLType xml = (XMLType)rs.getObject("content");
				return xml.getStringVal();
			}
		
		}catch (Exception e){
				throw e;
		}finally{
				try{
					if (statement!=null) statement.close();
					if (con!=null) con.close();
					
				}catch (Exception e1){
					throw e1;
				}
				
		}
		return null;
	}

    /*
     * (non-Javadoc)
     * @see com.ibm.btt.web2.profile.BTTWeb2ProfileDAO#addGlobalProfile(java.lang.String, java.lang.String)
     */
	public int addGlobalProfile(String configType, String xmlContent) throws Exception{
		if (configType==null || xmlContent==null )
			throw new Exception("addGlobalProfile, input configType or xmlContent should not be null !");
		
		
		Connection con=getConnection( );
		
		PreparedStatement statement=null;
		try{
			
			statement = con.prepareStatement("insert into  BTTWEB2GLOBALPROFILE values(?,?)");
			
			XMLType xmltype =  XMLType.createXML(con, xmlContent);
			statement.setString(1, configType);
			statement.setObject(2, xmltype);
			int count = statement.executeUpdate( );
			
			return count;
		}catch (Exception e){
				throw e;
		}finally{
				try{
					if (statement!=null) statement.close();
					if (con!=null) con.close();
					
				}catch (Exception e1){
					throw e1;
				}
				
		}
		 
	}

	/*
	 * (non-Javadoc)
	 * @see com.ibm.btt.web2.profile.BTTWeb2ProfileDAO#addUserProfile(java.lang.String, java.lang.String, java.lang.String)
	 */
	public int addUserProfile(String configType, String userID, String xmlContent) throws Exception{
		if (configType==null || xmlContent==null || userID==null)
			throw new Exception("addUserProfile, input configType or xmlContent or userID should not be null !");
		
		
		Connection con=getConnection( );
		
		PreparedStatement statement=null;
		try{
			
			statement = con.prepareStatement("insert into  BTTWEB2USERPROFILE values(?,?,?)");
			XMLType xmltype =  XMLType.createXML(con, xmlContent);
			statement.setString(1, userID);
			statement.setString(2, configType);
			statement.setObject(3, xmltype);
			int count = statement.executeUpdate( );
			
			return count;
		}catch (Exception e){
				throw e;
		}finally{
				try{
					if (statement!=null) statement.close();
					if (con!=null) con.close();
					
				}catch (Exception e1){
					throw e1;
				}
				
		}
	}

	/*
	 * (non-Javadoc)
	 * @see com.ibm.btt.web2.profile.BTTWeb2ProfileDAO#updateGlobalProfile(java.lang.String, java.lang.String)
	 */
	public int updateGlobalProfile(String configType, String xmlContent) throws Exception{
		if (configType==null || xmlContent==null )
			throw new Exception("updateGlobalProfile, input configType or xmlContent should not be null !");
		
		
		Connection con=getConnection( );
		PreparedStatement statement=null;
		try{
			statement = con.prepareStatement("update  BTTWEB2GLOBALPROFILE  set content= ? where  configtype='"+configType+"'");
			XMLType xmltype =  XMLType.createXML(con, xmlContent);
			statement.setObject(1, xmltype);
			int count = statement.executeUpdate( );
			
			
			return count;
		}catch (Exception e){
				throw e;
		}finally{
				try{
					if (statement!=null) statement.close();
					if (con!=null) con.close();
					
				}catch (Exception e1){
					throw e1;
				}
				
		}
	}

	/*
	 * (non-Javadoc)
	 * @see com.ibm.btt.web2.profile.BTTWeb2ProfileDAO#updateUserProfile(java.lang.String, java.lang.String, java.lang.String)
	 */
	public int updateUserProfile(String configType, String userID, String xmlContent) throws Exception{
		if (configType==null || xmlContent==null || userID==null)
			throw new Exception("updateUserProfile, input configType or xmlContent or userID should not be null !");
		
		
		Connection con=getConnection( );
		PreparedStatement statement=null;
		try{
			statement = con.prepareStatement("update BTTWEB2USERPROFILE  set content= ? where  configtype='"+configType+"'  and userID='"+userID +"'" );
			XMLType xmltype =  XMLType.createXML(con, xmlContent);
			statement.setObject(1, xmltype);
			int count = statement.executeUpdate( );
			
			return count;
		}catch (Exception e){
				throw e;
		}finally{
				try{
					if (statement!=null) statement.close();
					if (con!=null) con.close();
					
				}catch (Exception e1){
					throw e1;
				}				
		}
	}
	
	private Connection getConnection() throws Exception {
		
		if (getDataSource() ==null && getDbDriverName()!=null) {
			try { 		
				Class.forName(getDbDriverName());
				Connection con=null;
				if (getDbUserName()!=null &&  getDbPassWord()!=null)
					 con=DriverManager.getConnection(getDbUrl(), getDbUserName(), getDbPassWord()   );
				else 
					 con=DriverManager.getConnection(getDbUrl()  );
				
				 
				return con;
			} catch (SQLException e) {
				throw new Exception(
						"failed in create connection to db with the following information:" ,e);
			}
		}else if (getDataSource() !=null){
			try{
				if (ctx==null) ctx = new javax.naming.InitialContext();
					
				if (ds==null) ds = (javax.sql.DataSource)ctx.lookup(getDataSource());
			    // initilized a connection;
				Connection con1 =ds.getConnection();
				return  con1;
				
			}catch(javax.naming.NamingException e1){
    			throw new Exception("Failed to get connection from datasource: "+getDataSource() , e1);
    		}catch (java.sql.SQLException e2) {
    			throw new Exception("Failed to get connection from datasource: "+getDataSource() , e2);
    		}
		}else 		
		    throw new Exception("For JDBC DAO, the DbDriverName or DataSource must be defined");
	}
}