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