001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.Validator;
023    
024    import java.io.File;
025    
026    /**
027     * @author Alexander Chow
028     */
029    public class DLAppUtil {
030    
031            public static String getExtension(String title, String sourceFileName) {
032                    String extension = FileUtil.getExtension(sourceFileName);
033    
034                    if (Validator.isNull(extension)) {
035                            extension = FileUtil.getExtension(title);
036                    }
037    
038                    return extension;
039            }
040    
041            public static String getMimeType(
042                    String sourceFileName, String mimeType, String title, File file) {
043    
044                    if (Validator.isNull(mimeType) ||
045                            mimeType.equals(ContentTypes.APPLICATION_OCTET_STREAM)) {
046    
047                            String extension = getExtension(title, sourceFileName);
048    
049                            mimeType = MimeTypesUtil.getContentType(file, "A." + extension);
050                    }
051    
052                    return mimeType;
053            }
054    
055            public static boolean isMajorVersion(
056                    FileVersion previousFileVersion, FileVersion currentFileVersion) {
057    
058                    long currentVersion = GetterUtil.getLong(
059                            currentFileVersion.getVersion());
060                    long previousVersion = GetterUtil.getLong(
061                            previousFileVersion.getVersion());
062    
063                    return (currentVersion - previousVersion) >= 1;
064            }
065    
066    }