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 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 return startAt;
39 }
40 public void setStartAt(Integer startAt) {
41 this.startAt = startAt;
42 }
43 public Integer getTotalResults() {
44 return totalResults;
45 }
46 public void setTotalResults(Integer totalResults) {
47 this.totalResults = totalResults;
48 }
49 public List<SearchResultRow> getRows() {
50 if (rows == null) {
51 rows = new ArrayList<SearchResultRow>(0);
52 }
53 return rows;
54 }
55 public void setRows(List<SearchResultRow> rows) {
56 this.rows = rows;
57 }
58 public String getSortColumn() {
59 return sortColumn;
60 }
61 public void setSortColumn(String sortColumn) {
62 this.sortColumn = sortColumn;
63 }
64 public SortDirection getSortDirection() {
65 return sortDirection;
66 }
67 public void setSortDirection(SortDirection sortDirection) {
68 this.sortDirection = sortDirection;
69 }
70
71 public void sortRows() {
72 if (sortColumn != null) {
73 Collections.sort(getRows(), new SearchResultRowComparator(sortColumn, sortDirection));
74 }
75 }
76
77
78
79
80
81 private static class SearchResultRowComparator implements Comparator<SearchResultRow> {
82 private String sortColumn;
83 private SortDirection sortDirection;
84
85 public SearchResultRowComparator(String sortColumn, SortDirection sortDirection) {
86 super();
87 this.sortColumn = sortColumn;
88 this.sortDirection = sortDirection;
89
90 }
91
92 @Override
93 public int compare(SearchResultRow r1, SearchResultRow r2) {
94 int compareResult = 0;
95
96
97 String v1 = null;
98 String v2 = null;
99 for (SearchResultCell c : r1.getCells()) {
100 if (sortColumn.equals(c.getKey())) {
101 v1 = c.getValue();
102 break;
103 }
104 }
105 for (SearchResultCell c : r2.getCells()) {
106 if (sortColumn.equals(c.getKey())) {
107 v2 = c.getValue();
108 break;
109 }
110 }
111
112
113 try {
114 Integer v1Integer = Integer.parseInt(v1);
115 Integer v2Integer = Integer.parseInt(v2);
116 compareResult = v1Integer.compareTo(v2Integer);
117 } catch (Exception e1) {
118 if (v1 != null && v2 != null && ("true".equals(v1.toLowerCase()) || "false".equals(v1.toLowerCase())) &&
119 ("true".equals(v2.toLowerCase()) || "false".equals(v2.toLowerCase()))) {
120 Boolean v1Boolean = Boolean.parseBoolean(v1);
121 Boolean v2Boolean = Boolean.parseBoolean(v2);
122 compareResult = v1Boolean.compareTo(v2Boolean);
123 } else {
124 try {
125 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
126 Date v1Date = df.parse(v1);
127 Date v2Date = df.parse(v2);
128 compareResult = v1Date.compareTo(v2Date);
129 } catch (Exception e) {
130 if (v1 != null && v2 != null) {
131 compareResult = v1.compareTo(v2);
132 } else if (v2 == null) {
133 compareResult = 0;
134 } else {
135 compareResult = -1;
136 }
137 }
138 }
139 }
140
141
142 if (SortDirection.DESC.equals(sortDirection)) {
143 return -1 * compareResult;
144 }
145 return compareResult;
146 }
147 }
148 }