1 23 24 package org.infoglue.cms.controllers.kernel.impl.simple; 25 26 import java.util.Collection ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 30 import org.apache.log4j.Logger; 31 import org.exolab.castor.jdo.Database; 32 import org.exolab.castor.jdo.OQLQuery; 33 import org.exolab.castor.jdo.QueryResults; 34 import org.infoglue.cms.entities.content.Content; 35 import org.infoglue.cms.entities.kernel.BaseEntityVO; 36 import org.infoglue.cms.entities.management.impl.simple.AvailableServiceBindingImpl; 37 import org.infoglue.cms.entities.management.impl.simple.ServiceDefinitionImpl; 38 import org.infoglue.cms.entities.structure.Qualifyer; 39 import org.infoglue.cms.entities.structure.ServiceBinding; 40 import org.infoglue.cms.entities.structure.ServiceBindingVO; 41 import org.infoglue.cms.entities.structure.SiteNode; 42 import org.infoglue.cms.entities.structure.SiteNodeVersion; 43 import org.infoglue.cms.entities.structure.impl.simple.ServiceBindingImpl; 44 import org.infoglue.cms.entities.structure.impl.simple.SiteNodeVersionImpl; 45 import org.infoglue.cms.exception.Bug; 46 import org.infoglue.cms.exception.ConstraintException; 47 import org.infoglue.cms.exception.SystemException; 48 import org.infoglue.cms.util.ConstraintExceptionBuffer; 49 50 51 59 public class ServiceBindingController extends BaseController 60 { 61 private final static Logger logger = Logger.getLogger(ServiceBindingController.class.getName()); 62 63 public static ServiceBindingController getController() 64 { 65 return new ServiceBindingController(); 66 } 67 68 public static ServiceBindingVO getServiceBindingVOWithId(Integer serviceBindingId) throws SystemException, Bug 69 { 70 return (ServiceBindingVO) getVOWithId(ServiceBindingImpl.class, serviceBindingId); 71 } 72 73 79 80 public static ServiceBinding getServiceBindingWithId(Integer serviceBindingId, Database db) throws SystemException, Bug 81 { 82 return (ServiceBinding) getObjectWithId(ServiceBindingImpl.class, serviceBindingId, db); 83 } 84 85 public List getServiceBindingVOList() throws SystemException, Bug 86 { 87 return getAllVOObjects(ServiceBindingImpl.class, "serviceBindingId"); 88 } 89 90 public static ServiceBindingVO create(ServiceBindingVO serviceBindingVO, String qualifyerXML, Integer availableServiceBindingId, Integer siteNodeVersionId, Integer serviceDefinitionId) throws ConstraintException, SystemException 91 { 92 logger.info("Creating a serviceBinding with the following..."); 93 94 logger.info("name:" + serviceBindingVO.getName()); 95 logger.info("bindingTypeId:" + serviceBindingVO.getBindingTypeId()); 96 logger.info("availableServiceBindingId:" + availableServiceBindingId); 97 logger.info("siteNodeVersionId:" + siteNodeVersionId); 98 logger.info("serviceDefinitionId:" + serviceDefinitionId); 99 100 Database db = CastorDatabaseService.getDatabase(); 101 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 102 103 ServiceBinding serviceBinding = null; 104 105 beginTransaction(db); 106 107 try 108 { 109 SiteNodeVersion siteNodeVersion = SiteNodeVersionController.getController().getSiteNodeVersionWithId(siteNodeVersionId, db); 110 serviceBinding = new ServiceBindingImpl(); 111 serviceBinding.setValueObject(serviceBindingVO); 112 serviceBinding.setAvailableServiceBinding((AvailableServiceBindingImpl)AvailableServiceBindingController.getController().getAvailableServiceBindingWithId(availableServiceBindingId, db)); 113 serviceBinding.setServiceDefinition((ServiceDefinitionImpl)ServiceDefinitionController.getController().getServiceDefinitionWithId(serviceDefinitionId, db)); 114 serviceBinding.setSiteNodeVersion((SiteNodeVersionImpl)siteNodeVersion); 115 116 118 logger.info("createEntity: " + serviceBinding.getSiteNodeVersion().getSiteNodeVersionId()); 119 120 serviceBinding.setBindingQualifyers(QualifyerController.createQualifyers(qualifyerXML, serviceBinding)); 121 db.create(serviceBinding); 122 123 siteNodeVersion.getServiceBindings().add(serviceBinding); 124 125 RegistryController.getController().updateSiteNodeVersion(siteNodeVersion, db); 126 127 commitTransaction(db); 128 } 129 catch(Exception e) 130 { 131 logger.error("An error occurred so we should not complete the transaction:" + e, e); 132 e.printStackTrace(); 133 rollbackTransaction(db); 134 throw new SystemException(e.getMessage()); 135 } 136 137 return serviceBinding.getValueObject(); 138 } 139 140 143 144 public ServiceBindingVO create(Database db, ServiceBindingVO serviceBindingVO, String qualifyerXML, Integer availableServiceBindingId, Integer siteNodeVersionId, Integer serviceDefinitionId) throws ConstraintException, SystemException 145 { 146 logger.info("Creating a serviceBinding with the following..."); 147 148 logger.info("name:" + serviceBindingVO.getName()); 149 logger.info("bindingTypeId:" + serviceBindingVO.getBindingTypeId()); 150 logger.info("availableServiceBindingId:" + availableServiceBindingId); 151 logger.info("siteNodeVersionId:" + siteNodeVersionId); 152 logger.info("serviceDefinitionId:" + serviceDefinitionId); 153 154 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 155 156 ServiceBinding serviceBinding = null; 157 158 try 159 { 160 SiteNodeVersion siteNodeVersion = SiteNodeVersionController.getController().getSiteNodeVersionWithId(siteNodeVersionId, db); 161 serviceBinding = new ServiceBindingImpl(); 162 serviceBinding.setValueObject(serviceBindingVO); 163 serviceBinding.setAvailableServiceBinding((AvailableServiceBindingImpl)AvailableServiceBindingController.getController().getAvailableServiceBindingWithId(availableServiceBindingId, db)); 164 serviceBinding.setServiceDefinition((ServiceDefinitionImpl)ServiceDefinitionController.getController().getServiceDefinitionWithId(serviceDefinitionId, db)); 165 serviceBinding.setSiteNodeVersion((SiteNodeVersionImpl)siteNodeVersion); 166 167 169 logger.info("createEntity: " + serviceBinding.getSiteNodeVersion().getSiteNodeVersionId()); 170 171 serviceBinding.setBindingQualifyers(QualifyerController.createQualifyers(qualifyerXML, serviceBinding)); 172 db.create((ServiceBinding)serviceBinding); 173 174 siteNodeVersion.getServiceBindings().add(serviceBinding); 175 176 RegistryController.getController().updateSiteNodeVersion(siteNodeVersion, db); 177 } 178 catch(Exception e) 179 { 180 logger.error("An error occurred so we should not complete the transaction:" + e, e); 181 e.printStackTrace(); 182 throw new SystemException(e.getMessage()); 183 } 184 185 return serviceBinding.getValueObject(); 186 } 187 188 189 protected static ServiceBinding create(ServiceBindingVO serviceBindingVO, Integer availableServiceBindingId, Integer siteNodeVersionId, Integer serviceDefinitionId, Database db) throws ConstraintException, SystemException, Exception 190 { 191 logger.info("Creating a serviceBinding with the following..."); 192 193 logger.info("name:" + serviceBindingVO.getName()); 194 logger.info("bindingTypeId:" + serviceBindingVO.getBindingTypeId()); 195 logger.info("availableServiceBindingId:" + availableServiceBindingId); 196 logger.info("siteNodeVersionId:" + siteNodeVersionId); 197 logger.info("serviceDefinitionId:" + serviceDefinitionId); 198 199 SiteNodeVersion siteNodeVersion = SiteNodeVersionController.getController().getSiteNodeVersionWithId(siteNodeVersionId, db); 200 201 ServiceBinding serviceBinding = null; 202 203 serviceBinding = new ServiceBindingImpl(); 204 serviceBinding.setValueObject(serviceBindingVO); 205 serviceBinding.setAvailableServiceBinding((AvailableServiceBindingImpl)AvailableServiceBindingController.getController().getAvailableServiceBindingWithId(availableServiceBindingId, db)); 206 serviceBinding.setServiceDefinition((ServiceDefinitionImpl)ServiceDefinitionController.getController().getServiceDefinitionWithId(serviceDefinitionId, db)); 207 serviceBinding.setSiteNodeVersion((SiteNodeVersionImpl)siteNodeVersion); 208 209 logger.info("createEntity: " + serviceBinding.getSiteNodeVersion().getSiteNodeVersionId()); 210 211 db.create(serviceBinding); 212 213 RegistryController.getController().updateSiteNodeVersion(siteNodeVersion, db); 214 215 return serviceBinding; 216 } 217 218 219 220 public static ServiceBindingVO update(ServiceBindingVO serviceBindingVO, String qualifyerXML) throws ConstraintException, SystemException 221 { 222 logger.info("Updating a serviceBinding with the following..."); 223 224 logger.info("name:" + serviceBindingVO.getName()); 225 logger.info("bindingTypeId:" + serviceBindingVO.getBindingTypeId()); 226 227 Database db = CastorDatabaseService.getDatabase(); 228 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 229 230 ServiceBinding serviceBinding = null; 231 232 beginTransaction(db); 233 234 try 235 { 236 serviceBinding = getServiceBindingWithId(serviceBindingVO.getServiceBindingId(), db); 237 serviceBinding.setPath(serviceBindingVO.getPath()); 238 serviceBinding.getBindingQualifyers().clear(); 239 Collection newQualifyers = QualifyerController.createQualifyers(qualifyerXML, serviceBinding); 240 serviceBinding.setBindingQualifyers(newQualifyers); 241 242 RegistryController.getController().updateSiteNodeVersion(serviceBinding.getSiteNodeVersion(), db); 243 244 commitTransaction(db); 245 } 246 catch(Exception e) 247 { 248 logger.error("An error occurred so we should not complete the transaction:" + e, e); 249 e.printStackTrace(); 250 rollbackTransaction(db); 251 throw new SystemException(e.getMessage()); 252 } 253 254 return serviceBinding.getValueObject(); 255 } 256 257 258 261 262 public static void deleteServiceBindingsReferencingContent(Content content, Database db) throws ConstraintException, SystemException, Exception 263 { 264 OQLQuery oql = db.getOQLQuery( "SELECT sb FROM org.infoglue.cms.entities.structure.impl.simple.ServiceBindingImpl sb WHERE sb.bindingQualifyers.name = $1 AND sb.bindingQualifyers.value = $2 ORDER BY sb.serviceBindingId"); 265 oql.bind("contentId"); 266 oql.bind(content.getContentId().toString()); 267 268 QueryResults results = oql.execute(); 269 logger.info("Fetching entity in read/write mode"); 270 271 while(results.hasMore()) 272 { 273 ServiceBinding serviceBinding = (ServiceBindingImpl)results.next(); 274 Collection qualifyers = serviceBinding.getBindingQualifyers(); 276 Iterator qualifyersIterator = qualifyers.iterator(); 277 while(qualifyersIterator.hasNext()) 278 { 279 Qualifyer qualifyer = (Qualifyer)qualifyersIterator.next(); 280 if(qualifyer.getName().equalsIgnoreCase("contentId") && qualifyer.getValue().equals(content.getContentId().toString())) 282 { 283 qualifyersIterator.remove(); 285 serviceBinding.getBindingQualifyers().remove(qualifyer); 287 288 if(serviceBinding.getBindingQualifyers() == null || serviceBinding.getBindingQualifyers().size() == 0) 290 { 291 db.remove(serviceBinding); 293 } 294 } 295 } 296 297 SiteNodeVersion siteNodeVersion = serviceBinding.getSiteNodeVersion(); 298 if(siteNodeVersion.getOwningSiteNode() == null) 299 SiteNodeVersionController.getController().delete(siteNodeVersion, db); 300 } 301 302 results.close(); 303 oql.close(); 304 } 305 306 307 310 311 public static void deleteServiceBindingsReferencingSiteNode(SiteNode siteNode, Database db) throws ConstraintException, SystemException, Exception 312 { 313 OQLQuery oql = db.getOQLQuery( "SELECT sb FROM org.infoglue.cms.entities.structure.impl.simple.ServiceBindingImpl sb WHERE sb.bindingQualifyers.name = $1 AND sb.bindingQualifyers.value = $2 ORDER BY sb.serviceBindingId"); 314 oql.bind("siteNodeId"); 315 oql.bind(siteNode.getSiteNodeId().toString()); 316 317 QueryResults results = oql.execute(); 318 logger.info("Fetching entity in read/write mode"); 319 320 while(results.hasMore()) 321 { 322 ServiceBinding serviceBinding = (ServiceBindingImpl)results.next(); 323 Collection qualifyers = serviceBinding.getBindingQualifyers(); 325 Iterator qualifyersIterator = qualifyers.iterator(); 326 while(qualifyersIterator.hasNext()) 327 { 328 Qualifyer qualifyer = (Qualifyer)qualifyersIterator.next(); 329 if(qualifyer.getName().equalsIgnoreCase("siteNodeId") && qualifyer.getValue().equals(siteNode.getSiteNodeId().toString())) 331 { 332 qualifyersIterator.remove(); 334 serviceBinding.getBindingQualifyers().remove(qualifyer); 336 337 if(serviceBinding.getBindingQualifyers() == null || serviceBinding.getBindingQualifyers().size() == 0) 339 { 340 db.remove(serviceBinding); 342 } 343 } 344 } 345 } 346 347 results.close(); 348 oql.close(); 349 } 350 351 354 355 public static void delete(ServiceBindingVO serviceBindingVO) throws ConstraintException, SystemException 356 { 357 Database db = CastorDatabaseService.getDatabase(); 358 beginTransaction(db); 359 try 360 { 361 ServiceBinding serviceBinding = ServiceBindingController.getServiceBindingWithId(serviceBindingVO.getServiceBindingId(), db); 362 SiteNodeVersion siteNodeVersion = serviceBinding.getSiteNodeVersion(); 365 366 db.remove(serviceBinding); 367 368 siteNodeVersion.getServiceBindings().remove(serviceBinding); 369 370 RegistryController.getController().updateSiteNodeVersion(siteNodeVersion, db); 371 372 commitTransaction(db); 373 } 374 catch(Exception e) 375 { 376 e.printStackTrace(); 377 rollbackTransaction(db); 378 throw new SystemException("An error occurred when we tried to remove a serviceBinding and it's qualifyers."); 379 } 380 } 381 382 385 386 public static void delete(ServiceBindingVO serviceBindingVO, Database db) throws ConstraintException, SystemException, Exception 387 { 388 ServiceBinding serviceBinding = ServiceBindingController.getServiceBindingWithId(serviceBindingVO.getServiceBindingId(), db); 389 390 db.remove(serviceBinding); 391 392 RegistryController.getController().updateSiteNodeVersion(serviceBinding.getSiteNodeVersion(), db); 393 } 394 395 396 400 401 public static List getQualifyerVOList(Integer serviceBindingId) throws ConstraintException, SystemException 402 { 403 Database db = CastorDatabaseService.getDatabase(); 404 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 405 406 List qualifyerVOList = null; 407 408 beginTransaction(db); 409 410 try 411 { 412 ServiceBinding serviceBinding = getServiceBindingWithId(serviceBindingId, db); 413 Collection qualifyerList = serviceBinding.getBindingQualifyers(); 414 qualifyerVOList = toVOList(qualifyerList); 415 416 ceb.throwIfNotEmpty(); 418 419 commitTransaction(db); 420 } 421 catch(ConstraintException ce) 422 { 423 logger.warn("An error occurred so we should not complete the transaction:" + ce, ce); 424 rollbackTransaction(db); 425 throw ce; 426 } 427 catch(Exception e) 428 { 429 logger.error("An error occurred so we should not complete the transaction:" + e, e); 430 rollbackTransaction(db); 431 throw new SystemException(e.getMessage()); 432 } 433 434 return qualifyerVOList; 435 } 436 437 441 442 public BaseEntityVO getNewVO() 443 { 444 return new ServiceBindingVO(); 445 } 446 447 } 448 | Popular Tags |