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;
016    
017    import com.liferay.portal.kernel.repository.model.FileVersion;
018    import com.liferay.portal.kernel.util.ContentTypes;
019    import com.liferay.portal.kernel.util.FileUtil;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.MimeTypesUtil;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.Validator;
024    
025    import java.io.File;
026    
027    /**
028     * @author Alexander Chow
029     */
030    public class DLAppUtil {
031    
032            public static String getExtension(String title, String sourceFileName) {
033                    String extension = FileUtil.getExtension(sourceFileName);
034    
035                    if (Validator.isNull(extension)) {
036                            extension = FileUtil.getExtension(title);
037                    }
038    
039                    return extension;
040            }
041    
042            public static String getMimeType(
043                    String sourceFileName, String mimeType, String title, File file) {
044    
045                    if (Validator.isNull(mimeType) ||
046                            mimeType.equals(ContentTypes.APPLICATION_OCTET_STREAM)) {
047    
048                            String extension = getExtension(title, sourceFileName);
049    
050                            mimeType = MimeTypesUtil.getContentType(file, "A." + extension);
051                    }
052    
053                    return mimeType;
054            }
055    
056            public static String getSourceFileName(FileVersion fileVersion) {
057                    String extension = fileVersion.getExtension();
058    
059                    if (Validator.isNull(extension)) {
060                            return fileVersion.getTitle();
061                    }
062    
063                    String suffix = StringPool.PERIOD + extension;
064    
065                    String title = fileVersion.getTitle();
066    
067                    if (title.endsWith(suffix)) {
068                            return title;
069                    }
070    
071                    return title + suffix;
072            }
073    
074            public static boolean isMajorVersion(
075                    FileVersion previousFileVersion, FileVersion currentFileVersion) {
076    
077                    long currentVersion = GetterUtil.getLong(
078                            currentFileVersion.getVersion());
079                    long previousVersion = GetterUtil.getLong(
080                            previousFileVersion.getVersion());
081    
082                    return (currentVersion - previousVersion) >= 1;
083            }
084    
085    }