| 1 |  |   | 
  | 2 |  |   | 
  | 3 |  |   | 
  | 4 |  |   | 
  | 5 |  |   | 
  | 6 |  |   | 
  | 7 |  |   | 
  | 8 |  |   | 
  | 9 |  |   | 
  | 10 |  |   | 
  | 11 |  |   | 
  | 12 |  |   | 
  | 13 |  |   | 
  | 14 |  |   | 
  | 15 |  |   | 
  | 16 |  |  package org.kuali.student.common.ui.client.widgets.search; | 
  | 17 |  |   | 
  | 18 |  |  import java.util.ArrayList; | 
  | 19 |  |  import java.util.List; | 
  | 20 |  |  import java.util.Set; | 
  | 21 |  |   | 
  | 22 |  |  import org.kuali.student.common.ui.client.application.Application; | 
  | 23 |  |  import org.kuali.student.common.ui.client.application.KSAsyncCallback; | 
  | 24 |  |  import org.kuali.student.common.ui.client.service.CachingSearchService; | 
  | 25 |  |  import org.kuali.student.common.ui.client.service.SearchRpcServiceAsync; | 
  | 26 |  |  import org.kuali.student.common.ui.client.widgets.pagetable.GenericTableModel; | 
  | 27 |  |  import org.kuali.student.common.ui.client.widgets.pagetable.PagingScrollTableBuilder; | 
  | 28 |  |  import org.kuali.student.common.ui.client.widgets.searchtable.ResultRow; | 
  | 29 |  |  import org.kuali.student.common.ui.client.widgets.searchtable.SearchColumnDefinition; | 
  | 30 |  |  import org.kuali.student.core.assembly.data.LookupResultMetadata; | 
  | 31 |  |  import org.kuali.student.core.search.dto.SearchRequest; | 
  | 32 |  |  import org.kuali.student.core.search.dto.SearchResult; | 
  | 33 |  |  import org.kuali.student.core.search.dto.SearchResultCell; | 
  | 34 |  |  import org.kuali.student.core.search.dto.SearchResultRow; | 
  | 35 |  |   | 
  | 36 |  |  import com.google.gwt.core.client.GWT; | 
  | 37 |  |  import com.google.gwt.gen2.table.client.AbstractColumnDefinition; | 
  | 38 |  |  import com.google.gwt.gen2.table.client.PagingOptions; | 
  | 39 |  |  import com.google.gwt.gen2.table.client.PagingScrollTable; | 
  | 40 |  |  import com.google.gwt.gen2.table.client.AbstractScrollTable.ResizePolicy; | 
  | 41 |  |  import com.google.gwt.gen2.table.event.client.RowSelectionHandler; | 
  | 42 |  |  import com.google.gwt.user.client.Window; | 
  | 43 |  |  import com.google.gwt.user.client.ui.Composite; | 
  | 44 |  |  import com.google.gwt.user.client.ui.Label; | 
  | 45 |  |  import com.google.gwt.user.client.ui.VerticalPanel; | 
  | 46 |  |   | 
  | 47 | 0 |  public class TempSearchBackedTable extends Composite{ | 
  | 48 |  |   | 
  | 49 | 0 |      private List<ResultRow> resultRows = new ArrayList<ResultRow>(); | 
  | 50 | 0 |      private List<AbstractColumnDefinition<ResultRow, ?>> columnDefs = new ArrayList<AbstractColumnDefinition<ResultRow, ?>>(); | 
  | 51 | 0 |      private GenericTableModel<ResultRow> tableModel = new GenericTableModel<ResultRow>(resultRows); | 
  | 52 | 0 |      private PagingScrollTableBuilder<ResultRow> builder = new PagingScrollTableBuilder<ResultRow>(); | 
  | 53 |  |      private String resultIdColumnKey; | 
  | 54 |  |      protected PagingScrollTable<ResultRow> pagingScrollTable; | 
  | 55 | 0 |      private VerticalPanel layout = new VerticalPanel(); | 
  | 56 |  |      private PagingOptions pagingOptions; | 
  | 57 |  |   | 
  | 58 | 0 |      private SearchRpcServiceAsync searchRpcServiceAsync = CachingSearchService.getSearchService(); | 
  | 59 |  |   | 
  | 60 |  |      private PagingOptions createPagingOptions(PagingScrollTable<ResultRow> pagingScrollTable) { | 
  | 61 | 0 |          PagingOptions pagingOptions = new PagingOptions(pagingScrollTable); | 
  | 62 | 0 |          pagingOptions.setPixelSize(pagingScrollTable.getOffsetWidth(), pagingOptions.getOffsetHeight()); | 
  | 63 | 0 |          return pagingOptions; | 
  | 64 |  |      } | 
  | 65 |  |   | 
  | 66 |  |      public TempSearchBackedTable(){ | 
  | 67 | 0 |          super(); | 
  | 68 | 0 |          redraw(); | 
  | 69 | 0 |          layout.setWidth("100%"); | 
  | 70 | 0 |          initWidget(layout); | 
  | 71 | 0 |      } | 
  | 72 |  |   | 
  | 73 |  |      public void clearTable(){ | 
  | 74 | 0 |          resultRows.clear(); | 
  | 75 | 0 |          this.redraw(); | 
  | 76 | 0 |      } | 
  | 77 |  |   | 
  | 78 |  |      public void removeSelected(){ | 
  | 79 | 0 |          for(ResultRow r: getSelectedRows()){ | 
  | 80 | 0 |              resultRows.remove(r); | 
  | 81 |  |          } | 
  | 82 | 0 |          this.redraw(); | 
  | 83 | 0 |      } | 
  | 84 |  |   | 
  | 85 |  |      public void performSearch(SearchRequest searchRequest, List<LookupResultMetadata> listResultMetadata, String resultIdKey){ | 
  | 86 |  |   | 
  | 87 | 0 |              initializeTable(listResultMetadata, resultIdKey); | 
  | 88 |  |   | 
  | 89 | 0 |              searchRequest.setNeededTotalResults(false); | 
  | 90 |  |   | 
  | 91 | 0 |          if(pagingScrollTable != null){ | 
  | 92 | 0 |              pagingScrollTable.setEmptyTableWidget(new Label("Processing Search...")); | 
  | 93 |  |          } | 
  | 94 |  |   | 
  | 95 | 0 |                  searchRpcServiceAsync.search(searchRequest, new KSAsyncCallback<SearchResult>(){ | 
  | 96 |  |   | 
  | 97 |  |                      @Override | 
  | 98 |  |                      public void handleFailure(Throwable cause) { | 
  | 99 | 0 |                              GWT.log("Failed to perform search", cause);  | 
  | 100 | 0 |                              Window.alert("Failed to perform search"); | 
  | 101 | 0 |                      } | 
  | 102 |  |   | 
  | 103 |  |                      @Override | 
  | 104 |  |                      public void onSuccess(SearchResult results) { | 
  | 105 |  |   | 
  | 106 | 0 |                              resultRows.clear(); | 
  | 107 | 0 |                          if(results != null){ | 
  | 108 | 0 |                              for (SearchResultRow r: results.getRows()){ | 
  | 109 | 0 |                                  ResultRow theRow = new ResultRow(); | 
  | 110 | 0 |                                  for(SearchResultCell c: r.getCells()){ | 
  | 111 | 0 |                                      if(c.getKey().equals(resultIdColumnKey)){ | 
  | 112 | 0 |                                          theRow.setId(c.getValue()); | 
  | 113 |  |                                      } | 
  | 114 | 0 |                                      theRow.setValue(c.getKey(), c.getValue()); | 
  | 115 |  |                                  } | 
  | 116 | 0 |                                  resultRows.add(theRow); | 
  | 117 | 0 |                              } | 
  | 118 |  |                          } | 
  | 119 | 0 |                          redraw(); | 
  | 120 | 0 |                      } | 
  | 121 |  |                  }); | 
  | 122 | 0 |      } | 
  | 123 |  |   | 
  | 124 |  |      public void initializeTable(List<LookupResultMetadata> listResultMetadata, String resultIdKey) { | 
  | 125 | 0 |              clearTable(); | 
  | 126 |  |   | 
  | 127 | 0 |          this.resultIdColumnKey = resultIdKey; | 
  | 128 | 0 |          builder = new PagingScrollTableBuilder<ResultRow>(); | 
  | 129 | 0 |          builder.tablePixelSize(400, 300); | 
  | 130 |  |   | 
  | 131 | 0 |          columnDefs = new ArrayList<AbstractColumnDefinition<ResultRow, ?>>(); | 
  | 132 | 0 |          for (LookupResultMetadata r: listResultMetadata){ | 
  | 133 | 0 |              String header = Application.getApplicationContext().getUILabel("", null, null, r.getName()); | 
  | 134 | 0 |              String key = r.getKey(); | 
  | 135 | 0 |              if(!r.isHidden()){ | 
  | 136 | 0 |                  columnDefs.add(new SearchColumnDefinition(header, key)); | 
  | 137 |  |              } | 
  | 138 | 0 |          } | 
  | 139 | 0 |          if(columnDefs.size() == 1){ | 
  | 140 |  |               | 
  | 141 | 0 |              columnDefs.get(0).setMinimumColumnWidth(370); | 
  | 142 |  |          } | 
  | 143 | 0 |          builder.columnDefinitions(columnDefs); | 
  | 144 | 0 |          tableModel.setColumnDefs(columnDefs); | 
  | 145 |  |   | 
  | 146 | 0 |          redraw(); | 
  | 147 | 0 |      } | 
  | 148 |  |   | 
  | 149 |  |      public void redraw(){ | 
  | 150 | 0 |          tableModel.setRows(resultRows); | 
  | 151 | 0 |          pagingScrollTable = builder.build(tableModel);  | 
  | 152 | 0 |          pagingScrollTable.setResizePolicy(ResizePolicy.FILL_WIDTH); | 
  | 153 | 0 |          pagingOptions = createPagingOptions(pagingScrollTable); | 
  | 154 | 0 |          layout.clear(); | 
  | 155 | 0 |          layout.add(pagingOptions); | 
  | 156 | 0 |          layout.add(pagingScrollTable); | 
  | 157 | 0 |          pagingScrollTable.reloadPage();                 | 
  | 158 | 0 |          pagingScrollTable.fillWidth(); | 
  | 159 | 0 |      } | 
  | 160 |  |   | 
  | 161 |  |      public void addSelectionHandler(RowSelectionHandler selectionHandler){ | 
  | 162 | 0 |          pagingScrollTable.getDataTable().addRowSelectionHandler(selectionHandler); | 
  | 163 | 0 |      } | 
  | 164 |  |   | 
  | 165 |  |      public List<ResultRow> getSelectedRows(){ | 
  | 166 | 0 |          List<ResultRow> rows = new ArrayList<ResultRow>(); | 
  | 167 | 0 |          Set<Integer> selectedRows = pagingScrollTable.getDataTable().getSelectedRows(); | 
  | 168 | 0 |          for(Integer i: selectedRows){ | 
  | 169 | 0 |              rows.add(pagingScrollTable.getRowValue(i)); | 
  | 170 |  |          } | 
  | 171 | 0 |          return rows; | 
  | 172 |  |      } | 
  | 173 |  |   | 
  | 174 |  |      public List<String> getSelectedIds(){ | 
  | 175 | 0 |          List<String> ids = new ArrayList<String>(); | 
  | 176 | 0 |          Set<Integer> selectedRows = pagingScrollTable.getDataTable().getSelectedRows(); | 
  | 177 | 0 |          for(Integer i: selectedRows){ | 
  | 178 | 0 |              ids.add(pagingScrollTable.getRowValue(i).getId()); | 
  | 179 |  |          } | 
  | 180 | 0 |          return ids; | 
  | 181 |  |      } | 
  | 182 |  |   | 
  | 183 |  |      public List<String> getAllIds(){ | 
  | 184 | 0 |          List<String> ids = new ArrayList<String>(); | 
  | 185 | 0 |          for(ResultRow r: resultRows){ | 
  | 186 | 0 |              ids.add(r.getId()); | 
  | 187 |  |          } | 
  | 188 | 0 |          return ids; | 
  | 189 |  |      } | 
  | 190 |  |   | 
  | 191 |  |      public List<ResultRow> getAllRows(){ | 
  | 192 | 0 |          List<ResultRow> rows = new ArrayList<ResultRow>(); | 
  | 193 | 0 |          for(ResultRow r: resultRows){ | 
  | 194 | 0 |              rows.add(r); | 
  | 195 |  |          } | 
  | 196 | 0 |          return rows; | 
  | 197 |  |      } | 
  | 198 |  |   | 
  | 199 |  |  } |