KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > blog > logic > impl > BlogControllerImpl


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenChronicle
5  *
6  * $Id: BlogControllerImpl.java,v 1.6 2007/02/01 07:34:37 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21  
22 package org.opensubsystems.blog.logic.impl;
23
24 import java.util.List JavaDoc;
25
26 import org.opensubsystems.blog.data.Blog;
27 import org.opensubsystems.blog.data.Entry;
28 import org.opensubsystems.blog.logic.BlogController;
29 import org.opensubsystems.blog.persist.BlogFactory;
30 import org.opensubsystems.blog.persist.EntryFactory;
31 import org.opensubsystems.core.data.DataObject;
32 import org.opensubsystems.core.data.ModifiableDataObject;
33 import org.opensubsystems.core.error.OSSException;
34 import org.opensubsystems.core.logic.impl.ModifiableDataControllerImpl;
35 import org.opensubsystems.core.persist.BasicDataFactory;
36 import org.opensubsystems.core.persist.DataFactory;
37 import org.opensubsystems.core.persist.DataFactoryManager;
38 import org.opensubsystems.core.util.CallContext;
39 import org.opensubsystems.core.util.GlobalConstants;
40
41 /**
42  * The main entry point to all business functionality connected with blogs.
43  *
44  * View-type has to be set to local due to bug XDT-867 affecting WebSphere
45  * Refs has to be set to local JNDI name since we do not want to use remote objects.
46  *
47  * @ejb.bean type="Stateless"
48  * name="BlogController"
49  * view-type="local"
50  * jndi-name="org.opensubsystems.blog.logic.BlogControllerRemote"
51  * local-jndi-name="org.opensubsystems.blog.logic.BlogController"
52  * @ejb.interface
53  * local-extends="javax.ejb.EJBLocalObject, org.opensubsystems.blog.logic.BlogController"
54  * extends="javax.ejb.EJBObject, org.opensubsystems.blog.logic.BlogController"
55  *
56  * @jonas.bean ejb-name="BlogController"
57  * jndi-name="org.opensubsystems.blog.logic.BlogControllerRemote"
58  *
59  * @version $Id: BlogControllerImpl.java,v 1.6 2007/02/01 07:34:37 bastafidli Exp $
60  * @author Miro Halas
61  * @code.reviewer Miro Halas
62  * @code.reviewed Initial revision
63  */

