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