View Javadoc

1   /* Copyright 2004, 2005 Acegi Technology Pty Limited
2    *
3    * Licensed under the Apache License, Version 2.0 (the "License");
4    * you may not use this file except in compliance with the License.
5    * You may obtain a copy of the License at
6    *
7    *     http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  
16  package org.acegisecurity.domain.service;
17  
18  import java.io.Serializable;
19  import java.util.Collection;
20  import java.util.List;
21  
22  import org.acegisecurity.domain.PersistableEntity;
23  import org.acegisecurity.domain.dao.Dao;
24  import org.acegisecurity.domain.dao.PaginatedList;
25  import org.acegisecurity.domain.util.GenericsUtils;
26  
27  import org.springframework.beans.factory.InitializingBean;
28  import org.springframework.context.support.ApplicationObjectSupport;
29  import org.springframework.util.Assert;
30  
31  /***
32   * Base {@link ImmutableManager} implementation.
33   *
34   * @author Ben Alex
35   * @version $Id: ImmutableManagerImpl.java,v 1.2 2005/11/17 00:55:51 benalex Exp $
36   */
37  public class ImmutableManagerImpl<E extends PersistableEntity> extends ApplicationObjectSupport implements ImmutableManager<E>, InitializingBean {
38      //~ Instance fields ========================================================
39  
40      /*** The class that this instance provides services for */
41      private Class supportsClass;
42  	private String beanName;
43  	
44  	protected Dao<E> dao;
45  
46      //~ Methods ================================================================
47  	
48  	public ImmutableManagerImpl() {
49  		this.supportsClass = GenericsUtils.getGeneric(getClass());
50  		if (supportsClass == null) {
51  			if (logger.isWarnEnabled()) {
52  				logger.warn("Could not determine the generics type - you will need to set manually");
53  			}
54  		}
55  	}
56  
57      public void setSupportsClass(Class supportClass) {
58          this.supportsClass = supportClass;
59      }
60  
61      public Class getSupportsClass() {
62          return supportsClass;
63      }
64  
65      public Dao<E> getDao() {
66  		return dao;
67  	}
68  
69  	public void setDao(Dao<E> dao) {
70  		this.dao = dao;
71  	}
72  
73  	/***
74  	 * @return the sort order column to be used by default by the scroll methods
75  	 */
76  	protected String getDefaultSortOrder() {
77  		return "id";
78  	}
79  	
80  	/***
81  	 * Provides hook for custom subclasses to provide initialization behaviour
82  	 * 
83  	 * @throws Exception
84  	 */
85  	protected void doInitManager() throws Exception {}
86  	
87  	public final void afterPropertiesSet() throws Exception {
88          Assert.notNull(supportsClass, "supportClass is required");
89          Assert.isTrue(PersistableEntity.class.isAssignableFrom(supportsClass),
90          "supportClass is not an implementation of PersistableEntity");
91  		Assert.notNull(dao, "Dao is null");
92  		Assert.isTrue(dao.supports(supportsClass), "Dao '" + dao + "' does not support '" + supportsClass + "'");
93  		doInitManager();
94  	}
95  
96      public List<E> findAll() {
97          return dao.findAll();
98      }
99  
100     public List<E> findId(Collection<Serializable> ids) {
101         Assert.notNull(ids, "Collection of IDs cannot be null");
102         Assert.notEmpty(ids, "There must be some values in the Collection list");
103         return dao.findId(ids);
104     }
105 
106     public E readId(Serializable id) {
107         Assert.notNull(id);
108         return dao.readId(id);
109     }
110 
111     public E readPopulatedId(Serializable id) {
112 		Assert.notNull(id);
113 		return dao.readPopulatedId(id);
114 	}
115 
116 	public PaginatedList<E> scroll(E value, int firstElement,
117         int maxElements) {
118         Assert.notNull(value);
119 		Assert.isInstanceOf(this.supportsClass, value, "Can only scroll with values this manager supports");
120 
121         return dao.scroll(value, firstElement, maxElements, getDefaultSortOrder());
122     }
123 
124     public PaginatedList<E> scrollPopulated(E value, int firstElement, int maxElements) {
125         Assert.notNull(value);
126 		Assert.isInstanceOf(this.supportsClass, value, "Can only scroll with values this manager supports");
127 		
128 		return dao.scrollPopulated(value, firstElement, maxElements, getDefaultSortOrder());
129 	}
130 
131 	public PaginatedList<E> scrollWithSubclasses(E value, int firstElement,
132 	        int maxElements) {
133 	        Assert.notNull(value);
134 			Assert.isInstanceOf(this.supportsClass, value, "Can only scroll with values this manager supports");
135 
136 	        return dao.scrollWithSubclasses(value, firstElement, maxElements, getDefaultSortOrder());
137 	    }
138 
139 	public PaginatedList<E> scrollPopulatedWithSubclasses(E value, int firstElement, int maxElements) {
140         Assert.notNull(value);
141 		Assert.isInstanceOf(this.supportsClass, value, "Can only scroll with values this manager supports");
142 
143 		return dao.scrollPopulatedWithSubclasses(value, firstElement, maxElements, getDefaultSortOrder());
144 	}
145 
146 	public boolean supports(Class clazz) {
147         Assert.notNull(clazz);
148 
149         return this.supportsClass.equals(clazz);
150     }
151 
152 	public void setBeanName(String beanName) {
153 		this.beanName = beanName;
154 	}
155 }