View Javadoc
1 /* 2 * ==================================================================== 3 * 4 * This code is subject to the freebxml License, Version 1.1 5 * 6 * Copyright (c) 2001 - 2003 freebxml.org. All rights reserved. 7 * 8 * ==================================================================== 9 */ 10 package com.sun.ebxml.registry.persistence.rdb; 11 12 import com.sun.ebxml.registry.RegistryException; 13 import com.sun.ebxml.registry.lcm.ObjectsNotFoundException; 14 import com.sun.ebxml.registry.lcm.ReferencesExistException; 15 import com.sun.ebxml.registry.security.authorization.AuthorizationServiceImpl; 16 import com.sun.ebxml.registry.util.BindingUtility; 17 18 import org.oasis.ebxml.registry.bindings.query.ResponseOption; 19 import org.oasis.ebxml.registry.bindings.rim.Address; 20 import org.oasis.ebxml.registry.bindings.rim.EmailAddress; 21 import org.oasis.ebxml.registry.bindings.rim.ObjectRef; 22 import org.oasis.ebxml.registry.bindings.rim.PersonName; 23 import org.oasis.ebxml.registry.bindings.rim.TelephoneNumber; 24 import org.oasis.ebxml.registry.bindings.rim.User; 25 26 import java.sql.ResultSet; 27 import java.sql.SQLException; 28 import java.sql.Statement; 29 30 import java.util.ArrayList; 31 import java.util.Iterator; 32 33 34 /*** 35 * 36 * @author Farrukh S. Najmi 37 * @version 38 */ 39 public class UserDAO extends RegistryObjectDAO { 40 static BindingUtility bu = BindingUtility.getInstance(); 41 42 public static String getTableNameStatic() { 43 return "User_"; 44 } 45 46 public String getTableName() { 47 return getTableNameStatic(); 48 } 49 50 /*** 51 * Does a bulk insert of a Collection of objects that match the type for this persister. 52 * 53 */ 54 public void insert(User submittingUser, java.sql.Connection connection, 55 ArrayList users) throws RegistryException { 56 if (users.size() == 0) { 57 return; 58 } 59 60 String userId = null; 61 Statement stmt = null; 62 63 try { 64 // Update the exisitng User 65 users = updateExistingObjects(submittingUser, connection, users); 66 67 // Check any new user-s 68 if (users.size() == 0) { 69 return; 70 } 71 72 System.err.println("Inserting " + users.size() + " Users"); 73 74 stmt = connection.createStatement(); 75 76 Iterator iter = users.iterator(); 77 EmailAddressDAO emailAddressDAO = new EmailAddressDAO(); 78 PostalAddressDAO postalAddressDAO = new PostalAddressDAO(); 79 TelephoneNumberDAO telephoneNumberDAO = new TelephoneNumberDAO(); 80 81 while (iter.hasNext()) { 82 User user = (User) iter.next(); 83 userId = user.getId(); 84 85 //Log.print(Log.TRACE, 8, "\tDATABASE EVENT: storing User " ); 86 String orgId = bu.getObjectId(user.getOrganization()); 87 88 PersonName personName = user.getPersonName(); 89 90 String firstName = personName.getFirstName(); 91 92 if (firstName != null) { 93 firstName = "'" + firstName + "'"; 94 } 95 96 String middleName = personName.getMiddleName(); 97 98 if (middleName != null) { 99 middleName = "'" + middleName + "'"; 100 } 101 102 String lastName = personName.getLastName(); 103 104 if (lastName != null) { 105 lastName = "'" + lastName + "'"; 106 } 107 108 String url = user.getUrl(); 109 110 if (url != null) { 111 url = "'" + url + "'"; 112 } 113 114 String str = "INSERT INTO User_ " + "VALUES(null, " + //AccessControlPolicy 115 "'" + user.getId() + "', " + "'User', 'junk', " + //the email column should not be there 116 "'" + orgId + "', " + firstName + ", " + middleName + ", " + 117 lastName + ", " + url + " )"; 118 119 System.err.println("stmt = '" + str + "'"); 120 stmt.addBatch(str); 121 } 122 123 if (users.size() > 0) { 124 int[] updateCounts = stmt.executeBatch(); 125 //stmt.close(); 126 super.insert(submittingUser, connection, users); 127 128 // Now generate the AE for inserting User 129 generateAuditbleEvent(connection, users, "Created", 130 submittingUser); 131 132 postalAddressDAO.insert(connection, users); 133 emailAddressDAO.insert(connection, users); 134 telephoneNumberDAO.insert(connection, users); 135 } 136 } catch (java.sql.SQLException e) { 137 e.printStackTrace(); 138 139 RegistryException exception = new RegistryException(e); 140 throw exception; 141 } finally { 142 try { 143 if (stmt != null) 144 stmt.close(); 145 } catch (SQLException sqle) { 146 sqle.printStackTrace(); 147 } 148 } 149 } 150 151 public void update(User submittingUser, java.sql.Connection connection, 152 ArrayList users) throws RegistryException { 153 /* 154 We do not need to check whether the user is authorized to update because the authorization is expected to be checked by higher level. 155 Should we move the authorization to persistance layer?????? 156 */ 157 update(submittingUser, connection, users, true, false); 158 } 159 160 public void update(User submittingUser, java.sql.Connection connection, 161 ArrayList users, boolean checkExistence, boolean checkAuthorization) 162 throws RegistryException { 163 if (users.size() == 0) { 164 return; 165 } 166 167 System.err.println("Updating " + users.size() + " Users"); 168 169 String userId = null; 170 Statement stmt = null; 171 172 try { 173 // Check whether the user can update the objects 174 if (checkAuthorization) { 175 AuthorizationServiceImpl authorization = AuthorizationServiceImpl.getInstance(); 176 authorization.checkAuthorization(submittingUser, 177 bu.getIdsFromRegistryObjects(users), 178 AuthorizationServiceImpl.UPDATE_OBJECTS_REQUEST); 179 } 180 181 stmt = connection.createStatement(); 182 183 Iterator iter = users.iterator(); 184 EmailAddressDAO emailAddressDAO = new EmailAddressDAO(); 185 PostalAddressDAO postalAddressDAO = new PostalAddressDAO(); 186 TelephoneNumberDAO telephoneNumberDAO = new TelephoneNumberDAO(); 187 ArrayList usersIds = new ArrayList(); 188 189 while (iter.hasNext()) { 190 User user = (User) iter.next(); 191 userId = user.getId(); 192 usersIds.add(userId); 193 194 //Log.print(Log.TRACE, 8, "\tDATABASE EVENT: updating User " ); 195 // checking whether it exists 196 if (checkExistence && 197 !registryObjectExist(connection, userId, getTableName())) { 198 ArrayList idList = new ArrayList(); 199 idList.add(userId); 200 throw new ObjectsNotFoundException(idList); 201 } 202 203 String orgId = bu.getObjectId(user.getOrganization()); 204 205 String url = user.getUrl(); 206 207 if (url != null) { 208 url = "'" + url + "'"; 209 } 210 211 // personName is non-mutable 212 String str = "UPDATE " + getTableName() + 213 " SET accessControlPolicy=null" + ", organization='" + 214 orgId + "'" + ", url=" + url + " WHERE id='" + userId + 215 "'"; 216 217 System.err.println("stmt = '" + str + "'"); 218 stmt.addBatch(str); 219 } 220 // end looping users 221 222 if (users.size() > 0) { 223 int[] updateCounts = stmt.executeBatch(); 224 //stmt.close(); 225 226 // Updated composed Name, Descrption, Slot, ExternalIdentifier and Classification 227 super.update(submittingUser, connection, users); 228 229 // Now generate the AE for updating User 230 generateAuditbleEvent(connection, users, "Updated", 231 submittingUser); 232 233 // updating PostalAddress 234 postalAddressDAO.delete(connection, usersIds); 235 postalAddressDAO.insert(connection, users); 236 237 // updating EmailAddress 238 emailAddressDAO.delete(connection, usersIds); 239 emailAddressDAO.insert(connection, users); 240 241 // updating TelephoneNumber 242 telephoneNumberDAO.delete(connection, usersIds); 243 telephoneNumberDAO.insert(connection, users); 244 } 245 } catch (java.sql.SQLException e) { 246 e.printStackTrace(); 247 248 RegistryException exception = new RegistryException(e); 249 throw exception; 250 } finally { 251 try { 252 if (stmt != null) 253 stmt.close(); 254 } catch (SQLException sqle) { 255 sqle.printStackTrace(); 256 } 257 } 258 } 259 260 // Should we make index on primaryContact in Organization???? 261 protected String checkOrganizationReferences(java.sql.Connection conn, 262 String userId) throws RegistryException { 263 String orgId = null; 264 Statement stmt = null; 265 266 try { 267 stmt = conn.createStatement(); 268 String sql = "SELECT id FROM Organization WHERE " + 269 "primaryContact='" + userId + "'"; 270 ResultSet rs = stmt.executeQuery(sql); 271 272 if (rs.next()) { 273 orgId = rs.getString("id"); 274 } 275 276 //stmt.close(); 277 278 return orgId; 279 } catch (SQLException e) { 280 throw new RegistryException(e); 281 } finally { 282 try { 283 if (stmt != null) 284 stmt.close(); 285 } catch (SQLException sqle) { 286 sqle.printStackTrace(); 287 } 288 } 289 } 290 291 /*** 292 * Does a bulk delete of a Collection of objects that match the type for this persister. 293 * 294 */ 295 public void delete(User user, java.sql.Connection connection, 296 ArrayList usersIds) throws RegistryException { 297 Iterator iter = usersIds.iterator(); 298 299 // Check whether this User is primaryContact of any Organization 300 while (iter.hasNext()) { 301 String userId = (String) iter.next(); 302 String orgId = checkOrganizationReferences(connection, userId); 303 304 if (orgId != null) { 305 throw new ReferencesExistException("Organization " + orgId + 306 " is referencing " + userId); 307 } 308 } 309 310 super.delete(user, connection, usersIds); 311 312 PostalAddressDAO postalAddressDAO = new PostalAddressDAO(); 313 postalAddressDAO.delete(connection, usersIds); 314 315 EmailAddressDAO emailAddressDAO = new EmailAddressDAO(); 316 emailAddressDAO.delete(connection, usersIds); 317 318 TelephoneNumberDAO telephoneNumberDAO = new TelephoneNumberDAO(); 319 telephoneNumberDAO.delete(connection, usersIds); 320 } 321 322 public void loadObjectFromResultSet(java.sql.Connection conn, Object obj, 323 ResultSet rs, ResponseOption responseOption, ArrayList objectRefs) 324 throws RegistryException { 325 try { 326 if (!(obj instanceof org.oasis.ebxml.registry.bindings.rim.User)) { 327 throw new RegistryException("Unexpected object " + obj + 328 ". Was expecting org.oasis.ebxml.registry.bindings.rim.User."); 329 } 330 331 User user = (User) obj; 332 super.loadObjectFromResultSet(conn, obj, rs, responseOption, 333 objectRefs); 334 335 String orgId = rs.getString("organization"); 336 ObjectRef or = new ObjectRef(); 337 or.setId(orgId); 338 user.setOrganization(or); 339 340 String firstName = rs.getString("personName_firstName"); 341 String middleName = rs.getString("personName_middleName"); 342 String lastName = rs.getString("personName_lastName"); 343 344 PersonName pn = new PersonName(); 345 pn.setFirstName(firstName); 346 pn.setMiddleName(middleName); 347 pn.setLastName(lastName); 348 349 user.setPersonName(pn); 350 351 String url = rs.getString("url"); 352 user.setUrl(url); 353 354 Address address = new Address(); 355 PostalAddressDAO postalAddressDAO = new PostalAddressDAO(); 356 postalAddressDAO.getPostalAddressByParent(conn, address, 357 user.getId()); 358 user.setAddress(address); 359 360 TelephoneNumberDAO telephoneNumberDAO = new TelephoneNumberDAO(); 361 ArrayList phones = telephoneNumberDAO.getTelephoneNumbersByParent(conn, 362 user.getId(), TelephoneNumber.class); 363 TelephoneNumber[] phones1 = new TelephoneNumber[phones.size()]; 364 Iterator iter = phones.iterator(); 365 int i = 0; 366 367 while (iter.hasNext()) { 368 phones1[i++] = (TelephoneNumber) iter.next(); 369 } 370 371 user.setTelephoneNumber(phones1); 372 373 EmailAddressDAO emailAddressDAO = new EmailAddressDAO(); 374 ArrayList emails = emailAddressDAO.getEmailAddressesByParent(conn, 375 user.getId()); 376 EmailAddress[] emails1 = new EmailAddress[emails.size()]; 377 iter = emails.iterator(); 378 i = 0; 379 380 while (iter.hasNext()) { 381 emails1[i++] = (EmailAddress) iter.next(); 382 } 383 384 user.setEmailAddress(emails1); 385 } catch (SQLException e) { 386 e.printStackTrace(); 387 throw new RegistryException(e); 388 } 389 } 390 391 public ArrayList getLeafObjectList(java.sql.Connection conn, ResultSet rs, 392 ResponseOption responseOption, ArrayList objectRefs) 393 throws RegistryException { 394 ArrayList res = new ArrayList(); 395 396 try { 397 while (rs.next()) { 398 User obj = new User(); 399 loadObjectFromResultSet(conn, obj, rs, responseOption, 400 objectRefs); 401 402 res.add(obj); 403 } 404 } catch (SQLException e) { 405 e.printStackTrace(); 406 throw new RegistryException(e); 407 } 408 409 return res; 410 } 411 }

This page was automatically generated by Maven