View Javadoc

1   /**
2    * Copyright 2010 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  
16  package org.kuali.student.lum.lu.ui.tools.client.widgets;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  import java.util.Set;
21  
22  import org.kuali.student.common.assembly.data.LookupResultMetadata;
23  import org.kuali.student.common.search.dto.SearchRequest;
24  import org.kuali.student.common.search.dto.SearchResult;
25  import org.kuali.student.common.search.dto.SearchResultCell;
26  import org.kuali.student.common.search.dto.SearchResultRow;
27  import org.kuali.student.common.ui.client.application.KSAsyncCallback;
28  import org.kuali.student.common.ui.client.service.CachingSearchService;
29  import org.kuali.student.common.ui.client.widgets.pagetable.GenericTableModel;
30  import org.kuali.student.common.ui.client.widgets.pagetable.PagingScrollTableBuilder;
31  import org.kuali.student.common.ui.client.widgets.searchtable.ResultRow;
32  import org.kuali.student.common.ui.client.widgets.searchtable.SearchColumnDefinition;
33  
34  import com.google.gwt.core.client.GWT;
35  import com.google.gwt.gen2.table.client.AbstractColumnDefinition;
36  import com.google.gwt.gen2.table.client.PagingScrollTable;
37  import com.google.gwt.gen2.table.client.AbstractScrollTable.ResizePolicy;
38  import com.google.gwt.gen2.table.event.client.RowSelectionHandler;
39  import com.google.gwt.user.client.ui.Composite;
40  import com.google.gwt.user.client.ui.Label;
41  import com.google.gwt.user.client.ui.VerticalPanel;
42  
43  public class SearchBackedTable extends Composite
44  {
45  
46  	private List<ResultRow> resultRows = new ArrayList<ResultRow> ();
47  	private List<AbstractColumnDefinition<ResultRow, ?>> columnDefs = new ArrayList<AbstractColumnDefinition<ResultRow, ?>> ();
48  	private GenericTableModel<ResultRow> tableModel = new GenericTableModel<ResultRow> (resultRows);
49  	private PagingScrollTableBuilder<ResultRow> builder = new PagingScrollTableBuilder<ResultRow> ();
50  	private String resultIdColumnKey;
51  	protected PagingScrollTable<ResultRow> pagingScrollTable;
52  	private VerticalPanel layout = new VerticalPanel ();
53  
54  	private CachingSearchService searchRpcServiceAsync = CachingSearchService.getSearchService();
55  
56  	public SearchBackedTable ()
57  	{
58  		super ();
59  		redraw ();
60  		layout.setWidth ("100%");
61  		initWidget (layout);
62  	}
63  
64  	public void clearTable ()
65  	{
66  		resultRows.clear ();
67  		this.redraw ();
68  	}
69  
70  	public void removeSelected ()
71  	{
72  		for (ResultRow r : getSelectedRows ())
73  		{
74  			resultRows.remove (r);
75  		}
76  		this.redraw ();
77  	}
78  
79  	public void performSearch (SearchRequest searchRequest,
80  			List<LookupResultMetadata> listResultMetadata,
81  			String resultIdKey)
82  	{
83  
84  		initializeTable (listResultMetadata, resultIdKey);
85  
86  		searchRequest.setNeededTotalResults (false);
87  
88  		if (pagingScrollTable != null)
89  		{
90  			pagingScrollTable.setEmptyTableWidget (new Label ("Processing Search..."));
91  		}
92  
93  		//  Window.alert ("About to invoke asynch search...");
94  		searchRpcServiceAsync.search (searchRequest, new KSAsyncCallback<SearchResult> (){
95  
96  			@Override
97  			public void onSuccess (SearchResult searchResults)
98  			{
99  				//    Window.alert ("Got back search results...");
100 				resultRows.clear ();
101 				if (searchResults != null)
102 				{
103 					for (SearchResultRow searchResultRow : searchResults.getRows ())
104 					{
105 						//      Window.alert ("adding row");
106 						ResultRow resultRow = new ResultRow ();
107 						for (SearchResultCell searchResultCell : searchResultRow.getCells ())
108 						{
109 							if (searchResultCell.getKey ().equals (resultIdColumnKey))
110 							{
111 								resultRow.setId (searchResultCell.getValue ());
112 							}
113 							resultRow.setValue (searchResultCell.getKey (), searchResultCell.getValue ());
114 						}
115 						resultRows.add (resultRow);
116 					}
117 				}
118 				//    Window.alert ("about to redraw...");
119 				redraw ();
120 			}
121 
122 				});
123 	}
124 
125 	private void initializeTable (List<LookupResultMetadata> listResultMetadata,
126 			String resultIdKey)
127 	{
128 		clearTable ();
129 
130 		this.resultIdColumnKey = resultIdKey;
131 		builder = new PagingScrollTableBuilder<ResultRow> ();
132 		builder.tablePixelSize (900, 200); // width, height
133 
134 		columnDefs = new ArrayList<AbstractColumnDefinition<ResultRow, ?>> ();
135 		for (LookupResultMetadata r : listResultMetadata)
136 		{
137 			//TODO: use this as a token to get a message from message service instead
138 			String header = r.getName ();
139 			String key = r.getKey ();
140 			if ( ! r.isHidden ())
141 			{
142 				columnDefs.add (new SearchColumnDefinition (header, key));
143 			}
144 		}
145 		if (columnDefs.size () == 1)
146 		{
147 			columnDefs.get (0).setMinimumColumnWidth (370);
148 		}
149 		builder.columnDefinitions (columnDefs);
150 		tableModel.setColumnDefs (columnDefs);
151 
152 		redraw ();
153 	}
154 
155 	public void redraw ()
156 	{
157 		tableModel.setRows (resultRows);
158 		pagingScrollTable = builder.build (tableModel); 
159 		pagingScrollTable.setResizePolicy (ResizePolicy.FILL_WIDTH);
160 		layout.clear ();
161 		layout.add (pagingScrollTable);
162 		pagingScrollTable.fillWidth ();
163 		pagingScrollTable.reloadPage();
164 	}
165 
166 	public void addSelectionHandler (RowSelectionHandler selectionHandler)
167 	{
168 		pagingScrollTable.getDataTable ().addRowSelectionHandler (selectionHandler);
169 	}
170 
171 	public List<ResultRow> getSelectedRows ()
172 	{
173 		List<ResultRow> rows = new ArrayList<ResultRow> ();
174 		Set<Integer> selectedRows =
175 			pagingScrollTable.getDataTable ().getSelectedRows ();
176 		for (Integer i : selectedRows)
177 		{
178 			rows.add (pagingScrollTable.getRowValue (i));
179 		}
180 		return rows;
181 	}
182 
183 	public List<String> getSelectedIds ()
184 	{
185 		List<String> ids = new ArrayList<String> ();
186 		Set<Integer> selectedRows =
187 			pagingScrollTable.getDataTable ().getSelectedRows ();
188 		for (Integer i : selectedRows)
189 		{
190 			ids.add (pagingScrollTable.getRowValue (i).getId ());
191 		}
192 		return ids;
193 	}
194 
195 	public List<String> getAllIds ()
196 	{
197 		List<String> ids = new ArrayList<String> ();
198 		for (ResultRow r : resultRows)
199 		{
200 			ids.add (r.getId ());
201 		}
202 		return ids;
203 	}
204 
205 	public List<ResultRow> getAllRows ()
206 	{
207 		List<ResultRow> rows = new ArrayList<ResultRow> ();
208 		for (ResultRow r : resultRows)
209 		{
210 			rows.add (r);
211 		}
212 		return rows;
213 	}
214 
215 }