KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > patterns > listdata > logic > impl > ListControllerImpl


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: ListControllerImpl.java,v 1.20 2007/01/28 06:54:48 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.patterns.listdata.logic.impl;
23
24 import java.util.List JavaDoc;
25
26 import org.opensubsystems.core.error.OSSException;
27 import org.opensubsystems.core.logic.impl.StatelessControllerImpl;
28 import org.opensubsystems.core.persist.DataFactoryManager;
29 import org.opensubsystems.core.util.CallContext;
30 import org.opensubsystems.core.util.GlobalConstants;
31 import org.opensubsystems.core.util.Messages;
32 import org.opensubsystems.patterns.listdata.data.ListDefinition;
33 import org.opensubsystems.patterns.listdata.data.ListOptions;
34 import org.opensubsystems.patterns.listdata.data.SimpleRule;
35 import org.opensubsystems.patterns.listdata.logic.ListController;
36 import org.opensubsystems.patterns.listdata.persist.ListFactory;
37
38 /**
39  * This controller handles all functionality connected with listing of data
40  * objects by pages, filtered with filters, sorted by columns, secured by access
41  * rights, and maybe much more in the future. You can use instances of this
42  * controller directly without the need to derive any new classes or code any
43  * new functionality. Just get instance of the controller from the controller
44  * manager, create list options and invoke the method of this controller you are
45  * interested in.
46  *
47  * @ejb.bean type="Stateless"
48  * name="ListController"
49  * jndi-name="org.opensubsystems.patterns.listdata.logic.ListControllerRemote"
50  * local-jndi-name="org.opensubsystems.patterns.listdata.logic.ListController"
51  * @ejb.interface
52  * local-extends="javax.ejb.EJBLocalObject,
53  * org.opensubsystems.patterns.listdata.logic.ListController"
54  * extends="javax.ejb.EJBObject, org.opensubsystems.patterns.listdata.logic.ListController"
55  * @ejb.home local-extends="javax.ejb.EJBLocalHome"
56  * extends="javax.ejb.EJBHome"
57  *
58  * @jonas.bean ejb-name="ListController"
59  * jndi-name="org.opensubsystems.patterns.listdata.logic.ListControllerRemote"
60  *
61  * @version $Id: ListControllerImpl.java,v 1.20 2007/01/28 06:54:48 bastafidli Exp $
62  * @author Julo Legeny
63  * @code.reviewer Miro Halas
64  * @code.reviewed 1.15 2006/03/13 16:49:16 bastafidli
65  */

