001    /**
002     * Copyright (c) 2000-2010 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.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.portlet.LiferayWindowState;
020    import com.liferay.portal.kernel.util.ParamUtil;
021    import com.liferay.portal.kernel.util.PrefsPropsUtil;
022    import com.liferay.portal.kernel.util.PropsKeys;
023    import com.liferay.portal.kernel.util.StringBundler;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.StringUtil;
026    import com.liferay.portal.kernel.util.WebKeys;
027    import com.liferay.portal.theme.ThemeDisplay;
028    import com.liferay.portal.util.PortalUtil;
029    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
030    import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
031    import com.liferay.portlet.documentlibrary.model.DLFolder;
032    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
033    import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
034    
035    import java.util.Collections;
036    import java.util.HashMap;
037    import java.util.HashSet;
038    import java.util.List;
039    import java.util.Map;
040    import java.util.Set;
041    
042    import javax.portlet.PortletURL;
043    import javax.portlet.RenderResponse;
044    
045    import javax.servlet.http.HttpServletRequest;
046    
047    /**
048     * @author Brian Wing Shun Chan
049     * @author Julio Camarero
050     */
051    public class DLUtil {
052    
053            public static void addPortletBreadcrumbEntries(
054                            DLFileEntry fileEntry, HttpServletRequest request,
055                            RenderResponse renderResponse)
056                    throws Exception {
057    
058                    DLFolder folder = fileEntry.getFolder();
059    
060                    if (folder.getFolderId() !=
061                                    DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
062    
063                            addPortletBreadcrumbEntries(folder, request, renderResponse);
064                    }
065    
066                    PortletURL portletURL = renderResponse.createRenderURL();
067    
068                    portletURL.setParameter(
069                            "struts_action", "/document_library/view_file_entry");
070                    portletURL.setParameter(
071                            "folderId", String.valueOf(fileEntry.getFolderId()));
072                    portletURL.setParameter("name", fileEntry.getName());
073    
074                    PortalUtil.addPortletBreadcrumbEntry(
075                            request, fileEntry.getTitle(), portletURL.toString());
076            }
077    
078            public static void addPortletBreadcrumbEntries(
079                            DLFileShortcut fileShortcut, HttpServletRequest request,
080                            RenderResponse renderResponse)
081                    throws Exception {
082    
083                    DLFolder folder = fileShortcut.getFolder();
084    
085                    if (folder.getFolderId() !=
086                                    DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
087    
088                            addPortletBreadcrumbEntries(folder, request, renderResponse);
089                    }
090    
091                    PortletURL portletURL = renderResponse.createRenderURL();
092    
093                    portletURL.setParameter(
094                            "struts_action", "/document_library/view_file_shortcut");
095                    portletURL.setParameter(
096                            "fileShortcutId", String.valueOf(fileShortcut.getFileShortcutId()));
097    
098                    PortalUtil.addPortletBreadcrumbEntry(
099                            request, fileShortcut.getToTitle(), portletURL.toString());
100            }
101    
102            public static void addPortletBreadcrumbEntries(
103                            long folderId, HttpServletRequest request,
104                            RenderResponse renderResponse)
105                    throws Exception {
106    
107                    if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
108                            DLFolder folder = DLFolderLocalServiceUtil.getFolder(folderId);
109    
110                            if (folder.getFolderId() !=
111                                            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
112    
113                                    addPortletBreadcrumbEntries(folder, request, renderResponse);
114                            }
115                    }
116            }
117    
118            public static void addPortletBreadcrumbEntries(
119                            DLFolder folder, HttpServletRequest request,
120                            RenderResponse renderResponse)
121                    throws Exception {
122    
123                    String strutsAction = ParamUtil.getString(
124                            request, "struts_action");
125    
126                    long groupId = ParamUtil.getLong(request, "groupId");
127    
128                    PortletURL portletURL = renderResponse.createRenderURL();
129    
130                    if (strutsAction.equals("/journal/select_document_library") ||
131                            strutsAction.equals("/document_library/select_file_entry") ||
132                            strutsAction.equals("/document_library/select_folder")) {
133    
134                            ThemeDisplay themeDisplay =     (ThemeDisplay)request.getAttribute(
135                                    WebKeys.THEME_DISPLAY);
136    
137                            portletURL.setWindowState(LiferayWindowState.POP_UP);
138    
139                            portletURL.setParameter("struts_action", strutsAction);
140                            portletURL.setParameter("groupId", String.valueOf(groupId));
141    
142                            PortalUtil.addPortletBreadcrumbEntry(
143                                    request, themeDisplay.translate("documents-home"),
144                                    portletURL.toString());
145                    }
146                    else {
147                            portletURL.setParameter("struts_action", "/document_library/view");
148                    }
149    
150                    List<DLFolder> ancestorFolders = folder.getAncestors();
151    
152                    Collections.reverse(ancestorFolders);
153    
154                    for (DLFolder ancestorFolder : ancestorFolders) {
155                            portletURL.setParameter(
156                                    "folderId", String.valueOf(ancestorFolder.getFolderId()));
157    
158                            PortalUtil.addPortletBreadcrumbEntry(
159                                    request, ancestorFolder.getName(), portletURL.toString());
160                    }
161    
162                    portletURL.setParameter(
163                            "folderId", String.valueOf(folder.getFolderId()));
164    
165                    if (folder.getFolderId() !=
166                                    DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
167    
168                            PortalUtil.addPortletBreadcrumbEntry(
169                                    request, folder.getName(), portletURL.toString());
170                    }
171            }
172    
173            public static int compareVersions(String version1, String version2) {
174                    int[] splitVersion1 = StringUtil.split(version1, StringPool.PERIOD, 0);
175                    int[] splitVersion2 = StringUtil.split(version2, StringPool.PERIOD, 0);
176    
177                    if ((splitVersion1.length != 2) && (splitVersion2.length != 2)) {
178                            return 0;
179                    }
180                    else if ((splitVersion1.length != 2)) {
181                            return -1;
182                    }
183                    else if ((splitVersion2.length != 2)) {
184                            return 1;
185                    }
186    
187                    if (splitVersion1[0] > splitVersion2[0]) {
188                            return 1;
189                    }
190                    else if (splitVersion1[0] < splitVersion2[0]) {
191                            return -1;
192                    }
193                    else if (splitVersion1[1] > splitVersion2[1]) {
194                            return 1;
195                    }
196                    else if (splitVersion1[1] < splitVersion2[1]) {
197                            return -1;
198                    }
199    
200                    return 0;
201            }
202    
203            public static String getFileIcon(String extension) {
204                    return _instance._getFileIcon(extension);
205            }
206    
207            public static String getGenericName(String extension) {
208                    return _instance._getGenericName(extension);
209            }
210    
211            public static String getLockId(long groupId, long folderId, String name) {
212                    StringBundler sb = new StringBundler(5);
213    
214                    sb.append(groupId);
215                    sb.append(StringPool.POUND);
216                    sb.append(folderId);
217                    sb.append(StringPool.POUND);
218                    sb.append(name);
219    
220                    return sb.toString();
221            }
222    
223            private DLUtil() {
224                    _fileIcons = new HashSet<String>();
225    
226                    String[] fileIcons = null;
227    
228                    try {
229                            fileIcons = PrefsPropsUtil.getStringArray(
230                                    PropsKeys.DL_FILE_ICONS, StringPool.COMMA);
231                    }
232                    catch (Exception e) {
233                            _log.error(e, e);
234    
235                            fileIcons = new String[] {StringPool.BLANK};
236                    }
237    
238                    for (int i = 0; i < fileIcons.length; i++) {
239    
240                            // Only process non wildcard extensions
241    
242                            if (!StringPool.STAR.equals(fileIcons[i])) {
243    
244                                    // Strip starting period
245    
246                                    String extension = fileIcons[i];
247                                    extension = extension.substring(1, extension.length());
248    
249                                    _fileIcons.add(extension);
250                            }
251                    }
252    
253                    _genericNames = new HashMap<String, String>();
254    
255                    _genericNames.put("lar", "compressed");
256                    _genericNames.put("rar", "compressed");
257                    _genericNames.put("zip", "compressed");
258    
259                    _genericNames.put("doc", "document");
260                    _genericNames.put("docx", "document");
261                    _genericNames.put("rtf", "document");
262                    _genericNames.put("odt", "document");
263    
264                    _genericNames.put("flv", "flash");
265                    _genericNames.put("swf", "flash");
266    
267                    _genericNames.put("bmp", "image");
268                    _genericNames.put("gif", "image");
269                    _genericNames.put("jpeg", "image");
270                    _genericNames.put("jpg", "image");
271                    _genericNames.put("odg", "image");
272                    _genericNames.put("png", "image");
273                    _genericNames.put("svg", "image");
274    
275                    _genericNames.put("acc", "music");
276                    _genericNames.put("mid", "music");
277                    _genericNames.put("mp3", "music");
278                    _genericNames.put("wav", "music");
279                    _genericNames.put("wma", "music");
280    
281                    _genericNames.put("pdf", "pdf");
282    
283                    _genericNames.put("key", "presentation");
284                    _genericNames.put("odp", "presentation");
285                    _genericNames.put("pps", "presentation");
286                    _genericNames.put("ppt", "presentation");
287                    _genericNames.put("pptx", "presentation");
288    
289                    _genericNames.put("csv", "spreadsheet");
290                    _genericNames.put("ods", "spreadsheet");
291                    _genericNames.put("xls", "spreadsheet");
292                    _genericNames.put("xlsx", "spreadsheet");
293    
294                    _genericNames.put("avi", "video");
295                    _genericNames.put("mov", "video");
296                    _genericNames.put("mp4", "video");
297                    _genericNames.put("mpg", "video");
298                    _genericNames.put("qt", "video");
299                    _genericNames.put("rm", "video");
300                    _genericNames.put("wmv", "video");
301            }
302    
303            private String _getFileIcon(String extension) {
304                    if (!_fileIcons.contains(extension)) {
305                            extension = _DEFAULT_FILE_ICON;
306                    }
307    
308                    return extension;
309            }
310    
311            private String _getGenericName(String extension) {
312                    String genericName = _genericNames.get(extension);
313    
314                    if (genericName == null) {
315                            genericName = _DEFAULT_GENERIC_NAME;
316                    }
317    
318                    return genericName;
319            }
320    
321            private static final String _DEFAULT_GENERIC_NAME = "default";
322    
323            private static final String _DEFAULT_FILE_ICON = "page";
324    
325            private static Log _log = LogFactoryUtil.getLog(DLUtil.class);
326    
327            private static DLUtil _instance = new DLUtil();
328    
329            private Map<String, String> _genericNames;
330            private Set<String> _fileIcons;
331    
332    }