001    /**
002     * Copyright (c) 2000-2011 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.configuration.Filter;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.portlet.LiferayPortletResponse;
023    import com.liferay.portal.kernel.portlet.LiferayWindowState;
024    import com.liferay.portal.kernel.repository.model.FileEntry;
025    import com.liferay.portal.kernel.repository.model.Folder;
026    import com.liferay.portal.kernel.util.GetterUtil;
027    import com.liferay.portal.kernel.util.OrderByComparator;
028    import com.liferay.portal.kernel.util.ParamUtil;
029    import com.liferay.portal.kernel.util.PrefsParamUtil;
030    import com.liferay.portal.kernel.util.PrefsPropsUtil;
031    import com.liferay.portal.kernel.util.PropsKeys;
032    import com.liferay.portal.kernel.util.PropsUtil;
033    import com.liferay.portal.kernel.util.SetUtil;
034    import com.liferay.portal.kernel.util.StringBundler;
035    import com.liferay.portal.kernel.util.StringPool;
036    import com.liferay.portal.kernel.util.StringUtil;
037    import com.liferay.portal.kernel.util.Validator;
038    import com.liferay.portal.kernel.util.WebKeys;
039    import com.liferay.portal.model.Group;
040    import com.liferay.portal.service.GroupLocalServiceUtil;
041    import com.liferay.portal.theme.ThemeDisplay;
042    import com.liferay.portal.util.PortalUtil;
043    import com.liferay.portlet.PortletPreferencesFactoryUtil;
044    import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
045    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
046    import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
047    import com.liferay.portlet.documentlibrary.util.comparator.RepositoryModelCreateDateComparator;
048    import com.liferay.portlet.documentlibrary.util.comparator.RepositoryModelModifiedDateComparator;
049    import com.liferay.portlet.documentlibrary.util.comparator.RepositoryModelNameComparator;
050    import com.liferay.portlet.documentlibrary.util.comparator.RepositoryModelReadCountComparator;
051    import com.liferay.portlet.documentlibrary.util.comparator.RepositoryModelSizeComparator;
052    
053    import java.util.Arrays;
054    import java.util.Collections;
055    import java.util.HashMap;
056    import java.util.HashSet;
057    import java.util.List;
058    import java.util.Map;
059    import java.util.Set;
060    import java.util.TreeSet;
061    
062    import javax.portlet.PortletPreferences;
063    import javax.portlet.PortletRequest;
064    import javax.portlet.PortletURL;
065    import javax.portlet.RenderResponse;
066    
067    import javax.servlet.http.HttpServletRequest;
068    
069    /**
070     * @author Brian Wing Shun Chan
071     * @author Julio Camarero
072     */
073    public class DLUtil {
074    
075            public static void addPortletBreadcrumbEntries(
076                            DLFileShortcut dlFileShortcut, HttpServletRequest request,
077                            RenderResponse renderResponse, boolean showGlobally)
078                    throws Exception {
079    
080                    Folder folder = dlFileShortcut.getFolder();
081    
082                    if (folder.getFolderId() !=
083                                    DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
084    
085                            addPortletBreadcrumbEntries(
086                                    folder, request, renderResponse, showGlobally);
087                    }
088    
089                    PortletURL portletURL = renderResponse.createRenderURL();
090    
091                    portletURL.setParameter(
092                            "struts_action", "/document_library/view_file_entry");
093                    portletURL.setParameter(
094                            "fileEntryId",
095                            String.valueOf(dlFileShortcut.getToFileEntryId()));
096    
097                    PortalUtil.addPortletBreadcrumbEntry(
098                            request, dlFileShortcut.getToTitle(), portletURL.toString());
099            }
100    
101            public static void addPortletBreadcrumbEntries(
102                            FileEntry fileEntry, HttpServletRequest request,
103                            RenderResponse renderResponse, boolean showGlobally)
104                    throws Exception {
105    
106                    Folder folder = fileEntry.getFolder();
107    
108                    if (folder.getFolderId() !=
109                                    DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
110    
111                            addPortletBreadcrumbEntries(
112                                    folder, request, renderResponse, showGlobally);
113                    }
114    
115                    PortletURL portletURL = renderResponse.createRenderURL();
116    
117                    portletURL.setParameter(
118                            "struts_action", "/document_library/view_file_entry");
119                    portletURL.setParameter(
120                            "fileEntryId", String.valueOf(fileEntry.getFileEntryId()));
121    
122                    PortalUtil.addPortletBreadcrumbEntry(
123                            request, fileEntry.getTitle(), portletURL.toString());
124            }
125    
126            public static void addPortletBreadcrumbEntries(
127                            Folder folder, HttpServletRequest request,
128                            LiferayPortletResponse liferayPortletResponse, boolean showGlobally)
129                    throws Exception {
130    
131                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
132                            WebKeys.THEME_DISPLAY);
133    
134                    PortletURL portletURL =
135                            (PortletURL)liferayPortletResponse.createResourceURL();
136    
137                    portletURL.setParameter("struts_action", "/document_library/view");
138                    portletURL.setParameter("showSiblings", Boolean.TRUE.toString());
139                    portletURL.setParameter("viewAddButton", Boolean.TRUE.toString());
140                    portletURL.setParameter("viewBreadcrumb", Boolean.TRUE.toString());
141                    portletURL.setParameter(
142                            "viewDisplayStyleButttons", Boolean.TRUE.toString());
143                    portletURL.setParameter("viewEntries", Boolean.TRUE.toString());
144                    portletURL.setParameter(
145                            "viewFileEntrySearch", Boolean.TRUE.toString());
146                    portletURL.setParameter("viewFolders", Boolean.TRUE.toString());
147                    portletURL.setParameter("viewSortButton", Boolean.TRUE.toString());
148    
149                    Map<String, Object> data = new HashMap<String, Object>();
150    
151                    data.put("folder-id", _getDefaultFolderId(request));
152                    data.put("refresh-folders", Boolean.TRUE.toString());
153    
154                    PortalUtil.addPortletBreadcrumbEntry(
155                            request, themeDisplay.translate("home"), portletURL.toString(),
156                            data);
157    
158                    addPortletBreadcrumbEntries(folder, request, portletURL, showGlobally);
159            }
160    
161            public static void addPortletBreadcrumbEntries(
162                            Folder folder, HttpServletRequest request,
163                            PortletURL portletURL, boolean showGlobally)
164                    throws Exception {
165    
166                    long defaultFolderId = _getDefaultFolderId(request);
167    
168                    List<Folder> ancestorFolders = Collections.emptyList();
169    
170                    if ((folder != null) && (folder.getFolderId() != defaultFolderId)) {
171                            ancestorFolders = folder.getAncestors();
172    
173                            int indexOfRootFolder = -1;
174    
175                            for (int i = 0; i < ancestorFolders.size(); i++) {
176                                    Folder ancestorFolder = ancestorFolders.get(i);
177    
178                                    if (defaultFolderId == ancestorFolder.getFolderId()) {
179                                            indexOfRootFolder = i;
180                                    }
181                            }
182    
183                            if (indexOfRootFolder > -1) {
184                                    ancestorFolders = ancestorFolders.subList(0, indexOfRootFolder);
185                            }
186                    }
187    
188                    Collections.reverse(ancestorFolders);
189    
190                    for (Folder ancestorFolder : ancestorFolders) {
191                            portletURL.setParameter(
192                                    "folderId", String.valueOf(ancestorFolder.getFolderId()));
193    
194                            Map<String, Object> data = new HashMap<String, Object>();
195    
196                            data.put("folder-id", ancestorFolder.getFolderId());
197                            data.put("refresh-folders", Boolean.TRUE.toString());
198                            data.put("show-globally", String.valueOf(showGlobally));
199    
200                            PortalUtil.addPortletBreadcrumbEntry(
201                                    request, ancestorFolder.getName(), portletURL.toString(), data);
202                    }
203    
204                    long folderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
205    
206                    if (folder != null) {
207                            folderId = folder.getFolderId();
208                    }
209    
210                    portletURL.setParameter("folderId", String.valueOf(folderId));
211    
212                    if ((folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) &&
213                            (folderId != defaultFolderId)) {
214    
215                            Map<String, Object> data = new HashMap<String, Object>();
216    
217                            data.put("folder-id", folderId);
218                            data.put("refresh-folders", Boolean.TRUE.toString());
219                            data.put("show-globally", String.valueOf(showGlobally));
220    
221                            PortalUtil.addPortletBreadcrumbEntry(
222                                    request, folder.getName(), portletURL.toString(), data);
223                    }
224            }
225    
226            public static void addPortletBreadcrumbEntries(
227                            Folder folder, HttpServletRequest request,
228                            RenderResponse renderResponse, boolean showGlobally)
229                    throws Exception {
230    
231                    String strutsAction = ParamUtil.getString(
232                            request, "struts_action");
233    
234                    long groupId = ParamUtil.getLong(request, "groupId");
235    
236                    PortletURL portletURL = renderResponse.createRenderURL();
237    
238                    if (strutsAction.equals("/journal/select_document_library") ||
239                            strutsAction.equals("/document_library/select_file_entry") ||
240                            strutsAction.equals("/document_library/select_folder") ||
241                            strutsAction.equals("/document_library_display/select_folder") ||
242                            strutsAction.equals("/image_gallery_display/select_folder")) {
243    
244                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
245                                    WebKeys.THEME_DISPLAY);
246    
247                            portletURL.setWindowState(LiferayWindowState.POP_UP);
248    
249                            portletURL.setParameter("struts_action", strutsAction);
250                            portletURL.setParameter("groupId", String.valueOf(groupId));
251    
252                            Map<String, Object> data = new HashMap<String, Object>();
253    
254                            data.put("folder-id", _getDefaultFolderId(request));
255                            data.put("refresh-folders", Boolean.TRUE.toString());
256    
257                            PortalUtil.addPortletBreadcrumbEntry(
258                                    request, themeDisplay.translate("home"), portletURL.toString(),
259                                    data);
260                    }
261                    else {
262                            portletURL.setParameter("struts_action", "/document_library/view");
263                    }
264    
265                    addPortletBreadcrumbEntries(folder, request, portletURL, showGlobally);
266            }
267    
268            public static void addPortletBreadcrumbEntries(
269                            long folderId, HttpServletRequest request,
270                            RenderResponse renderResponse, boolean showGlobally)
271                    throws Exception {
272    
273                    if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
274                            Folder folder = DLAppLocalServiceUtil.getFolder(folderId);
275    
276                            if (folder.getFolderId() !=
277                                            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
278    
279                                    addPortletBreadcrumbEntries(
280                                            folder, request, renderResponse, showGlobally);
281                            }
282                    }
283            }
284    
285            public static int compareVersions(String version1, String version2) {
286                    int[] splitVersion1 = StringUtil.split(version1, StringPool.PERIOD, 0);
287                    int[] splitVersion2 = StringUtil.split(version2, StringPool.PERIOD, 0);
288    
289                    if ((splitVersion1.length != 2) && (splitVersion2.length != 2)) {
290                            return 0;
291                    }
292                    else if ((splitVersion1.length != 2)) {
293                            return -1;
294                    }
295                    else if ((splitVersion2.length != 2)) {
296                            return 1;
297                    }
298    
299                    if (splitVersion1[0] > splitVersion2[0]) {
300                            return 1;
301                    }
302                    else if (splitVersion1[0] < splitVersion2[0]) {
303                            return -1;
304                    }
305                    else if (splitVersion1[1] > splitVersion2[1]) {
306                            return 1;
307                    }
308                    else if (splitVersion1[1] < splitVersion2[1]) {
309                            return -1;
310                    }
311    
312                    return 0;
313            }
314    
315            public static Set<String> getAllMediaGalleryMimeTypes() {
316                    return _instance._allMediaGalleryMimeTypes;
317            }
318    
319            public static String getDividedPath(long id) {
320                    StringBundler sb = new StringBundler(16);
321    
322                    long dividend = id;
323    
324                    while ((dividend / _DIVISOR) != 0) {
325                            sb.append(StringPool.SLASH);
326                            sb.append(dividend % _DIVISOR);
327    
328                            dividend = dividend / _DIVISOR;
329                    }
330    
331                    sb.append(StringPool.SLASH);
332                    sb.append(id);
333    
334                    return sb.toString();
335            }
336    
337            public static String getFileIcon(String extension) {
338                    return _instance._getFileIcon(extension);
339            }
340    
341            public static String getGenericName(String extension) {
342                    return _instance._getGenericName(extension);
343            }
344    
345            public static long[] getGroupIds(long groupId)
346                    throws PortalException, SystemException {
347    
348                    Group scopeGroup = GroupLocalServiceUtil.getGroup(groupId);
349    
350                    Group companyGroup = GroupLocalServiceUtil.getCompanyGroup(
351                            scopeGroup.getCompanyId());
352    
353                    if (scopeGroup.isLayout()) {
354                            return new long[] {
355                                    scopeGroup.getParentGroupId(), companyGroup.getGroupId()
356                            };
357                    }
358                    else {
359                            return new long[] {groupId, companyGroup.getGroupId()};
360                    }
361            }
362    
363            public static long[] getGroupIds(ThemeDisplay themeDisplay)
364                    throws PortalException, SystemException {
365    
366                    return getGroupIds(themeDisplay.getScopeGroupId());
367            }
368    
369            public static String[] getMediaGalleryMimeTypes(
370                    PortletPreferences portletPreferences, PortletRequest portletRequest) {
371    
372                    String mimeTypes = PrefsParamUtil.getString(
373                            portletPreferences, portletRequest, "mimeTypes",
374                            _instance._allMediaGalleryMimeTypesString);
375    
376                    String[] mimeTypesArray = StringUtil.split(mimeTypes);
377    
378                    Arrays.sort(mimeTypesArray);
379    
380                    return mimeTypesArray;
381            }
382    
383            public static OrderByComparator getRepositoryModelOrderByComparator(
384                    String orderByCol, String orderByType) {
385    
386                    boolean orderByAsc = true;
387    
388                    if (orderByType.equals("desc")) {
389                            orderByAsc = false;
390                    }
391    
392                    OrderByComparator orderByComparator = null;
393    
394                    if (orderByCol.equals("creationDate")) {
395                            orderByComparator = new RepositoryModelCreateDateComparator(
396                                    orderByAsc);
397                    }
398                    else if (orderByCol.equals("downloads")) {
399                            orderByComparator = new RepositoryModelReadCountComparator(
400                                    orderByAsc);
401                    }
402                    else if (orderByCol.equals("modifiedDate")) {
403                            orderByComparator = new RepositoryModelModifiedDateComparator(
404                                    orderByAsc);
405                    }
406                    else if (orderByCol.equals("size")) {
407                            orderByComparator = new RepositoryModelSizeComparator(orderByAsc);
408                    }
409                    else {
410                            orderByComparator = new RepositoryModelNameComparator(orderByAsc);
411                    }
412    
413                    return orderByComparator;
414            }
415    
416            public static String getTempFileId(long id, String version) {
417                    return getTempFileId(id, version, null);
418            }
419    
420            public static String getTempFileId(
421                    long id, String version, String languageId) {
422    
423                    if (Validator.isNull(languageId)) {
424                            return String.valueOf(id).concat(StringPool.PERIOD).concat(version);
425                    }
426    
427                    StringBundler sb = new StringBundler(5);
428    
429                    sb.append(id);
430                    sb.append(StringPool.PERIOD);
431                    sb.append(version);
432                    sb.append(StringPool.PERIOD);
433                    sb.append(languageId);
434    
435                    return sb.toString();
436            }
437    
438            private static long _getDefaultFolderId(HttpServletRequest request)
439                    throws Exception {
440    
441                    PortletPreferences portletPreferences =
442                            PortletPreferencesFactoryUtil.getPortletPreferences(
443                                    request, PortalUtil.getPortletId(request));
444    
445                    return GetterUtil.getLong(
446                            portletPreferences.getValue(
447                                    "rootFolderId",
448                                    String.valueOf(DLFolderConstants.DEFAULT_PARENT_FOLDER_ID)));
449            }
450    
451            private DLUtil() {
452                    _allMediaGalleryMimeTypes.addAll(
453                            SetUtil.fromArray(
454                                    PropsUtil.getArray(
455                                            PropsKeys.DL_FILE_ENTRY_PREVIEW_AUDIO_MIME_TYPES)));
456                    _allMediaGalleryMimeTypes.addAll(
457                            SetUtil.fromArray(
458                                    PropsUtil.getArray(
459                                            PropsKeys.DL_FILE_ENTRY_PREVIEW_VIDEO_MIME_TYPES)));
460                    _allMediaGalleryMimeTypes.addAll(
461                            SetUtil.fromArray(
462                                    PropsUtil.getArray(
463                                            PropsKeys.DL_FILE_ENTRY_PREVIEW_IMAGE_MIME_TYPES)));
464    
465                    _allMediaGalleryMimeTypesString = StringUtil.merge(
466                            _allMediaGalleryMimeTypes);
467    
468                    String[] fileIcons = null;
469    
470                    try {
471                            fileIcons = PrefsPropsUtil.getStringArray(
472                                    PropsKeys.DL_FILE_ICONS, StringPool.COMMA);
473                    }
474                    catch (Exception e) {
475                            _log.error(e, e);
476    
477                            fileIcons = new String[] {StringPool.BLANK};
478                    }
479    
480                    for (int i = 0; i < fileIcons.length; i++) {
481    
482                            // Only process non wildcard extensions
483    
484                            if (!StringPool.STAR.equals(fileIcons[i])) {
485    
486                                    // Strip starting period
487    
488                                    String extension = fileIcons[i];
489                                    extension = extension.substring(1, extension.length());
490    
491                                    _fileIcons.add(extension);
492                            }
493                    }
494    
495                    String[] genericNames = PropsUtil.getArray(
496                            PropsKeys.DL_FILE_GENERIC_NAMES);
497    
498                    for (String genericName : genericNames) {
499                            _populateGenericNamesMap(genericName);
500                    }
501            }
502    
503            private String _getFileIcon(String extension) {
504                    if (!_fileIcons.contains(extension)) {
505                            extension = _DEFAULT_FILE_ICON;
506                    }
507    
508                    return extension;
509            }
510    
511            private String _getGenericName(String extension) {
512                    String genericName = _genericNames.get(extension);
513    
514                    if (genericName == null) {
515                            genericName = _DEFAULT_GENERIC_NAME;
516                    }
517    
518                    return genericName;
519            }
520    
521            private void _populateGenericNamesMap(String genericName) {
522                    String[] extensions = PropsUtil.getArray(
523                            PropsKeys.DL_FILE_GENERIC_EXTENSIONS, new Filter(genericName));
524    
525                    for (String extension : extensions) {
526                            _genericNames.put(extension, genericName);
527                    }
528            }
529    
530            private static final String _DEFAULT_FILE_ICON = "page";
531    
532            private static final String _DEFAULT_GENERIC_NAME = "default";
533    
534            private static final long _DIVISOR = 256;;
535    
536            private static Log _log = LogFactoryUtil.getLog(DLUtil.class);
537    
538            private static DLUtil _instance = new DLUtil();
539    
540            private Set<String> _allMediaGalleryMimeTypes = new TreeSet<String>();
541            private String _allMediaGalleryMimeTypesString;
542            private Set<String> _fileIcons = new HashSet<String>();
543            private Map<String, String> _genericNames = new HashMap<String, String>();
544    
545    }