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;
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    }