001/* 002 * (C) Copyright 2010 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Nuxeo - initial API and implementation 018 */ 019 020package org.nuxeo.ecm.platform.comment.impl; 021 022import java.util.Calendar; 023 024import org.nuxeo.ecm.core.api.DocumentModel; 025import org.nuxeo.ecm.core.api.PropertyException; 026import org.nuxeo.ecm.core.api.Sorter; 027 028public class CommentSorter implements Sorter { 029 030 private static final long serialVersionUID = 1L; 031 032 private boolean asc = true; 033 034 public CommentSorter(boolean asc) { 035 this.asc = asc; 036 } 037 038 public int compare(DocumentModel doc1, DocumentModel doc2) { 039 040 if (doc1 == null && doc2 == null) { 041 return 0; 042 } else if (doc1 == null) { 043 return asc ? -1 : 1; 044 } else if (doc2 == null) { 045 return asc ? 1 : -1; 046 } 047 048 int cmp = 0; 049 try { 050 Calendar created1 = (Calendar) doc1.getPropertyValue("dc:created"); 051 Calendar created2 = (Calendar) doc2.getPropertyValue("dc:created"); 052 053 if (created1 == null && created2 == null) { 054 return 0; 055 } else if (created1 == null) { 056 return asc ? -1 : 1; 057 } else if (created2 == null) { 058 return asc ? 1 : -1; 059 } 060 cmp = created1.compareTo(created2); 061 } catch (PropertyException e) { 062 } 063 return asc ? cmp : -cmp; 064 } 065 066}