66 public class ListControllerImpl extends StatelessControllerImpl
67                                 implements ListController
68 {
69    // Constants ////////////////////////////////////////////////////////////////
70

71    /**
72     * Lock used in synchronized sections.
73     */

74    protected static final String JavaDoc IMPL_LOCK = "IMPL_LOCK";
75    
76    // Attributes ///////////////////////////////////////////////////////////////
77

78    /**
79     * Generated serial version id for this class.
80     */

81    private static final long serialVersionUID = -3369714862377860814L;
82
83    // Business logic ///////////////////////////////////////////////////////////
84

85    /**
86     * {@inheritDoc}
87     *
88     * @ejb.interface-method
89     * @ejb.transaction type="Supports"
90     */

91    public Object JavaDoc[] getShowList(
92       ListOptions options
93    ) throws OSSException
94    {
95       ListFactory factory;
96
97       factory = preprocess(options);
98
99       ListDefinition definition;
100       int[] arrOrderColumnCodes;
101       String JavaDoc[] arrOrderDirections;
102       int iClientOrderCode = 0;
103       String JavaDoc strClientOrderDirection = "";
104
105       definition = options.getDefinition();
106       // If there is already preset page size then keep it
107
// 0 is currently ListOptions.PAGE_SIZE_ALL therefore if somebody requests
108
// that we want to keep it
109
if (options.getPageSize() < 0)
110       {
111          // Page size wasn't initialized so initialize it
112
options.setPageSize(definition.getPageSize());
113       }
114       options.setBeginPosition(1);
115       // If the page size is still undefined reset it to the default one
116
if (options.getPageSize() < 1)
117       {
118          options.setPageSize(ListOptions.PAGE_SIZE_ALL);
119          options.setActualPage(1);
120       }
121
122       arrOrderColumnCodes = definition.getOrderColumnCodes();
123       arrOrderDirections = definition.getOrderDirections();
124       options.setOrdering(arrOrderColumnCodes, arrOrderDirections);
125       
126       if ((arrOrderColumnCodes != null) && (arrOrderColumnCodes.length > 0))
127       {
128          iClientOrderCode = arrOrderColumnCodes[0];
129          strClientOrderDirection = arrOrderDirections[0];
130       }
131       options.setClientOrderCode(iClientOrderCode);
132       options.setClientOrderDirection(strClientOrderDirection);
133       
134       List JavaDoc objectList = null;
135       if (options.getSecurityFilter() == null)
136       {
137          CallContext.getInstance().getMessages().addMessage(
138             Messages.ACCESSRIGHT_ERRORS, "No rights to view list");
139       }
140       else
141       {
142          objectList = factory.get(options);
143       }
144       
145       return postprocess(options, objectList, factory);
146    }
147    
148    /**
149     * {@inheritDoc}
150     *
151     * @ejb.interface-method
152     * @ejb.transaction type="Supports"
153     */

154    public Object JavaDoc[] getSetDefinition(
155       ListOptions options
156    ) throws OSSException
157    {
158       ListFactory factory;
159    
160       factory = preprocess(options);
161
162       ListDefinition definition;
163       int[] arrOrderColumnCodes;
164       String JavaDoc[] arrOrderDirections;
165       int iClientOrderCode = 0;
166       String JavaDoc strClientOrderDirection = "";
167
168       definition = options.getDefinition();
169       options.setPageSize(definition.getPageSize());
170       options.setBeginPosition(1);
171       // If the page size is still undefined reset it to the default one
172
if (options.getPageSize() < 1)
173       {
174          options.setPageSize(ListOptions.PAGE_SIZE_ALL);
175          options.setActualPage(1);
176       }
177       
178       arrOrderColumnCodes = definition.getOrderColumnCodes();
179       arrOrderDirections = definition.getOrderDirections();
180       options.setOrdering(arrOrderColumnCodes, arrOrderDirections);
181       
182       if ((arrOrderColumnCodes != null) && (arrOrderColumnCodes.length > 0))
183       {
184          iClientOrderCode = arrOrderColumnCodes[0];
185          strClientOrderDirection = arrOrderDirections[0];
186       }
187       options.setClientOrderCode(iClientOrderCode);
188       options.setClientOrderDirection(strClientOrderDirection);
189             
190       List JavaDoc objectList = null;
191       if (options.getSecurityFilter() == null)
192       {
193          CallContext.getInstance().getMessages().addMessage(
194             Messages.ACCESSRIGHT_ERRORS, "No rights to view list");
195       }
196       else
197       {
198          objectList = factory.get(options);
199       }
200       
201       return postprocess(options, objectList, factory);
202    }
203
204    /**
205     * {@inheritDoc}
206     *
207     * @ejb.interface-method
208     * @ejb.transaction type="Supports"
209     */

210    public Object JavaDoc[] getSetOrder(
211       ListOptions options
212    ) throws OSSException
213    {
214       ListFactory factory;
215    
216       factory = preprocess(options);
217       
218       int[] arrOrderColumnCodes;
219       String JavaDoc[] arrOrderDirections;
220       int iClientOrderCode = 0;
221       String JavaDoc strClientOrderDirection = "";
222
223       options.setBeginPosition(1);
224       // If the page size is still undefined reset it to the default one
225
if (options.getPageSize() < 1)
226       {
227          options.setPageSize(ListOptions.PAGE_SIZE_ALL);
228          options.setActualPage(1);
229       }
230       
231       arrOrderColumnCodes = options.getOrderColumnCodes();
232       arrOrderDirections = options.getOrderDirections();
233       if ((arrOrderColumnCodes != null) && (arrOrderColumnCodes.length > 0))
234       {
235          iClientOrderCode = arrOrderColumnCodes[0];
236          strClientOrderDirection = arrOrderDirections[0];
237       }
238       options.setClientOrderCode(iClientOrderCode);
239       options.setClientOrderDirection(strClientOrderDirection);
240       
241       List JavaDoc objectList = null;
242       if (options.getSecurityFilter() == null)
243       {
244          CallContext.getInstance().getMessages().addMessage(
245             Messages.ACCESSRIGHT_ERRORS, "No rights to view list");
246       }
247       else
248       {
249          objectList = factory.get(options);
250       }
251       
252       return postprocess(options, objectList, factory);
253    }
254    
255    /**
256     * {@inheritDoc}
257     *
258     * @ejb.interface-method
259     * @ejb.transaction type="Supports"
260     */

261    public Object JavaDoc[] getSetPageSize(
262       ListOptions options
263    ) throws OSSException
264    {
265       ListFactory factory;
266    
267       factory = preprocess(options);
268
269       if (options.getPageSize() < 1)
270       {
271          options.setPageSize(ListOptions.PAGE_SIZE_ALL);
272          options.setBeginPosition(1);
273          options.setActualPage(1);
274       }
275       
276       List JavaDoc objectList = null;
277       if (options.getSecurityFilter() == null)
278       {
279          CallContext.getInstance().getMessages().addMessage(
280             Messages.ACCESSRIGHT_ERRORS, "No rights to view list");
281       }
282       else
283       {
284          objectList = factory.get(options);
285       }
286       
287       return postprocess(options, objectList, factory);
288    }
289
290    /**
291     * {@inheritDoc}
292     *
293     * @ejb.interface-method
294     * @ejb.transaction type="Supports"
295     */

296    public Object JavaDoc[] getPreviousPage(
297       ListOptions options
298    ) throws OSSException
299    {
300       ListFactory factory;
301    
302       factory = preprocess(options);
303
304       options.setBeginPosition(options.getBeginPosition() - options.getPageSize());
305       
306       if (options.getBeginPosition() < 1)
307       {
308          options.setBeginPosition(1);
309       }
310       
311       if (options.getPageSize() < 1)
312       {
313          options.setPageSize(ListOptions.PAGE_SIZE_ALL);
314          options.setBeginPosition(1);
315          options.setActualPage(1);
316       }
317
318       List JavaDoc objectList = null;
319       if (options.getSecurityFilter() == null)
320       {
321          CallContext.getInstance().getMessages().addMessage(
322             Messages.ACCESSRIGHT_ERRORS, "No rights to view list");
323       }
324       else
325       {
326          objectList = factory.get(options);
327       }
328       
329       return postprocess(options, objectList, factory);
330    }
331
332    /**
333     * {@inheritDoc}
334     *
335     * @ejb.interface-method
336     * @ejb.transaction type="Supports"
337     */

338    public Object JavaDoc[] getNextPage(
339       ListOptions options
340    ) throws OSSException
341    {
342       ListFactory factory;
343    
344       factory = preprocess(options);
345
346       if (options.getEndPosition() < 1)
347       {
348          options.setEndPosition(1);
349       }
350       
351       options.setBeginPosition(options.getEndPosition() + 1);
352       
353       if (options.getPageSize() < 1)
354       {
355          options.setPageSize(ListOptions.PAGE_SIZE_ALL);
356          options.setBeginPosition(1);
357          options.setActualPage(1);
358       }
359
360       List JavaDoc objectList = null;
361       if (options.getSecurityFilter() == null)
362       {
363          CallContext.getInstance().getMessages().addMessage(
364             Messages.ACCESSRIGHT_ERRORS, "No rights to view list");
365       }
366       else
367       {
368          objectList = factory.get(options);
369       }
370       
371       return postprocess(options, objectList, factory);
372    }
373    
374    /**
375     * {@inheritDoc}
376     *
377     * @ejb.interface-method
378     * @ejb.transaction type="Supports"
379     */

380    public Object JavaDoc[] getExactPage(
381       ListOptions options
382    ) throws OSSException
383    {
384       ListFactory factory;
385    
386       factory = preprocess(options);
387
388       if (options.getActualPage() < 1)
389       {
390          options.setActualPage(1);
391       }
392       
393       options.setBeginPosition(
394          ((options.getActualPage() - 1) * options.getPageSize()) + 1
395       );
396          
397       if (options.getPageSize() < 1)
398       {
399          options.setPageSize(ListOptions.PAGE_SIZE_ALL);
400          options.setBeginPosition(1);
401          options.setActualPage(1);
402       }
403
404       List JavaDoc objectList = null;
405       if (options.getSecurityFilter() == null)
406       {
407          CallContext.getInstance().getMessages().addMessage(
408             Messages.ACCESSRIGHT_ERRORS, "No rights to view list");
409       }
410       else
411       {
412          objectList = factory.get(options);
413       }
414       
415       return postprocess(options, objectList, factory);
416    }
417
418
419    /**
420     * {@inheritDoc}
421     *
422     * @ejb.interface-method
423     * @ejb.transaction type="Supports"
424     */

425    public List JavaDoc getDefinitionList(
426       int iDataType
427    ) throws OSSException
428    {
429       return null;
430    }
431
432    /**
433     * {@inheritDoc}
434     *
435     * @ejb.interface-method
436     * @ejb.transaction type="Supports"
437     */

438    public void constructor(
439    ) throws OSSException
440    {
441       // Nothing to initialize here
442
}
443
444    // Helper methods ///////////////////////////////////////////////////////////
445

446    /**
447     * Get security filter for this list definiting what data object is user
448     * authorized to access. User can by default see all data, no security
449     * implemented at this level
450     *
451     * @param factory - list factory for given options
452     * @param options - list options
453     * @return SimpleRule - rule representing filter to retrieve only data which
454     * user is authorized to see
455     * @throws OSSException - an erro has occured
456     */

457    protected SimpleRule getSecurityFilter(
458       ListFactory factory,
459       ListOptions options
460    ) throws OSSException
461    {
462       // User can by default see all data, no security implemented at this level.
463
return SimpleRule.ALL_DATA;
464    }
465    
466    /**
467     * Get list definition to use for specified options.
468     *
469     * @param factory - list factory for given options
470     * @param options - list options to get the definition for
471     * @return ListDefinition - definition object, never null
472     * @throws OSSException - an error has occured
473     */

474    protected ListDefinition getDefinition(
475       ListFactory factory,
476       ListOptions options
477    ) throws OSSException
478    {
479       ListDefinition definition;
480
481       // If there is already a definition then keep it since thats what the
482
// client has setup, if there is not one the load the default one from
483
// the factory
484
definition = options.getDefinition();
485       if (definition == null)
486       {
487          definition = factory.getDefaultListDefinition();
488       
489          if (GlobalConstants.ERROR_CHECKING)
490          {
491             assert definition != null : "Cannot get any usable list definition.";
492          }
493       
494          // Initialize the show columns from definition but only if it doesn't
495
// already have show columns defined since the requested columns should
496
// take precendence over the default ones.
497
if (((options.getShowColumnCodes() == null)
498                || (options.getShowColumnCodes().length == 0))
499             && (definition.getShowColumnCodes() != null))
500          {
501             options.setShowColumnCodes(definition.getShowColumnCodes());
502          }
503       }
504       
505       return definition;
506    }
507
508    /**
509     * Preprocess list options before the list controller functionality is invoked
510     *
511     * @param options - list options to preprocess
512     * @return ListFactory - list factory to use to load the data
513     * @throws OSSException - an error has occured during preprocessing
514     */

515    protected ListFactory preprocess(
516       ListOptions options
517    ) throws OSSException
518    {
519       ListFactory factory;
520    
521       factory = (ListFactory)DataFactoryManager.getInstance(
522                                 options.getListFactoryClass());
523       
524       ListDefinition definition;
525       
526       definition = getDefinition(factory, options);
527       options.setDefinition(definition);
528       
529       SimpleRule securityFilter;
530       
531       securityFilter = getSecurityFilter(factory, options);
532       options.setSecurityFilter(securityFilter);
533
534       return factory;
535    }
536    
537    /**
538     * Postprocess list of retrieved data objectcts. This method can be
539     * overwritten and can modify returned results.
540     *
541     * @param options - list options to postprocess
542     * @param objectList - list of retrieved data objects
543     * @param factory - list factory to use to load the data
544     * @return Object[] - index 0 - updated list options
545     * - index 1 - list of DataObjects
546     * - index 2 - object tatatype
547     * @throws OSSException - an error has occured during preprocessing
548     */

549    protected Object JavaDoc[] postprocess(
550       ListOptions options,
551       List JavaDoc objectList,
552       ListFactory factory
553    ) throws OSSException
554    {
555       return new Object JavaDoc[] {options, objectList, new Integer JavaDoc(factory.getDataType())};
556    }
557 }
558
Popular Tags