001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.documentlibrary.util;
022:
023: import com.liferay.portal.kernel.language.LanguageUtil;
024: import com.liferay.portal.kernel.portlet.LiferayWindowState;
025: import com.liferay.portal.kernel.util.ParamUtil;
026: import com.liferay.portal.kernel.util.StringMaker;
027: import com.liferay.portal.kernel.util.StringPool;
028: import com.liferay.portal.kernel.util.Validator;
029: import com.liferay.portal.util.PropsValues;
030: import com.liferay.portlet.documentlibrary.model.DLFileEntry;
031: import com.liferay.portlet.documentlibrary.model.DLFolder;
032: import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
033: import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
034: import com.liferay.util.CollectionFactory;
035:
036: import java.util.Set;
037:
038: import javax.portlet.PortletURL;
039: import javax.portlet.RenderRequest;
040: import javax.portlet.RenderResponse;
041: import javax.portlet.WindowState;
042:
043: import javax.servlet.jsp.PageContext;
044:
045: /**
046: * <a href="DLUtil.java.html"><b><i>View Source</i></b></a>
047: *
048: * @author Brian Wing Shun Chan
049: *
050: */
051: public class DLUtil {
052:
053: public static String getBreadcrumbs(long folderId, String name,
054: PageContext pageContext, RenderRequest req,
055: RenderResponse res) throws Exception {
056:
057: if ((folderId > 0) && Validator.isNotNull(name)) {
058: DLFileEntry fileEntry = DLFileEntryLocalServiceUtil
059: .getFileEntry(folderId, name);
060:
061: return getBreadcrumbs(fileEntry.getFolder(), fileEntry,
062: pageContext, req, res);
063: } else {
064: DLFolder folder = null;
065:
066: try {
067: folder = DLFolderLocalServiceUtil.getFolder(folderId);
068: } catch (Exception e) {
069: }
070:
071: return getBreadcrumbs(folder, null, pageContext, req, res);
072: }
073: }
074:
075: public static String getBreadcrumbs(DLFolder folder,
076: DLFileEntry fileEntry, PageContext pageContext,
077: RenderRequest req, RenderResponse res) throws Exception {
078:
079: long groupId = ParamUtil.getLong(req, "groupId");
080:
081: if ((fileEntry != null) && (folder == null)) {
082: folder = fileEntry.getFolder();
083: }
084:
085: PortletURL foldersURL = res.createRenderURL();
086:
087: WindowState windowState = req.getWindowState();
088:
089: if (windowState.equals(LiferayWindowState.POP_UP)) {
090: foldersURL.setWindowState(LiferayWindowState.POP_UP);
091:
092: String strutsAction = ParamUtil.getString(req,
093: "struts_action");
094:
095: foldersURL.setParameter("struts_action", strutsAction);
096: foldersURL.setParameter("groupId", String.valueOf(groupId));
097: } else {
098: //foldersURL.setWindowState(WindowState.MAXIMIZED);
099:
100: foldersURL.setParameter("struts_action",
101: "/document_library/view");
102: }
103:
104: String foldersLink = "<a href=\"" + foldersURL.toString()
105: + "\">" + LanguageUtil.get(pageContext, "folders")
106: + "</a>";
107:
108: if (folder == null) {
109: return foldersLink;
110: }
111:
112: String breadcrumbs = StringPool.BLANK;
113:
114: if (folder != null) {
115: for (int i = 0;; i++) {
116: folder = folder.toEscapedModel();
117:
118: PortletURL portletURL = res.createRenderURL();
119:
120: if (windowState.equals(LiferayWindowState.POP_UP)) {
121: portletURL
122: .setWindowState(LiferayWindowState.POP_UP);
123:
124: String strutsAction = ParamUtil.getString(req,
125: "struts_action");
126:
127: portletURL.setParameter("struts_action",
128: strutsAction);
129: portletURL.setParameter("groupId", String
130: .valueOf(groupId));
131: portletURL.setParameter("folderId", String
132: .valueOf(folder.getFolderId()));
133: } else {
134: portletURL.setWindowState(WindowState.MAXIMIZED);
135:
136: portletURL.setParameter("struts_action",
137: "/document_library/view");
138: portletURL.setParameter("folderId", String
139: .valueOf(folder.getFolderId()));
140: }
141:
142: String folderLink = "<a href=\""
143: + portletURL.toString() + "\">"
144: + folder.getName() + "</a>";
145:
146: if (i == 0) {
147: breadcrumbs = folderLink;
148: } else {
149: breadcrumbs = folderLink + " » "
150: + breadcrumbs;
151: }
152:
153: if (folder.isRoot()) {
154: break;
155: }
156:
157: folder = DLFolderLocalServiceUtil.getFolder(folder
158: .getParentFolderId());
159: }
160: }
161:
162: breadcrumbs = foldersLink + " » " + breadcrumbs;
163:
164: if (fileEntry != null) {
165: fileEntry = fileEntry.toEscapedModel();
166:
167: PortletURL fileEntryURL = res.createRenderURL();
168:
169: fileEntryURL.setWindowState(WindowState.MAXIMIZED);
170:
171: fileEntryURL.setParameter("struts_action",
172: "/document_library/edit_file_entry");
173: fileEntryURL.setParameter("folderId", String
174: .valueOf(fileEntry.getFolderId()));
175: fileEntryURL.setParameter("name", fileEntry.getName());
176:
177: String fileEntryLink = "<a href=\""
178: + fileEntryURL.toString() + "\">"
179: + fileEntry.getName() + "</a>";
180:
181: breadcrumbs = breadcrumbs + " » " + fileEntryLink;
182: }
183:
184: return breadcrumbs;
185: }
186:
187: public static String getFileExtension(String name) {
188: return _instance._getFileExtension(name);
189: }
190:
191: public static String getLockId(long folderId, String name) {
192: StringMaker sm = new StringMaker();
193:
194: sm.append(folderId);
195: sm.append(StringPool.POUND);
196: sm.append(name);
197:
198: return sm.toString();
199: }
200:
201: private DLUtil() {
202: _fileExtensions = CollectionFactory.getHashSet();
203:
204: String[] fileExtensions = PropsValues.DL_FILE_EXTENSIONS;
205:
206: for (int i = 0; i < fileExtensions.length; i++) {
207:
208: // Only process non wildcard extensions
209:
210: if (!StringPool.STAR.equals(fileExtensions[i])) {
211:
212: // Strip starting period
213:
214: String extension = fileExtensions[i];
215: extension = extension.substring(1, extension.length());
216:
217: _fileExtensions.add(extension);
218: }
219: }
220: }
221:
222: private String _getFileExtension(String name) {
223: String extension = StringPool.BLANK;
224:
225: int pos = name.lastIndexOf(StringPool.PERIOD);
226:
227: if (pos != -1) {
228: extension = name.substring(pos + 1, name.length())
229: .toLowerCase();
230: }
231:
232: if (!_fileExtensions.contains(extension)) {
233: extension = _DEFAULT_FILE_EXTENSION;
234: }
235:
236: return extension;
237: }
238:
239: private static final String _DEFAULT_FILE_EXTENSION = "page";
240:
241: private static DLUtil _instance = new DLUtil();
242:
243: private Set _fileExtensions;
244:
245: }
|