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 11 package com.sun.ebxml.registry.persistence.rdb; 12 13 import java.sql.ResultSet; 14 import java.sql.SQLException; 15 import java.sql.Statement; 16 import java.util.ArrayList; 17 import java.util.Iterator; 18 19 import org.oasis.ebxml.registry.bindings.query.ResponseOption; 20 import org.oasis.ebxml.registry.bindings.rim.ExternalIdentifier; 21 import org.oasis.ebxml.registry.bindings.rim.ObjectRef; 22 import org.oasis.ebxml.registry.bindings.rim.User; 23 24 import com.sun.ebxml.registry.RegistryException; 25 import com.sun.ebxml.registry.lcm.ObjectsNotFoundException; 26 import com.sun.ebxml.registry.lcm.ReferencedObjectNotFoundException; 27 import com.sun.ebxml.registry.lcm.RegistryObjectExistsException; 28 import com.sun.ebxml.registry.util.BindingUtility; 29 30 31 /*** 32 * ExternalIdentifier instances provide the additional identifier information 33 * to RegistryEntry such as DUNS number, Social Security Number, or an alias 34 * name of the organization. The attribute name inherited from Object is 35 * used to contain the identification scheme (Social Security Number, etc), 36 * and the attribute value contains the actual information. Each RegistryEntry 37 * may have 0 or more association(s) with ExternalIdentifier. 38 * 39 * @see <{RegistryEntry}> 40 * @author Farrukh S. Najmi 41 * @author Adrian Chong 42 */ 43 public class ExternalIdentifierDAO extends RegistryObjectDAO { 44 protected ExternalIdentifierDAO(){} 45 46 public static String getTableNameStatic() { 47 return "ExternalIdentifier"; 48 } 49 50 public String getTableName() { 51 return getTableNameStatic(); 52 } 53 54 public void insert(User user, java.sql.Connection connection, 55 ArrayList extIds) throws RegistryException { 56 throw new RegistryException("ExternalIdentifier cannot be first level! Spec. bug!"); 57 } 58 /*** 59 * Does a bulk insert of a Collection of objects that match the type for this persister. 60 * 61 */ 62 public void insert(User user, java.sql.Connection connection, String parentId, 63 ArrayList extIds) throws RegistryException { 64 if (extIds.size()==0) { 65 return; 66 } 67 System.err.println("Inserting " + extIds.size() + " ExternalIdentifiers"); 68 String extIdId = null; 69 Statement stmt = null; 70 try { 71 72 stmt = connection.createStatement(); 73 74 Iterator iter = extIds.iterator(); 75 while (iter.hasNext()) { 76 ExternalIdentifier extId = (ExternalIdentifier)iter.next(); 77 extIdId = extId.getId(); 78 //Log.print(Log.TRACE, 8, "\tDATABASE EVENT: storing ExternalIdentifier " ); 79 80 String schemeId = BindingUtility.getInstance().getObjectId(extId.getIdentificationScheme()); 81 82 // check whether the referenced ClassificationScheme exist 83 // in database, if it is specified by ObjectRef 84 if (extId.getIdentificationScheme() instanceof ObjectRef) { 85 String objectType = getObjectType(connection, schemeId 86 , super.getTableName()); 87 if (objectType==null) { 88 throw new ReferencedObjectNotFoundException( 89 "The ClassificationScheme " + schemeId + 90 " referenced by ExternalIdentifier " + extIdId + 91 " does not exist" ); 92 } 93 else if (!objectType.equalsIgnoreCase("ClassificationScheme")){ 94 throw new ReferencedObjectNotFoundException( 95 "The object " + schemeId + 96 " referenced by ExternalIdentifier " + extIdId + 97 " exists, but the objectType is not " + 98 "ClassificationScheme" ); 99 } 100 101 } 102 103 // How can i get the parent RegistryObject?????? 104 /* 105 String roId = BindingUtility.getInstance().getObjectId(extId.getRegistryObject()); 106 // check whether the referenced RegistryObject exist 107 // in database, if it is specified by ObjectRef 108 if (extId.getRegistryObject() instanceof ObjectRef) { 109 if (registryObjectExist(connection, roId)) { 110 throw new ReferencedObjectNotFoundException( 111 "The RegistryObject " + roId + 112 " referenced by ExternalIdentifier " + extIdId + 113 " does not exist" ); 114 } 115 } 116 */ 117 String value = extId.getValue(); 118 119 String str = "INSERT INTO ExternalIdentifier " + 120 "VALUES(null, " + //AccessControlPolicy 121 "'" + extId.getId() + "', " + 122 "'ExternalIdentifier', " + 123 "'" + parentId + "', " + 124 "'" + schemeId + "', " + 125 "'" + value + "' )"; 126 127 System.err.println("stmt = " + str); 128 stmt.addBatch(str); 129 130 } 131 132 if (extIds.size() > 0) { 133 int [] updateCounts = stmt.executeBatch(); 134 //stmt.close(); 135 generateAuditbleEvent(connection, extIds, "Created", user); 136 super.insert(user, connection, extIds); 137 } 138 } 139 catch (java.sql.SQLException e) { 140 e.printStackTrace(); 141 // It is trying to find out whether the SQLException is because 142 // the ExternalIdentifier already exist 143 if (extIdId != null && registryObjectExist(connection, extIdId)) { 144 throw new RegistryObjectExistsException(extIdId); 145 } 146 RegistryException exception = new RegistryException(e); 147 throw exception; 148 } finally { 149 try { 150 if (stmt != null) 151 stmt.close(); 152 } catch (SQLException sqle) { 153 sqle.printStackTrace(); 154 } 155 } 156 } 157 158 public void update(User user, java.sql.Connection connection, 159 ArrayList extIds) throws RegistryException { 160 throw new RegistryException("ExternalIdentifier cannot be first level! Spec. bug!"); 161 } 162 163 /*** 164 * Does a bulk update of a ArrayList of ExternalIdentifier. The attribute 165 * registryObject is not mutable 166 */ 167 public void update(User user, java.sql.Connection connection, String parentId, 168 ArrayList extIds) throws RegistryException { 169 System.err.println("Updating " + extIds.size() + " ExternalIdentifiers"); 170 String extIdId = null; 171 Statement stmt = null; 172 try { 173 stmt = connection.createStatement(); 174 Iterator iter = extIds.iterator(); 175 while (iter.hasNext()) { 176 ExternalIdentifier extId = (ExternalIdentifier)iter.next(); 177 extIdId = extId.getId(); 178 179 // checking whether it exists 180 if (!registryObjectExist(connection, extIdId, getTableName())) { 181 ArrayList idList = new ArrayList(); 182 idList.add(extIdId); 183 throw new ObjectsNotFoundException(idList); 184 } 185 186 //Log.print(Log.TRACE, 8, "\tDATABASE EVENT: updating ExternalIdentifier " ); 187 188 String schemeId = BindingUtility.getInstance().getObjectId(extId.getIdentificationScheme()); 189 190 // check whether the referenced ClassificationScheme exist 191 // in database, if it is specified by ObjectRef 192 if (extId.getIdentificationScheme() instanceof ObjectRef) { 193 String objectType = getObjectType(connection, schemeId 194 , super.getTableName()); 195 if (objectType==null) { 196 throw new ReferencedObjectNotFoundException( 197 "The ClassificationScheme " + schemeId + 198 " referenced by ExternalIdentifier " + extIdId + 199 " does not exist" ); 200 } 201 else if (!objectType.equalsIgnoreCase("ClassificationScheme")){ 202 throw new ReferencedObjectNotFoundException( 203 "The object " + schemeId + 204 " referenced by ExternalIdentifier " + extIdId + 205 " exists, but the objectType is not " + 206 "ClassificationScheme" ); 207 } 208 209 } 210 211 String value = extId.getValue(); 212 213 // registryObject is not mutable 214 String str = "UPDATE " + getTableName() + 215 " SET accessControlPolicy=null" + 216 ", identificationScheme='" + schemeId + "'" + 217 ", value='" + value + "'" + 218 " WHERE id='" + extIdId + "'"; 219 220 System.err.println("stmt = " + str); 221 stmt.addBatch(str); 222 } 223 if (extIds.size() > 0) { 224 int [] updateCounts = stmt.executeBatch(); 225 //stmt.close(); 226 // Updated composed Name, Descrption, Slot, ExternalIdentifier and Classification 227 super.update(user, connection, extIds); 228 generateAuditbleEvent(connection, extIds, "Updated", user); 229 } 230 } 231 catch (java.sql.SQLException e) { 232 e.printStackTrace(); 233 RegistryException exception = new RegistryException(e); 234 throw exception; 235 } finally { 236 try { 237 if (stmt != null) 238 stmt.close(); 239 } catch (SQLException sqle) { 240 sqle.printStackTrace(); 241 } 242 } 243 244 } 245 246 /*** 247 * Gets objects for specified parentIds 248 * 249 */ 250 public ArrayList getByParentId(java.sql.Connection connection, String parentId, ArrayList objectRefs) throws RegistryException { 251 ArrayList extIds = new ArrayList(); 252 Statement stmt = null; 253 try 254 { 255 stmt = connection.createStatement(); 256 ResultSet rs = null; 257 258 259 rs = stmt.executeQuery("SELECT * from " + getTableName() + " WHERE registryObject = '" + 260 parentId + "' "); 261 262 while (rs.next()) { 263 ExternalIdentifier obj = new ExternalIdentifier(); 264 265 ResponseOption responseOption = new ResponseOption(); 266 267 responseOption.setReturnComposedObjects(true); 268 loadObjectFromResultSet(connection, obj, rs, responseOption, objectRefs); 269 extIds.add(obj); 270 } 271 } 272 catch (java.sql.SQLException e) { 273 throw new RegistryException(e); 274 } finally { 275 try { 276 if (stmt != null) 277 stmt.close(); 278 } catch (SQLException sqle) { 279 sqle.printStackTrace(); 280 } 281 } 282 283 284 return extIds; 285 } 286 287 /*** 288 * Does a bulk delete of objects for specified parentIds 289 * 290 */ 291 public void deleteByParentIds(User user, java.sql.Connection connection, ArrayList parentIds) throws RegistryException { 292 Statement stmt = null; 293 try 294 { 295 stmt = connection.createStatement(); 296 ResultSet rs = null; 297 298 ArrayList ids = new ArrayList(); 299 300 Iterator iter = parentIds.iterator(); 301 while (iter.hasNext()) { 302 String parentId = (String)iter.next(); 303 304 rs = stmt.executeQuery("SELECT id from " + getTableName() + " WHERE registryObject = '" + 305 parentId + "' "); 306 307 while (rs.next()) { 308 String id = rs.getString(1); 309 ids.add(id); 310 } 311 } 312 313 if (ids.size() > 0) { 314 delete(user, connection, ids); 315 } 316 //stmt.close(); 317 } 318 catch (java.sql.SQLException e) { 319 RegistryException exception = new RegistryException(e); 320 throw exception; 321 } finally { 322 try { 323 if (stmt != null) 324 stmt.close(); 325 } catch (SQLException sqle) { 326 sqle.printStackTrace(); 327 } 328 } 329 330 } 331 332 public void loadObjectFromResultSet(java.sql.Connection conn, Object obj, ResultSet rs, ResponseOption responseOption, ArrayList objectRefs) throws RegistryException { 333 try { 334 if (!(obj instanceof org.oasis.ebxml.registry.bindings.rim.ExternalIdentifier)) { 335 throw new RegistryException("Unexpected object " + obj + ". Was expecting org.oasis.ebxml.registry.bindings.rim.ExternalIdentifier."); 336 } 337 338 ExternalIdentifier ei = (ExternalIdentifier)obj; 339 super.loadObjectFromResultSet(conn, obj, rs, responseOption, objectRefs); 340 341 String schemeId = rs.getString("identificationScheme"); 342 343 ObjectRef or = null; 344 if (schemeId != null) { 345 or = new ObjectRef(); 346 or.setId(schemeId); 347 objectRefs.add(or); 348 } 349 ei.setIdentificationScheme(or); 350 351 String value = rs.getString("value"); 352 ei.setValue(value); 353 354 } 355 catch (SQLException e) { 356 e.printStackTrace(); 357 throw new RegistryException(e); 358 } 359 } 360 361 public ArrayList getLeafObjectList(java.sql.Connection conn, ResultSet rs, ResponseOption responseOption, ArrayList objectRefs) throws RegistryException { 362 ArrayList res = new ArrayList(); 363 364 try { 365 while(rs.next()) { 366 ExternalIdentifier obj = new ExternalIdentifier(); 367 loadObjectFromResultSet(conn, obj, rs, responseOption, objectRefs); 368 369 res.add(obj); 370 } 371 } 372 catch (SQLException e) { 373 e.printStackTrace(); 374 throw new RegistryException(e); 375 } 376 377 378 return res; 379 } 380 }

This page was automatically generated by Maven