/portal-impl/src/com/liferay/portlet/documentlibrary/action/FindFileEntryAction.java

https://github.com/spreddy/liferay-portal · Java · 146 lines · 97 code · 33 blank · 16 comment · 11 complexity · 16ee730482dc2a2bbf06d378868bb4fc MD5 · raw file

  1. /**
  2. * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. package com.liferay.portlet.documentlibrary.action;
  15. import com.liferay.portal.NoSuchLayoutException;
  16. import com.liferay.portal.kernel.repository.model.FileEntry;
  17. import com.liferay.portal.kernel.util.ParamUtil;
  18. import com.liferay.portal.model.Layout;
  19. import com.liferay.portal.model.LayoutConstants;
  20. import com.liferay.portal.model.LayoutTypePortlet;
  21. import com.liferay.portal.service.LayoutLocalServiceUtil;
  22. import com.liferay.portal.util.PortalUtil;
  23. import com.liferay.portal.util.PortletKeys;
  24. import com.liferay.portlet.PortletURLImpl;
  25. import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
  26. import javax.portlet.PortletMode;
  27. import javax.portlet.PortletRequest;
  28. import javax.portlet.PortletURL;
  29. import javax.portlet.WindowState;
  30. import javax.servlet.http.HttpServletRequest;
  31. import javax.servlet.http.HttpServletResponse;
  32. import org.apache.struts.action.Action;
  33. import org.apache.struts.action.ActionForm;
  34. import org.apache.struts.action.ActionForward;
  35. import org.apache.struts.action.ActionMapping;
  36. /**
  37. * @author Ryan Park
  38. */
  39. public class FindFileEntryAction extends Action {
  40. @Override
  41. public ActionForward execute(
  42. ActionMapping mapping, ActionForm form, HttpServletRequest request,
  43. HttpServletResponse response)
  44. throws Exception {
  45. try {
  46. long plid = ParamUtil.getLong(request, "p_l_id");
  47. long fileEntryId = ParamUtil.getLong(request, "fileEntryId");
  48. plid = getPlid(plid, fileEntryId);
  49. PortletURL portletURL = new PortletURLImpl(
  50. request, getPortletId(plid), plid, PortletRequest.RENDER_PHASE);
  51. portletURL.setWindowState(WindowState.NORMAL);
  52. portletURL.setPortletMode(PortletMode.VIEW);
  53. portletURL.setParameter(
  54. "struts_action", "/document_library/view_file_entry");
  55. portletURL.setParameter("fileEntryId", String.valueOf(fileEntryId));
  56. response.sendRedirect(portletURL.toString());
  57. return null;
  58. }
  59. catch (Exception e) {
  60. PortalUtil.sendError(e, request, response);
  61. return null;
  62. }
  63. }
  64. protected long getPlid(long plid, long fileEntryId) throws Exception {
  65. if (plid != LayoutConstants.DEFAULT_PLID) {
  66. try {
  67. Layout layout = LayoutLocalServiceUtil.getLayout(plid);
  68. LayoutTypePortlet layoutTypePortlet =
  69. (LayoutTypePortlet)layout.getLayoutType();
  70. if (layoutTypePortlet.hasPortletId(
  71. PortletKeys.DOCUMENT_LIBRARY) ||
  72. layoutTypePortlet.hasPortletId(
  73. PortletKeys.DOCUMENT_LIBRARY_DISPLAY) ||
  74. layoutTypePortlet.hasPortletId(
  75. PortletKeys.IMAGE_GALLERY_DISPLAY)) {
  76. return plid;
  77. }
  78. }
  79. catch (NoSuchLayoutException nsle) {
  80. }
  81. }
  82. FileEntry fileEntry = DLAppLocalServiceUtil.getFileEntry(fileEntryId);
  83. plid = PortalUtil.getPlidFromPortletId(
  84. fileEntry.getRepositoryId(), PortletKeys.DOCUMENT_LIBRARY);
  85. if (plid != LayoutConstants.DEFAULT_PLID) {
  86. return plid;
  87. }
  88. plid = PortalUtil.getPlidFromPortletId(
  89. fileEntry.getRepositoryId(), PortletKeys.DOCUMENT_LIBRARY_DISPLAY);
  90. if (plid != LayoutConstants.DEFAULT_PLID) {
  91. return plid;
  92. }
  93. plid = PortalUtil.getPlidFromPortletId(
  94. fileEntry.getRepositoryId(), PortletKeys.IMAGE_GALLERY_DISPLAY);
  95. if (plid != LayoutConstants.DEFAULT_PLID) {
  96. return plid;
  97. }
  98. throw new NoSuchLayoutException(
  99. "No page was found with the Document Library portlet");
  100. }
  101. protected String getPortletId(long plid) throws Exception {
  102. Layout layout = LayoutLocalServiceUtil.getLayout(plid);
  103. LayoutTypePortlet layoutTypePortlet =
  104. (LayoutTypePortlet)layout.getLayoutType();
  105. for (String portletId : layoutTypePortlet.getPortletIds()) {
  106. if (portletId.startsWith(PortletKeys.DOCUMENT_LIBRARY) ||
  107. portletId.startsWith(PortletKeys.DOCUMENT_LIBRARY_DISPLAY)) {
  108. return portletId;
  109. }
  110. }
  111. return PortletKeys.DOCUMENT_LIBRARY;
  112. }
  113. }