64 public class BlogControllerImpl extends ModifiableDataControllerImpl
65                                 implements BlogController
66 {
67    // Cached values ////////////////////////////////////////////////////////////
68

69    /**
70     * Factory to use to execute persistence operations.
71     */

72    protected BlogFactory m_blogFactory = null;
73    
74    /**
75     * Factory to use to execute persistence operations.
76     */

77    protected EntryFactory m_entryFactory = null;
78
79    // Attributes ///////////////////////////////////////////////////////////////
80

81    /**
82     * Generated serial version id for this class.
83     */

84    private static final long serialVersionUID = -4304971836362014336L;
85
86    // Constructors /////////////////////////////////////////////////////////////
87

88    /**
89     * Default constructor.
90     */

91    public BlogControllerImpl(
92    )
93    {
94       super();
95       
96       // Do not cache anything here since if this controller is run as a stateless
97
// session bean the referenced objects may not be ready
98
m_blogFactory = null;
99       m_entryFactory = null;
100    }
101
102    // Business logic ///////////////////////////////////////////////////////////
103

104    /**
105     * {@inheritDoc}
106     *
107     * @ejb.interface-method
108     * @ejb.transaction type="Supports"
109     */

110    public Blog get(
111       String JavaDoc strFolder
112    ) throws OSSException
113    {
114       return m_blogFactory.get(strFolder);
115    }
116    
117    /**
118     * {@inheritDoc}
119     *
120     * @ejb.interface-method
121     * @ejb.transaction type="Supports"
122     */

123    public List JavaDoc getEntries(
124       int iBlogId
125    ) throws OSSException
126    {
127       List JavaDoc lstEntries;
128       
129       lstEntries = m_entryFactory.getAll(iBlogId);
130       
131       return lstEntries;
132    }
133    
134    /**
135     * {@inheritDoc}
136     *
137     * @ejb.interface-method
138     * @ejb.transaction type="Supports"
139     */

140    public Object JavaDoc[] getWithEntries(
141       String JavaDoc strFolder
142    ) throws OSSException
143    {
144       Object JavaDoc[] returnValue = {null, null};
145       Blog blog;
146       List JavaDoc lstEntries;
147       
148       // This demonstrate important technique and that is session facade pattern
149
// One call to controller is used to fetch multiple pieces of data.
150
// This is important if controller is remote from the presentation tier
151
// and then we want to perform only as little remote calls as possible.
152
blog = m_blogFactory.get(strFolder);
153       if (blog != null)
154       {
155          returnValue[0] = blog;
156          lstEntries = m_entryFactory.getAll(blog.getId());
157          if (lstEntries != null)
158          {
159             returnValue[1] = lstEntries;
160          }
161       }
162       
163       return returnValue;
164    }
165
166    /**
167     * {@inheritDoc}
168     *
169     * @ejb.interface-method
170     * @ejb.transaction type="Supports"
171     */

172    public Object JavaDoc[] getWithEntry(
173       String JavaDoc strFolder,
174       int iEntryId
175    ) throws OSSException
176    {
177       Object JavaDoc[] returnValue = {null, null};
178       Blog blog;
179       Entry entry;
180       
181       // This demonstrate important technique and that is session facade pattern
182
// One call to controller is used to fetch multiple pieces of data.
183
// This is important if controller is remote from the presentation tier
184
// and then we want to perform only as little remote calls as possible.
185

186       // TODO: Performace: Here we are making two calls to the database, one for
187
// entry and one for blog. But the relationship is 1:1 so we could fetch
188
// entry and it's blog with one call and then just construct two objects
189
// from the one result set.
190

191       // Performance improvement: Since we know the entry ID try to load
192
// it first and then find the blog by its id instead of searching by folder
193
entry = (Entry)m_entryFactory.get(iEntryId,
194                         CallContext.getInstance().getCurrentDomainId());
195       if (entry != null)
196       {
197          returnValue[1] = entry;
198          if (entry.getParentId() != DataObject.NEW_ID)
199          {
200             // Entry was found, now find the blog faster way
201
blog = (Blog)m_blogFactory.get(entry.getParentId(),
202                             CallContext.getInstance().getCurrentDomainId());
203             
204             if (GlobalConstants.ERROR_CHECKING)
205             {
206                assert blog != null : "Blog cannot be null when entry was found";
207             }
208          }
209          else
210          {
211             // The entry doesn't exist yet so still have to find blog by the folder
212
blog = m_blogFactory.get(strFolder);
213          }
214       }
215       else
216       {
217          // Entry wasn't found but we can still try to find blog by the folder
218
blog = m_blogFactory.get(strFolder);
219       }
220       if (blog != null)
221       {
222          returnValue[0] = blog;
223       }
224       
225       return returnValue;
226    }
227    
228    /**
229     * {@inheritDoc}
230     *
231     * @ejb.interface-method
232     * @ejb.transaction type="Supports"
233     */

234    public Object JavaDoc[] getWithEntry(
235       int iBlogId,
236       int iEntryId
237    ) throws OSSException
238    {
239       Object JavaDoc[] returnValue = {null, null};
240       Blog blog;
241       Entry entry;
242       
243       // This demonstrate important technique and that is session facade pattern
244
// One call to controller is used to fetch multiple pieces of data.
245
// This is important if controller is remote from the presentation tier
246
// and then we want to perform only as little remote calls as possible.
247

248       // TODO: Performace: Here we are making two calls to the database, one for
249
// entry and one for blog. But the relationship is 1:1 so we could fetch
250
// entry and it's blog with one call and then just construct two objects
251
// from the one result set.
252

253       blog = (Blog)m_blogFactory.get(iBlogId,
254                       CallContext.getInstance().getCurrentDomainId());
255       if (blog != null)
256       {
257          returnValue[0] = blog;
258          entry = (Entry)m_entryFactory.get(iEntryId,
259                            CallContext.getInstance().getCurrentDomainId());
260          if (entry != null)
261          {
262             if (iEntryId == DataObject.NEW_ID)
263             {
264                entry.setParentId(iBlogId);
265             }
266             returnValue[1] = entry;
267          }
268       }
269       
270       return returnValue;
271    }
272
273    /**
274     * {@inheritDoc}
275     *
276     * @ejb.interface-method
277     * @ejb.transaction type="Supports"
278     */

279    public List JavaDoc getAll(
280    ) throws OSSException
281    {
282       return m_blogFactory.getAll();
283    }
284
285    /**
286     * {@inheritDoc}
287     *
288     * @ejb.interface-method
289     * @ejb.transaction type="Required"
290     */

291    public void deleteEntry(
292       int iBlogEntryId
293    ) throws OSSException
294    {
295       m_entryFactory.delete(iBlogEntryId,
296                             CallContext.getInstance().getCurrentDomainId());
297    }
298
299    /**
300     * {@inheritDoc}
301     *
302     * @ejb.interface-method
303     * @ejb.transaction type="Supports"
304     */

305    public void constructor(
306    ) throws OSSException
307    {
308       m_blogFactory = (BlogFactory)DataFactoryManager.getInstance(BlogFactory.class);
309       m_entryFactory = (EntryFactory)DataFactoryManager.getInstance(EntryFactory.class);
310    }
311
312    // Helper methods ///////////////////////////////////////////////////////////
313

314    /**
315     * {@inheritDoc}
316     */

317    protected DataFactory getDataFactory(
318    )
319    {
320       // Blog is the default entity for this controller so return the blog factory
321
return m_blogFactory;
322    }
323
324    /**
325     * {@inheritDoc}
326     */

327    protected BasicDataFactory getDataFactory(
328       DataObject data
329    )
330    {
331       BasicDataFactory factory = null;
332       
333       if (GlobalConstants.ERROR_CHECKING)
334       {
335          assert data != null : "Cannot return factory for null data object";
336       }
337       
338       if (data instanceof Blog)
339       {
340          factory = m_blogFactory;
341       }
342       else if (data instanceof Entry)
343       {
344          factory = m_entryFactory;
345       }
346       else
347       {
348          if (GlobalConstants.ERROR_CHECKING)
349          {
350             assert false : "Cannot return factory for unrecognized data type "
351                            + data.getClass().getName();
352          }
353       }
354       
355       return factory;
356    }
357
358    // XDoclet EJB bug workaround methods ///////////////////////////////////////
359
// TODO: Bug: XDoclet: 1.2.3: XDoclet incorrectly generates interfaces for
360
// these methods so override the to force it to generate correct information
361
// Verified on 8/30/2006 with JBoxx 4.0.4
362

363    /**
364     * {@inheritDoc}
365     *
366     * @ejb.interface-method
367     * @ejb.transaction type="Required"
368     */

369    public ModifiableDataObject save(ModifiableDataObject data) throws OSSException
370    {
371       return super.save(data);
372    }
373
374    /**
375     * {@inheritDoc}
376     *
377     * @ejb.interface-method
378     * @ejb.transaction type="Required"
379     */

380    public DataObject create(DataObject data) throws OSSException
381    {
382       return super.create(data);
383    }
384
385    /**
386     * {@inheritDoc}
387     *
388     * @ejb.interface-method
389     * @ejb.transaction type="Required"
390     */

391    public void delete(int iId) throws OSSException
392    {
393       super.delete(iId);
394    }
395
396    /**
397     * {@inheritDoc}
398     *
399     * @ejb.interface-method
400     * @ejb.transaction type="Supports"
401     */

402    public DataObject get(int iId) throws OSSException
403    {
404       return super.get(iId);
405    }
406 }
407
Popular Tags