KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > controllers > kernel > impl > simple > TransactionHistoryController


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.cms.controllers.kernel.impl.simple;
25
26 import java.util.List JavaDoc;
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     /**
47      * Factory method
48      */

49
50     public static TransactionHistoryController getController()
51     {
52         return new TransactionHistoryController();
53     }
54     
55     public TransactionHistoryVO getTransactionHistoryVOWithId(Integer JavaDoc transactionHistoryId) throws SystemException, Bug
56     {
57         return (TransactionHistoryVO) getVOWithId(TransactionHistoryImpl.class, transactionHistoryId);
58     }
59
60     public TransactionHistory getTransactionHistoryWithId(Integer JavaDoc transactionHistoryId, Database db) throws SystemException, Bug
61     {
62         return (TransactionHistory) getObjectWithId(TransactionHistoryImpl.class, transactionHistoryId, db);
63     }
64
65
66     public List JavaDoc getTransactionHistoryVOList() throws SystemException, Bug
67     {
68         return getAllVOObjects(TransactionHistoryImpl.class, "transactionHistoryId");
69     }
70
71
72     /**
73      * This method deletes the TransactionHistory sent in from the system.
74      */

75     
76     public void deleteTransactionHistory(Integer JavaDoc transactionHistoryId, Database db) throws SystemException, Bug
77     {
78         try
79         {
80             db.remove(getTransactionHistoryWithId(transactionHistoryId, db));
81         }
82         catch(Exception JavaDoc 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 JavaDoc entClass, Integer JavaDoc 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 JavaDoc 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             //add validation here if needed
138
transactionHistory = getTransactionHistoryWithId(transactionHistoryVO.getTransactionHistoryId(), db);
139             transactionHistory.setValueObject(transactionHistoryVO);
140
141             //If any of the validations or setMethods reported an error, we throw them up now before create.
142
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 JavaDoc 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     /**
164      * This method is a bit different from other creates as it does not use the common base-class-method.
165      * Using it would result in a recursive loop of new notificationMessages.
166      */

167     
168     public Integer JavaDoc 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 JavaDoc(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 JavaDoc 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     /**
219      * This is a method that gives the user back an newly initialized ValueObject for this entity that the controller
220      * is handling.
221      */

222
223     public BaseEntityVO getNewVO()
224     {
225         return new TransactionHistoryVO();
226     }
227
228 }
229  
230
Popular Tags