1 23 24 package org.infoglue.cms.controllers.kernel.impl.simple; 25 26 import java.util.List ; 27 28 import org.apache.log4j.Logger; 29 import org.exolab.castor.jdo.Database; 30 import org.exolab.castor.jdo.OQLQuery; 31 import org.exolab.castor.jdo.QueryResults; 32 import org.infoglue.cms.entities.kernel.BaseEntityVO; 33 import org.infoglue.cms.entities.management.ServiceDefinition; 34 import org.infoglue.cms.entities.management.ServiceDefinitionVO; 35 import org.infoglue.cms.entities.management.impl.simple.ServiceDefinitionImpl; 36 import org.infoglue.cms.exception.Bug; 37 import org.infoglue.cms.exception.ConstraintException; 38 import org.infoglue.cms.exception.SystemException; 39 import org.infoglue.cms.util.ConstraintExceptionBuffer; 40 41 public class ServiceDefinitionController extends BaseController 42 { 43 private final static Logger logger = Logger.getLogger(ServiceDefinitionController.class.getName()); 44 45 48 49 public static ServiceDefinitionController getController() 50 { 51 return new ServiceDefinitionController(); 52 } 53 54 public ServiceDefinitionVO getServiceDefinitionVOWithId(Integer serviceDefinitionId) throws SystemException, Bug 55 { 56 return (ServiceDefinitionVO) getVOWithId(ServiceDefinitionImpl.class, serviceDefinitionId); 57 } 58 59 public ServiceDefinitionVO create(ServiceDefinitionVO vo) throws ConstraintException, SystemException 60 { 61 ServiceDefinition ent = new ServiceDefinitionImpl(); 62 ent.setValueObject(vo); 63 ent = (ServiceDefinition) createEntity(ent); 64 return ent.getValueObject(); 65 } 66 67 70 71 public void delete(ServiceDefinitionVO vo) throws ConstraintException, SystemException 72 { 73 Database db = CastorDatabaseService.getDatabase(); 74 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 75 76 beginTransaction(db); 77 78 try 79 { 80 ServiceDefinition serviceDefinition = getServiceDefinitionWithId(vo.getServiceDefinitionId(), db); 81 if(serviceDefinition.getName().equalsIgnoreCase("Core content service") || serviceDefinition.getName().equalsIgnoreCase("Core structure service")) 82 { 83 throw new ConstraintException("ServiceDefinition.deleteAction", "3200"); 84 } 85 } 86 catch(ConstraintException ce) 87 { 88 throw ce; 89 } 90 catch(SystemException se) 91 { 92 throw se; 93 } 94 catch(Exception e) 95 { 96 throw new SystemException("An error occurred in ServiceDefinitionController.delete(). Reason:" + e.getMessage(), e); 97 } 98 finally 99 { 100 commitTransaction(db); 101 } 102 103 deleteEntity(ServiceDefinitionImpl.class, vo.getServiceDefinitionId()); 104 } 105 106 public ServiceDefinition getServiceDefinitionWithId(Integer serviceDefinitionId, Database db) throws SystemException, Bug 107 { 108 return (ServiceDefinition) getObjectWithId(ServiceDefinitionImpl.class, serviceDefinitionId, db); 109 } 110 111 public List getServiceDefinitionVOList() throws SystemException, Bug 112 { 113 return getAllVOObjects(ServiceDefinitionImpl.class, "serviceDefinitionId"); 114 } 115 116 119 120 public void deleteServiceDefinition(Integer serviceDefinitionId, Database db) throws SystemException, Bug 121 { 122 try 123 { 124 db.remove(getServiceDefinitionWithId(serviceDefinitionId, db)); 125 } 126 catch(Exception e) 127 { 128 throw new SystemException("An error occurred when we tried to delete ServiceDefinition in the database. Reason: " + e.getMessage(), e); 129 } 130 } 131 132 public ServiceDefinitionVO update(ServiceDefinitionVO serviceDefinitionVO) throws ConstraintException, SystemException 133 { 134 Database db = CastorDatabaseService.getDatabase(); 135 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 136 137 ServiceDefinition serviceDefinition = null; 138 139 beginTransaction(db); 140 141 try 142 { 143 serviceDefinition = getServiceDefinitionWithId(serviceDefinitionVO.getServiceDefinitionId(), db); 145 serviceDefinition.setValueObject(serviceDefinitionVO); 146 147 ceb.throwIfNotEmpty(); 149 150 commitTransaction(db); 151 } 152 catch(ConstraintException ce) 153 { 154 logger.warn("An error occurred so we should not complete the transaction:" + ce, ce); 155 rollbackTransaction(db); 156 throw ce; 157 } 158 catch(Exception e) 159 { 160 logger.error("An error occurred so we should not complete the transaction:" + e, e); 161 rollbackTransaction(db); 162 throw new SystemException(e.getMessage()); 163 } 164 165 166 return serviceDefinition.getValueObject(); 167 } 168 169 170 176 177 public ServiceDefinitionVO getServiceDefinitionVOWithName(String name) throws SystemException, Bug 178 { 179 ServiceDefinitionVO serviceDefinitionVO = null; 180 181 Database db = CastorDatabaseService.getDatabase(); 182 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 183 184 beginTransaction(db); 185 186 try 187 { 188 ServiceDefinition serviceDefinition = getServiceDefinitionWithName(name, db, true); 189 if(serviceDefinition != null) 190 serviceDefinitionVO = serviceDefinition.getValueObject(); 191 192 commitTransaction(db); 193 } 194 catch(Exception e) 195 { 196 rollbackTransaction(db); 197 throw new SystemException("An error occurred when we tried to fetch a ServiceDefinition by name. Reason:" + e.getMessage(), e); 198 } 199 200 return serviceDefinitionVO; 201 } 202 203 204 213 214 public ServiceDefinition getServiceDefinitionWithName(String name, Database db, boolean readOnly) throws SystemException, Bug 215 { 216 ServiceDefinition serviceDefinition = null; 217 218 try 219 { 220 OQLQuery oql = db.getOQLQuery("SELECT a FROM org.infoglue.cms.entities.management.impl.simple.ServiceDefinitionImpl a WHERE a.name = $1"); 221 oql.bind(name); 222 223 QueryResults results = null; 224 if(readOnly) 225 results = oql.execute(Database.ReadOnly); 226 else 227 { 228 this.logger.info("Fetching entity in read/write mode" + name); 229 results = oql.execute(); 230 } 231 232 if(results.hasMore()) 233 { 234 serviceDefinition = (ServiceDefinition)results.next(); 235 } 236 237 results.close(); 238 oql.close(); 239 } 240 catch(Exception e) 241 { 242 throw new SystemException("An error occurred when we tried to fetch a named AvailableServiceBinding. Reason:" + e.getMessage(), e); 243 } 244 245 return serviceDefinition; 246 } 247 248 249 253 254 public BaseEntityVO getNewVO() 255 { 256 return new ServiceDefinitionVO(); 257 } 258 259 } 260 261 | Popular Tags |