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    @Override
039    public int compare(DocumentModel doc1, DocumentModel doc2) {
040
041        if (doc1 == null && doc2 == null) {
042            return 0;
043        } else if (doc1 == null) {
044            return asc ? -1 : 1;
045        } else if (doc2 == null) {
046            return asc ? 1 : -1;
047        }
048
049        int cmp = 0;
050        try {
051            Calendar created1 = (Calendar) doc1.getPropertyValue("dc:created");
052            Calendar created2 = (Calendar) doc2.getPropertyValue("dc:created");
053
054            if (created1 == null && created2 == null) {
055                return 0;
056            } else if (created1 == null) {
057                return asc ? -1 : 1;
058            } else if (created2 == null) {
059                return asc ? 1 : -1;
060            }
061            cmp = created1.compareTo(created2);
062        } catch (PropertyException e) {
063        }
064        return asc ? cmp : -cmp;
065    }
066
067}