001    /**
002     * Copyright 2010 The Kuali Foundation Licensed under the
003     * Educational Community License, Version 2.0 (the "License"); you may
004     * not use this file except in compliance with the License. You may
005     * obtain a copy of the License at
006     *
007     * http://www.osedu.org/licenses/ECL-2.0
008     *
009     * Unless required by applicable law or agreed to in writing,
010     * software distributed under the License is distributed on an "AS IS"
011     * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
012     * or implied. See the License for the specific language governing
013     * permissions and limitations under the License.
014     */
015    
016    package org.kuali.student.common.search.dto;
017    
018    import java.io.Serializable;
019    import java.text.SimpleDateFormat;
020    import java.util.ArrayList;
021    import java.util.Collections;
022    import java.util.Comparator;
023    import java.util.Date;
024    import java.util.List;
025    
026    import javax.xml.bind.annotation.XmlAccessType;
027    import javax.xml.bind.annotation.XmlAccessorType;
028    
029    @XmlAccessorType(XmlAccessType.FIELD)
030    public class SearchResult implements Serializable {
031            private static final long serialVersionUID = 1L;
032            private Integer startAt;
033            private Integer totalResults;
034            private List<SearchResultRow> rows;
035            private String sortColumn;
036            private SortDirection sortDirection;
037            public Integer getStartAt() {
038                    return startAt;
039            }
040            public void setStartAt(Integer startAt) {
041                    this.startAt = startAt;
042            }
043            public Integer getTotalResults() {
044                    return totalResults;
045            }
046            public void setTotalResults(Integer totalResults) {
047                    this.totalResults = totalResults;
048            }
049            public List<SearchResultRow> getRows() {
050                    if (rows == null) {
051                            rows = new ArrayList<SearchResultRow>(0);
052                    }
053                    return rows;
054            }
055            public void setRows(List<SearchResultRow> rows) {
056                    this.rows = rows;
057            }
058            public String getSortColumn() {
059                    return sortColumn;
060            }
061            public void setSortColumn(String sortColumn) {
062                    this.sortColumn = sortColumn;
063            }
064            public SortDirection getSortDirection() {
065                    return sortDirection;
066            }
067            public void setSortDirection(SortDirection sortDirection) {
068                    this.sortDirection = sortDirection;
069            }
070            
071        public void sortRows() {
072            if (sortColumn != null) {
073                Collections.sort(getRows(), new SearchResultRowComparator(sortColumn, sortDirection));
074            }
075        }
076            
077            /**
078         * Compares two SearchResultRow rows with a given sort direction and column
079         *
080         */
081        private static class SearchResultRowComparator implements Comparator<SearchResultRow> {
082            private String sortColumn;
083            private SortDirection sortDirection;
084    
085            public SearchResultRowComparator(String sortColumn, SortDirection sortDirection) {
086                super();
087                this.sortColumn = sortColumn;
088                this.sortDirection = sortDirection;
089    
090            }
091    
092            @Override
093            public int compare(SearchResultRow r1, SearchResultRow r2) {
094                int compareResult = 0;
095    
096                //Pares out the cell values to compare
097                String v1 = null;
098                String v2 = null;
099                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                //Compare the values wiuth the right type (SHould be done more efficiently
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                //Sort reverse if order is descending
142                if (SortDirection.DESC.equals(sortDirection)) {
143                    return -1 * compareResult;
144                }
145                return compareResult;
146            }
147        }
148    }