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.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.FileVersion;
026    import com.liferay.portal.kernel.repository.model.Folder;
027    import com.liferay.portal.kernel.util.GetterUtil;
028    import com.liferay.portal.kernel.util.HtmlUtil;
029    import com.liferay.portal.kernel.util.HttpUtil;
030    import com.liferay.portal.kernel.util.OrderByComparator;
031    import com.liferay.portal.kernel.util.ParamUtil;
032    import com.liferay.portal.kernel.util.PrefsParamUtil;
033    import com.liferay.portal.kernel.util.PrefsPropsUtil;
034    import com.liferay.portal.kernel.util.PropsKeys;
035    import com.liferay.portal.kernel.util.PropsUtil;
036    import com.liferay.portal.kernel.util.SetUtil;
037    import com.liferay.portal.kernel.util.StringBundler;
038    import com.liferay.portal.kernel.util.StringPool;
039    import com.liferay.portal.kernel.util.StringUtil;
040    import com.liferay.portal.kernel.util.Validator;
041    import com.liferay.portal.kernel.util.WebKeys;
042    import com.liferay.portal.model.Group;
043    import com.liferay.portal.service.GroupLocalServiceUtil;
044    import com.liferay.portal.service.WorkflowDefinitionLinkLocalServiceUtil;
045    import com.liferay.portal.theme.ThemeDisplay;
046    import com.liferay.portal.util.PortalUtil;
047    import com.liferay.portlet.PortletPreferencesFactoryUtil;
048    import com.liferay.portlet.documentlibrary.model.DLFileEntryType;
049    import com.liferay.portlet.documentlibrary.model.DLFileEntryTypeConstants;
050    import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
051    import com.liferay.portlet.documentlibrary.model.DLFolder;
052    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
053    import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
054    import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
055    import com.liferay.portlet.documentlibrary.util.comparator.RepositoryModelCreateDateComparator;
056    import com.liferay.portlet.documentlibrary.util.comparator.RepositoryModelModifiedDateComparator;
057    import com.liferay.portlet.documentlibrary.util.comparator.RepositoryModelNameComparator;
058    import com.liferay.portlet.documentlibrary.util.comparator.RepositoryModelReadCountComparator;
059    import com.liferay.portlet.documentlibrary.util.comparator.RepositoryModelSizeComparator;
060    
061    import java.util.Arrays;
062    import java.util.Collections;
063    import java.util.Date;
064    import java.util.HashMap;
065    import java.util.HashSet;
066    import java.util.List;
067    import java.util.Map;
068    import java.util.Set;
069    import java.util.TreeSet;
070    
071    import javax.portlet.PortletPreferences;
072    import javax.portlet.PortletRequest;
073    import javax.portlet.PortletURL;
074    import javax.portlet.RenderResponse;
075    
076    import javax.servlet.http.HttpServletRequest;
077    
078    /**
079     * @author Brian Wing Shun Chan
080     * @author Julio Camarero
081     */
082    public class DLUtil {
083    
084            public static final String OFFICE_EXTENSION = "officeExtension";
085    
086            public static final String OFFICE_EXTENSION_PATH =
087                    StringPool.SLASH + OFFICE_EXTENSION;
088    
089            public static void addPortletBreadcrumbEntries(
090                            DLFileShortcut dlFileShortcut, HttpServletRequest request,
091                            RenderResponse renderResponse)
092                    throws Exception {
093    
094                    Folder folder = dlFileShortcut.getFolder();
095    
096                    if (folder.getFolderId() !=
097                                    DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
098    
099                            addPortletBreadcrumbEntries(folder, request, renderResponse);
100                    }
101    
102                    PortletURL portletURL = renderResponse.createRenderURL();
103    
104                    portletURL.setParameter(
105                            "struts_action", "/document_library/view_file_entry");
106                    portletURL.setParameter(
107                            "fileEntryId", String.valueOf(dlFileShortcut.getToFileEntryId()));
108    
109                    PortalUtil.addPortletBreadcrumbEntry(
110                            request, dlFileShortcut.getToTitle(), portletURL.toString());
111            }
112    
113            public static void addPortletBreadcrumbEntries(
114                            FileEntry fileEntry, HttpServletRequest request,
115                            RenderResponse renderResponse)
116                    throws Exception {
117    
118                    Folder folder = fileEntry.getFolder();
119    
120                    if (folder.getFolderId() !=
121                                    DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
122    
123                            addPortletBreadcrumbEntries(folder, request, renderResponse);
124                    }
125    
126                    PortletURL portletURL = renderResponse.createRenderURL();
127    
128                    portletURL.setParameter(
129                            "struts_action", "/document_library/view_file_entry");
130                    portletURL.setParameter(
131                            "fileEntryId", String.valueOf(fileEntry.getFileEntryId()));
132    
133                    PortalUtil.addPortletBreadcrumbEntry(
134                            request, fileEntry.getTitle(), portletURL.toString());
135            }
136    
137            public static void addPortletBreadcrumbEntries(
138                            Folder folder, HttpServletRequest request,
139                            LiferayPortletResponse liferayPortletResponse)
140                    throws Exception {
141    
142                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
143                            WebKeys.THEME_DISPLAY);
144    
145                    PortletURL portletURL = liferayPortletResponse.createRenderURL();
146    
147                    portletURL.setParameter("struts_action", "/document_library/view");
148    
149                    Map<String, Object> data = new HashMap<String, Object>();
150    
151                    data.put("direction-right", Boolean.TRUE.toString());
152                    data.put("folder-id", _getDefaultFolderId(request));
153    
154                    PortalUtil.addPortletBreadcrumbEntry(
155                            request, themeDisplay.translate("home"), portletURL.toString(),
156                            data);
157    
158                    addPortletBreadcrumbEntries(folder, request, portletURL);
159            }
160    
161            public static void addPortletBreadcrumbEntries(
162                            Folder folder, HttpServletRequest request, PortletURL portletURL)
163                    throws Exception {
164    
165                    long defaultFolderId = _getDefaultFolderId(request);
166    
167                    List<Folder> ancestorFolders = Collections.emptyList();
168    
169                    if ((folder != null) && (folder.getFolderId() != defaultFolderId)) {
170                            ancestorFolders = folder.getAncestors();
171    
172                            int indexOfRootFolder = -1;
173    
174                            for (int i = 0; i < ancestorFolders.size(); i++) {
175                                    Folder ancestorFolder = ancestorFolders.get(i);
176    
177                                    if (defaultFolderId == ancestorFolder.getFolderId()) {
178                                            indexOfRootFolder = i;
179                                    }
180                            }
181    
182                            if (indexOfRootFolder > -1) {
183                                    ancestorFolders = ancestorFolders.subList(0, indexOfRootFolder);
184                            }
185                    }
186    
187                    Collections.reverse(ancestorFolders);
188    
189                    for (Folder ancestorFolder : ancestorFolders) {
190                            portletURL.setParameter(
191                                    "folderId", String.valueOf(ancestorFolder.getFolderId()));
192    
193                            Map<String, Object> data = new HashMap<String, Object>();
194    
195                            data.put("direction-right", Boolean.TRUE.toString());
196                            data.put("folder-id", ancestorFolder.getFolderId());
197    
198                            PortalUtil.addPortletBreadcrumbEntry(
199                                    request, ancestorFolder.getName(), portletURL.toString(), data);
200                    }
201    
202                    long folderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
203    
204                    if (folder != null) {
205                            folderId = folder.getFolderId();
206                    }
207    
208                    portletURL.setParameter("folderId", String.valueOf(folderId));
209    
210                    if ((folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) &&
211                            (folderId != defaultFolderId)) {
212    
213                            Map<String, Object> data = new HashMap<String, Object>();
214    
215                            data.put("direction-right", Boolean.TRUE.toString());
216                            data.put("folder-id", folderId);
217    
218                            PortalUtil.addPortletBreadcrumbEntry(
219                                    request, folder.getName(), portletURL.toString(), data);
220                    }
221            }
222    
223            public static void addPortletBreadcrumbEntries(
224                            Folder folder, HttpServletRequest request,
225                            RenderResponse renderResponse)
226                    throws Exception {
227    
228                    String strutsAction = ParamUtil.getString(request, "struts_action");
229    
230                    long groupId = ParamUtil.getLong(request, "groupId");
231    
232                    PortletURL portletURL = renderResponse.createRenderURL();
233    
234                    if (strutsAction.equals("/journal/select_document_library") ||
235                            strutsAction.equals("/document_library/select_file_entry") ||
236                            strutsAction.equals("/document_library/select_folder") ||
237                            strutsAction.equals("/document_library_display/select_folder") ||
238                            strutsAction.equals("/image_gallery_display/select_folder")) {
239    
240                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
241                                    WebKeys.THEME_DISPLAY);
242    
243                            portletURL.setWindowState(LiferayWindowState.POP_UP);
244    
245                            portletURL.setParameter("struts_action", strutsAction);
246                            portletURL.setParameter("groupId", String.valueOf(groupId));
247    
248                            PortalUtil.addPortletBreadcrumbEntry(
249                                    request, themeDisplay.translate("home"), portletURL.toString());
250                    }
251                    else {
252                            portletURL.setParameter("struts_action", "/document_library/view");
253                    }
254    
255                    addPortletBreadcrumbEntries(folder, request, portletURL);
256            }
257    
258            public static void addPortletBreadcrumbEntries(
259                            long folderId, HttpServletRequest request,
260                            RenderResponse renderResponse)
261                    throws Exception {
262    
263                    if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
264                            Folder folder = DLAppLocalServiceUtil.getFolder(folderId);
265    
266                            if (folder.getFolderId() !=
267                                            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
268    
269                                    addPortletBreadcrumbEntries(folder, request, renderResponse);
270                            }
271                    }
272            }
273    
274            public static int compareVersions(String version1, String version2) {
275                    int[] splitVersion1 = StringUtil.split(version1, StringPool.PERIOD, 0);
276                    int[] splitVersion2 = StringUtil.split(version2, StringPool.PERIOD, 0);
277    
278                    if ((splitVersion1.length != 2) && (splitVersion2.length != 2)) {
279                            return 0;
280                    }
281                    else if (splitVersion1.length != 2) {
282                            return -1;
283                    }
284                    else if (splitVersion2.length != 2) {
285                            return 1;
286                    }
287    
288                    if (splitVersion1[0] > splitVersion2[0]) {
289                            return 1;
290                    }
291                    else if (splitVersion1[0] < splitVersion2[0]) {
292                            return -1;
293                    }
294                    else if (splitVersion1[1] > splitVersion2[1]) {
295                            return 1;
296                    }
297                    else if (splitVersion1[1] < splitVersion2[1]) {
298                            return -1;
299                    }
300    
301                    return 0;
302            }
303    
304            public static Set<String> getAllMediaGalleryMimeTypes() {
305                    return _instance._allMediaGalleryMimeTypes;
306            }
307    
308            public static String getDDMStructureKey(DLFileEntryType dlFileEntryType) {
309                    return getDDMStructureKey(dlFileEntryType.getUuid());
310            }
311    
312            public static String getDDMStructureKey(String fileEntryTypeUuid) {
313                    return "auto_" + fileEntryTypeUuid;
314            }
315    
316            public static String getDeprecatedDDMStructureKey(
317                    DLFileEntryType dlFileEntryType) {
318    
319                    return getDeprecatedDDMStructureKey(
320                            dlFileEntryType.getFileEntryTypeId());
321            }
322    
323            public static String getDeprecatedDDMStructureKey(long fileEntryTypeId) {
324                    return "auto_" + fileEntryTypeId;
325            }
326    
327            public static String getDividedPath(long id) {
328                    StringBundler sb = new StringBundler(16);
329    
330                    long dividend = id;
331    
332                    while ((dividend / _DIVISOR) != 0) {
333                            sb.append(StringPool.SLASH);
334                            sb.append(dividend % _DIVISOR);
335    
336                            dividend = dividend / _DIVISOR;
337                    }
338    
339                    sb.append(StringPool.SLASH);
340                    sb.append(id);
341    
342                    return sb.toString();
343            }
344    
345            public static String getFileEntryImage(
346                    FileEntry fileEntry, ThemeDisplay themeDisplay) {
347    
348                    StringBundler sb = new StringBundler(5);
349    
350                    sb.append("<img style=\"border-width: 0; text-align: left;\" src=\"");
351                    sb.append(themeDisplay.getPathThemeImages());
352                    sb.append("/file_system/small/");
353                    sb.append(fileEntry.getIcon());
354                    sb.append(".png\">");
355    
356                    return sb.toString();
357            }
358    
359            public static String getFileIcon(String extension) {
360                    return _instance._getFileIcon(extension);
361            }
362    
363            public static String getGenericName(String extension) {
364                    return _instance._getGenericName(extension);
365            }
366    
367            public static long[] getGroupIds(long groupId)
368                    throws PortalException, SystemException {
369    
370                    Group scopeGroup = GroupLocalServiceUtil.getGroup(groupId);
371    
372                    Group companyGroup = GroupLocalServiceUtil.getCompanyGroup(
373                            scopeGroup.getCompanyId());
374    
375                    if (scopeGroup.isLayout()) {
376                            return new long[] {
377                                    groupId, scopeGroup.getParentGroupId(),
378                                    companyGroup.getGroupId()
379                            };
380                    }
381                    else {
382                            return new long[] {groupId, companyGroup.getGroupId()};
383                    }
384            }
385    
386            public static long[] getGroupIds(ThemeDisplay themeDisplay)
387                    throws PortalException, SystemException {
388    
389                    return getGroupIds(themeDisplay.getScopeGroupId());
390            }
391    
392            public static String[] getMediaGalleryMimeTypes(
393                    PortletPreferences portletPreferences, PortletRequest portletRequest) {
394    
395                    String mimeTypes = PrefsParamUtil.getString(
396                            portletPreferences, portletRequest, "mimeTypes",
397                            _instance._allMediaGalleryMimeTypesString);
398    
399                    String[] mimeTypesArray = StringUtil.split(mimeTypes);
400    
401                    Arrays.sort(mimeTypesArray);
402    
403                    return mimeTypesArray;
404            }
405    
406            public static String getPreviewURL(
407                    FileEntry fileEntry, FileVersion fileVersion, ThemeDisplay themeDisplay,
408                    String queryString) {
409    
410                    return getPreviewURL(
411                            fileEntry, fileVersion, themeDisplay, queryString, true, true);
412            }
413    
414            /**
415             * @deprecated {@link #getPreviewURL(FileEntry, FileVersion, ThemeDisplay,
416             *             String, boolean, boolean)}
417             */
418            public static String getPreviewURL(
419                    FileEntry fileEntry, FileVersion fileVersion, ThemeDisplay themeDisplay,
420                    String queryString, boolean appendToken) {
421    
422                    return getPreviewURL(
423                            fileEntry, fileVersion, themeDisplay, queryString, true, true);
424            }
425    
426            public static String getPreviewURL(
427                    FileEntry fileEntry, FileVersion fileVersion, ThemeDisplay themeDisplay,
428                    String queryString, boolean appendVersion, boolean absoluteURL) {
429    
430                    StringBundler sb = new StringBundler(15);
431    
432                    if (themeDisplay != null) {
433                            if (absoluteURL) {
434                                    sb.append(themeDisplay.getPortalURL());
435                            }
436                    }
437    
438                    sb.append(PortalUtil.getPathContext());
439                    sb.append("/documents/");
440                    sb.append(fileEntry.getRepositoryId());
441                    sb.append(StringPool.SLASH);
442                    sb.append(fileEntry.getFolderId());
443                    sb.append(StringPool.SLASH);
444                    sb.append(HttpUtil.encodeURL(HtmlUtil.unescape(fileEntry.getTitle())));
445                    sb.append(StringPool.SLASH);
446                    sb.append(fileEntry.getUuid());
447    
448                    if (appendVersion) {
449                            sb.append("?version=");
450                            sb.append(fileVersion.getVersion());
451                    }
452    
453                    if (ImageProcessorUtil.isImageSupported(fileVersion)) {
454                            if (appendVersion) {
455                                    sb.append("&t=");
456                            }
457                            else {
458                                    sb.append("?t=");
459                            }
460    
461                            Date modifiedDate = fileVersion.getModifiedDate();
462    
463                            sb.append(modifiedDate.getTime());
464                    }
465    
466                    sb.append(queryString);
467    
468                    String previewURL = sb.toString();
469    
470                    if ((themeDisplay != null) && themeDisplay.isAddSessionIdToURL()) {
471                            return PortalUtil.getURLWithSessionId(
472                                    previewURL, themeDisplay.getSessionId());
473                    }
474    
475                    return previewURL;
476            }
477    
478            public static OrderByComparator getRepositoryModelOrderByComparator(
479                    String orderByCol, String orderByType) {
480    
481                    boolean orderByAsc = true;
482    
483                    if (orderByType.equals("desc")) {
484                            orderByAsc = false;
485                    }
486    
487                    OrderByComparator orderByComparator = null;
488    
489                    if (orderByCol.equals("creationDate")) {
490                            orderByComparator = new RepositoryModelCreateDateComparator(
491                                    orderByAsc);
492                    }
493                    else if (orderByCol.equals("downloads")) {
494                            orderByComparator = new RepositoryModelReadCountComparator(
495                                    orderByAsc);
496                    }
497                    else if (orderByCol.equals("modifiedDate")) {
498                            orderByComparator = new RepositoryModelModifiedDateComparator(
499                                    orderByAsc);
500                    }
501                    else if (orderByCol.equals("size")) {
502                            orderByComparator = new RepositoryModelSizeComparator(orderByAsc);
503                    }
504                    else {
505                            orderByComparator = new RepositoryModelNameComparator(orderByAsc);
506                    }
507    
508                    return orderByComparator;
509            }
510    
511            public static String getTempFileId(long id, String version) {
512                    return getTempFileId(id, version, null);
513            }
514    
515            public static String getTempFileId(
516                    long id, String version, String languageId) {
517    
518                    if (Validator.isNull(languageId)) {
519                            return String.valueOf(id).concat(StringPool.PERIOD).concat(version);
520                    }
521    
522                    StringBundler sb = new StringBundler(5);
523    
524                    sb.append(id);
525                    sb.append(StringPool.PERIOD);
526                    sb.append(version);
527                    sb.append(StringPool.PERIOD);
528                    sb.append(languageId);
529    
530                    return sb.toString();
531            }
532    
533            public static String getThumbnailSrc(
534                            FileEntry fileEntry, DLFileShortcut dlFileShortcut,
535                            ThemeDisplay themeDisplay)
536                    throws Exception {
537    
538                    return getThumbnailSrc(
539                            fileEntry, fileEntry.getFileVersion(), dlFileShortcut,
540                            themeDisplay);
541            }
542    
543            public static String getThumbnailSrc(
544                            FileEntry fileEntry, FileVersion fileVersion,
545                            DLFileShortcut dlFileShortcut, ThemeDisplay themeDisplay)
546                    throws Exception {
547    
548                    StringBundler sb = new StringBundler(4);
549    
550                    sb.append(themeDisplay.getPathThemeImages());
551                    sb.append("/file_system/large/");
552                    sb.append(DLUtil.getGenericName(fileEntry.getExtension()));
553                    sb.append(".png");
554    
555                    String thumbnailSrc = sb.toString();
556    
557                    if (dlFileShortcut == null) {
558                            String thumbnailQueryString = null;
559    
560                            if (GetterUtil.getBoolean(
561                                            PropsUtil.get(PropsKeys.DL_FILE_ENTRY_THUMBNAIL_ENABLED))) {
562    
563                                    if (ImageProcessorUtil.hasImages(fileVersion)) {
564                                            thumbnailQueryString = "&imageThumbnail=1";
565                                    }
566                                    else if (PDFProcessorUtil.hasImages(fileVersion)) {
567                                            thumbnailQueryString = "&documentThumbnail=1";
568                                    }
569                                    else if (VideoProcessorUtil.hasVideo(fileVersion)) {
570                                            thumbnailQueryString = "&videoThumbnail=1";
571                                    }
572                            }
573    
574                            if (Validator.isNotNull(thumbnailQueryString)) {
575                                    thumbnailSrc = getPreviewURL(
576                                            fileEntry, fileVersion, themeDisplay, thumbnailQueryString,
577                                            true, true);
578                            }
579                    }
580    
581                    return thumbnailSrc;
582            }
583    
584            public static String getThumbnailStyle() throws Exception {
585                    StringBundler sb = new StringBundler(5);
586    
587                    sb.append("max-height: ");
588                    sb.append(
589                            PrefsPropsUtil.getLong(
590                                    PropsKeys.DL_FILE_ENTRY_THUMBNAIL_MAX_HEIGHT));
591                    sb.append("px; max-width: ");
592                    sb.append(
593                            PrefsPropsUtil.getLong(
594                                    PropsKeys.DL_FILE_ENTRY_THUMBNAIL_MAX_WIDTH));
595                    sb.append("px;");
596    
597                    return sb.toString();
598            }
599    
600            public static String getTitleWithExtension(FileEntry fileEntry) {
601                    String title = fileEntry.getTitle();
602                    String extension = fileEntry.getExtension();
603    
604                    return getTitleWithExtension(title, extension);
605            }
606    
607            public static String getTitleWithExtension(String title, String extension) {
608                    if (Validator.isNotNull(extension)) {
609                            String periodAndExtension = StringPool.PERIOD + extension;
610    
611                            if (!title.endsWith(periodAndExtension)) {
612                                    title += periodAndExtension;
613                            }
614                    }
615    
616                    return title;
617            }
618    
619            public static String getWebDavURL(
620                            ThemeDisplay themeDisplay, Folder folder, FileEntry fileEntry)
621                    throws PortalException, SystemException {
622    
623                    return getWebDavURL(themeDisplay, folder, fileEntry, false);
624            }
625    
626            public static String getWebDavURL(
627                            ThemeDisplay themeDisplay, Folder folder, FileEntry fileEntry,
628                            boolean openDocumentUrl)
629                    throws PortalException, SystemException {
630    
631                    StringBundler webDavURL = new StringBundler(7);
632    
633                    boolean secure = false;
634    
635                    if (themeDisplay.isSecure() || _WEBDAV_SERVLET_HTTPS_REQUIRED) {
636                            secure = true;
637                    }
638    
639                    String portalURL = PortalUtil.getPortalURL(
640                            themeDisplay.getServerName(), themeDisplay.getServerPort(), secure);
641    
642                    webDavURL.append(portalURL);
643    
644                    webDavURL.append(themeDisplay.getPathContext());
645                    webDavURL.append("/api/secure/webdav");
646    
647                    String fileEntryTitle = null;
648    
649                    if (fileEntry != null) {
650                            String extension = fileEntry.getExtension();
651    
652                            fileEntryTitle = HtmlUtil.unescape(fileEntry.getTitle());
653    
654                            if (openDocumentUrl && isOfficeExtension(extension) &&
655                                    !fileEntryTitle.endsWith(StringPool.PERIOD + extension)) {
656    
657                                    webDavURL.append(OFFICE_EXTENSION_PATH);
658    
659                                    fileEntryTitle += StringPool.PERIOD + extension;
660                            }
661                    }
662    
663                    Group group = themeDisplay.getScopeGroup();
664    
665                    webDavURL.append(group.getFriendlyURL());
666                    webDavURL.append("/document_library");
667    
668                    StringBuilder sb = new StringBuilder();
669    
670                    if ((folder != null) &&
671                            (folder.getFolderId() !=
672                                    DLFolderConstants.DEFAULT_PARENT_FOLDER_ID)) {
673    
674                            Folder curFolder = folder;
675    
676                            while (true) {
677                                    sb.insert(0, HttpUtil.encodeURL(curFolder.getName(), true));
678                                    sb.insert(0, StringPool.SLASH);
679    
680                                    if (curFolder.getParentFolderId() ==
681                                                    DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
682    
683                                            break;
684                                    }
685    
686                                    curFolder = DLAppLocalServiceUtil.getFolder(
687                                            curFolder.getParentFolderId());
688                            }
689                    }
690    
691                    if (fileEntry != null) {
692                            sb.append(StringPool.SLASH);
693                            sb.append(HttpUtil.encodeURL(fileEntryTitle, true));
694                    }
695    
696                    webDavURL.append(sb.toString());
697    
698                    return webDavURL.toString();
699            }
700    
701            public static boolean hasWorkflowDefinitionLink(
702                            long companyId, long groupId, long folderId, long fileEntryTypeId)
703                    throws Exception {
704    
705                    while (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
706                            DLFolder dlFolder = DLFolderLocalServiceUtil.fetchDLFolder(
707                                    folderId);
708    
709                            if (dlFolder == null) {
710                                    return false;
711                            }
712    
713                            if (dlFolder.isOverrideFileEntryTypes()) {
714                                    break;
715                            }
716    
717                            folderId = dlFolder.getParentFolderId();
718                    }
719    
720                    if (WorkflowDefinitionLinkLocalServiceUtil.hasWorkflowDefinitionLink(
721                                    companyId, groupId, DLFolderConstants.getClassName(), folderId,
722                                    fileEntryTypeId) ||
723                            WorkflowDefinitionLinkLocalServiceUtil.hasWorkflowDefinitionLink(
724                                    companyId, groupId, DLFolderConstants.getClassName(), folderId,
725                                    DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_ALL)) {
726    
727                            return true;
728                    }
729    
730                    return false;
731            }
732    
733            public static boolean isAutoGeneratedDLFileEntryTypeDDMStructureKey(
734                    String ddmStructureKey) {
735    
736                    if (ddmStructureKey.startsWith("auto_")) {
737                            return true;
738                    }
739    
740                    return false;
741            }
742    
743            public static boolean isOfficeExtension(String extension) {
744                    if (extension.equalsIgnoreCase("doc") ||
745                            extension.equalsIgnoreCase("docx") ||
746                            extension.equalsIgnoreCase("dot") ||
747                            extension.equalsIgnoreCase("ppt") ||
748                            extension.equalsIgnoreCase("pptx") ||
749                            extension.equalsIgnoreCase("xls") ||
750                            extension.equalsIgnoreCase("xlsx")) {
751    
752                            return true;
753                    }
754    
755                    return false;
756            }
757    
758            private static long _getDefaultFolderId(HttpServletRequest request)
759                    throws Exception {
760    
761                    PortletPreferences portletPreferences =
762                            PortletPreferencesFactoryUtil.getPortletPreferences(
763                                    request, PortalUtil.getPortletId(request));
764    
765                    return GetterUtil.getLong(
766                            portletPreferences.getValue(
767                                    "rootFolderId",
768                                    String.valueOf(DLFolderConstants.DEFAULT_PARENT_FOLDER_ID)));
769            }
770    
771            private DLUtil() {
772                    _allMediaGalleryMimeTypes.addAll(
773                            SetUtil.fromArray(
774                                    PropsUtil.getArray(
775                                            PropsKeys.DL_FILE_ENTRY_PREVIEW_AUDIO_MIME_TYPES)));
776                    _allMediaGalleryMimeTypes.addAll(
777                            SetUtil.fromArray(
778                                    PropsUtil.getArray(
779                                            PropsKeys.DL_FILE_ENTRY_PREVIEW_VIDEO_MIME_TYPES)));
780                    _allMediaGalleryMimeTypes.addAll(
781                            SetUtil.fromArray(
782                                    PropsUtil.getArray(
783                                            PropsKeys.DL_FILE_ENTRY_PREVIEW_IMAGE_MIME_TYPES)));
784    
785                    _allMediaGalleryMimeTypesString = StringUtil.merge(
786                            _allMediaGalleryMimeTypes);
787    
788                    String[] fileIcons = null;
789    
790                    try {
791                            fileIcons = PrefsPropsUtil.getStringArray(
792                                    PropsKeys.DL_FILE_ICONS, StringPool.COMMA);
793                    }
794                    catch (Exception e) {
795                            _log.error(e, e);
796    
797                            fileIcons = new String[] {StringPool.BLANK};
798                    }
799    
800                    for (int i = 0; i < fileIcons.length; i++) {
801    
802                            // Only process non wildcard extensions
803    
804                            if (!StringPool.STAR.equals(fileIcons[i])) {
805    
806                                    // Strip starting period
807    
808                                    String extension = fileIcons[i];
809                                    extension = extension.substring(1);
810    
811                                    _fileIcons.add(extension);
812                            }
813                    }
814    
815                    String[] genericNames = PropsUtil.getArray(
816                            PropsKeys.DL_FILE_GENERIC_NAMES);
817    
818                    for (String genericName : genericNames) {
819                            _populateGenericNamesMap(genericName);
820                    }
821            }
822    
823            private String _getFileIcon(String extension) {
824                    if (!_fileIcons.contains(extension)) {
825                            extension = _DEFAULT_FILE_ICON;
826                    }
827    
828                    return extension;
829            }
830    
831            private String _getGenericName(String extension) {
832                    String genericName = _genericNames.get(extension);
833    
834                    if (genericName == null) {
835                            genericName = _DEFAULT_GENERIC_NAME;
836                    }
837    
838                    return genericName;
839            }
840    
841            private void _populateGenericNamesMap(String genericName) {
842                    String[] extensions = PropsUtil.getArray(
843                            PropsKeys.DL_FILE_GENERIC_EXTENSIONS, new Filter(genericName));
844    
845                    for (String extension : extensions) {
846                            _genericNames.put(extension, genericName);
847                    }
848            }
849    
850            private static final String _DEFAULT_FILE_ICON = "page";
851    
852            private static final String _DEFAULT_GENERIC_NAME = "default";
853    
854            private static final long _DIVISOR = 256;;
855    
856            private static final boolean _WEBDAV_SERVLET_HTTPS_REQUIRED =
857                    GetterUtil.getBoolean(
858                            PropsUtil.get(PropsKeys.WEBDAV_SERVLET_HTTPS_REQUIRED));
859    
860            private static Log _log = LogFactoryUtil.getLog(DLUtil.class);
861    
862            private static DLUtil _instance = new DLUtil();
863    
864            private Set<String> _allMediaGalleryMimeTypes = new TreeSet<String>();
865            private String _allMediaGalleryMimeTypesString;
866            private Set<String> _fileIcons = new HashSet<String>();
867            private Map<String, String> _genericNames = new HashMap<String, String>();
868    
869    }