View Javadoc

1   // Copyright 2008 Thiago H. de Paula Figueiredo
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  package br.com.arsmachina.tapestrycrud.grid;
16  
17  import java.io.Serializable;
18  import java.util.List;
19  
20  import org.apache.tapestry5.grid.GridDataSource;
21  
22  import br.com.arsmachina.controller.Controller;
23  import br.com.arsmachina.controller.ReadableController;
24  import br.com.arsmachina.dao.SortCriterion;
25  
26  /**
27   * {@link GridDataSource} implementation using a {@link Controller} instance, specifically its
28   * {@link Controller#findAll(int, int, SortCriterion[])} method.
29   * 
30   * @param <T> the entity class related to this controller.
31   * @param <K> the type of the field that represents the entity class' primary key.
32   * @author Thiago H. de Paula Figueiredo
33   */
34  public class ControllerGridDataSource<T, K extends Serializable> extends
35  		PagedSearchGridDataSource<T> {
36  
37  	/**
38  	 * Single construtctor of this class.
39  	 * 
40  	 * @param controller a {@link Controller}. It cannot be <code>null</code>.
41  	 */
42  	@SuppressWarnings("unchecked")
43  	public ControllerGridDataSource(Class<T> clasz, ReadableController<T, K> controller) {
44  		super(clasz, new ReadableControllerPagedSearch(controller));
45  	}
46  
47  	final private static class ReadableControllerPagedSearch<T, K extends Serializable> implements
48  			PagedSearch<T> {
49  
50  		final private ReadableController<T, K> controller;
51  
52  		/**
53  		 * Single constructor of this class.
54  		 * 
55  		 * @param controller a {@link Controller}. It cannot be null.
56  		 */
57  		public ReadableControllerPagedSearch(ReadableController<T, K> controller) {
58  
59  			if (controller == null) {
60  				throw new IllegalArgumentException("Parameter controller cannot be null");
61  			}
62  
63  			this.controller = controller;
64  
65  		}
66  
67  		public int count() {
68  			return controller.countAll();
69  		}
70  
71  		public List<T> search(int firstIndex, int maximumResults, SortCriterion... sortingConstraints) {
72  			return controller.findAll(firstIndex, maximumResults, sortingConstraints);
73  		}
74  
75  	}
76  
77  }