View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2005 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
16   */
17  package ch.elca.el4j.services.search.events;
18  
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.Collections;
22  import java.util.Iterator;
23  import java.util.List;
24  
25  import org.springframework.context.ApplicationEvent;
26  
27  import ch.elca.el4j.services.search.QueryObject;
28  import ch.elca.el4j.util.codingsupport.CollectionUtils;
29  import ch.elca.el4j.util.codingsupport.Reject;
30  
31  /**
32   * Event for query objects.
33   *
34   * @svnLink $Revision: 3883 $;$Date: 2009-08-04 15:35:01 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/core/src/main/java/ch/elca/el4j/services/search/events/QueryObjectEvent.java $
35   *
36   * @author Martin Zeltner (MZE)
37   */
38  public class QueryObjectEvent extends ApplicationEvent {
39  	/**
40  	 * Are the query objecs of this event.
41  	 */
42  	private final List<QueryObject> m_queryObjects;
43  
44  	/**
45  	 * Constructor for one query object.
46  	 *
47  	 * @param source Is the place this event has been created.
48  	 * @param queryObject Is the query object of this event.
49  	 */
50  	public QueryObjectEvent(Object source, QueryObject queryObject) {
51  		super(source);
52  		Reject.ifNull(queryObject);
53  		List<QueryObject> queryObjects = new ArrayList<QueryObject>();
54  		queryObjects.add(queryObject);
55  		m_queryObjects = Collections.unmodifiableList(queryObjects);
56  	}
57  	
58  	/**
59  	 * Constructor for multiple query objects.
60  	 *
61  	 * @param source Is the place this event has been created.
62  	 * @param queryObjects Are the query objects of this event.
63  	 */
64  	public QueryObjectEvent(Object source, Collection<QueryObject> queryObjects) {
65  		super(source);
66  		Reject.ifEmpty(queryObjects, "Minimum one query object required.");
67  		CollectionUtils.containsOnlyObjectsOfType(
68  			queryObjects, QueryObject.class);
69  		m_queryObjects
70  			= Collections.unmodifiableList(new ArrayList<QueryObject>(queryObjects));
71  	}
72  
73  	
74  	/**
75  	 * @return Returns the first unspecific query object of this event. This has
76  	 *         the same effect as method invocation
77  	 *         {@link #getQueryObject(Class)} with <code>null</code>.
78  	 */
79  	public QueryObject getQueryObject() {
80  		return getQueryObject(null);
81  	}
82  	
83  	/**
84  	 * @param beanClass
85  	 *            Is the bean class the requested query object must be made for.
86  	 * @return Returns the query object that is made for the given bean class.
87  	 */
88  	public QueryObject getQueryObject(Class<?> beanClass) {
89  		QueryObject queryObject = null;
90  		Iterator<QueryObject> it = m_queryObjects.iterator();
91  		while (queryObject == null && it.hasNext()) {
92  			QueryObject element = (QueryObject) it.next();
93  			if (element.getBeanClass() == beanClass) {
94  				queryObject = element;
95  			}
96  		}
97  		return queryObject;
98  	}
99  	
100 	/**
101 	 * @return Returns the list of query objects of this event.
102 	 */
103 	public List<QueryObject> getQueryObjects() {
104 		return m_queryObjects;
105 	}
106 }