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.TransactionHistory; 34 import org.infoglue.cms.entities.management.TransactionHistoryVO; 35 import org.infoglue.cms.entities.management.impl.simple.TransactionHistoryImpl; 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 import org.infoglue.cms.util.NotificationMessage; 41 42 public class TransactionHistoryController extends BaseController 43 { 44 private final static Logger logger = Logger.getLogger(TransactionHistoryController.class.getName()); 45 46 49 50 public static TransactionHistoryController getController() 51 { 52 return new TransactionHistoryController(); 53 } 54 55 public TransactionHistoryVO getTransactionHistoryVOWithId(Integer transactionHistoryId) throws SystemException, Bug 56 { 57 return (TransactionHistoryVO) getVOWithId(TransactionHistoryImpl.class, transactionHistoryId); 58 } 59 60 public TransactionHistory getTransactionHistoryWithId(Integer transactionHistoryId, Database db) throws SystemException, Bug 61 { 62 return (TransactionHistory) getObjectWithId(TransactionHistoryImpl.class, transactionHistoryId, db); 63 } 64 65 66 public List getTransactionHistoryVOList() throws SystemException, Bug 67 { 68 return getAllVOObjects(TransactionHistoryImpl.class, "transactionHistoryId"); 69 } 70 71 72 75 76 public void deleteTransactionHistory(Integer transactionHistoryId, Database db) throws SystemException, Bug 77 { 78 try 79 { 80 db.remove(getTransactionHistoryWithId(transactionHistoryId, db)); 81 } 82 catch(Exception e) 83 { 84 throw new SystemException("An error occurred when we tried to delete TransactionHistory in the database. Reason: " + e.getMessage(), e); 85 } 86 } 87 88 public TransactionHistoryVO getLatestTransactionHistoryVOForEntity(Class entClass, Integer entityId) throws SystemException 89 { 90 Database db = CastorDatabaseService.getDatabase(); 91 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 92 TransactionHistoryVO transactionHistoryVO = null; 93 beginTransaction(db); 94 95 try 96 { 97 98 OQLQuery oql = db.getOQLQuery( "SELECT th FROM org.infoglue.cms.entities.management.impl.simple.TransactionHistoryImpl th WHERE th.transactionObjectName LIKE $1 AND th.transactionObjectId = $2 ORDER BY th.transactionDateTime desc"); 99 oql.bind(entClass.getName() + "%"); 100 oql.bind(entityId); 101 QueryResults results = oql.execute(Database.ReadOnly); 102 103 if (results.hasMore()) 104 { 105 TransactionHistory transactionHistory = (TransactionHistory)results.next(); 106 transactionHistoryVO = transactionHistory.getValueObject(); 107 } 108 109 results.close(); 110 oql.close(); 111 112 commitTransaction(db); 113 } 114 catch(Exception e) 115 { 116 logger.error("An error occurred so we should not completes the transaction:" + e, e); 117 rollbackTransaction(db); 118 throw new SystemException(e.getMessage()); 119 } 120 121 return transactionHistoryVO; 122 123 124 } 125 126 public TransactionHistoryVO update(TransactionHistoryVO transactionHistoryVO) throws ConstraintException, SystemException 127 { 128 Database db = CastorDatabaseService.getDatabase(); 129 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 130 131 TransactionHistory transactionHistory = null; 132 133 beginTransaction(db); 134 135 try 136 { 137 transactionHistory = getTransactionHistoryWithId(transactionHistoryVO.getTransactionHistoryId(), db); 139 transactionHistory.setValueObject(transactionHistoryVO); 140 141 ceb.throwIfNotEmpty(); 143 144 commitTransaction(db); 145 } 146 catch(ConstraintException ce) 147 { 148 logger.warn("An error occurred so we should not complete the transaction:" + ce, ce); 149 rollbackTransaction(db); 150 throw ce; 151 } 152 catch(Exception e) 153 { 154 logger.error("An error occurred so we should not complete the transaction:" + e, e); 155 rollbackTransaction(db); 156 throw new SystemException(e.getMessage()); 157 } 158 159 return transactionHistory.getValueObject(); 160 } 161 162 163 167 168 public Integer create(NotificationMessage notificationMessage) throws SystemException 169 { 170 logger.info("Creating a transactionHistory object..."); 171 Database db = CastorDatabaseService.getDatabase(); 172 TransactionHistory transactionHistory = null; 173 174 try 175 { 176 beginTransaction(db); 177 logger.info("Began transaction..."); 178 179 TransactionHistoryVO transVO = new TransactionHistoryVO(); 180 transactionHistory = new TransactionHistoryImpl(); 181 182 transVO.setName(notificationMessage.getName()); 183 transVO.setSystemUserName(notificationMessage.getSystemUserName()); 184 transVO.setTransactionDateTime(java.util.Calendar.getInstance().getTime()); 185 transVO.setTransactionTypeId(new Integer (notificationMessage.getType())); 186 transVO.setTransactionObjectId(notificationMessage.getObjectId().toString()); 187 transVO.setTransactionObjectName(notificationMessage.getObjectName()); 188 189 transactionHistory.setValueObject(transVO); 190 logger.info("Created the transaction object and filled it with values..."); 191 logger.info("transactionHistory.getId():" + transactionHistory.getId()); 192 logger.info("transactionHistory.getName():" + transactionHistory.getName()); 193 logger.info("transactionHistory.getSystemUserName():" + transactionHistory.getSystemUserName()); 194 logger.info("transactionHistory.getTransactionDateTime():" + transactionHistory.getTransactionDateTime()); 195 logger.info("transactionHistory.getTransactionObjectId():" + transactionHistory.getTransactionObjectId()); 196 logger.info("transactionHistory.getTransactionObjectName():" + transactionHistory.getTransactionObjectName()); 197 logger.info("transactionHistory.getTransactionTypeId():" + transactionHistory.getTransactionTypeId()); 198 logger.info("isActive=" + db.isActive()); 199 200 db.create(transactionHistory); 201 logger.info("Created the transaction object in the database.."); 202 203 commitTransaction(db); 204 logger.info("Committed the transaction.."); 205 } 206 catch(Exception e) 207 { 208 logger.error("An error occurred so we should not complete the transaction:" + e, e); 209 rollbackTransaction(db); 210 throw new SystemException(e.getMessage()); 211 } 212 213 logger.info("TransactionHistory object all done.."); 214 215 return transactionHistory.getValueObject().getTransactionHistoryId(); 216 } 217 218 222 223 public BaseEntityVO getNewVO() 224 { 225 return new TransactionHistoryVO(); 226 } 227 228 } 229 230 | Popular Tags |