com.liferay.hello.soy.web.internal.portlet.action.HelloSoyViewMVCRenderCommand.java Source code

Java tutorial

Introduction

Here is the source code for com.liferay.hello.soy.web.internal.portlet.action.HelloSoyViewMVCRenderCommand.java

Source

/**
 * Copyright 2000-present Liferay, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.liferay.hello.soy.web.internal.portlet.action;

import com.liferay.portal.kernel.model.Layout;
import com.liferay.portal.kernel.portlet.bridges.mvc.MVCRenderCommand;
import com.liferay.portal.kernel.template.Template;
import com.liferay.portal.kernel.theme.ThemeDisplay;
import com.liferay.portal.kernel.util.ReleaseInfo;
import com.liferay.portal.kernel.util.WebKeys;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.portlet.PortletURL;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.osgi.service.component.annotations.Component;

/**
 * @author Bruno Basto
 */
@Component(immediate = true, property = { "javax.portlet.name=hello_soy_portlet", "mvc.command.name=/",
        "mvc.command.name=View" }, service = MVCRenderCommand.class)
public class HelloSoyViewMVCRenderCommand implements MVCRenderCommand {

    @Override
    public String render(RenderRequest renderRequest, RenderResponse renderResponse) {

        Template template = (Template) renderRequest.getAttribute(WebKeys.TEMPLATE);

        ThemeDisplay themeDisplay = (ThemeDisplay) renderRequest.getAttribute(WebKeys.THEME_DISPLAY);

        List<Layout> layouts = themeDisplay.getLayouts();

        Stream<Layout> layoutStream = layouts.stream();

        template.put("layouts", layoutStream.map(layout -> new HashMap<String, String>() {
            {
                put("friendlyURL", layout.getFriendlyURL());
                put("nameCurrentValue", layout.getNameCurrentValue());
            }
        }).collect(Collectors.toList()));

        PortletURL navigationURL = renderResponse.createRenderURL();

        navigationURL.setParameter("mvcRenderCommandName", "Navigation");

        template.put("navigationURL", navigationURL.toString());

        template.put("releaseInfo", ReleaseInfo.getReleaseInfo());

        return "View";
    }

}