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.documentselector.util;
016    
017    import com.liferay.portal.kernel.util.ArrayUtil;
018    import com.liferay.portal.kernel.util.ParamUtil;
019    import com.liferay.portal.kernel.util.StringUtil;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.util.PortalUtil;
022    import com.liferay.portlet.documentlibrary.util.AudioProcessorUtil;
023    import com.liferay.portlet.documentlibrary.util.ImageProcessorUtil;
024    import com.liferay.portlet.documentlibrary.util.VideoProcessorUtil;
025    
026    import java.util.Collections;
027    import java.util.Set;
028    
029    import javax.servlet.http.HttpServletRequest;
030    
031    /**
032     * @author Eudaldo Alonso
033     */
034    public class DocumentSelectorUtil {
035    
036            public static String getCKEditorFuncNum(HttpServletRequest request) {
037                    String ckEditorFuncNum = ParamUtil.getString(
038                            request, "ckEditorFuncNum");
039    
040                    HttpServletRequest originalRequest =
041                            PortalUtil.getOriginalServletRequest(request);
042    
043                    return ParamUtil.getString(
044                            originalRequest, "CKEditorFuncNum", ckEditorFuncNum);
045            }
046    
047            public static String[] getMimeTypes(HttpServletRequest request) {
048                    Set<String> mimeTypes = _getMimeTypes(getType(request));
049    
050                    return ArrayUtil.toStringArray(mimeTypes.toArray());
051            }
052    
053            public static String[] getTabs1Names(HttpServletRequest request) {
054                    String tabs1Names = ParamUtil.getString(request, "tabs1Names");
055    
056                    if (Validator.isNotNull(tabs1Names)) {
057                            return StringUtil.split(tabs1Names);
058                    }
059    
060                    if (Validator.isNotNull(getType(request))) {
061                            return new String[] {_TYPE_DOCUMENTS};
062                    }
063    
064                    return new String[] {_TYPE_DOCUMENTS, _TYPE_PAGES};
065            }
066    
067            public static String getType(HttpServletRequest request) {
068                    String type = ParamUtil.getString(request, "type");
069    
070                    HttpServletRequest originalRequest =
071                            PortalUtil.getOriginalServletRequest(request);
072    
073                    return ParamUtil.getString(originalRequest, "Type", type);
074            }
075    
076            private static Set<String> _getMimeTypes(String type) {
077                    if (StringUtil.equalsIgnoreCase(type, "audio")) {
078                            return AudioProcessorUtil.getAudioMimeTypes();
079                    }
080                    else if (StringUtil.equalsIgnoreCase(type, "image")) {
081                            return ImageProcessorUtil.getImageMimeTypes();
082                    }
083                    else if (StringUtil.equalsIgnoreCase(type, "video")) {
084                            return VideoProcessorUtil.getVideoMimeTypes();
085                    }
086                    else {
087                            return Collections.emptySet();
088                    }
089            }
090    
091            private static final String _TYPE_DOCUMENTS = "documents";
092    
093            private static final String _TYPE_PAGES = "pages";
094    
095    }