001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.documentlibrary.util.comparator;
016    
017    import com.liferay.portal.kernel.repository.model.FileEntry;
018    import com.liferay.portal.kernel.repository.model.Folder;
019    import com.liferay.portal.kernel.util.DateUtil;
020    import com.liferay.portal.kernel.util.OrderByComparator;
021    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
022    import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
023    import com.liferay.portlet.documentlibrary.model.DLFolder;
024    
025    import java.util.Date;
026    
027    /**
028     * @author Alexander Chow
029     */
030    public class RepositoryModelCreateDateComparator extends OrderByComparator {
031    
032            public static final String ORDER_BY_ASC = "createDate ASC";
033    
034            public static final String ORDER_BY_DESC = "createDate DESC";
035    
036            public static final String[] ORDER_BY_FIELDS = {"createDate"};
037    
038            public RepositoryModelCreateDateComparator() {
039                    this(false);
040            }
041    
042            public RepositoryModelCreateDateComparator(boolean ascending) {
043                    _ascending = ascending;
044            }
045    
046            @Override
047            public int compare(Object obj1, Object obj2) {
048                    Date createDate1 = getCreateDate(obj1);
049                    Date createDate2 = getCreateDate(obj2);
050    
051                    int value = DateUtil.compareTo(createDate1, createDate2);
052    
053                    if (_ascending) {
054                            return value;
055                    }
056                    else {
057                            return -value;
058                    }
059            }
060    
061            @Override
062            public String getOrderBy() {
063                    if (_ascending) {
064                            return ORDER_BY_ASC;
065                    }
066                    else {
067                            return ORDER_BY_DESC;
068                    }
069            }
070    
071            @Override
072            public String[] getOrderByFields() {
073                    return ORDER_BY_FIELDS;
074            }
075    
076            @Override
077            public boolean isAscending() {
078                    return _ascending;
079            }
080    
081            protected Date getCreateDate(Object obj) {
082                    if (obj instanceof DLFileEntry) {
083                            DLFileEntry dlFileEntry = (DLFileEntry)obj;
084    
085                            return dlFileEntry.getCreateDate();
086                    }
087                    else if (obj instanceof DLFileShortcut) {
088                            DLFileShortcut dlFileShortcut = (DLFileShortcut)obj;
089    
090                            return dlFileShortcut.getCreateDate();
091                    }
092                    else if (obj instanceof DLFolder) {
093                            DLFolder dlFolder = (DLFolder)obj;
094    
095                            return dlFolder.getCreateDate();
096                    }
097                    else if (obj instanceof FileEntry) {
098                            FileEntry fileEntry = (FileEntry)obj;
099    
100                            return fileEntry.getCreateDate();
101                    }
102                    else {
103                            Folder folder = (Folder)obj;
104    
105                            return folder.getCreateDate();
106                    }
107            }
108    
109            private boolean _ascending;
110    
111    }