KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collection JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29
30 import org.apache.log4j.Logger;
31 import org.exolab.castor.jdo.Database;
32 import org.infoglue.cms.entities.kernel.BaseEntityVO;
33 import org.infoglue.cms.entities.management.InterceptionPoint;
34 import org.infoglue.cms.entities.management.Interceptor;
35 import org.infoglue.cms.entities.management.InterceptorVO;
36 import org.infoglue.cms.entities.management.impl.simple.InterceptorImpl;
37 import org.infoglue.cms.exception.Bug;
38 import org.infoglue.cms.exception.ConstraintException;
39 import org.infoglue.cms.exception.SystemException;
40 import org.infoglue.cms.util.ConstraintExceptionBuffer;
41
42 /**
43  * This class is a helper class for the use case handle Interceptor
44  *
45  * @author Mattias Bogeblad
46  */

47
48 public class InterceptorController extends BaseController
49 {
50     private final static Logger logger = Logger.getLogger(InterceptorController.class.getName());
51
52     /**
53      * Factory method
54      */

55
56     public static InterceptorController getController()
57     {
58         return new InterceptorController();
59     }
60     
61     public Interceptor getInterceptorWithId(Integer JavaDoc interceptorId, Database db) throws SystemException, Bug
62     {
63         return (Interceptor) getObjectWithId(InterceptorImpl.class, interceptorId, db);
64     }
65     
66     public InterceptorVO getInterceptorVOWithId(Integer JavaDoc interceptorId) throws SystemException, Bug
67     {
68         return (InterceptorVO) getVOWithId(InterceptorImpl.class, interceptorId);
69     }
70   
71     public List JavaDoc getInterceptorVOList() throws SystemException, Bug
72     {
73         return getAllVOObjects(InterceptorImpl.class, "interceptorId");
74     }
75
76     public List JavaDoc getInterceptionPointVOList(Integer JavaDoc interceptorId) throws SystemException, Bug
77     {
78         List JavaDoc interceptionPointVOList = null;
79         
80         Database db = CastorDatabaseService.getDatabase();
81
82         try
83         {
84             beginTransaction(db);
85
86             interceptionPointVOList = getInterceptionPointVOList(interceptorId, db);
87
88             commitTransaction(db);
89         }
90         catch (Exception JavaDoc e)
91         {
92             logger.info("An error occurred so we should not complete the transaction:" + e);
93             rollbackTransaction(db);
94             throw new SystemException(e.getMessage());
95         }
96         
97         return interceptionPointVOList;
98     }
99     
100     public List JavaDoc getInterceptionPointVOList(Integer JavaDoc interceptorId, Database db) throws SystemException, Bug
101     {
102         Interceptor interceptor = this.getInterceptorWithId(interceptorId, db);
103         
104         Collection JavaDoc interceptionPoints = interceptor.getInterceptionPoints();
105         
106         return toVOList(interceptionPoints);
107     }
108     
109     /**
110      * Creates a new InterceptorVO
111      *
112      * @param interceptorVO
113      * @return
114      * @throws ConstraintException
115      * @throws SystemException
116      */

117     
118     public InterceptorVO create(InterceptorVO interceptorVO) throws ConstraintException, SystemException
119     {
120         InterceptorVO newinterceptorVO = null;
121         
122         Database db = CastorDatabaseService.getDatabase();
123
124         try
125         {
126             beginTransaction(db);
127
128             newinterceptorVO = create(interceptorVO, db);
129                 
130             commitTransaction(db);
131         }
132         catch (Exception JavaDoc e)
133         {
134             logger.info("An error occurred so we should not complete the transaction:" + e);
135             rollbackTransaction(db);
136             throw new SystemException(e.getMessage());
137         }
138         
139         return newinterceptorVO;
140     }
141     
142     /**
143      * Creates a new InterceptorVO within a transaction
144      *
145      * @param interceptorVO
146      * @return
147      * @throws ConstraintException
148      * @throws SystemException
149      */

150     
151     public InterceptorVO create(InterceptorVO interceptorVO, Database db) throws SystemException, Exception JavaDoc
152     {
153         Interceptor interceptor = new InterceptorImpl();
154         interceptor.setValueObject(interceptorVO);
155         
156         db.create(interceptor);
157                     
158         return interceptor.getValueObject();
159     }
160
161     
162     public InterceptorVO update(InterceptorVO interceptorVO) throws ConstraintException, SystemException
163     {
164         return (InterceptorVO) updateEntity(InterceptorImpl.class, (BaseEntityVO)interceptorVO);
165     }
166
167     public void update(InterceptorVO interceptorVO, String JavaDoc[] values) throws ConstraintException, SystemException
168     {
169         Database db = CastorDatabaseService.getDatabase();
170
171         try
172         {
173             beginTransaction(db);
174             
175             ConstraintExceptionBuffer ceb = interceptorVO.validate();
176             ceb.throwIfNotEmpty();
177             
178             logger.info("InterceptorId:" + interceptorVO.getInterceptorId());
179             Interceptor interceptor = this.getInterceptorWithId(interceptorVO.getInterceptorId(), db);
180
181             interceptor.setValueObject(interceptorVO);
182             
183             Collection JavaDoc interceptionPoints = interceptor.getInterceptionPoints();
184             Iterator JavaDoc interceptionPointsIterator = interceptionPoints.iterator();
185             while(interceptionPointsIterator.hasNext())
186             {
187                 InterceptionPoint interceptionPoint = (InterceptionPoint)interceptionPointsIterator.next();
188                 interceptionPoint.getInterceptors().remove(interceptor);
189             }
190             
191             interceptor.getInterceptionPoints().clear();
192             
193             if(values != null)
194             {
195                 for(int i=0; i<values.length; i++)
196                 {
197                     String JavaDoc interceptionPointId = values[i];
198                     InterceptionPoint interceptionPoint = InterceptionPointController.getController().getInterceptionPointWithId(new Integer JavaDoc(interceptionPointId), db);
199                     interceptor.getInterceptionPoints().add(interceptionPoint);
200                     interceptionPoint.getInterceptors().add(interceptor);
201                 }
202             }
203             
204             commitTransaction(db);
205         }
206         catch (Exception JavaDoc e)
207         {
208             e.printStackTrace();
209             logger.info("An error occurred so we should not complete the transaction:" + e);
210             rollbackTransaction(db);
211             throw new SystemException(e.getMessage());
212         }
213     }
214     
215     public void delete(InterceptorVO interceptorVO) throws ConstraintException, SystemException
216     {
217         deleteEntity(InterceptorImpl.class, interceptorVO.getInterceptorId());
218     }
219
220     /*
221     public void delete(String name, String value, Database db) throws SystemException, Exception
222     {
223         List AccessList = getAccessList(name, value, db);
224         Iterator i = AccessList.iterator();
225         while(i.hasNext())
226         {
227             Access Access = (Access)i.next();
228             db.remove(Access);
229         }
230         
231     }
232     */

233
234     /**
235      * This is a method that gives the user back an newly initialized ValueObject for this entity that the controller
236      * is handling.
237      */

238
239     public BaseEntityVO getNewVO()
240     {
241         return new InterceptorVO();
242     }
243
244 }
245  
Popular Tags