001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/content/tags/sakai_2-4-1/content-impl/impl/src/java/org/sakaiproject/content/impl/ContentHostingComparator.java $
003: * $Id: ContentHostingComparator.java 29157 2007-04-19 01:38:26Z ajpoland@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004, 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.content.impl;
021:
022: import java.util.Comparator;
023:
024: import org.sakaiproject.content.api.ContentCollection;
025: import org.sakaiproject.content.api.ContentEntity;
026: import org.sakaiproject.content.api.ContentHostingService;
027: import org.sakaiproject.content.api.ContentResource;
028: import org.sakaiproject.entity.api.Entity;
029: import org.sakaiproject.entity.api.ResourceProperties;
030: import org.sakaiproject.time.api.Time;
031:
032: /**
033: * <p>
034: * ContentHostingComparator can be used to sort stuff (collections, resources) from the content hosting service.
035: * </p>
036: */
037: public class ContentHostingComparator implements Comparator {
038: /** The property name used for the sort. */
039: String m_property = null;
040:
041: /** true if the sort is to be ascending (false for descending). */
042: boolean m_ascending = true;
043:
044: /**
045: * Construct.
046: *
047: * @param property
048: * The property name used for the sort.
049: * @param asc
050: * true if the sort is to be ascending (false for descending).
051: */
052: public ContentHostingComparator(String property, boolean ascending) {
053: m_property = property;
054: m_ascending = ascending;
055:
056: } // ContentHostingComparator
057:
058: /**
059: * Compare these objects based on my property and ascending settings. Collections sort lower than Resources.
060: *
061: * @param o1
062: * The first object, ContentCollection or ContentResource
063: * @param o2
064: * The second object, ContentCollection or ContentResource
065: * @return The compare result: -1 if o1 < o2, 0 if they are equal, and 1 if o1 > o2
066: */
067: public int compare(Object o1, Object o2) {
068: String property = m_property;
069:
070: // PROP_CONTENT_PRIORITY is special because it allows
071: // intermixing folders and files.
072: if (property.equals(ResourceProperties.PROP_CONTENT_PRIORITY)) {
073: Entity entity1 = (Entity) o1;
074: Entity entity2 = (Entity) o2;
075: String str1 = entity1.getProperties().getProperty(property);
076: String str2 = entity2.getProperties().getProperty(property);
077:
078: if (str1 == null || str2 == null) {
079: // ignore -- default to a different sort
080: } else {
081: try {
082: Integer rank1 = new Integer(str1);
083: Integer rank2 = new Integer(str2);
084: return m_ascending ? rank1.compareTo(rank2) : rank2
085: .compareTo(rank1);
086: } catch (NumberFormatException e) {
087: // ignore -- default to a different sort
088: }
089: }
090: // if unable to do a priority sort, sort by title
091: property = ResourceProperties.PROP_DISPLAY_NAME;
092: }
093:
094: // collections sort lower than resources
095: if ((o1 instanceof ContentCollection)
096: && (o2 instanceof ContentResource)) {
097: return (m_ascending ? -1 : 1);
098: }
099: if ((o1 instanceof ContentResource)
100: && (o2 instanceof ContentCollection)) {
101: return (m_ascending ? 1 : -1);
102: }
103:
104: if (property.equals(ResourceProperties.PROP_CONTENT_LENGTH)
105: && o1 instanceof ContentCollection) {
106: int size1 = ((ContentCollection) o1).getMemberCount();
107: int size2 = ((ContentCollection) o2).getMemberCount();
108: int rv = ((size1 < size2) ? -1 : ((size1 > size2) ? 1 : 0));
109: if (!m_ascending)
110: rv = -rv;
111: return rv;
112: }
113:
114: // ok, they are both the same: resources or collections
115:
116: // try a numeric interpretation
117: try {
118: long l1 = ((Entity) o1).getProperties().getLongProperty(
119: property);
120: long l2 = ((Entity) o2).getProperties().getLongProperty(
121: property);
122: int rv = ((l1 < l2) ? -1 : ((l1 > l2) ? 1 : 0));
123: if (!m_ascending)
124: rv = -rv;
125: return rv;
126: } catch (Exception ignore) {
127: }
128:
129: // try a Time interpretation
130: try {
131: Time t1 = ((Entity) o1).getProperties().getTimeProperty(
132: property);
133: Time t2 = ((Entity) o2).getProperties().getTimeProperty(
134: property);
135: int rv = t1.compareTo(t2);
136: if (!m_ascending)
137: rv = -rv;
138: return rv;
139: } catch (Exception ignore) {
140: }
141:
142: // do a formatted interpretation - case insensitive
143: if (o1 == null)
144: return -1;
145: if (o2 == null)
146: return +1;
147: String s1 = ((Entity) o1).getProperties().getPropertyFormatted(
148: property);
149: String s2 = ((Entity) o2).getProperties().getPropertyFormatted(
150: property);
151: int rv = s1.compareToIgnoreCase(s2);
152: if (!m_ascending)
153: rv = -rv;
154: return rv;
155:
156: } // compare
157:
158: public String toString() {
159: return this .getClass().getName() + ": property(" + m_property
160: + ") ascending(" + m_ascending + ")";
161:
162: }
163:
164: } // ClassResourcesComparator
|