Java tutorial
/** * Copyright (c) 2001-2012 "Redbasin Networks, INC" [http://redbasin.org] * * This file is part of Redbasin OpenDocShare community project. * * Redbasin OpenDocShare is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package dao; import java.sql.Connection; import java.util.List; import javax.sql.DataSource; import model.Hdlogin; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.jboss.cache.Fqn; import util.*; /** * This object implements RegistrationDaoDb * * @author Smitha Gudur (smitha@redbasin.com) * @version $Revision: 1.1 $ */ public class RegistrationDaoDb extends BaseDao implements RegistrationDao { protected final Log logger = LogFactory.getLog(getClass()); private volatile GlobalConst webConstants; private volatile SMTPTool hdmail; protected volatile DiaryAdmin diaryAdmin; //private volatile HdprofileAddQuery hdprofileAddQuery; //private volatile PersonalinfoAddQuery personalinfoAddQuery; //private volatile HdloginAddQuery hdloginAddQuery; //private volatile PasswordUpdateQuery passwordUpdateQuery; private volatile HdprofileQuery hdprofileQuery; private volatile ActivateAccountQuery activateAccountQuery; private volatile BaseCommonQuery hdloginAddQuery; private volatile BaseCommonQuery hdprofileAddQuery; private volatile BaseCommonQuery personalinfoAddQuery; private volatile BaseCommonQuery passwordUpdateQuery; // personal profile, settings private volatile PersonalinfoDeleteQuery personalinfoDeleteQuery; private volatile HdprofileDeleteQuery hdprofileDeleteQuery; // DeActivateAccountQuery deActivateAccountQuery private volatile BaseCommonQuery deActivateAccountQuery; // DeActivateUsertabAccountQuery deActivateUserTabAccountQuery private volatile BaseCommonQuery deActivateUserTabAccountQuery; // cobrand private volatile UserDeleteCobrandQuery deleteUserCobrandQuery; // personal blogs private volatile PblogDeleteQuery pblogDeleteQuery; private volatile PblogMessageDeleteAllQuery pblogDeleteMsgQuery; private volatile PblogTopicDeleteAllQuery pblogDeleteTopicQuery; private volatile PblogTopicAttrDeleteAllQuery pblogDeleteTopicAttrQuery; private volatile PblogMsgAttrDeleteAllQuery pblogDeleteMsgAttrQuery; private volatile PblogTagDeleteQuery pblogDeleteTagsQuery; private volatile YourkeywordsDeleteQuery yourkeyWordsDeleteQuery; /* not required anymore */ // private volatile UserTrafficDeleteQuery userTrafficDeleteQuery; /* not required anymore */ // friends private volatile PendingfriendsDeleteQuery pendingfriendsDeleteQuery; private volatile FriendDeleteQuery hdfriendsDeleteQuery; private volatile DisplaypageDeleteQuery displayUserDeleteQuery; private volatile ShareuserDeleteAllQuery shareUserDeleteQuery; // membercontacts, hdcontacts, sharecontact private volatile ContactDeleteAllMemberQuery memberContactDeleteQuery; private volatile ContactDeleteAllShareQuery shareContactDeleteQuery; private volatile ContactDeleteAllQuery contactsDeleteQuery; //carryon private volatile ContactDeleteAllQuery carryonDeleteQuery; //collabrum private volatile CollMemberBlockDeleteQuery collblockDeleteQuery; //messages private volatile InboxDeleteAllQuery inboxDeleteAllQuery; private volatile OutboxDeleteAllQuery outboxDeleteAllQuery; private volatile HdloginEmailQuery emailQuery; // collabrum // private volatile CollMembersDeleteQuery deleteMemberQuery; // private volatile CollabrumDeleteQuery deleteQuery; // private volatile CollabrumDeleteAdminQuery deleteAdminQuery; /** * Given a login add the user in hdlogin, hdprofile and usertab tables. * @param login - login * @param fname - first name * @param lname - last name * @param email - email * @param password - password * @param aFlag - activation code * @param hear - hear about us * @param budget - budget * @param contact - contact user * @param bphone - bphone * @param biz - business that this user belongs to * @return String - (0) failure (1) success (send activation code msg), (2) login exists * @throws BaseDaoException If we have a problem interpreting the data or the data is missing or incorrect **/ public String addUser(String login, String fname, String lname, String mname, String email, String password, String aFlag, String hear, String budget, String contact, String bphone, String biz) throws BaseDaoException { boolean ldapActive = WebUtil.isLdapActive(); if (WebUtil.isLdapActive()) { if (RegexStrUtil.isNull(login) || RegexStrUtil.isNull(password)) { throw new BaseDaoException("params are null"); } } else { if (RegexStrUtil.isNull(login) || RegexStrUtil.isNull(email) || RegexStrUtil.isNull(password)) { throw new BaseDaoException("params are null"); } } if (!RegexStrUtil.isNull(fname)) logger.info("fname = " + fname); if (!RegexStrUtil.isNull(lname)) logger.info("lname = " + lname); if (!RegexStrUtil.isNull(email)) logger.info("email = " + email); if (!RegexStrUtil.isNull(biz)) logger.info("biz = " + biz); /** * set the flag to false, if user exists */ Hdlogin hdlogin = getLoginid(login); if (hdlogin != null) { String loginid = hdlogin.getValue(DbConstants.LOGIN_ID); if (loginid != null) { return DbConstants.LOGIN_EXISTS; } } /** * hdlogin, hdprofile, usertab - no partitioned */ String sourceName = null; sourceName = scalabilityManager.getWriteZeroScalability(); ds = scalabilityManager.getSource(sourceName); if (ds == null) { throw new BaseDaoException("ds null " + sourceName); } int aCode = getActivationCode(login, email); Connection conn = null; try { conn = ds.getConnection(); conn.setAutoCommit(false); /** * add login, new user, get the latest id of the login */ if (fname == null) fname = ""; if (lname == null) lname = ""; if (mname == null) mname = ""; String[] params = new String[12]; params[0] = login; params[1] = fname; params[2] = lname; params[3] = mname; params[4] = email; params[5] = password; params[6] = new Integer(aCode).toString(); params[7] = aFlag; params[8] = hear; params[9] = budget; params[10] = contact; params[11] = biz; String queryName = scalabilityManager.getWriteZeroScalability("hdloginaddquery"); hdloginAddQuery = getQueryMapper().getCommonQuery(queryName); hdloginAddQuery.run(conn, params); /** * set inform flags to zero */ queryName = scalabilityManager.getWriteZeroScalability("hdprofileaddquery"); hdprofileAddQuery = getQueryMapper().getCommonQuery(queryName); params = new String[3]; params[0] = "0"; params[1] = "0"; params[2] = "0"; hdprofileAddQuery.run(conn, params); /** * set inform flags to zero */ queryName = scalabilityManager.getWriteZeroScalability("personalinfoaddquery"); personalinfoAddQuery = getQueryMapper().getCommonQuery(queryName); params = new String[35]; for (int i = 0; i < 35; i++) params[i] = ""; params[16] = "0"; // male params[25] = bphone; // bphone personalinfoAddQuery.run(conn, params); //personalinfoAddQuery.run(conn, "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",gender, "", "", "", "", "", "","", "", "", "", "", "", "", loginid, "", ""); } catch (Exception e) { try { conn.rollback(); } catch (Exception e1) { try { if (conn != null) { conn.setAutoCommit(true); conn.close(); } } catch (Exception e2) { throw new BaseDaoException("connection close exception", e2); } throw new BaseDaoException("error occured while rollingback entries from hdlogin/hdprofile/usertab", e1); } throw new BaseDaoException("error occured while making entries in hdlogin/hdprofile/usertab", e); } try { conn.commit(); } catch (Exception e3) { throw new BaseDaoException("commit exception", e3); } try { if (conn != null) { conn.setAutoCommit(true); conn.close(); } } catch (Exception e4) { throw new BaseDaoException("connection close exception", e4); } // mail the activation code only if ldap is not enabled if (!WebUtil.isLdapActive()) { mailActivationCode(login, fname, lname, email, aCode); } return DbConstants.SUCCESS; } /** * This method returns activation code * @param login * @return activation code */ public int getActivationCode(String login, String email) { return BasicMathUtil.randomInt(10000, 100000); } /** * This method returns activation code * @param login * @return activation code */ private void mailActivationCode(String login, String fname, String lname, String emailTo, int aCode) { if (RegexStrUtil.isNull(login) || RegexStrUtil.isNull(emailTo)) { throw new BaseDaoException("params are null"); } StringBuffer sb = new StringBuffer("Hi, "); sb.append(login); sb.append("\n"); sb.append(webConstants.getRegisterationmsg()); sb.append("\n Your activation code is: "); sb.append(aCode); sb.append(". Click this "); sb.append(webConstants.getHddomain()); sb.append("/"); sb.append(webConstants.getActivationview()); sb.append("login="); sb.append(login); sb.append("&acode="); sb.append(aCode); sb.append(" to activate your account."); hdmail.sendEmail(emailTo, "Activation code", sb.toString(), webConstants.getMailfrom()); } /** * Given a login activate the user * @param login - the login * @param aCode - activation code * @return String - status message (error or success msg) * @throws BaseDaoException If we have a problem interpreting the data or the data is missing or incorrect */ public String activateAccount(String login, String aCode) throws BaseDaoException { /** * Given a login activate the user in hdlogin, and add the user in hdprofile and usertab */ boolean ldapActive = WebUtil.isLdapActive(); if (ldapActive) { if (RegexStrUtil.isNull(login)) { throw new BaseDaoException("login are null"); } } else { if (RegexStrUtil.isNull(login) || RegexStrUtil.isNull(aCode)) { throw new BaseDaoException("params are null"); } } /** * set the flag to false, if user exists */ Hdlogin hdlogin = getLoginid(login); String loginid = null; if (hdlogin != null) { loginid = hdlogin.getValue(DbConstants.LOGIN_ID); if (!ldapActive) { if (!aCode.equalsIgnoreCase(hdlogin.getValue(DbConstants.ACODE))) { return webConstants.getActivationerror(); } } } else { return (login + " is incorrect."); } /** * hdlogin, hdprofile, usertab - no partitioned */ String sourceName = null; sourceName = scalabilityManager.getWriteZeroScalability(); ds = scalabilityManager.getSource(sourceName); if (ds == null) { throw new BaseDaoException("ds null " + sourceName); } Connection conn = null; try { conn = ds.getConnection(); activateAccountQuery.run(conn, login, "1"); } catch (Exception e) { try { if (conn != null) { conn.close(); } } catch (Exception e1) { throw new BaseDaoException("close() exception, activateAccountQuery.run(), login =" + login, e1); } throw new BaseDaoException("activateAccountQuery.run() exception, login =" + login, e); } try { if (conn != null) { conn.close(); } } catch (Exception e1) { throw new BaseDaoException("close() exception for activateAccountQuery login= " + login, e1); } Fqn fqn = cacheUtil.fqn(DbConstants.ID_INFO); if (treeCache.exists(fqn, login)) { treeCache.remove(fqn, login); } fqn = cacheUtil.fqn(DbConstants.LOGIN_INFO); if (treeCache.exists(fqn, loginid)) { treeCache.remove(fqn, loginid); } return null; } /** * This method deActivates account * @param login * @throws BaseDaoException */ public void deActivateAccount(String login, String diaryAdminLogin) { if (RegexStrUtil.isNull(login) || (RegexStrUtil.isNull(diaryAdminLogin))) { throw new BaseDaoException("params are null"); } if (!diaryAdmin.isDiaryAdmin(diaryAdminLogin)) { throw new BaseDaoException("cannot deactivate another user's account"); } /** * hdlogin, hdprofile, usertab - no partitioned */ String sourceName = null; sourceName = scalabilityManager.getWriteZeroScalability(); ds = scalabilityManager.getSource(sourceName); if (ds == null) { throw new BaseDaoException("ds null " + sourceName); } Hdlogin hdlogin = getLoginid(diaryAdminLogin); String adminPassword = null; String adminEmail = null; if (hdlogin != null) { adminPassword = hdlogin.getValue(DbConstants.PASSWORD); adminEmail = hdlogin.getValue(DbConstants.EMAIL); } /** * deactivate the user */ String loginid = null; hdlogin = getLoginid(login); if (hdlogin != null) { loginid = hdlogin.getValue(DbConstants.LOGIN_ID); } String queryName = scalabilityManager.getWriteZeroScalability("deactivateaccountquery"); deActivateAccountQuery = getQueryMapper().getCommonQuery(queryName); String usertabQueryName = scalabilityManager.getWriteZeroScalability("deactivateusertabaccountquery"); deActivateUserTabAccountQuery = getQueryMapper().getCommonQuery(usertabQueryName); Connection conn = null; try { conn = ds.getConnection(); /** * set aflag = 3 * set password to admin's password * set email to admin's email */ String[] params = { loginid, "3", adminPassword, adminEmail }; deActivateAccountQuery.run(conn, params); String[] params2 = { loginid }; deActivateUserTabAccountQuery.run(conn, params2); } catch (Exception e) { try { if (conn != null) { conn.close(); } } catch (Exception e1) { throw new BaseDaoException("error deActivateAccountQueryexception", e1); } throw new BaseDaoException("error deActivateAccountQueryexception", e); } try { if (conn != null) { conn.close(); } } catch (Exception e) { throw new BaseDaoException("error deActivateAccountQueryexception", e); } Fqn fqn = cacheUtil.fqn(DbConstants.ID_INFO); if (treeCache.exists(fqn, login)) { treeCache.remove(fqn, login); } fqn = cacheUtil.fqn(DbConstants.LOGIN_INFO); if (treeCache.exists(fqn, loginid)) { treeCache.remove(fqn, loginid); } } public void olddeActivateAccount(String login) { if (RegexStrUtil.isNull(login)) { throw new BaseDaoException("params are null"); } /** * hdlogin, hdprofile, usertab - no partitioned */ String sourceName = null; sourceName = scalabilityManager.getWriteZeroScalability(); ds = scalabilityManager.getSource(sourceName); if (ds == null) { throw new BaseDaoException("ds null " + sourceName); } Hdlogin hdlogin = getLoginid(login); String loginid = null; if (hdlogin != null) { loginid = hdlogin.getValue(DbConstants.LOGIN_ID); } Connection conn = null; try { conn = ds.getConnection(); conn.setAutoCommit(false); /** * set inform flags to zero, delete */ //int gender = 0; // male activateAccountQuery.run(conn, login, "0"); personalinfoDeleteQuery.run(conn, loginid); hdprofileDeleteQuery.run(conn, loginid); inboxDeleteAllQuery.run(conn, loginid); outboxDeleteAllQuery.run(conn, loginid); pblogDeleteQuery.run(conn, loginid); pblogDeleteMsgQuery.run(conn, loginid); pblogDeleteTopicQuery.run(conn, loginid); pblogDeleteTopicAttrQuery.run(conn, loginid); pblogDeleteMsgAttrQuery.run(conn, loginid); pblogDeleteTagsQuery.run(conn, loginid); yourkeyWordsDeleteQuery.run(conn, loginid); /* not required anymore */ //userTrafficDeleteQuery.run(conn, loginid); /* not required anymore */ pendingfriendsDeleteQuery.run(conn, loginid); hdfriendsDeleteQuery.run(conn, loginid); displayUserDeleteQuery.run(conn, loginid); shareUserDeleteQuery.run(conn, loginid); shareContactDeleteQuery.run(conn, loginid); memberContactDeleteQuery.run(conn, loginid); contactsDeleteQuery.run(conn, loginid); carryonDeleteQuery.run(conn, loginid); collblockDeleteQuery.run(conn, loginid); // deleting member as admin from collabrum, directories } catch (Exception e) { try { conn.rollback(); } catch (Exception e1) { try { if (conn != null) { conn.setAutoCommit(true); conn.close(); } } catch (Exception e2) { throw new BaseDaoException("connection close exception", e2); } throw new BaseDaoException("error occured while rollingback entries from hdlogin/hdprofile/usertab", e1); } } try { conn.commit(); } catch (Exception e3) { throw new BaseDaoException("commit exception", e3); } try { if (conn != null) { conn.setAutoCommit(true); conn.close(); } } catch (Exception e4) { throw new BaseDaoException("connection close exception", e4); } } /** * This method verifies if the user is allowed to login into account. * @param login - the login * @param password - the password * @return String - the status msg (success or failure) * @throws BaseDaoException If we have a problem interpreting the data or the data is missing or incorrect */ public String verifySignIn(String login, String password) { /** * Given a login and password, verify passwords */ if (RegexStrUtil.isNull(login) || RegexStrUtil.isNull(password)) { throw new BaseDaoException("params are null"); } /** * set the flag to false, if user exists */ Hdlogin hdlogin = getLoginid(login); String loginid = null; if (hdlogin != null) { if (hdlogin.getValue(DbConstants.AFLAG).equals("0")) { return webConstants.getNotactivatederror(); } if (!password.equalsIgnoreCase(hdlogin.getValue(DbConstants.PASSWORD))) { return webConstants.getPassworderror(); } } else { return DbConstants.INVALID_LOGIN; } return DbConstants.SUCCESS; } private boolean mailPassword(Hdlogin hdlogin) { if (hdlogin == null) { throw new BaseDaoException("params are null"); } String emailTo = hdlogin.getValue(DbConstants.EMAIL); if (RegexStrUtil.isNull(emailTo)) { throw new BaseDaoException("email is null"); } StringBuffer sb = new StringBuffer("Hi, "); sb.append((String) hdlogin.getValue(DbConstants.LOGIN)); sb.append("\n"); sb.append("Your login is: "); sb.append((String) hdlogin.getValue(DbConstants.LOGIN)); sb.append("\nPassword is: "); sb.append((String) hdlogin.getValue(DbConstants.PASSWORD)); sb.append("\n\nClick here to login "); sb.append(webConstants.getHddomain()); sb.append("/showlogin"); sb.append("\n\n Your activation code is: "); sb.append((String) hdlogin.getValue(DbConstants.ACODE)); sb.append("/n"); hdmail.sendEmail(emailTo, "Forgot password", sb.toString(), webConstants.getMailfrom()); return true; } /** * This method mails the password * @param login - the login * @return String - the status msg (success or failure) * @throws BaseDaoException If we have a problem interpreting the data or the data is missing or incorrect */ public String forgotPasswordByLogin(String login) throws BaseDaoException { if (RegexStrUtil.isNull(login)) { throw new BaseDaoException("params are null"); } Hdlogin hdlogin = getLoginid(login); if (hdlogin == null) { return DbConstants.INVALID_LOGIN; } if (mailPassword(hdlogin)) { return DbConstants.SUCCESS; } else { return DbConstants.FAILURE; } } /** * This method mails the password * @param email - the email * @return String - the status msg (success or failure) * @throws BaseDaoException If we have a problem interpreting the data or the data is missing or incorrect */ public String forgotPasswordByEmail(String email) throws BaseDaoException { if (RegexStrUtil.isNull(email)) { throw new BaseDaoException("params are null"); } Hdlogin hdlogin = null; try { Object[] params = { (Object) email }; List result = emailQuery.execute(params); if (result != null && result.size() > 0) { hdlogin = (Hdlogin) result.get(0); } } catch (Exception e) { throw new BaseDaoException("emailQuery error " + emailQuery.getSql(), e); } if (hdlogin == null) { return DbConstants.INVALID_EMAIL; } if (mailPassword(hdlogin)) { return DbConstants.SUCCESS; } else { return DbConstants.FAILURE; } } /** * Update the password */ public void updatePassword(String login, String password) { if (RegexStrUtil.isNull(login) || RegexStrUtil.isNull(password)) { throw new BaseDaoException("params are null"); } String queryName = scalabilityManager.getWriteZeroScalability("passwordupdatequery"); passwordUpdateQuery = getQueryMapper().getCommonQuery(queryName); Connection conn = null; try { conn = ds.getConnection(); String[] params = { password, login }; passwordUpdateQuery.run(conn, params); } catch (Exception e) { try { if (conn != null) { conn.close(); } } catch (Exception e1) { throw new BaseDaoException("conn.close() error, PasswordUpdateQuery ", e1); } throw new BaseDaoException("error occured while executing PasswordUpdateQuery", e); } try { if (conn != null) { conn.close(); } } catch (Exception e) { throw new BaseDaoException("conn.close() error after executing, PasswordUpdateQuery ", e); } } /** * This method signsout the user * @return String - the status msg (success or failure) * @throws BaseDaoException If we have a problem interpreting the data or the data is missing or incorrect */ /* public String signOut(String login) throws BaseDaoException { String sourceName = null; sourceName = scalabilityManager.getWriteZeroScalability(); ds = scalabilityManager.getSource(sourceName); if (ds == null) { throw new BaseDaoException("ds null " + sourceName); } Connection conn = null; try { conn = ds.getConnection(); signOutQuery.run(conn, login); } catch (Exception e) { try { if (conn != null) { conn.close(); } } catch(Exception e1) { throw new BaseDaoException("error in signOut, conn.close() error", e1); } throw new BaseDaoException("error in signOut", e); } try { if (conn != null) { conn.close(); } } catch (Exception e) { throw new BaseDaoException("conn.close() error", e); } return SUCCESS; } */ /** * checks to see if these properties are set by spring */ public void setJdbcSource(DataSource ds) { this.ds = ds; } public void setMailutils(SMTPTool mailutils) { this.hdmail = mailutils; } public void setWebconstants(GlobalConst webconstants) { this.webConstants = webconstants; } public void sethdprofileaddQuery(BaseCommonQuery haq) { this.hdprofileAddQuery = haq; } public void sethdloginaddQuery(BaseCommonQuery haq) { this.hdloginAddQuery = haq; } public void setpersonalinfoaddQuery(BaseCommonQuery paq) { this.personalinfoAddQuery = paq; } public void setHdprofileQuery(HdprofileQuery hpq) { this.hdprofileQuery = hpq; } public void setactivateaccountQuery(ActivateAccountQuery daq) { this.activateAccountQuery = daq; } public void setpersonalinfodeleteQuery(PersonalinfoDeleteQuery daq) { this.personalinfoDeleteQuery = daq; } public void sethdprofiledeleteQuery(HdprofileDeleteQuery daq) { this.hdprofileDeleteQuery = daq; } public void setuserdeletecobrandQuery(UserDeleteCobrandQuery daq) { this.deleteUserCobrandQuery = daq; } public void setpblogdeleteQuery(PblogDeleteQuery daq) { this.pblogDeleteQuery = daq; } public void setpblogmessagedeleteallQuery(PblogMessageDeleteAllQuery daq) { this.pblogDeleteMsgQuery = daq; } public void setpblogtopicdeleteallQuery(PblogTopicDeleteAllQuery daq) { this.pblogDeleteTopicQuery = daq; } public void setpblogtopicattrdeleteallQuery(PblogTopicAttrDeleteAllQuery daq) { this.pblogDeleteTopicAttrQuery = daq; } public void setpblogmsgattrdeleteallQuery(PblogMsgAttrDeleteAllQuery daq) { this.pblogDeleteMsgAttrQuery = daq; } public void setpblogtagdeleteQuery(PblogTagDeleteQuery daq) { this.pblogDeleteTagsQuery = daq; } public void setyourkeywordsdeleteQuery(YourkeywordsDeleteQuery daq) { this.yourkeyWordsDeleteQuery = daq; } /* not required anymore public void setusertrafficdeleteQuery(UserTrafficDeleteQuery daq) { this.userTrafficDeleteQuery = daq; } */ public void sethdloginemailQuery(HdloginEmailQuery daq) { this.emailQuery = daq; } /* public void setsignoutQuery(SignOutQuery daq) { this.signOutQuery = daq; } */ public void setdiaryadmin(DiaryAdmin daq) { this.diaryAdmin = daq; } public void setpasswordupdateQuery(BaseCommonQuery haq) { this.passwordUpdateQuery = haq; } }