Utils.java

  1. /*
  2.  * Copyright (C) 2003-2010 eXo Platform SAS.
  3.  *
  4.  * This program is free software; you can redistribute it and/or
  5.  * modify it under the terms of the GNU Affero General Public License
  6.  * as published by the Free Software Foundation; either version 3
  7.  * of the License, or (at your option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, see<http://www.gnu.org/licenses/>.
  16.  */
  17. package org.exoplatform.wiki.commons;

  18. import java.net.URL;
  19. import java.net.URLEncoder;
  20. import java.util.*;
  21. import java.util.Map.Entry;

  22. import javax.portlet.PortletPreferences;
  23. import javax.servlet.http.HttpServletRequest;
  24. import javax.servlet.http.HttpSession;

  25. import org.apache.commons.lang.StringUtils;
  26. import org.xwiki.context.Execution;
  27. import org.xwiki.context.ExecutionContext;
  28. import org.xwiki.rendering.syntax.Syntax;

  29. import org.exoplatform.commons.utils.MimeTypeResolver;
  30. import org.exoplatform.container.ExoContainer;
  31. import org.exoplatform.container.ExoContainerContext;
  32. import org.exoplatform.container.PortalContainer;
  33. import org.exoplatform.portal.application.PortalRequestContext;
  34. import org.exoplatform.portal.config.model.PortalConfig;
  35. import org.exoplatform.portal.webui.portal.UIPortal;
  36. import org.exoplatform.portal.webui.util.Util;
  37. import org.exoplatform.services.organization.OrganizationService;
  38. import org.exoplatform.services.organization.User;
  39. import org.exoplatform.services.organization.UserStatus;
  40. import org.exoplatform.services.security.ConversationState;
  41. import org.exoplatform.services.security.Identity;
  42. import org.exoplatform.services.security.IdentityConstants;
  43. import org.exoplatform.social.core.space.model.Space;
  44. import org.exoplatform.social.core.space.spi.SpaceService;
  45. import org.exoplatform.webui.application.WebuiRequestContext;
  46. import org.exoplatform.webui.application.portlet.PortletRequestContext;
  47. import org.exoplatform.webui.core.UIComponent;
  48. import org.exoplatform.webui.event.Event;
  49. import org.exoplatform.webui.form.UIForm;
  50. import org.exoplatform.webui.form.UIFormTextAreaInput;
  51. import org.exoplatform.wiki.mow.api.*;
  52. import org.exoplatform.wiki.rendering.RenderingService;
  53. import org.exoplatform.wiki.rendering.impl.RenderingServiceImpl;
  54. import org.exoplatform.wiki.resolver.PageResolver;
  55. import org.exoplatform.wiki.service.WikiContext;
  56. import org.exoplatform.wiki.service.WikiPageParams;
  57. import org.exoplatform.wiki.service.WikiService;
  58. import org.exoplatform.wiki.service.impl.SessionManager;
  59. import org.exoplatform.wiki.tree.utils.TreeUtils;
  60. import org.exoplatform.wiki.webui.UIWikiPageEditForm;
  61. import org.exoplatform.wiki.webui.UIWikiPortlet;
  62. import org.exoplatform.wiki.webui.UIWikiRichTextArea;
  63. import org.exoplatform.wiki.webui.WikiMode;

  64. public class Utils {

  65.   public static final int    DEFAULT_VALUE_UPLOAD_PORTAL = -1;

  66.   public static final String SLASH                       = "/";

  67.   public static final String DRAFT_ID                    = "draftId";

  68.   public static String upperFirstCharacter(String str) {
  69.     if (StringUtils.isEmpty(str)) {
  70.       return str;
  71.     }
  72.     return str.substring(0, 1).toUpperCase() + str.substring(1);
  73.   }

  74.   public static String getCurrentSpaceName() throws Exception {
  75.     Wiki currentSpace = Utils.getCurrentWiki();
  76.     if (currentSpace == null) {
  77.       return StringUtils.EMPTY;
  78.     }
  79.     return getSpaceName(currentSpace);
  80.   }

  81.   public static String getSpaceName(Wiki wiki) throws Exception {
  82.     WikiType wikiType = WikiType.valueOf(wiki.getType().toUpperCase());
  83.     if (WikiType.PORTAL.equals(wikiType)) {
  84.       String displayName = wiki.getOwner();
  85.       int slashIndex = displayName.lastIndexOf('/');
  86.       if (slashIndex > -1) {
  87.         displayName = displayName.substring(slashIndex + 1);
  88.       }
  89.       return Utils.upperFirstCharacter(displayName);
  90.     }

  91.     if (WikiType.USER.equals(wikiType)) {
  92.       String currentUser = org.exoplatform.wiki.utils.Utils.getCurrentUser();
  93.       if (wiki.getOwner().equals(currentUser)) {
  94.         WebuiRequestContext context = WebuiRequestContext.getCurrentInstance();
  95.         ResourceBundle res = context.getApplicationResourceBundle();
  96.         String mySpaceLabel = res.getString("UISpaceSwitcher.title.my-space");
  97.         return mySpaceLabel;
  98.       }
  99.       return wiki.getOwner();
  100.     }

  101.     WikiService wikiService = (WikiService) PortalContainer.getComponent(WikiService.class);
  102.     return wikiService.getSpaceNameByGroupId(wiki.getOwner());
  103.   }

  104.   public static String getCurrentRequestURL() throws Exception {
  105.     PortalRequestContext portalRequestContext = Util.getPortalRequestContext();
  106.     HttpServletRequest request = portalRequestContext.getRequest();
  107.     String requestURL = java.net.URLDecoder.decode(request.getRequestURL().toString(), "UTF-8");
  108.     UIPortal uiPortal = Util.getUIPortal();
  109.     String pageNodeSelected = uiPortal.getSelectedUserNode().getURI();
  110.     if (!requestURL.contains(pageNodeSelected)) {
  111.       // Happens at the first time processRender() called when add wiki portlet
  112.       // manually
  113.       requestURL = portalRequestContext.getPortalURI() + pageNodeSelected;
  114.     }
  115.     return requestURL;
  116.   }

  117.   public static WikiPageParams getCurrentWikiPageParams() throws Exception {
  118.     String requestURL = getCurrentRequestURL();
  119.     PageResolver pageResolver = (PageResolver) PortalContainer.getComponent(PageResolver.class);
  120.     WikiPageParams params = pageResolver.extractWikiPageParams(requestURL, Util.getUIPortal().getSelectedUserNode());
  121.     HttpServletRequest request = Util.getPortalRequestContext().getRequest();
  122.     Map<String, String[]> paramsMap = request.getParameterMap();
  123.     params.setParameters(paramsMap);
  124.     return params;
  125.   }

  126.   /**
  127.    * Gets current wiki page directly from data base
  128.    *
  129.    * @return current wiki page
  130.    * @throws Exception
  131.    */
  132.   public static Page getCurrentWikiPage() throws Exception {
  133.     String requestURL = Utils.getCurrentRequestURL();
  134.     PageResolver pageResolver = (PageResolver) PortalContainer.getComponent(PageResolver.class);
  135.     Page page = pageResolver.resolve(requestURL, Util.getUIPortal().getSelectedUserNode());
  136.     return page;
  137.   }

  138.   public static boolean canModifyPagePermission() throws Exception {
  139.     WikiService wikiService = (WikiService) PortalContainer.getComponent(WikiService.class);
  140.     String currentUser = org.exoplatform.wiki.utils.Utils.getCurrentUser();
  141.     Page currentPage = Utils.getCurrentWikiPage();
  142.     if (currentPage == null) {
  143.       return false;
  144.     }
  145.     return wikiService.canModifyPagePermission(currentPage, currentUser);
  146.   }

  147.   public static boolean isPagePublic(Page page) throws Exception {
  148.     WikiService wikiService = (WikiService) PortalContainer.getComponent(WikiService.class);
  149.     return (page != null)
  150.         && wikiService.hasPermissionOnPage(page, PermissionType.EDITPAGE, new Identity(IdentityConstants.ANONIM));
  151.   }

  152.   public static boolean isCurrentPagePublic() throws Exception {
  153.     Page currentPage = Utils.getCurrentWikiPage();
  154.     return isPagePublic(currentPage);
  155.   }

  156.   public static String getSpaceHomeURL(String spaceGroupId) {
  157.     SpaceService spaceService = ExoContainerContext.getCurrentContainer().getComponentInstanceOfType(SpaceService.class);
  158.     Space space = spaceService.getSpaceByGroupId(spaceGroupId);
  159.     String spaceLink = org.exoplatform.social.webui.Utils.getSpaceHomeURL(space);
  160.     return spaceLink;
  161.   }

  162.   public static String getURLFromParams(WikiPageParams params) throws Exception {
  163.     if (StringUtils.isEmpty(params.getType()) || StringUtils.isEmpty(params.getOwner())) {
  164.       return StringUtils.EMPTY;
  165.     }

  166.     if (params.getType().equals(PortalConfig.GROUP_TYPE)) {
  167.       StringBuilder spaceUrl = new StringBuilder(getSpaceHomeURL(params.getOwner()));
  168.       if (!spaceUrl.toString().endsWith("/")) {
  169.         spaceUrl.append("/");
  170.       }
  171.       // spaceUrl.append("wiki/");
  172.       spaceUrl.append(getWikiAppNameInSpace(params.getOwner())).append("/");
  173.       if (!StringUtils.isEmpty(params.getPageName())) {
  174.         spaceUrl.append(params.getPageName());
  175.       }
  176.       return spaceUrl.toString();
  177.     }
  178.     return org.exoplatform.wiki.utils.Utils.getPermanlink(params, false);
  179.   }

  180.   private static String getWikiAppNameInSpace(String spaceId) {
  181.     SpaceService spaceService = org.exoplatform.wiki.rendering.util.Utils.getService(SpaceService.class);
  182.     Space space = spaceService.getSpaceByGroupId(spaceId);
  183.     String apps = space.getApp();
  184.     if (apps != null) {
  185.       for (String app : apps.split(",")) {
  186.         String[] appInfos = app.split(":");
  187.         if (appInfos.length > 1 && "WikiPortlet".equals(appInfos[0])) {
  188.           return appInfos[1];
  189.         }
  190.       }
  191.     }
  192.     return "wiki";
  193.   }

  194.   public static Page getCurrentNewDraftWikiPage() throws Exception {
  195.     WikiService wikiService = (WikiService) PortalContainer.getComponent(WikiService.class);
  196.     return wikiService.getExsitedOrNewDraftPageById(null, null, org.exoplatform.wiki.utils.Utils.getPageNameForAddingPage());
  197.   }

  198.   public static String getExtension(String filename) throws Exception {
  199.     MimeTypeResolver mimeResolver = new MimeTypeResolver();
  200.     try {
  201.       return mimeResolver.getExtension(mimeResolver.getMimeType(filename));
  202.     } catch (Exception e) {
  203.       return mimeResolver.getDefaultMimeType();
  204.     }
  205.   }

  206.   public static Wiki getCurrentWiki() throws Exception {
  207.     WikiService wikiService = (WikiService) PortalContainer.getComponent(WikiService.class);
  208.     WikiPageParams params = Utils.getCurrentWikiPageParams();
  209.     return wikiService.getWikiByTypeAndOwner(params.getType(), params.getOwner());
  210.   }

  211.   public static WikiContext setUpWikiContext(UIWikiPortlet wikiPortlet) throws Exception {
  212.     RenderingService renderingService = (RenderingService) ExoContainerContext.getCurrentContainer()
  213.                                                                               .getComponentInstanceOfType(RenderingService.class);
  214.     Execution ec = ((RenderingServiceImpl) renderingService).getExecution();
  215.     if (ec.getContext() == null) {
  216.       ec.setContext(new ExecutionContext());
  217.     }
  218.     WikiContext wikiContext = createWikiContext(wikiPortlet);
  219.     ec.getContext().setProperty(WikiContext.WIKICONTEXT, wikiContext);
  220.     return wikiContext;
  221.   }

  222.   public static void feedDataForWYSIWYGEditor(UIWikiPageEditForm pageEditForm, String markup) throws Exception {
  223.     UIWikiPortlet wikiPortlet = pageEditForm.getAncestorOfType(UIWikiPortlet.class);
  224.     UIWikiRichTextArea richTextArea = pageEditForm.getChild(UIWikiRichTextArea.class);
  225.     RenderingService renderingService = (RenderingService) PortalContainer.getComponent(RenderingService.class);
  226.     HttpSession session = Util.getPortalRequestContext().getRequest().getSession(false);
  227.     UIFormTextAreaInput markupInput = pageEditForm.getUIFormTextAreaInput(UIWikiPageEditForm.FIELD_CONTENT);
  228.     String markupSyntax = getDefaultSyntax();
  229.     WikiContext wikiContext = Utils.setUpWikiContext(wikiPortlet);
  230.     if (markup == null) {
  231.       markup = (markupInput.getValue() == null) ? "" : markupInput.getValue();
  232.     }
  233.     String xhtmlContent = renderingService.render(markup, markupSyntax, Syntax.ANNOTATED_XHTML_1_0.toIdString(), false);
  234.     richTextArea.getUIFormTextAreaInput().setValue(xhtmlContent);
  235.     session.setAttribute(UIWikiRichTextArea.SESSION_KEY, xhtmlContent);
  236.     session.setAttribute(UIWikiRichTextArea.WIKI_CONTEXT, wikiContext);
  237.     ExoContainer container = ExoContainerContext.getCurrentContainer();
  238.     SessionManager sessionManager = (SessionManager) container.getComponentInstanceOfType(SessionManager.class);
  239.     sessionManager.addSessionContext(session.getId(), Utils.createWikiContext(wikiPortlet));

  240.     sessionManager.addSessionContext(ConversationState.getCurrent().getIdentity().getUserId(),
  241.                                      Utils.createWikiContext(wikiPortlet));
  242.     if (sessionManager.getSessionContainer(session.getId()) != null) {
  243.       sessionManager.addSessionContainer(ConversationState.getCurrent().getIdentity().getUserId(),
  244.                                          sessionManager.getSessionContainer(session.getId()));
  245.     }

  246.   }

  247.   public static String getCurrentWikiPagePath() throws Exception {
  248.     return TreeUtils.getPathFromPageParams(getCurrentWikiPageParams());
  249.   }

  250.   public static String getDefaultSyntax() throws Exception {
  251.     WikiPreferences currentPreferences = Utils.getCurrentPreferences();
  252.     String currentDefaultSyntax;
  253.     if (currentPreferences != null) {
  254.       currentDefaultSyntax = currentPreferences.getWikiPreferencesSyntax().getDefaultSyntax();
  255.       if (currentDefaultSyntax == null) {
  256.         WikiService wservice = (WikiService) PortalContainer.getComponent(WikiService.class);
  257.         currentDefaultSyntax = wservice.getDefaultWikiSyntaxId();
  258.       }
  259.     } else {
  260.       WikiService wikiService = (WikiService) PortalContainer.getComponent(WikiService.class);
  261.       currentDefaultSyntax = wikiService.getDefaultWikiSyntaxId();
  262.     }
  263.     return currentDefaultSyntax;
  264.   }

  265.   public static WikiPreferences getCurrentPreferences() throws Exception {
  266.     Wiki currentWiki = getCurrentWiki();
  267.     return currentWiki.getPreferences();
  268.   }

  269.   public static WikiContext createWikiContext(UIWikiPortlet wikiPortlet) throws Exception {
  270.     PortalRequestContext portalRequestContext = Util.getPortalRequestContext();
  271.     WikiMode currentMode = wikiPortlet.getWikiMode();
  272.     List<WikiMode> editModes = Arrays.asList(new WikiMode[] { WikiMode.EDITPAGE, WikiMode.ADDPAGE, WikiMode.EDITTEMPLATE,
  273.         WikiMode.ADDTEMPLATE });
  274.     UIPortal uiPortal = Util.getUIPortal();
  275.     String portalURI = portalRequestContext.getPortalURI();
  276.     URL requestURL = new URL(portalRequestContext.getRequest().getRequestURL().toString());
  277.     String domainURL = requestURL.getProtocol() + "://" + requestURL.getAuthority();
  278.     String portalURL = domainURL + portalURI;
  279.     String pageNodeSelected = uiPortal.getSelectedUserNode().getURI();
  280.     String treeRestURL = getCurrentRestURL().concat("/wiki/tree/children/");

  281.     WikiContext wikiContext = new WikiContext();
  282.     wikiContext.setPortalURL(portalURL);
  283.     wikiContext.setTreeRestURI(treeRestURL);
  284.     wikiContext.setRestURI(getCurrentRestURL());
  285.     wikiContext.setRedirectURI(wikiPortlet.getRedirectURL());
  286.     wikiContext.setPortletURI(pageNodeSelected);
  287.     WikiPageParams params = Utils.getCurrentWikiPageParams();
  288.     wikiContext.setType(params.getType());
  289.     wikiContext.setOwner(params.getOwner());
  290.     if (editModes.contains(currentMode)) {
  291.       wikiContext.setSyntax(getDefaultSyntax());
  292.     } else {
  293.       WikiService service = (WikiService) PortalContainer.getComponent(WikiService.class);
  294.       Page currentPage = service.getPageOfWikiByName(params.getType(), params.getOwner(), params.getPageName());
  295.       if (currentPage != null) {
  296.         wikiContext.setSyntax(currentPage.getSyntax());
  297.       }
  298.     }
  299.     if (wikiPortlet.getWikiMode() == WikiMode.ADDPAGE) {
  300.       wikiContext.setPageName(org.exoplatform.wiki.utils.Utils.getPageNameForAddingPage());
  301.     } else {
  302.       wikiContext.setPageName(params.getPageName());
  303.     }
  304.     wikiContext.setBaseUrl(getBaseUrl());
  305.     return wikiContext;
  306.   }

  307.   public static String getBaseUrl() throws Exception {
  308.     WikiPageParams params = getCurrentWikiPageParams();
  309.     params.setPageName(null);
  310.     return getURLFromParams(params);
  311.   }

  312.   public static String getCurrentWikiNodeUri() throws Exception {
  313.     PortalRequestContext portalRequestContext = Util.getPortalRequestContext();
  314.     StringBuilder sb = new StringBuilder(portalRequestContext.getPortalURI());
  315.     UIPortal uiPortal = Util.getUIPortal();
  316.     String pageNodeSelected = uiPortal.getSelectedUserNode().getURI();
  317.     sb.append(pageNodeSelected);
  318.     return sb.toString();
  319.   }

  320.   public static void redirect(WikiPageParams pageParams, WikiMode mode) throws Exception {
  321.     redirect(pageParams, mode, null);
  322.   }

  323.   /**
  324.    * Get the full path for current wiki page
  325.    */
  326.   public static String getPageLink() throws Exception {
  327.     WikiPageParams params = getCurrentWikiPageParams();
  328.     params.setPageName(null);
  329.     if (PortalConfig.PORTAL_TYPE.equals(params.getType())) {
  330.       String navigationURI = Util.getUIPortal().getNavPath().getURI();
  331.       String requestURI = Util.getPortalRequestContext().getRequestURI();
  332.       if (requestURI.indexOf(navigationURI) < 0) {
  333.         navigationURI = "wiki";
  334.       }
  335.       return requestURI.substring(0, requestURI.indexOf(navigationURI) + navigationURI.length()) + "/";
  336.     }
  337.     return getURLFromParams(params);
  338.   }

  339.   public static void redirect(WikiPageParams pageParams, WikiMode mode, Map<String, String[]> params) throws Exception {
  340.     PortalRequestContext portalRequestContext = Util.getPortalRequestContext();
  341.     portalRequestContext.setResponseComplete(true);
  342.     if (PortalConfig.GROUP_TYPE.equals(Utils.getCurrentWiki().getType())) {
  343.       pageParams.setPageName(URLEncoder.encode(pageParams.getPageName(), "UTF-8"));
  344.     }
  345.     portalRequestContext.sendRedirect(createURLWithMode(pageParams, mode, params));
  346.   }

  347.   public static void redirect(String url) throws Exception {
  348.     PortalRequestContext portalRequestContext = Util.getPortalRequestContext();
  349.     portalRequestContext.setResponseComplete(true);
  350.     portalRequestContext.sendRedirect(url);
  351.   }

  352.   public static void ajaxRedirect(Event<? extends UIComponent> event,
  353.                                   WikiPageParams pageParams,
  354.                                   WikiMode mode,
  355.                                   Map<String, String[]> params) throws Exception {
  356.     String redirectLink = Utils.createURLWithMode(pageParams, mode, params);
  357.     ajaxRedirect(event, redirectLink);
  358.   }

  359.   public static void ajaxRedirect(Event<? extends UIComponent> event, String redirectLink) throws Exception {
  360.     event.getRequestContext()
  361.          .getJavascriptManager()
  362.          .addCustomizedOnLoadScript("eXo.wiki.UIWikiPortlet.ajaxRedirect('" + redirectLink + "');");
  363.   }

  364.   public static String createURLWithMode(WikiPageParams pageParams, WikiMode mode, Map<String, String[]> params) throws Exception {
  365.     StringBuffer sb = new StringBuffer();
  366.     sb.append(getURLFromParams(pageParams));
  367.     // sb.append(getPageLink());
  368.     // if(!StringUtils.isEmpty(pageParams.getPageName())){
  369.     // sb.append(URLEncoder.encode(pageParams.getPageName(), "UTF-8"));
  370.     // }
  371.     if (!mode.equals(WikiMode.VIEW)) {
  372.       sb.append("#").append(Utils.getActionFromWikiMode(mode));
  373.     }
  374.     if (params != null) {
  375.       Iterator<Entry<String, String[]>> iter = params.entrySet().iterator();
  376.       while (iter.hasNext()) {
  377.         Entry<String, String[]> entry = iter.next();
  378.         sb.append("&");
  379.         sb.append(entry.getKey()).append("=").append(entry.getValue()[0]);
  380.       }
  381.     }
  382.     return sb.toString();
  383.   }

  384.   public static String createFormActionLink(UIComponent uiComponent, String action, String beanId) throws Exception {
  385.     WebuiRequestContext context = WebuiRequestContext.getCurrentInstance();
  386.     boolean isForm = UIForm.class.isInstance(uiComponent);
  387.     UIForm form = isForm ? (UIForm) uiComponent : uiComponent.getAncestorOfType(UIForm.class);
  388.     if (form != null) {
  389.       String formId = form.getId();
  390.       if (context instanceof PortletRequestContext) {
  391.         formId = ((PortletRequestContext) context).getWindowId() + "#" + formId;
  392.       }
  393.       StringBuilder b = new StringBuilder();

  394.       b.append("javascript:eXo.wiki.UIForm.submitPageEvent('").append(formId).append("','");
  395.       b.append(action).append("','");
  396.       if (!isForm) {
  397.         b.append("&amp;").append(UIForm.SUBCOMPONENT_ID).append("=").append(uiComponent.getId());
  398.         if (beanId != null) {
  399.           b.append("&amp;").append(UIComponent.OBJECTID).append("=").append(beanId);
  400.         }
  401.       }
  402.       b.append("')");
  403.       return b.toString();
  404.     } else {
  405.       return form.event(action, uiComponent.getId(), action);
  406.     }
  407.   }

  408.   public static String getActionFromWikiMode(WikiMode mode) {
  409.     switch (mode) {
  410.     case EDITPAGE:
  411.       return "EditPage";
  412.     case ADDPAGE:
  413.       return "AddPage";
  414.     case ADDTEMPLATE:
  415.       return "AddTemplate";
  416.     case EDITTEMPLATE:
  417.       return "EditTemplate";
  418.     case SPACESETTING:
  419.       return "SpaceSetting";
  420.     case MYDRAFTS:
  421.       return "MyDrafts";
  422.     default:
  423.       return "";
  424.     }
  425.   }

  426.   public static String getCurrentRestURL() {
  427.     StringBuilder sb = new StringBuilder();
  428.     sb.append("/").append(PortalContainer.getCurrentPortalContainerName()).append("/");
  429.     sb.append(PortalContainer.getCurrentRestContextName());
  430.     return sb.toString();
  431.   }

  432.   public static WikiMode getModeFromAction(String actionParam) {
  433.     String[] params = actionParam.split(WikiConstants.WITH);
  434.     String name = params[0];
  435.     if (name != null) {
  436.       try {
  437.         WikiMode mode = WikiMode.valueOf(name.toUpperCase());
  438.         if (mode != null)
  439.           return mode;
  440.       } catch (IllegalArgumentException e) {
  441.         return null;
  442.       }
  443.     }
  444.     return null;
  445.   }

  446.   /**
  447.    * render macro to XHtml string.
  448.    *
  449.    * @param uiComponent - component that contain the macro.
  450.    * @param macroName - name of macro
  451.    * @param wikiSyntax - wiki syntax referred from {@link Syntax}
  452.    * @return String in format {@link Syntax#XHTML_1_0}
  453.    */
  454.   public static String renderMacroToXHtml(UIComponent uiComponent, String macroName, String wikiSyntax) {
  455.     try {
  456.       RenderingService renderingService = (RenderingService) PortalContainer.getComponent(RenderingService.class);
  457.       setUpWikiContext(uiComponent.getAncestorOfType(UIWikiPortlet.class));
  458.       String content = renderingService.render(macroName, wikiSyntax, Syntax.XHTML_1_0.toIdString(), false);
  459.       return content;
  460.     } catch (Exception e) {
  461.       return "";
  462.     }
  463.   }

  464.   public static void removeWikiContext() throws Exception {
  465.     RenderingService renderingService = (RenderingService) PortalContainer.getComponent(RenderingService.class);
  466.     Execution ec = ((RenderingServiceImpl) renderingService).getExecution();
  467.     if (ec != null) {
  468.       ec.removeContext();
  469.     }
  470.   }

  471.   public static int getLimitUploadSize() {
  472.     PortletRequestContext pcontext = (PortletRequestContext) WebuiRequestContext.getCurrentInstance();
  473.     PortletPreferences portletPref = pcontext.getRequest().getPreferences();
  474.     int limitMB = DEFAULT_VALUE_UPLOAD_PORTAL;
  475.     try {
  476.       limitMB = Integer.parseInt(portletPref.getValue("uploadFileSizeLimitMB", "").trim());
  477.     } catch (Exception e) {
  478.       limitMB = 10;
  479.     }
  480.     return limitMB;
  481.   }

  482.   public static String getFullName(String userId) {
  483.     try {
  484.       OrganizationService organizationService = (OrganizationService) PortalContainer.getComponent(OrganizationService.class);
  485.       User user = organizationService.getUserHandler().findUserByName(userId, UserStatus.ANY);
  486.       return user.getFullName();
  487.     } catch (Exception e) {
  488.       WebuiRequestContext context = WebuiRequestContext.getCurrentInstance();
  489.       ResourceBundle res = context.getApplicationResourceBundle();
  490.       return res.getString("UIWikiPortlet.label.Anonymous");
  491.     }
  492.   }

  493.   public static String getDraftIdSessionKey() {
  494.     return ConversationState.getCurrent().getIdentity().getUserId() + DRAFT_ID;
  495.   }

  496.   public static String getWikiTypeFromWikiId(String wikiId) {
  497.     String wikiType = "";
  498.     if (wikiId.startsWith("/spaces/")) {
  499.       wikiType = PortalConfig.GROUP_TYPE;
  500.     } else if (wikiId.startsWith("/user/")) {
  501.       wikiType = PortalConfig.USER_TYPE;
  502.     } else {
  503.       if (wikiId.startsWith("/")) {
  504.         wikiType = PortalConfig.PORTAL_TYPE;
  505.       }
  506.     }
  507.     return wikiType;
  508.   }

  509.   public static String getWikiOwnerFromWikiId(String wikiId) {
  510.     String wikiType = getWikiTypeFromWikiId(wikiId);
  511.     if (PortalConfig.USER_TYPE.equals(wikiType) || PortalConfig.PORTAL_TYPE.equals(wikiType)) {
  512.       wikiId = wikiId.substring(wikiId.lastIndexOf('/') + 1);
  513.     }
  514.     return wikiId;
  515.   }
  516. }