001/*
002 * (C) Copyright 2010 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 */
017
018package org.nuxeo.ecm.platform.comment.impl;
019
020import java.util.Calendar;
021
022import org.nuxeo.ecm.core.api.DocumentModel;
023import org.nuxeo.ecm.core.api.PropertyException;
024import org.nuxeo.ecm.core.api.Sorter;
025
026public class CommentSorter implements Sorter {
027
028    private static final long serialVersionUID = 1L;
029
030    private boolean asc = true;
031
032    public CommentSorter(boolean asc) {
033        this.asc = asc;
034    }
035
036    public int compare(DocumentModel doc1, DocumentModel doc2) {
037
038        if (doc1 == null && doc2 == null) {
039            return 0;
040        } else if (doc1 == null) {
041            return asc ? -1 : 1;
042        } else if (doc2 == null) {
043            return asc ? 1 : -1;
044        }
045
046        int cmp = 0;
047        try {
048            Calendar created1 = (Calendar) doc1.getPropertyValue("dc:created");
049            Calendar created2 = (Calendar) doc2.getPropertyValue("dc:created");
050
051            if (created1 == null && created2 == null) {
052                return 0;
053            } else if (created1 == null) {
054                return asc ? -1 : 1;
055            } else if (created2 == null) {
056                return asc ? 1 : -1;
057            }
058            cmp = created1.compareTo(created2);
059        } catch (PropertyException e) {
060        }
061        return asc ? cmp : -cmp;
062    }
063
064}