Example usage for com.liferay.portal.kernel.portlet PortletRequestModel getParameters

List of usage examples for com.liferay.portal.kernel.portlet PortletRequestModel getParameters

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.portlet PortletRequestModel getParameters.

Prototype

public Map<String, String[]> getParameters() 

Source Link

Usage

From source file:com.liferay.journal.service.impl.JournalArticleLocalServiceImpl.java

License:Open Source License

protected JournalArticleDisplay getArticleDisplay(JournalArticle article, String ddmTemplateKey,
        String viewMode, String languageId, int page, PortletRequestModel portletRequestModel,
        ThemeDisplay themeDisplay, boolean propagateException) throws PortalException {

    String content = null;/*  www.  jav a 2  s. com*/

    if (page < 1) {
        page = 1;
    }

    int numberOfPages = 1;
    boolean paginate = false;
    boolean pageFlow = false;

    boolean cacheable = true;

    Map<String, String> tokens = JournalUtil.getTokens(article.getGroupId(), portletRequestModel, themeDisplay);

    if ((themeDisplay == null) && (portletRequestModel == null)) {
        tokens.put("company_id", String.valueOf(article.getCompanyId()));

        Group companyGroup = groupLocalService.getCompanyGroup(article.getCompanyId());

        tokens.put("article_group_id", String.valueOf(article.getGroupId()));
        tokens.put("company_group_id", String.valueOf(companyGroup.getGroupId()));

        // Deprecated tokens

        tokens.put("group_id", String.valueOf(article.getGroupId()));
    }

    tokens.put(TemplateConstants.CLASS_NAME_ID,
            String.valueOf(classNameLocalService.getClassNameId(DDMStructure.class)));
    tokens.put("article_resource_pk", String.valueOf(article.getResourcePrimKey()));

    DDMStructure ddmStructure = article.getDDMStructure();

    tokens.put("ddm_structure_key", String.valueOf(ddmStructure.getStructureKey()));
    tokens.put("ddm_structure_id", String.valueOf(ddmStructure.getStructureId()));

    // Deprecated token

    tokens.put("structure_id", article.getDDMStructureKey());

    String defaultDDMTemplateKey = article.getDDMTemplateKey();

    if (Validator.isNull(ddmTemplateKey)) {
        ddmTemplateKey = defaultDDMTemplateKey;
    }

    Document document = article.getDocument();

    document = document.clone();

    Element rootElement = document.getRootElement();

    List<Element> pages = rootElement.elements("page");

    if (!pages.isEmpty()) {
        pageFlow = true;

        String targetPage = null;

        Map<String, String[]> parameters = portletRequestModel.getParameters();

        if (parameters != null) {
            String[] values = parameters.get("targetPage");

            if ((values != null) && (values.length > 0)) {
                targetPage = values[0];
            }
        }

        Element pageElement = null;

        if (Validator.isNotNull(targetPage)) {
            targetPage = HtmlUtil.escapeXPathAttribute(targetPage);

            XPath xPathSelector = SAXReaderUtil.createXPath("/root/page[@id = " + targetPage + "]");

            pageElement = (Element) xPathSelector.selectSingleNode(document);
        }

        if (pageElement != null) {
            document = SAXReaderUtil.createDocument(pageElement);

            rootElement = document.getRootElement();

            numberOfPages = pages.size();
        } else {
            if (page > pages.size()) {
                page = 1;
            }

            pageElement = pages.get(page - 1);

            document = SAXReaderUtil.createDocument(pageElement);

            rootElement = document.getRootElement();

            numberOfPages = pages.size();
            paginate = true;
        }
    }

    JournalUtil.addAllReservedEls(rootElement, tokens, article, languageId, themeDisplay);

    try {
        if (_log.isDebugEnabled()) {
            _log.debug(
                    "Transforming " + article.getArticleId() + " " + article.getVersion() + " " + languageId);
        }

        // Try with specified template first (in the current group and the
        // global group). If a template is not specified, use the default
        // one. If the specified template does not exist, use the default
        // one. If the default one does not exist, throw an exception.

        DDMTemplate ddmTemplate = null;

        try {
            ddmTemplate = ddmTemplateLocalService.getTemplate(PortalUtil.getSiteGroupId(article.getGroupId()),
                    classNameLocalService.getClassNameId(DDMStructure.class), ddmTemplateKey, true);

            Group companyGroup = groupLocalService.getCompanyGroup(article.getCompanyId());

            if (companyGroup.getGroupId() == ddmTemplate.getGroupId()) {
                tokens.put("company_group_id", String.valueOf(companyGroup.getGroupId()));
            }
        } catch (NoSuchTemplateException nste) {
            if (!defaultDDMTemplateKey.equals(ddmTemplateKey)) {
                ddmTemplate = ddmTemplateLocalService.getTemplate(
                        PortalUtil.getSiteGroupId(article.getGroupId()),
                        classNameLocalService.getClassNameId(DDMStructure.class), defaultDDMTemplateKey);
            } else {
                throw nste;
            }
        }

        tokens.put("ddm_template_key", String.valueOf(ddmTemplate.getTemplateKey()));
        tokens.put("ddm_template_id", String.valueOf(ddmTemplate.getTemplateId()));

        // Deprecated token

        tokens.put("template_id", ddmTemplateKey);

        String script = ddmTemplate.getScript();
        String langType = ddmTemplate.getLanguage();
        cacheable = ddmTemplate.isCacheable();

        content = JournalUtil.transform(themeDisplay, tokens, viewMode, languageId, document,
                portletRequestModel, script, langType, propagateException);

        if (!pageFlow) {
            JournalServiceConfiguration journalServiceConfiguration = configurationProvider
                    .getCompanyConfiguration(JournalServiceConfiguration.class, article.getCompanyId());

            String[] pieces = StringUtil.split(content,
                    journalServiceConfiguration.journalArticlePageBreakToken());

            if (pieces.length > 1) {
                if (page > pieces.length) {
                    page = 1;
                }

                content = pieces[page - 1];
                numberOfPages = pieces.length;
                paginate = true;
            }
        }
    } catch (Exception e) {
        throw new SystemException(e);
    }

    return new JournalArticleDisplayImpl(article.getCompanyId(), article.getId(), article.getResourcePrimKey(),
            article.getGroupId(), article.getUserId(), article.getArticleId(), article.getVersion(),
            article.getTitle(languageId), article.getUrlTitle(), article.getDescription(languageId),
            article.getAvailableLanguageIds(), content, article.getDDMStructureKey(), ddmTemplateKey,
            article.isSmallImage(), article.getSmallImageId(), article.getSmallImageURL(), numberOfPages, page,
            paginate, cacheable);
}