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.ReferencedObjectNotFoundException; 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.Classification; 20 import org.oasis.ebxml.registry.bindings.rim.ObjectRef; 21 import org.oasis.ebxml.registry.bindings.rim.User; 22 23 import java.sql.ResultSet; 24 import java.sql.SQLException; 25 import java.sql.Statement; 26 27 import java.util.ArrayList; 28 import java.util.Iterator; 29 30 31 public class ClassificationDAO extends RegistryObjectDAO { 32 static BindingUtility bu = BindingUtility.getInstance(); 33 34 protected ClassificationDAO() { 35 } 36 37 public static String getTableNameStatic() { 38 return "Classification"; 39 } 40 41 public String getTableName() { 42 return getTableNameStatic(); 43 } 44 45 /*** 46 * Does a bulk insert of a Collection of objects that match the type for this persister. 47 * 48 */ 49 public void insert(User user, java.sql.Connection connection, 50 ArrayList classifications) throws RegistryException { 51 if (classifications.size() == 0) { 52 return; 53 } 54 55 String classId = null; 56 Statement stmt = null; 57 58 try { 59 // Update the exisitng Classification 60 classifications = updateExistingObjects(user, connection, 61 classifications); 62 63 if (classifications.size() == 0) { 64 return; 65 } 66 67 System.err.println("Inserting " + classifications.size() + 68 " Classifications"); 69 70 stmt = connection.createStatement(); 71 72 Iterator iter = classifications.iterator(); 73 74 while (iter.hasNext()) { 75 Classification classification = (Classification) iter.next(); 76 classId = classification.getId(); 77 78 //Log.print(Log.TRACE, 8, "\tDATABASE EVENT: storing Classification " ); 79 // check whether the referenced ClassificationNode exist 80 // in database, if it is specified by ObjectRef 81 if (classification.getClassificationNode() instanceof ObjectRef) { 82 String nodeId = ((ObjectRef) classification.getClassificationNode()).getId(); 83 String tableName = ClassificationNodeDAO.getTableNameStatic(); 84 if (!registryObjectExist(connection, nodeId, tableName)) { 85 86 String objectType = getObjectType(connection, nodeId, 87 super.getTableName()); 88 89 if (objectType == null) { 90 throw new ReferencedObjectNotFoundException( 91 "The ClassificationNode " + nodeId + 92 " referenced by Classification " + 93 classification.getId() + " does not exist"); 94 } else if (!objectType.equalsIgnoreCase( 95 "ClassificationNode")) { 96 throw new ReferencedObjectNotFoundException( 97 "The object " + nodeId + 98 " referenced by Classification " + 99 classification.getId() + 100 " exists, but the objectType is not " + 101 "ClassificationNode"); 102 } 103 } 104 } 105 106 // check whether the classified object exists 107 // in database, if it is specified by ObjectRef 108 if (classification.getClassifiedObject() instanceof ObjectRef) { 109 String classifiedObjId = ((ObjectRef) classification.getClassifiedObject()).getId(); 110 111 if (!registryObjectExist(connection, classifiedObjId)) { 112 throw new ReferencedObjectNotFoundException( 113 "The object " + classifiedObjId + 114 " referenced by Classification " + 115 classification.getId() + " does not exist"); 116 } 117 } 118 119 String classificationNodeId = bu.getObjectId(classification.getClassificationNode()); 120 121 if (classificationNodeId != null) { 122 classificationNodeId = "'" + classificationNodeId + "'"; 123 } 124 125 String schemeId = bu.getObjectId(classification.getClassificationScheme()); 126 127 if (schemeId != null) { 128 schemeId = "'" + schemeId + "'"; 129 } 130 131 String classifiedObjectId = bu.getObjectId(classification.getClassifiedObject()); 132 133 String nodeRep = classification.getNodeRepresentation(); 134 135 if (nodeRep != null) { 136 nodeRep = "'" + nodeRep + "'"; 137 } 138 139 String str = "INSERT INTO Classification " + "VALUES(null, " + //AccessControlPolicy 140 "'" + classification.getId() + "', " + 141 "'Classification', " + classificationNodeId + ", " + 142 schemeId + ", " + "'" + classifiedObjectId + "', " + 143 nodeRep + " )"; 144 145 //System.err.println("stmt = " + str); 146 stmt.addBatch(str); 147 } 148 149 if (classifications.size() > 0) { 150 int[] updateCounts = stmt.executeBatch(); 151 //stmt.close(); 152 super.insert(user, connection, classifications); 153 154 // Now generate the AE for inserting Classification 155 generateAuditbleEvent(connection, classifications, "Created", 156 user); 157 } 158 } catch (java.sql.SQLException e) { 159 e.printStackTrace(); 160 161 RegistryException exception = new RegistryException(e); 162 throw exception; 163 } finally { 164 try { 165 if (stmt != null) 166 stmt.close(); 167 } catch (SQLException sqle) { 168 sqle.printStackTrace(); 169 } 170 } 171 172 } 173 174 public void update(User user, java.sql.Connection connection, 175 ArrayList classifications) throws RegistryException { 176 /* 177 We do not need to check whether the user is authorized to update because the authorization is expected to be checked by higher level. 178 Should we move the authorization to persistance layer?????? 179 */ 180 update(user, connection, classifications, true, false); 181 } 182 183 /*** 184 * Does a bulk insert of a Collection of objects that match the type for this persister. 185 */ 186 public void update(User user, java.sql.Connection connection, 187 ArrayList classifications, boolean checkExistence, 188 boolean checkAuthorization) throws RegistryException { 189 if (classifications.size() == 0) { 190 return; 191 } 192 193 System.err.println("Updating " + classifications.size() + 194 " Classifications"); 195 196 String classId = null; 197 198 // Check whether the user can update the objects 199 if (checkAuthorization) { 200 AuthorizationServiceImpl authorization = AuthorizationServiceImpl.getInstance(); 201 authorization.checkAuthorization(user, 202 bu.getIdsFromRegistryObjects(classifications), 203 AuthorizationServiceImpl.UPDATE_OBJECTS_REQUEST); 204 } 205 206 Iterator iter = classifications.iterator(); 207 208 while (iter.hasNext()) { 209 Classification classification = (Classification) iter.next(); 210 classId = classification.getId(); 211 212 // checking whether it exists 213 if (!registryObjectExist(connection, classId, getTableName())) { 214 ArrayList idList = new ArrayList(); 215 idList.add(classId); 216 throw new ObjectsNotFoundException(idList); 217 } 218 219 // No attributes are mutable 220 } 221 // end looping all the classifications 222 223 if (classifications.size() > 0) { 224 // Updated composed Name, Descrption, Slot, ExternalIdentifier and Classification 225 super.update(user, connection, classifications); 226 227 // Now generate the AE for updating Classification 228 generateAuditbleEvent(connection, classifications, "Updated", user); 229 } 230 } 231 232 /*** 233 * Gets objects for specified parentIds 234 * 235 */ 236 public ArrayList getByParentId(java.sql.Connection connection, 237 String parentId, ResponseOption responseOption, ArrayList objectRefs) 238 throws RegistryException { 239 ArrayList classifications = new ArrayList(); 240 Statement stmt = null; 241 242 try { 243 stmt = connection.createStatement(); 244 ResultSet rs = null; 245 246 rs = stmt.executeQuery("SELECT * from " + getTableName() + 247 " WHERE classifiedObject = '" + parentId + "' "); 248 249 while (rs.next()) { 250 Classification obj = new Classification(); 251 ObjectRef refClassifiedObject = new ObjectRef(); 252 253 String classifiedObjectId = rs.getString("classifiedObject"); 254 refClassifiedObject.setId(classifiedObjectId); 255 obj.setClassifiedObject(refClassifiedObject); 256 257 String classSchemeId = rs.getString("classificationScheme"); 258 259 if (classSchemeId != null) { 260 ObjectRef refClassificationScheme = new ObjectRef(); 261 refClassificationScheme.setId(classSchemeId); 262 obj.setClassificationScheme(refClassificationScheme); 263 } 264 265 String classNodeId = rs.getString("classificationNode"); 266 267 if (classNodeId != null) { 268 ObjectRef refClassificationNode = new ObjectRef(); 269 refClassificationNode.setId(classNodeId); 270 obj.setClassificationNode(refClassificationNode); 271 } 272 273 loadObjectFromResultSet(connection, obj, rs, responseOption, 274 objectRefs); 275 classifications.add(obj); 276 } 277 } catch (java.sql.SQLException e) { 278 throw new RegistryException(e); 279 } finally { 280 try { 281 if (stmt != null) 282 stmt.close(); 283 } catch (SQLException sqle) { 284 sqle.printStackTrace(); 285 } 286 } 287 288 289 return classifications; 290 } 291 292 /*** 293 * Does a bulk delete of objects for specified parentIds 294 * 295 */ 296 public void deleteByParentIds(User user, java.sql.Connection connection, 297 ArrayList parentIds) throws RegistryException { 298 Statement stmt = null; 299 try { 300 stmt = connection.createStatement(); 301 ResultSet rs = null; 302 303 ArrayList ids = new ArrayList(); 304 305 Iterator iter = parentIds.iterator(); 306 307 while (iter.hasNext()) { 308 String parentId = (String) iter.next(); 309 310 rs = stmt.executeQuery("SELECT id from " + getTableName() + 311 " WHERE classifiedObject = '" + parentId + "' "); 312 313 while (rs.next()) { 314 String id = rs.getString(1); 315 ids.add(id); 316 } 317 } 318 319 if (ids.size() > 0) { 320 delete(user, connection, ids); 321 } 322 323 //stmt.close(); 324 } catch (java.sql.SQLException e) { 325 RegistryException exception = new RegistryException(e); 326 throw exception; 327 } finally { 328 try { 329 if (stmt != null) 330 stmt.close(); 331 } catch (SQLException sqle) { 332 sqle.printStackTrace(); 333 } 334 } 335 336 } 337 338 public void loadObjectFromResultSet(java.sql.Connection conn, Object obj, 339 ResultSet rs, ResponseOption responseOption, ArrayList objectRefs) 340 throws RegistryException { 341 try { 342 if (!(obj instanceof org.oasis.ebxml.registry.bindings.rim.Classification)) { 343 throw new RegistryException("Unexpected object " + obj + 344 ". Was expecting org.oasis.ebxml.registry.bindings.rim.Classification."); 345 } 346 347 Classification cl = (Classification) obj; 348 super.loadObjectFromResultSet(conn, cl, rs, responseOption, 349 objectRefs); 350 351 String classificationNodeId = rs.getString("classificationNode"); 352 ObjectRef or = null; 353 354 if (classificationNodeId != null) { 355 or = new ObjectRef(); 356 or.setId(classificationNodeId); 357 objectRefs.add(or); 358 } 359 360 cl.setClassificationNode(or); 361 362 String classificationSchemeId = rs.getString("classificationScheme"); 363 or = null; 364 365 if (classificationSchemeId != null) { 366 or = new ObjectRef(); 367 or.setId(classificationSchemeId); 368 objectRefs.add(or); 369 } 370 371 cl.setClassificationScheme(or); 372 373 String classifiedObjectId = rs.getString("classifiedObject"); 374 or = new ObjectRef(); 375 or.setId(classifiedObjectId); 376 objectRefs.add(or); 377 cl.setClassifiedObject(or); 378 379 String nodeRep = rs.getString("nodeRepresentation"); 380 cl.setNodeRepresentation(nodeRep); 381 } catch (SQLException e) { 382 e.printStackTrace(); 383 throw new RegistryException(e); 384 } 385 } 386 387 public ArrayList getLeafObjectList(java.sql.Connection conn, ResultSet rs, 388 ResponseOption responseOption, ArrayList objectRefs) 389 throws RegistryException { 390 ArrayList res = new ArrayList(); 391 392 try { 393 while (rs.next()) { 394 Classification obj = new Classification(); 395 loadObjectFromResultSet(conn, obj, rs, responseOption, 396 objectRefs); 397 398 res.add(obj); 399 } 400 } catch (SQLException e) { 401 e.printStackTrace(); 402 throw new RegistryException(e); 403 } 404 405 return res; 406 } 407 }

This page was automatically generated by Maven