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 Brian Wing Shun Chan
029     * @author Alexander Chow
030     */
031    public class RepositoryModelModifiedDateComparator extends OrderByComparator {
032    
033            public static final String ORDER_BY_ASC = "modifiedDate ASC";
034    
035            public static final String ORDER_BY_DESC = "modifiedDate DESC";
036    
037            public static final String[] ORDER_BY_FIELDS = {"modifiedDate"};
038    
039            public RepositoryModelModifiedDateComparator() {
040                    this(false);
041            }
042    
043            public RepositoryModelModifiedDateComparator(boolean ascending) {
044                    _ascending = ascending;
045            }
046    
047            @Override
048            public int compare(Object obj1, Object obj2) {
049                    Date modifiedDate1 = getModifiedDate(obj1);
050                    Date modifiedDate2 = getModifiedDate(obj2);
051    
052                    int value = DateUtil.compareTo(modifiedDate1, modifiedDate2);
053    
054                    if (_ascending) {
055                            return value;
056                    }
057                    else {
058                            return -value;
059                    }
060            }
061    
062            @Override
063            public String getOrderBy() {
064                    if (_ascending) {
065                            return ORDER_BY_ASC;
066                    }
067                    else {
068                            return ORDER_BY_DESC;
069                    }
070            }
071    
072            @Override
073            public String[] getOrderByFields() {
074                    return ORDER_BY_FIELDS;
075            }
076    
077            @Override
078            public boolean isAscending() {
079                    return _ascending;
080            }
081    
082            protected Date getModifiedDate(Object obj) {
083                    if (obj instanceof DLFileEntry) {
084                            DLFileEntry dlFileEntry = (DLFileEntry)obj;
085    
086                            return dlFileEntry.getModifiedDate();
087                    }
088                    else if (obj instanceof DLFileShortcut) {
089                            DLFileShortcut dlFileShortcut = (DLFileShortcut)obj;
090    
091                            return dlFileShortcut.getModifiedDate();
092                    }
093                    else if (obj instanceof DLFolder) {
094                            DLFolder dlFolder = (DLFolder)obj;
095    
096                            return dlFolder.getModifiedDate();
097                    }
098                    else if (obj instanceof FileEntry) {
099                            FileEntry fileEntry = (FileEntry)obj;
100    
101                            return fileEntry.getModifiedDate();
102                    }
103                    else {
104                            Folder folder = (Folder)obj;
105    
106                            return folder.getModifiedDate();
107                    }
108            }
109    
110            private boolean _ascending;
111    
112    }