| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.student.lum.common.client.lo; |
| 17 | |
|
| 18 | |
import java.util.ArrayList; |
| 19 | |
import java.util.HashSet; |
| 20 | |
import java.util.List; |
| 21 | |
|
| 22 | |
import org.kuali.student.common.ui.client.application.KSAsyncCallback; |
| 23 | |
import org.kuali.student.common.ui.client.mvc.Callback; |
| 24 | |
import org.kuali.student.common.ui.client.service.ServerPropertiesRpcService; |
| 25 | |
import org.kuali.student.common.ui.client.service.ServerPropertiesRpcServiceAsync; |
| 26 | |
import org.kuali.student.common.ui.client.widgets.searchtable.ResultRow; |
| 27 | |
import org.kuali.student.common.ui.client.widgets.table.scroll.Column; |
| 28 | |
import org.kuali.student.common.ui.client.widgets.table.scroll.DefaultTableModel; |
| 29 | |
import org.kuali.student.common.ui.client.widgets.table.scroll.Row; |
| 30 | |
import org.kuali.student.common.ui.client.widgets.table.scroll.Table; |
| 31 | |
import org.kuali.student.lum.common.client.lo.rpc.LoCategoryRpcService; |
| 32 | |
import org.kuali.student.lum.common.client.lo.rpc.LoCategoryRpcServiceAsync; |
| 33 | |
import org.kuali.student.lum.lo.dto.LoCategoryInfo; |
| 34 | |
|
| 35 | |
import com.google.gwt.core.client.GWT; |
| 36 | |
import com.google.gwt.user.client.Window; |
| 37 | |
import com.google.gwt.user.client.ui.Composite; |
| 38 | |
import com.google.gwt.user.client.ui.FlowPanel; |
| 39 | |
import com.google.gwt.user.client.ui.VerticalPanel; |
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | 0 | public class CategoryManagementTable extends Composite { |
| 48 | 0 | static String NAME_COLUMN_HEADER = "Category"; |
| 49 | 0 | static String TYPE_COLUMN_HEADER = "Type"; |
| 50 | 0 | static String STATE_COLUMN_HEADER = "State"; |
| 51 | 0 | static String ID_COLUMN_KEY = "id"; |
| 52 | 0 | static String NAME_COLUMN_KEY = "name"; |
| 53 | 0 | static String TYPE_COLUMN_KEY = "type"; |
| 54 | 0 | static String STATE_COLUMN_KEY = "state"; |
| 55 | 0 | private List<ResultRow> resultRows = new ArrayList<ResultRow>(); |
| 56 | 0 | private DefaultTableModel model = new DefaultTableModel(); |
| 57 | 0 | private Table table = new Table(); |
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | 0 | private FlowPanel layout = new FlowPanel(); |
| 62 | |
private static Boolean displayOnlyActiveCategories; |
| 63 | 0 | private boolean hideInactiveCategories = false; |
| 64 | |
|
| 65 | 0 | private LoCategoryRpcServiceAsync loCatRpcServiceAsync = GWT.create(LoCategoryRpcService.class); |
| 66 | 0 | private static ServerPropertiesRpcServiceAsync serverProperties = GWT.create(ServerPropertiesRpcService.class); |
| 67 | |
|
| 68 | |
class CategoryRow extends Row{ |
| 69 | |
ResultRow row; |
| 70 | |
|
| 71 | 0 | public CategoryRow(ResultRow row){ |
| 72 | 0 | this.row = row; |
| 73 | 0 | } |
| 74 | |
@Override |
| 75 | |
public Object getCellData(String columnId) { |
| 76 | 0 | return row.getValue(columnId); |
| 77 | |
} |
| 78 | |
@Override |
| 79 | |
public void setCellData(String columnId, Object newValue) { |
| 80 | 0 | row.setValue(columnId, newValue.toString()); |
| 81 | 0 | } |
| 82 | |
|
| 83 | |
public ResultRow getResultRowData(){ |
| 84 | 0 | return row; |
| 85 | |
} |
| 86 | |
} |
| 87 | |
|
| 88 | |
public Table getTable(){ |
| 89 | 0 | return table; |
| 90 | |
} |
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
public static void setDisplayOnlyActiveCategories() { |
| 99 | 0 | if (null == displayOnlyActiveCategories) { |
| 100 | 0 | serverProperties.get("ks.lum.ui.displayOnlyActiveLoCategories", new KSAsyncCallback<String>() { |
| 101 | |
@Override |
| 102 | |
public void handleFailure(Throwable caught) { |
| 103 | 0 | GWT.log("get displayOnlyActiveLoCategories failed", caught); |
| 104 | 0 | Window.alert("Failed to get displayOnlyActiveLoCategories setting"); |
| 105 | 0 | } |
| 106 | |
|
| 107 | |
@Override |
| 108 | |
public void onSuccess(String result) { |
| 109 | 0 | if (result != null) { |
| 110 | 0 | displayOnlyActiveCategories = Boolean.parseBoolean(result); |
| 111 | |
} |
| 112 | 0 | } |
| 113 | |
}); |
| 114 | |
} |
| 115 | 0 | } |
| 116 | |
|
| 117 | |
private void initCategoryManagementTable(boolean isMultiSelect){ |
| 118 | 0 | layout.setWidth("100%"); |
| 119 | 0 | table.setWidth("550px"); |
| 120 | 0 | table.getScrollPanel().setHeight("400px"); |
| 121 | 0 | initWidget(layout); |
| 122 | 0 | createColumnDefs(); |
| 123 | 0 | if(isMultiSelect){ |
| 124 | 0 | model.setMultipleSelectable(true); |
| 125 | 0 | model.installCheckBoxRowHeaderColumn(); |
| 126 | |
} |
| 127 | |
else{ |
| 128 | 0 | model.setMultipleSelectable(false); |
| 129 | |
} |
| 130 | 0 | table.setTableModel(model); |
| 131 | |
|
| 132 | 0 | layout.add(table); |
| 133 | 0 | } |
| 134 | |
public CategoryManagementTable() { |
| 135 | 0 | super(); |
| 136 | 0 | initCategoryManagementTable(false); |
| 137 | 0 | } |
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
public CategoryManagementTable(boolean hideInactiveCategories, boolean isMultiSelect) { |
| 144 | 0 | super(); |
| 145 | 0 | this.hideInactiveCategories = hideInactiveCategories; |
| 146 | 0 | initCategoryManagementTable(isMultiSelect); |
| 147 | 0 | } |
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
public boolean isHideInactiveCategories() { |
| 156 | 0 | if(hideInactiveCategories){ |
| 157 | 0 | return true; |
| 158 | |
} |
| 159 | 0 | if((displayOnlyActiveCategories == null)||( displayOnlyActiveCategories.booleanValue() == false)){ |
| 160 | 0 | return false; |
| 161 | |
}else { |
| 162 | 0 | return true; |
| 163 | |
} |
| 164 | |
} |
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
public void setHideInactiveCategories(boolean show) { |
| 169 | 0 | this.hideInactiveCategories = show; |
| 170 | 0 | } |
| 171 | |
public void redraw(){ |
| 172 | 0 | model.clearRows(); |
| 173 | 0 | for(ResultRow row: resultRows){ |
| 174 | 0 | model.addRow(new CategoryRow(row)); |
| 175 | |
} |
| 176 | 0 | model.fireTableDataChanged(); |
| 177 | 0 | } |
| 178 | |
|
| 179 | |
public void redraw(List<ResultRow> filteredRows){ |
| 180 | 0 | model.clearRows(); |
| 181 | 0 | table.removeAllRows(); |
| 182 | 0 | for(ResultRow row: filteredRows){ |
| 183 | 0 | model.addRow(new CategoryRow(row)); |
| 184 | |
} |
| 185 | 0 | model.setCurrentIndex(0); |
| 186 | 0 | model.fireTableDataChanged(); |
| 187 | 0 | } |
| 188 | |
|
| 189 | |
public void clearTable(){ |
| 190 | 0 | resultRows.clear(); |
| 191 | 0 | redraw(); |
| 192 | 0 | } |
| 193 | |
|
| 194 | |
public void removeSelected(){ |
| 195 | 0 | for(ResultRow r: getSelectedRows()){ |
| 196 | 0 | resultRows.remove(r); |
| 197 | |
} |
| 198 | 0 | this.redraw(); |
| 199 | 0 | } |
| 200 | |
public List<ResultRow> getAllRows(){ |
| 201 | 0 | List<ResultRow> rows = new ArrayList<ResultRow>(); |
| 202 | 0 | for(ResultRow r: resultRows){ |
| 203 | 0 | rows.add(r); |
| 204 | |
} |
| 205 | 0 | return rows; |
| 206 | |
} |
| 207 | |
public List<ResultRow> getSelectedRows(){ |
| 208 | 0 | List<ResultRow> rows = new ArrayList<ResultRow>(); |
| 209 | 0 | List<Row> selectedRows = model.getSelectedRows(); |
| 210 | 0 | for(Row r: selectedRows){ |
| 211 | 0 | rows.add(((CategoryRow)r).getResultRowData()); |
| 212 | |
} |
| 213 | 0 | return rows; |
| 214 | |
} |
| 215 | |
public List<LoCategoryInfo> getSelectedLoCategoryInfos(){ |
| 216 | 0 | List<LoCategoryInfo> loCategoryInfos = new ArrayList<LoCategoryInfo>(); |
| 217 | 0 | List<Row> selectedRows = model.getSelectedRows(); |
| 218 | 0 | if(selectedRows.isEmpty()) { |
| 219 | 0 | return loCategoryInfos; |
| 220 | |
} |
| 221 | 0 | for(Row r: selectedRows){ |
| 222 | 0 | LoCategoryInfo loCategoryInfo = new LoCategoryInfo(); |
| 223 | 0 | loCategoryInfo.setId(r.getCellData(ID_COLUMN_KEY).toString()); |
| 224 | 0 | loCategoryInfo.setName(r.getCellData(NAME_COLUMN_KEY).toString()); |
| 225 | 0 | loCategoryInfo.setType(r.getCellData(TYPE_COLUMN_KEY).toString()); |
| 226 | 0 | loCategoryInfo.setState(r.getCellData(STATE_COLUMN_KEY).toString()); |
| 227 | 0 | loCategoryInfos.add(loCategoryInfo); |
| 228 | 0 | } |
| 229 | 0 | return loCategoryInfos; |
| 230 | |
} |
| 231 | |
|
| 232 | |
public String getSelectedLoCategoryInfoId(){ |
| 233 | 0 | List<Row> selectedRows = model.getSelectedRows(); |
| 234 | 0 | if(selectedRows.isEmpty()) { |
| 235 | 0 | return null; |
| 236 | |
} |
| 237 | 0 | String id = null; |
| 238 | 0 | for(Row r: selectedRows){ |
| 239 | 0 | id = r.getCellData(ID_COLUMN_KEY).toString(); |
| 240 | 0 | break; |
| 241 | |
} |
| 242 | 0 | return id; |
| 243 | |
|
| 244 | |
} |
| 245 | |
|
| 246 | |
private void createColumnDefs() { |
| 247 | |
|
| 248 | 0 | Column name = new Column(); |
| 249 | 0 | name.setName(NAME_COLUMN_HEADER); |
| 250 | 0 | name.setId(NAME_COLUMN_KEY); |
| 251 | 0 | name.setSortable(false); |
| 252 | 0 | model.addColumn(name); |
| 253 | 0 | name.setWidth("250px"); |
| 254 | |
|
| 255 | 0 | Column type = new Column(); |
| 256 | 0 | type.setName(TYPE_COLUMN_HEADER); |
| 257 | 0 | type.setId(TYPE_COLUMN_KEY); |
| 258 | 0 | type.setSortable(false); |
| 259 | 0 | model.addColumn(type); |
| 260 | |
|
| 261 | 0 | if (!isHideInactiveCategories()) { |
| 262 | 0 | Column state = new Column(); |
| 263 | 0 | state.setName(TYPE_COLUMN_HEADER); |
| 264 | 0 | state.setId(TYPE_COLUMN_KEY); |
| 265 | 0 | state.setSortable(false); |
| 266 | 0 | model.addColumn(state); |
| 267 | |
} |
| 268 | 0 | } |
| 269 | |
|
| 270 | |
|
| 271 | |
|
| 272 | |
private List<LoCategoryInfo> filterResults(List<LoCategoryInfo> result) { |
| 273 | |
|
| 274 | 0 | if(isHideInactiveCategories()) { |
| 275 | 0 | List<LoCategoryInfo> filteredResult = new ArrayList<LoCategoryInfo>(); |
| 276 | 0 | for(LoCategoryInfo info : result) { |
| 277 | 0 | if (info.getState().equals("active") ) { |
| 278 | 0 | filteredResult.add(info); |
| 279 | |
} |
| 280 | |
} |
| 281 | 0 | return filteredResult; |
| 282 | |
} |
| 283 | 0 | return result; |
| 284 | |
} |
| 285 | |
|
| 286 | |
public void loadTable(final Callback<Boolean> callback) { |
| 287 | 0 | table.displayLoading(true); |
| 288 | 0 | loCatRpcServiceAsync.getLoCategories("kuali.loRepository.key.singleUse", new KSAsyncCallback<List<LoCategoryInfo>>() { |
| 289 | |
@Override |
| 290 | |
public void handleFailure(Throwable caught) { |
| 291 | 0 | GWT.log("getLoCategories failed", caught); |
| 292 | 0 | Window.alert("Get LoCategories failed"); |
| 293 | 0 | callback.exec(false); |
| 294 | 0 | } |
| 295 | |
|
| 296 | |
@Override |
| 297 | |
public void onSuccess(List<LoCategoryInfo> results) { |
| 298 | |
|
| 299 | 0 | List<LoCategoryInfo> filteredResults = filterResults(results); |
| 300 | 0 | loadTable(filteredResults); |
| 301 | 0 | callback.exec(true); |
| 302 | 0 | table.displayLoading(false); |
| 303 | 0 | } |
| 304 | |
}); |
| 305 | 0 | } |
| 306 | |
|
| 307 | |
private void loadTable(List<LoCategoryInfo> loCategoryInfos) { |
| 308 | 0 | resultRows.clear(); |
| 309 | 0 | HashSet<String> hashSet = new HashSet<String>(); |
| 310 | 0 | String id = null; |
| 311 | 0 | for(LoCategoryInfo info : loCategoryInfos) { |
| 312 | 0 | ResultRow resultRow = new ResultRow(); |
| 313 | 0 | id = info.getId(); |
| 314 | 0 | if(!hashSet.contains(id)) { |
| 315 | 0 | hashSet.add(id); |
| 316 | 0 | resultRow.setValue(ID_COLUMN_KEY, id); |
| 317 | 0 | resultRow.setValue(NAME_COLUMN_KEY, info.getName()); |
| 318 | 0 | resultRow.setValue(TYPE_COLUMN_KEY, info.getType()); |
| 319 | 0 | resultRow.setValue(STATE_COLUMN_KEY, info.getState()); |
| 320 | 0 | resultRows.add(resultRow); |
| 321 | |
} |
| 322 | 0 | } |
| 323 | 0 | redraw(); |
| 324 | 0 | } |
| 325 | |
|
| 326 | |
public List<ResultRow> getRowsByType(String type){ |
| 327 | 0 | List<ResultRow> bufferList = new ArrayList<ResultRow>(); |
| 328 | 0 | for(ResultRow row : resultRows) { |
| 329 | 0 | if(row.getValue(TYPE_COLUMN_KEY).contains(type)){ |
| 330 | 0 | bufferList.add(row); |
| 331 | |
} |
| 332 | |
} |
| 333 | 0 | return bufferList; |
| 334 | |
} |
| 335 | |
|
| 336 | |
public List<ResultRow> getRowsLikeName(String name){ |
| 337 | 0 | List<ResultRow> bufferList = new ArrayList<ResultRow>(); |
| 338 | 0 | for(ResultRow row : resultRows) { |
| 339 | 0 | String nameValue = row.getValue(NAME_COLUMN_KEY); |
| 340 | 0 | if(nameValue != null) { |
| 341 | 0 | String[] words = nameValue.split("\\W"); |
| 342 | 0 | for(String word : words){ |
| 343 | 0 | if(word.toUpperCase().startsWith(name.toUpperCase())){ |
| 344 | 0 | bufferList.add(row); |
| 345 | 0 | break; |
| 346 | |
} |
| 347 | |
} |
| 348 | |
} |
| 349 | 0 | } |
| 350 | 0 | return bufferList; |
| 351 | |
} |
| 352 | |
} |