1 25 package org.infoglue.cms.controllers.kernel.impl.simple; 26 27 import java.util.ArrayList ; 28 import java.util.Collection ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 32 import org.exolab.castor.jdo.Database; 33 import org.exolab.castor.jdo.PersistenceException; 34 import org.infoglue.cms.entities.kernel.BaseEntityVO; 35 import org.infoglue.cms.entities.management.Category; 36 import org.infoglue.cms.entities.management.PropertiesCategory; 37 import org.infoglue.cms.entities.management.PropertiesCategoryVO; 38 import org.infoglue.cms.entities.management.impl.simple.CategoryImpl; 39 import org.infoglue.cms.entities.management.impl.simple.PropertiesCategoryImpl; 40 import org.infoglue.cms.exception.SystemException; 41 42 51 public class PropertiesCategoryController extends BaseController 52 { 53 private static final PropertiesCategoryController instance = new PropertiesCategoryController(); 54 55 private static final String findByProperties = new StringBuffer ("SELECT c ") 56 .append("FROM org.infoglue.cms.entities.management.impl.simple.PropertiesCategoryImpl c ") 57 .append("WHERE c.entityName = $1 AND c.entityId = $2").toString(); 58 59 private static final String findByPropertiesAttribute = new StringBuffer ("SELECT c ") 60 .append("FROM org.infoglue.cms.entities.management.impl.simple.PropertiesCategoryImpl c ") 61 .append("WHERE c.attributeName = $1 ") 62 .append("AND c.entityName = $2") 63 .append("AND c.entityId = $3") 64 .append("ORDER BY c.category.name").toString(); 65 66 private static final String findByCategory = new StringBuffer ("SELECT c ") 67 .append("FROM org.infoglue.cms.entities.management.impl.simple.PropertiesCategoryImpl c ") 68 .append("WHERE c.category.categoryId = $1 ").toString(); 69 70 public static PropertiesCategoryController getController() 71 { 72 return instance; 73 } 74 75 private PropertiesCategoryController() {} 76 77 83 public PropertiesCategoryVO findById(Integer id) throws SystemException 84 { 85 return (PropertiesCategoryVO)getVOWithId(PropertiesCategoryImpl.class, id); 86 } 87 88 95 public List findByPropertiesAttribute(String attribute, String entityName, Integer entityId) throws SystemException 96 { 97 List params = new ArrayList (); 98 params.add(attribute); 99 params.add(entityName); 100 params.add(entityId); 101 return executeQuery(findByPropertiesAttribute, params); 102 } 103 104 111 public List findByPropertiesAttribute(String attribute, String entityName, Integer entityId, Database db) throws SystemException 112 { 113 List params = new ArrayList (); 114 params.add(attribute); 115 params.add(entityName); 116 params.add(entityId); 117 return executeQuery(findByPropertiesAttribute, params, db); 118 } 119 120 126 public List findByProperties(String entityName, Integer entityId) throws SystemException 127 { 128 List params = new ArrayList (); 129 params.add(entityName); 130 params.add(entityId); 131 return executeQuery(findByProperties, params); 132 } 133 134 140 public List findByCategory(Integer categoryId) throws SystemException 141 { 142 List params = new ArrayList (); 143 params.add(categoryId); 144 return executeQuery(findByCategory, params); 145 } 146 147 153 public PropertiesCategoryVO save(PropertiesCategoryVO c) throws SystemException 154 { 155 return c.isUnsaved() ? create(c) : (PropertiesCategoryVO)updateEntity(PropertiesCategoryImpl.class, c); 156 } 157 158 161 private PropertiesCategoryVO create(PropertiesCategoryVO c) throws SystemException 162 { 163 Database db = beginTransaction(); 164 165 try 166 { 167 PropertiesCategory propertiesCategory = createWithDatabase(c, db); 168 commitTransaction(db); 169 return propertiesCategory.getValueObject(); 170 } 171 catch (Exception e) 172 { 173 rollbackTransaction(db); 174 throw new SystemException(e.getMessage()); 175 } 176 } 177 178 public PropertiesCategory createWithDatabase(PropertiesCategoryVO c, Database db) throws SystemException, PersistenceException 179 { 180 Category category = (Category)getObjectWithId(CategoryImpl.class, c.getCategory().getId(), db); 184 185 PropertiesCategory propertiesCategory = new PropertiesCategoryImpl(); 186 propertiesCategory.setValueObject(c); 187 propertiesCategory.setCategory((CategoryImpl)category); 188 db.create(propertiesCategory); 189 return propertiesCategory; 190 } 191 192 197 public void delete(Integer id) throws SystemException 198 { 199 deleteEntity(PropertiesCategoryImpl.class, id); 200 } 201 202 207 public void deleteByProperties(String entityName, Integer entityId) throws SystemException 208 { 209 delete(findByProperties(entityName, entityId)); 210 } 211 212 218 public void deleteByPropertiesVersion(String entityName, Integer entityId, Database db) throws SystemException 219 { 220 delete(findByProperties(entityName, entityId), db); 221 } 222 223 228 public void deleteByCategory(Integer categoryId) throws SystemException 229 { 230 delete(findByCategory(categoryId)); 231 } 232 233 239 public void deleteByCategory(Integer categoryId, Database db) throws SystemException 240 { 241 delete(findByCategory(categoryId), db); 242 } 243 244 250 public void deleteByPropertiesVersionAttribute(String attribute, String entityName, Integer entityId) throws SystemException 251 { 252 delete(findByPropertiesAttribute(attribute, entityName, entityId)); 253 } 254 255 262 public void deleteByPropertiesVersionAttribute(String attribute, String entityName, Integer entityId, Database db) throws SystemException 263 { 264 delete(findByPropertiesAttribute(attribute, entityName, entityId), db); 265 } 266 267 272 private static void delete(Collection propertiesCategories) throws SystemException 273 { 274 Database db = beginTransaction(); 275 276 try 277 { 278 delete(propertiesCategories, db); 279 commitTransaction(db); 280 } 281 catch (Exception e) 282 { 283 rollbackTransaction(db); 284 throw new SystemException(e); 285 } 286 } 287 288 294 private static void delete(Collection propertiesCategories, Database db) throws SystemException 295 { 296 for (Iterator i = propertiesCategories.iterator(); i.hasNext();) 297 deleteEntity(PropertiesCategoryImpl.class, ((PropertiesCategoryVO)i.next()).getId(), db); 298 } 299 300 303 public BaseEntityVO getNewVO() 304 { 305 return new PropertiesCategoryVO(); 306 } 307 } 308 | Popular Tags |