001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/sam/trunk/component/src/java/org/sakaiproject/tool/assessment/util/BeanDateComparator.java $
003: * $Id: BeanDateComparator.java 9273 2006-05-10 22:34:28Z daisyf@stanford.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the"License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.tool.assessment.util;
021:
022: import java.util.*;
023: import java.text.*;
024:
025: /**
026: * DOCUMENTATION PENDING
027: *
028: * @author $author$
029: * @version $Id: BeanDateComparator.java 9273 2006-05-10 22:34:28Z daisyf@stanford.edu $
030: */
031: public class BeanDateComparator extends BeanSortComparator {
032: private String propertyName;
033:
034: /**
035: * The only public constructor. Requires a valid property name for a a Java
036: * Bean as a sole parameter.
037: *
038: * @param propertyName the property name for Java Bean to sort by
039: */
040: public BeanDateComparator(String propertyName) {
041: this .propertyName = propertyName;
042: }
043:
044: /**
045: * Creates a new BeanDateComparator object.
046: */
047: protected BeanDateComparator() {
048: };
049:
050: /**
051: * standard compare method
052: *
053: * @param o1 object
054: * @param o2 object
055: *
056: * @return lt, eq, gt zero depending on whether o1 numerically lt,eq,gt o2
057: *
058: * @throws java.lang.UnsupportedOperationException DOCUMENTATION PENDING
059: */
060: public int compare(Object o1, Object o2) {
061: Map m1 = describeBean(o1);
062: Map m2 = describeBean(o2);
063: String s1 = (String) m1.get(propertyName);
064: String s2 = (String) m2.get(propertyName);
065: // we do not want to use null values for sorting
066: if (s1 == null)
067: s1 = "";
068: if (s2 == null)
069: s2 = "";
070:
071: DateFormat dateFormat = new SimpleDateFormat(
072: "yyyy-MM-dd hh:mm:ss.SSS");
073: Date i1 = null;
074: Date i2 = null;
075:
076: boolean firstDateValid = true;
077: boolean secondDateValid = true;
078:
079: try {
080: i1 = dateFormat.parse(s1);
081: } catch (ParseException e) {
082: firstDateValid = false;
083: }
084:
085: try {
086: i2 = dateFormat.parse(s2);
087: } catch (ParseException e) {
088: secondDateValid = false;
089: }
090:
091: int returnValue = 0;
092: if (firstDateValid && secondDateValid) {
093: if (i1 != null) {
094: returnValue = i1.compareTo(i2);
095: }
096: }
097: if (firstDateValid && !secondDateValid)
098: returnValue = 1;
099: if (!firstDateValid && secondDateValid)
100: returnValue = -1;
101: if (!firstDateValid && !secondDateValid)
102: returnValue = 0;
103:
104: return returnValue;
105: }
106: }
|