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 com.sun.ebxml.registry.RegistryException; 14 15 import java.sql.Connection; 16 import java.sql.SQLException; 17 import java.util.ArrayList; 18 import org.oasis.ebxml.registry.bindings.rim.Service; 19 20 /*** 21 * 22 * @author kwalsh 23 * @author Adrian Chong 24 * @version 25 */ 26 public class ServiceDAO extends RegistryEntryDAO { 27 protected ServiceDAO(){} 28 29 static com.sun.ebxml.registry.util.BindingUtility bu = com.sun.ebxml.registry.util.BindingUtility.getInstance(); 30 31 public static String getTableNameStatic() { 32 return "Service"; 33 } 34 35 public String getTableName() { 36 return getTableNameStatic(); 37 } 38 39 /*** 40 * Does a bulk insert of a Collection of objects that match the type for this persister. 41 * 42 */ 43 public void insert(org.oasis.ebxml.registry.bindings.rim.User user, Connection connection, ArrayList services) throws RegistryException { 44 if (services.size()==0) { 45 return; 46 } 47 48 String serviceId = null; 49 java.sql.Statement stmt = null; 50 try { 51 52 // Update the exisitng Service 53 services = updateExistingObjects(user, connection , services); 54 if (services.size()==0) { 55 return; 56 } 57 58 System.err.println("Inserting " + services.size() + " Services"); 59 60 stmt = connection.createStatement(); 61 62 java.util.Iterator iter = services.iterator(); 63 ServiceBindingDAO serviceBindingDAO = new ServiceBindingDAO(); 64 while (iter.hasNext()) { 65 Service service = (Service)iter.next(); 66 serviceId = service.getId(); 67 68 java.util.Date expiration = service.getExpiration(); 69 String expirationAsString = null; 70 if (expiration != null) { 71 expirationAsString = "'" + (new java.sql.Timestamp(expiration.getTime())) + "'"; 72 //??The timestamp is being truncated to work around a bug in PostgreSQL 7.2.2 JDBC driver 73 expirationAsString = expirationAsString.substring(0,19); 74 } 75 76 int majorVersion = service.getMajorVersion(); 77 int minorVersion = service.getMinorVersion(); 78 79 org.oasis.ebxml.registry.bindings.rim.types.StabilityType s = service.getStability(); 80 String stability = null; 81 if (s != null) { 82 stability = "'" + s + "'"; 83 } 84 85 org.oasis.ebxml.registry.bindings.rim.types.StatusType st = service.getStatus(); 86 String status = "'Submitted'"; 87 if (st != null) { 88 status = "'" + st + "'"; 89 } 90 91 String userVersion = service.getUserVersion(); 92 if (userVersion != null) { 93 userVersion = "'" + userVersion + "'"; 94 } 95 96 String str = "INSERT INTO " + getTableName() + 97 " (accessControlPolicy" + 98 ", id" + 99 ", objectType" + 100 ", expiration" + 101 ", majorVersion" + 102 ", minorVersion" + 103 ", stability" + 104 ", status" + 105 ", userVersion)"; 106 str += " VALUES (null, " + //AccessControlPolicy 107 "'" + serviceId + "', " + 108 "'Service', " + 109 expirationAsString + ", " + 110 majorVersion + ", " + 111 minorVersion + ", " + 112 stability + ", " + 113 status + ", " + 114 userVersion + ")"; 115 116 System.err.println("stmt = '" + str + "'"); 117 stmt.addBatch(str); 118 119 //Now insert any composed ServiceBindings 120 org.oasis.ebxml.registry.bindings.rim.ServiceBinding[] serviceBindings = service.getServiceBinding(); 121 ArrayList serviceBindingsAL = new ArrayList(); 122 for (int i=0; i<serviceBindings.length; i++) { 123 //Make sure parent Service is set on ServiceBindings 124 serviceBindings[i].setService(service); 125 serviceBindingsAL.add(serviceBindings[i]); 126 } 127 if (serviceBindingsAL.size() > 0) { 128 serviceBindingDAO.insert(user, connection, serviceBindingsAL); 129 } 130 } 131 132 if (services.size() > 0) { 133 int [] updateCounts = stmt.executeBatch(); 134 //stmt.close(); 135 super.insert(user, connection, services); 136 // Now generate the AE for inserting Service 137 generateAuditbleEvent(connection, services, "Created", user); 138 } 139 } 140 catch (SQLException e) { 141 e.printStackTrace(); 142 RegistryException exception = new RegistryException(e); 143 throw exception; 144 } finally { 145 try { 146 if (stmt != null) 147 stmt.close(); 148 } catch (SQLException sqle) { 149 sqle.printStackTrace(); 150 } 151 } 152 } 153 154 public void update(org.oasis.ebxml.registry.bindings.rim.User user, Connection connection, ArrayList services) throws RegistryException { 155 /* 156 * We do not need to check whether the user is authorized to update because the authorization is expected to be checked by higher level. 157 * Should we move the authorization to persistance layer?????? 158 */ 159 update(user, connection, services, true, false); 160 161 } 162 163 164 /*** 165 * Does a bulk delete of a ArrayList of Service. The service itself does not have 166 * any attribute to update but the composed ServiceBinding and SpecificationLink will 167 * be firstly deleted (they are submitted before) and inserted again. 168 */ 169 public void update(org.oasis.ebxml.registry.bindings.rim.User user, Connection connection, ArrayList services, boolean checkExistence, boolean checkAuthorization) throws RegistryException { 170 171 if (services.size()==0) { 172 return; 173 } 174 175 String serviceId = null; 176 java.sql.Statement stmt = null; 177 178 try { 179 // Check whether the user can update the objects 180 if (checkAuthorization) { 181 com.sun.ebxml.registry.security.authorization.AuthorizationServiceImpl authorization = com.sun.ebxml.registry.security.authorization.AuthorizationServiceImpl.getInstance(); 182 authorization.checkAuthorization(user, bu.getIdsFromRegistryObjects(services), com.sun.ebxml.registry.security.authorization.AuthorizationServiceImpl.UPDATE_OBJECTS_REQUEST); 183 } 184 stmt = connection.createStatement(); 185 java.util.Iterator iter = services.iterator(); 186 ServiceBindingDAO serviceBindingDAO = new ServiceBindingDAO(); 187 188 java.util.HashMap idMap = new java.util.HashMap(); 189 ArrayList servicesIds = new ArrayList(); 190 ArrayList bindings = new ArrayList(); 191 192 while (iter.hasNext()) { 193 Service service = (Service)iter.next(); 194 serviceId = service.getId(); 195 196 // checking whether it exists 197 if (checkExistence && !registryObjectExist(connection, serviceId, getTableName())) { 198 ArrayList idList = new ArrayList(); 199 idList.add(serviceId); 200 throw new com.sun.ebxml.registry.lcm.ObjectsNotFoundException(idList); 201 } 202 203 java.util.Date expiration = service.getExpiration(); 204 String expirationAsString = null; 205 if (expiration != null) { 206 expirationAsString = "'" + (new java.sql.Timestamp(expiration.getTime())) + "'"; 207 } 208 209 int majorVersion = service.getMajorVersion(); 210 int minorVersion = service.getMinorVersion(); 211 212 org.oasis.ebxml.registry.bindings.rim.types.StabilityType s = service.getStability(); 213 String stability = null; 214 if (s != null) { 215 stability = "'" + s + "'"; 216 } 217 218 org.oasis.ebxml.registry.bindings.rim.types.StatusType st = service.getStatus(); 219 String status = "'Submitted'"; 220 if (st != null) { 221 status = "'" + st + "'"; 222 } 223 224 String userVersion = service.getUserVersion(); 225 if (userVersion != null) { 226 userVersion = "'" + userVersion + "'"; 227 } 228 229 String str = "UPDATE " + getTableName() + 230 " SET accessControlPolicy=null" + 231 ", expiration=" + expirationAsString + 232 ", stability=" + stability + 233 ", userVersion=" + userVersion; 234 str += " WHERE id='" + serviceId + "'"; 235 236 System.err.println("stmt = '" + str + "'"); 237 stmt.addBatch(str); 238 239 servicesIds.add(serviceId); 240 241 //Now remember any composed ServiceBindings for updating them later 242 org.oasis.ebxml.registry.bindings.rim.ServiceBinding[] _bindings = service.getServiceBinding(); 243 for (int i = 0; i < _bindings.length; i++) { 244 bindings.add(_bindings[i]); 245 } 246 247 } // end looping services 248 249 if (services.size() > 0) { 250 int [] updateCount = stmt.executeBatch(); 251 //stmt.close(); 252 // Updated composed Name, Descrption, Slot, ExternalIdentifier and Classification 253 super.update(user, connection, services); 254 generateAuditbleEvent(connection, services, "Updated", user); 255 // Delete the ServiceBinidngs and SpecificationLink that were submitted 256 serviceBindingDAO.deleteByParentIds(user, connection, servicesIds); 257 // Insert the ServiceBindings and SpecificationLink in this update request 258 // Firstly fix the ids if composed new ServiceBinding and SpecificationLink 259 com.sun.ebxml.registry.lcm.LifeCycleManagerImpl lcm = com.sun.ebxml.registry.lcm.LifeCycleManagerImpl.getInstance(); 260 lcm.fixTemporaryIds(services, idMap); 261 // Now insert the composed new ServiceBinding and SpecificationLink 262 serviceBindingDAO.insert(user, connection, bindings); 263 } 264 } 265 catch (SQLException e) { 266 e.printStackTrace(); 267 RegistryException exception = new RegistryException(e); 268 throw exception; 269 } finally { 270 try { 271 if (stmt != null) 272 stmt.close(); 273 } catch (SQLException sqle) { 274 sqle.printStackTrace(); 275 } 276 } 277 } 278 279 /*** 280 * Does a bulk delete of a Collection of objects that match the type for this persister. 281 * 282 */ 283 public void delete(org.oasis.ebxml.registry.bindings.rim.User user, Connection connection, ArrayList registryObjectIds) throws RegistryException { 284 285 super.delete(user, connection, registryObjectIds); 286 ServiceBindingDAO serviceBindingDAO = new ServiceBindingDAO(); 287 serviceBindingDAO.deleteByParentIds(user, connection, registryObjectIds); 288 } 289 290 public void loadObjectFromResultSet(Connection conn, Object obj, java.sql.ResultSet rs, org.oasis.ebxml.registry.bindings.query.ResponseOption responseOption, ArrayList objectRefs) throws RegistryException { 291 if (!(obj instanceof Service)) { 292 throw new RegistryException("Unexpected object " + obj + ". Was expecting org.oasis.ebxml.registry.bindings.rim.Service."); 293 } 294 295 Service service = (Service)obj; 296 297 super.loadObjectFromResultSet(conn, service, rs, responseOption, objectRefs); 298 299 boolean returnComposedObjects = responseOption.getReturnComposedObjects(); 300 if (returnComposedObjects) { 301 ServiceBindingDAO serviceBindingDAO = new ServiceBindingDAO(); 302 ArrayList serviceBindings = serviceBindingDAO.getByParentId(conn, service.getId(), objectRefs); 303 java.util.Iterator iter = serviceBindings.iterator(); 304 while (iter.hasNext()) { 305 org.oasis.ebxml.registry.bindings.rim.ServiceBinding sb = (org.oasis.ebxml.registry.bindings.rim.ServiceBinding)iter.next(); 306 service.addServiceBinding(sb); 307 } 308 309 } 310 } 311 312 public ArrayList getLeafObjectList(Connection conn, java.sql.ResultSet rs, org.oasis.ebxml.registry.bindings.query.ResponseOption responseOption, ArrayList objectRefs) throws RegistryException { 313 ArrayList res = new ArrayList(); 314 315 try { 316 while(rs.next()) { 317 Service obj = new Service(); 318 loadObjectFromResultSet(conn, obj, rs, responseOption, objectRefs); 319 320 res.add(obj); 321 } 322 } 323 catch (SQLException e) { 324 e.printStackTrace(); 325 throw new RegistryException(e); 326 } 327 328 329 return res; 330 } 331 }

This page was automatically generated by Maven