| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.student.common.search.dto; |
| 17 | |
|
| 18 | |
import java.io.Serializable; |
| 19 | |
import java.text.SimpleDateFormat; |
| 20 | |
import java.util.ArrayList; |
| 21 | |
import java.util.Collections; |
| 22 | |
import java.util.Comparator; |
| 23 | |
import java.util.Date; |
| 24 | |
import java.util.List; |
| 25 | |
|
| 26 | |
import javax.xml.bind.annotation.XmlAccessType; |
| 27 | |
import javax.xml.bind.annotation.XmlAccessorType; |
| 28 | |
|
| 29 | |
@XmlAccessorType(XmlAccessType.FIELD) |
| 30 | 0 | public class SearchResult implements Serializable { |
| 31 | |
private static final long serialVersionUID = 1L; |
| 32 | |
private Integer startAt; |
| 33 | |
private Integer totalResults; |
| 34 | |
private List<SearchResultRow> rows; |
| 35 | |
private String sortColumn; |
| 36 | |
private SortDirection sortDirection; |
| 37 | |
public Integer getStartAt() { |
| 38 | 0 | return startAt; |
| 39 | |
} |
| 40 | |
public void setStartAt(Integer startAt) { |
| 41 | 0 | this.startAt = startAt; |
| 42 | 0 | } |
| 43 | |
public Integer getTotalResults() { |
| 44 | 0 | return totalResults; |
| 45 | |
} |
| 46 | |
public void setTotalResults(Integer totalResults) { |
| 47 | 0 | this.totalResults = totalResults; |
| 48 | 0 | } |
| 49 | |
public List<SearchResultRow> getRows() { |
| 50 | 0 | if (rows == null) { |
| 51 | 0 | rows = new ArrayList<SearchResultRow>(0); |
| 52 | |
} |
| 53 | 0 | return rows; |
| 54 | |
} |
| 55 | |
public void setRows(List<SearchResultRow> rows) { |
| 56 | 0 | this.rows = rows; |
| 57 | 0 | } |
| 58 | |
public String getSortColumn() { |
| 59 | 0 | return sortColumn; |
| 60 | |
} |
| 61 | |
public void setSortColumn(String sortColumn) { |
| 62 | 0 | this.sortColumn = sortColumn; |
| 63 | 0 | } |
| 64 | |
public SortDirection getSortDirection() { |
| 65 | 0 | return sortDirection; |
| 66 | |
} |
| 67 | |
public void setSortDirection(SortDirection sortDirection) { |
| 68 | 0 | this.sortDirection = sortDirection; |
| 69 | 0 | } |
| 70 | |
|
| 71 | |
public void sortRows() { |
| 72 | 0 | if (sortColumn != null) { |
| 73 | 0 | Collections.sort(getRows(), new SearchResultRowComparator(sortColumn, sortDirection)); |
| 74 | |
} |
| 75 | 0 | } |
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | 0 | private static class SearchResultRowComparator implements Comparator<SearchResultRow> { |
| 82 | |
private String sortColumn; |
| 83 | |
private SortDirection sortDirection; |
| 84 | |
|
| 85 | |
public SearchResultRowComparator(String sortColumn, SortDirection sortDirection) { |
| 86 | 0 | super(); |
| 87 | 0 | this.sortColumn = sortColumn; |
| 88 | 0 | this.sortDirection = sortDirection; |
| 89 | |
|
| 90 | 0 | } |
| 91 | |
|
| 92 | |
@Override |
| 93 | |
public int compare(SearchResultRow r1, SearchResultRow r2) { |
| 94 | 0 | int compareResult = 0; |
| 95 | |
|
| 96 | |
|
| 97 | 0 | String v1 = null; |
| 98 | 0 | String v2 = null; |
| 99 | 0 | for (SearchResultCell c : r1.getCells()) { |
| 100 | 0 | if (sortColumn.equals(c.getKey())) { |
| 101 | 0 | v1 = c.getValue(); |
| 102 | 0 | break; |
| 103 | |
} |
| 104 | |
} |
| 105 | 0 | for (SearchResultCell c : r2.getCells()) { |
| 106 | 0 | if (sortColumn.equals(c.getKey())) { |
| 107 | 0 | v2 = c.getValue(); |
| 108 | 0 | break; |
| 109 | |
} |
| 110 | |
} |
| 111 | |
|
| 112 | |
|
| 113 | |
try { |
| 114 | 0 | Integer v1Integer = Integer.parseInt(v1); |
| 115 | 0 | Integer v2Integer = Integer.parseInt(v2); |
| 116 | 0 | compareResult = v1Integer.compareTo(v2Integer); |
| 117 | 0 | } catch (Exception e1) { |
| 118 | 0 | if (v1 != null && v2 != null && ("true".equals(v1.toLowerCase()) || "false".equals(v1.toLowerCase())) && |
| 119 | |
("true".equals(v2.toLowerCase()) || "false".equals(v2.toLowerCase()))) { |
| 120 | 0 | Boolean v1Boolean = Boolean.parseBoolean(v1); |
| 121 | 0 | Boolean v2Boolean = Boolean.parseBoolean(v2); |
| 122 | 0 | compareResult = v1Boolean.compareTo(v2Boolean); |
| 123 | 0 | } else { |
| 124 | |
try { |
| 125 | 0 | SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); |
| 126 | 0 | Date v1Date = df.parse(v1); |
| 127 | 0 | Date v2Date = df.parse(v2); |
| 128 | 0 | compareResult = v1Date.compareTo(v2Date); |
| 129 | 0 | } catch (Exception e) { |
| 130 | 0 | if (v1 != null && v2 != null) { |
| 131 | 0 | compareResult = v1.compareTo(v2); |
| 132 | 0 | } else if (v2 == null) { |
| 133 | 0 | compareResult = 0; |
| 134 | |
} else { |
| 135 | 0 | compareResult = -1; |
| 136 | |
} |
| 137 | 0 | } |
| 138 | |
} |
| 139 | 0 | } |
| 140 | |
|
| 141 | |
|
| 142 | 0 | if (SortDirection.DESC.equals(sortDirection)) { |
| 143 | 0 | return -1 * compareResult; |
| 144 | |
} |
| 145 | 0 | return compareResult; |
| 146 | |
} |
| 147 | |
} |
| 148 | |
} |