Coverage Report - org.kuali.student.common.search.dto.SearchResult
 
Classes in this File Line Coverage Branch Coverage Complexity
SearchResult
0%
0/22
0%
0/4
2.462
SearchResult$SearchResultRowComparator
0%
0/40
0%
0/28
2.462
 
 1  
 /**
 2  
  * Copyright 2010 The Kuali Foundation Licensed under the
 3  
  * Educational Community License, Version 2.0 (the "License"); you may
 4  
  * not use this file except in compliance with the License. You may
 5  
  * obtain a copy of the License at
 6  
  *
 7  
  * http://www.osedu.org/licenses/ECL-2.0
 8  
  *
 9  
  * Unless required by applicable law or agreed to in writing,
 10  
  * software distributed under the License is distributed on an "AS IS"
 11  
  * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 12  
  * or implied. See the License for the specific language governing
 13  
  * permissions and limitations under the License.
 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  
      * Compares two SearchResultRow rows with a given sort direction and column
 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  
             //Pares out the cell values to compare
 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  
             //Compare the values wiuth the right type (SHould be done more efficiently
 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  
             //Sort reverse if order is descending
 142  0
             if (SortDirection.DESC.equals(sortDirection)) {
 143  0
                 return -1 * compareResult;
 144  
             }
 145  0
             return compareResult;
 146  
         }
 147  
     }
 148  
 }