/*
* This software and supporting documentation were developed by
*
* Siemens Corporate Technology
* Competence Center Knowledge Management and Business Transformation
* D-81730 Munich, Germany
*
* Authors (representing a really great team ;-) )
* Stefan B. Augustin, Thorbj�rn Hansen, Manfred Langen
*
* This software is Open Source under GNU General Public License (GPL).
* Read the text of this license in LICENSE.TXT
* or look at www.opensource.org/licenses/
*
* Once more we emphasize, that:
* THIS SOFTWARE IS MADE AVAILABLE, AS IS, WITHOUT ANY WARRANTY
* REGARDING THE SOFTWARE, ITS PERFORMANCE OR
* FITNESS FOR ANY PARTICULAR USE, FREEDOM FROM ANY COMPUTER DISEASES OR
* ITS CONFORMITY TO ANY SPECIFICATION. THE ENTIRE RISK AS TO QUALITY AND
* PERFORMANCE OF THE SOFTWARE IS WITH THE USER.
*
*/
// PS_Db
// ************ package ******************************************************
package appl.Portal.Personalize.DB;
// ************ imports ******************************************************
// KFM packages
import KFM.Exceptions.KFM_SQLException;
import KFM.DB.*;
import KFM.log.*;
import KFM.Converter;
import KFM.Exceptions.ProgrammerException;
// Java packages
import java.sql.*;
import java.util.*;
// PS packages
import appl.Portal.Personalize.Servlet.PS_Props;
/** DB class for `PersonalizeSubscribe�.
*
* <P>Contains commands to update the DB, but not to query.
* We use the "view" classes to query the DB.</P>
*
*
* <H2>Related classes</H2>
*
* <P>Cut+paste-source was `P_Db�</P>
*
* @version 0.1 (2001-02-05)
*/
public class PS_Db extends ApplicationDb2
{
// ************************************************************
// Variables
// ************************************************************
/** Log, copy of `KFM_PropertyServlet2_Props.mLog� for easier access. */
protected KFMLog mLog;
/** Servlet properties. */
PS_Props mServletProps = null;
// ************************************************************
// Methods
// ************************************************************
/** Constructor.
*
* @param aAdapter The KFM_JdbcAdapter of the DB to be used.
*/
public PS_Db (KFM_JdbcAdapter aAdapter, KFMLog aLog, PS_Props aServletProps)
{
super(aAdapter, "appl.Portal.Personalize.DB.PS_Db");
mLog = aLog;
mServletProps = aServletProps;
}
/** Inserts a new user in the database.
*
* <P>Note: This method does *not* commit. Do that in the ApplPage.</P>
*
* @param aValues vector containing P_DBEntry with column name, new value and column type of
* the columns which have to be inserted in the database.
* @param aNick nick of the user
* @param aWhere column to use for the where clause
*
*/
public void insertMember (Vector aValues, String aNick, String aWhere)
throws KFM_SQLException
{
mLog.detail("PS_Db::insertMember: Entering."+aNick);
Vector tDesktopNames = new Vector();
int mUpdateCmd = -1;
boolean tWasException = false; //indicates an exception in the main path.
try {
// insert part
String tSqlString = "INSERT INTO members (";
// names part
StringBuffer tNamesBuf=new StringBuffer();
for (int i=0; i<aValues.size(); i++){
int tType=((P_DBEntry)aValues.elementAt(i)).getDBType();
if (tType==PersonalizeDbConsts.cText){
//insert only the name of the desktop into the vector tDesktopNames
//for correct initializing
tDesktopNames.addElement(((P_DBEntry)aValues.elementAt(i)).getName());
}
else {
String tName = ((P_DBEntry)aValues.elementAt(i)).getName();
tNamesBuf.append(((P_DBEntry)aValues.elementAt(i)).getName());
tNamesBuf.append(", ");
}
}
String tNames=tNamesBuf.toString();
if (tNames.endsWith(", "))
tNames = tNames.substring(0, (tNames.length() - 2));
tNames+=") ";
// values part
StringBuffer tValuesBuf =new StringBuffer("VALUES (");
for (int i=0; i<aValues.size(); i++){
int tType=((P_DBEntry)aValues.elementAt(i)).getDBType();
if (tType==PersonalizeDbConsts.cText){
//do Nothing because desktops mustn't be inserted in the UPDATE clause
}
else {
String tName = ((P_DBEntry)aValues.elementAt(i)).getName();
tValuesBuf.append("?,");
}
}
String tValues=tValuesBuf.toString();
if (tValues.endsWith(","))
tValues = tValues.substring(0, (tValues.length() - 1));
tValues+=")";
// complete SQL update statement
tSqlString=tSqlString+tNames+tValues;
mUpdateCmd = mDbA.prepareStmt(tSqlString);
Vector tDesktopValues = new Vector();
// replace the "?" with values
// empty values will be written as SQL null
int j = -1;
for (int i=0; i<aValues.size(); i++){
String tName = ((P_DBEntry)aValues.elementAt(i)).getName();
String tValue=((P_DBEntry)aValues.elementAt(i)).getValue();
int tType=((P_DBEntry)aValues.elementAt(i)).getDBType();
// VarChar or Text
j ++;
if (tType==PersonalizeDbConsts.cText){
//oracle
tDesktopValues.addElement(tValue);
j --;
// VarChar
} else if ((tType==PersonalizeDbConsts.cVarChar)
|| (tType==PersonalizeDbConsts.cChar)){
mDbA.setStringOrNull(mUpdateCmd, j+1, tValue);
// Integer
} else if (tType==PersonalizeDbConsts.cInt){
if (tValue.equals("")){
mDbA.setNull(mUpdateCmd, j+1, java.sql.Types.INTEGER);
} else {
int tIntValue = Integer.parseInt(tValue);
mDbA.setValue(mUpdateCmd, j+1, tIntValue);
}
// Tiny Integer
} else {
if (tValue.equals("")){
mDbA.setNull(mUpdateCmd, j+1, java.sql.Types.TINYINT);
} else {
int tIntValue = Integer.parseInt(tValue);
mDbA.setValue(mUpdateCmd, j+1, tIntValue);
}
}
}
if (tDesktopValues.size() == 0)
{
throw new ProgrammerException("Desktop data are required for inserting members!");
}
mDbA.executeUpdate(mUpdateCmd);
//now insert the desktop values
String tMembersId = getMembersId(aNick, aWhere);
try {
PersonalizeDbServices tServices = new PersonalizeDbServices(
mDbA, mLog);
StringBuffer tDesktopSQL = new StringBuffer("Insert into desktops ( ");
tDesktopSQL.append("members_id,");
tDesktopSQL.append("desktopName,");
tDesktopSQL.append("item0,");
tDesktopSQL.append("item1,");
tDesktopSQL.append("item2,");
tDesktopSQL.append("item3,");
tDesktopSQL.append("item4,");
tDesktopSQL.append("item5)");
tDesktopSQL.append(" VALUES(?,?,?,?,?,?,?,?)");
String[] tDesktopNamesStr = new String[tDesktopNames.size()];
tDesktopNames.copyInto(tDesktopNamesStr);
tServices.executeP_DesktopsUpdate(
tDesktopSQL.toString(),
tDesktopNamesStr,
tDesktopValues,
tMembersId);
//closes the statement automatically
} catch (KFM_SQLException ex){
//rollback
mLog.error("", ex);
mDbA.rollback();
throw niceSqlException(ex); //inform the user about the error
}
mDbA.commit(); //no error has occured so save the values
} catch(SQLException e) {
tWasException = true;
throw niceSqlException(e);
} finally {
try {
if(mUpdateCmd != -1) {
//close the statement to free db resources
mDbA.closeStmt(mUpdateCmd);
}
} catch(SQLException e) {
if (!tWasException){
tWasException = true;
throw niceSqlException(e);
}
}
}
mLog.detail("PS_Db::insertMember: Leaving.");
}
/** Checks whether a nick exists.
*
*
*@param aNick nick to check.
*@return whether nick exists.
*@exception ProgrammerException
*/
public boolean checkUserProfileExists (String aNick, String aAuthtype)
throws KFM_SQLException
{
//we use delegation to centralize the code on one place
PersonalizeDbServices tServices = new PersonalizeDbServices(mDbA, mLog);
return tServices.checkUserProfileExists(aNick, aAuthtype);
}
/** Returns the ID of the member with the given nick.
*
*@param aNick nick of the user
*@param aWhere column to use for the where clause
*@exception ProgrammerException
*/
public String getMembersId (String aNick, String aWhere)
throws KFM_SQLException
{
//we use delegation to centralize the code on one place
PersonalizeDbServices tServices = new PersonalizeDbServices(mDbA, mLog);
return tServices.getMembersId(aNick, aWhere);
}
}