D:\Sam\InterviewsMaterial\SiteWorx\SiteWorxTest\src\java\db_adapter\StoreUserDigitsInputDataObject_BD_Adapter.java
  1 /*
  2  * To change this template, choose Tools | Templates
  3  * and open the template in the editor.
  4  */
  5 package db_adapter;
  6 
  7 import java.io.*;
  8 import java.sql.Connection;
  9 import java.sql.DriverManager;
 10 import java.sql.PreparedStatement;
 11 import java.sql.ResultSet;
 12 import java.sql.SQLException;
 13 import java.util.ArrayList;
 14 import java.util.Hashtable;
 15 import java.util.MissingResourceException;
 16 import java.util.ResourceBundle;
 17 
 18 /**
 19  *
 20  * @author sameldin
 21  */
 22 import utils.*;
 23 import constants.*;
 24 import dataobjects.*;
 25 public class StoreUserDigitsInputDataObject_BD_Adapter 
 26 {
 27     Connection  localConnection = null;
 28     
 29     public StoreUserDigitsInputDataObject_BD_Adapter()
 30     {
 31         
 32     }
 33     /*
 34      * 
 35      */
 36     public StoreUserDigitsInputDataObject_BD_Adapter(UserDigitsInputDAO passedUserDigitsInputDAO)
 37     {
 38         BD_Connector localBD_Connector = new BD_Connector();
 39         localConnection = localBD_Connector.getConnection();  
 40         insertUpdate(Constants.DEFAULT_DATABASE_NAME_LABEL,  passedUserDigitsInputDAO);
 41     }
 42     /*
 43      *
 44      */
 45      public int insertUpdate(String passedTableName, UserDigitsInputDAO passedUserDigitsInputDAO)
 46      {
 47         if(null == localConnection)
 48             return(Constants.ERROR_RUN);
 49         PreparedStatement   localPreparedStatement      = null;
 50         ResultSet           localResultSet              = null;
 51         //==========================
 52         try
 53         {
 54                 String qryString        = "";
 55                 String tableName        = DatabaseKeysPropertyManager.getParameter(passedTableName);
 56                 //==========================
 57                 qryString       =       "SELECT * FROM  "
 58                                                 + tableName
 59                                                 + " WHERE USER_LAST_NAME = "
 60                                                 + "'"
 61                                                 + passedUserDigitsInputDAO.getUserLastName()
 62                                                 + "'"
 63                                                 + " AND "
 64                                                 + "USER_ID_NUMBER = "
 65                                                 + "'"
 66                                                 + passedUserDigitsInputDAO.getUserID()
 67                                                 + "'";
 68                 localPreparedStatement = localConnection.prepareStatement(qryString);
 69                 localPreparedStatement.executeQuery();
 70                 //==========================
 71                 int numberOfRecords = 0;
 72                 localResultSet = localPreparedStatement.executeQuery();
 73                 if(localResultSet.next())
 74                         numberOfRecords = 1;
 75                 // close - sam
 76                 localPreparedStatement.close();
 77                 //=============== Start of updating/insert table =========================
 78                         switch(numberOfRecords)
 79                 {
 80                         case 0:
 81                             qryString   =       "INSERT INTO "
 82                                                 + tableName
 83                                                 + "(USER_ID_NUMBER, "
 84                                                 + "USER_FIRST_NAME, "
 85                                                 + "USER_LAST_NAME, "
 86                                                 + "USER_EMAIL_ADDRESS, "
 87                                                 + "TOTAL_COUNT, "
 88                                                 + "SUM_VALUE,"
 89                                                 + "AVERAGE_VALUE, "
 90                                                 + "MEDIAN_VALUE, "
 91                                                 + "HIGHTEST_VALUE, "
 92                                                 + "LOWEST_VALUE)"
 93                                                 + "VALUES "
 94                                                 + "("
 95                                                 + "'"
 96                                                 + passedUserDigitsInputDAO.getUserID()
 97                                                 + "',"
 98                                                 + "'"
 99                                                 + passedUserDigitsInputDAO.getUserFirstName()
100                                                 + "',"
101                                                 + "'"
102                                                 + passedUserDigitsInputDAO.getUserLastName()
103                                                 + "',"
104                                                 + "'"
105                                                 + passedUserDigitsInputDAO.getUserEmailAddress()
106                                                 + "',"
107                                                 + "'"
108                                                 + passedUserDigitsInputDAO.getTotalCount()
109                                                 + "',"
110                                                 + "'"
111                                                 + passedUserDigitsInputDAO.getSumValue()
112                                                 + "',"
113                                                 + "'"
114                                                 + passedUserDigitsInputDAO.getAverageValue()
115                                                 + "',"
116                                                 + "'"
117                                                 + passedUserDigitsInputDAO.getMedianValue()
118                                                 + "',"
119                                                 + "'"
120                                                 + passedUserDigitsInputDAO.getHighestValue()
121                                                 + "',"
122                                                 + "'"
123                                                 + passedUserDigitsInputDAO.getLowestValue()
124                                                 + "')";
125                                     localPreparedStatement = localConnection.prepareStatement(qryString);
126                                     localPreparedStatement.executeQuery();
127                                     localPreparedStatement.close();
128                                         break;
129                         default:
130                                 qryString       =       "UPDATE "
131                                                 + tableName
132                                                 + "(USER_ID_NUMBER, "
133                                                 + "USER_FIRST_NAME, "
134                                                 + "USER_LAST_NAME, "
135                                                 + "USER_EMAIL_ADDRESS, "
136                                                 + "TOTAL_COUNT, "
137                                                 + "SUM_VALUE,"
138                                                 + "AVERAGE_VALUE, "
139                                                 + "MEDIAN_VALUE, "
140                                                 + "HIGHTEST_VALUE, "
141                                                 + "LOWEST_VALUE)"
142                                                 + "VALUES "
143                                                 + "("
144                                                 + "'"
145                                                 + passedUserDigitsInputDAO.getUserID()
146                                                 + "',"
147                                                 + "'"
148                                                 + passedUserDigitsInputDAO.getUserFirstName()
149                                                 + "',"
150                                                 + "'"
151                                                 + passedUserDigitsInputDAO.getUserLastName()
152                                                 + "',"
153                                                 + "'"
154                                                 + passedUserDigitsInputDAO.getUserEmailAddress()
155                                                 + "',"
156                                                 + "'"
157                                                 + passedUserDigitsInputDAO.getTotalCount()
158                                                 + "',"
159                                                 + "'"
160                                                 + passedUserDigitsInputDAO.getSumValue()
161                                                 + "',"
162                                                 + "'"
163                                                 + passedUserDigitsInputDAO.getAverageValue()
164                                                 + "',"
165                                                 + "'"
166                                                 + passedUserDigitsInputDAO.getMedianValue()
167                                                 + "',"
168                                                 + "'"
169                                                 + passedUserDigitsInputDAO.getHighestValue()
170                                                 + "',"
171                                                 + "'"
172                                                 + passedUserDigitsInputDAO.getLowestValue()
173                                                 + "')";
174                                     localPreparedStatement = localConnection.prepareStatement(qryString);
175                                     localPreparedStatement.executeQuery();
176                                     localPreparedStatement.close();
177                 }
178 }
179         catch(MissingResourceException eMissingResourceException)
180         {
181             eMissingResourceException.printStackTrace();
182             return(Constants.ERROR_RUN);
183         }
184         catch(SQLException eSQLException)
185         {
186             eSQLException.printStackTrace();
187             return(Constants.ERROR_RUN);
188         }        
189         return(Constants.NORMAL_RUN);
190      }
191 }
192