001package org.apache.turbine.modules.screens;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.commons.lang3.StringUtils;
023import org.apache.turbine.annotation.TurbineService;
024import org.apache.turbine.pipeline.PipelineData;
025import org.apache.turbine.services.jsp.JspService;
026import org.apache.turbine.services.template.TemplateService;
027import org.apache.turbine.util.RunData;
028
029/**
030 * Base JSP Screen that should be subclassed by screens that want to
031 * use JSP.  Subclasses should override the doBuildTemplate() method.
032 *
033 * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
034 * @author Frank Y. Kim
035 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
036 * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
037 * @version $Id$
038 */
039public class BaseJspScreen
040        extends TemplateScreen
041{
042    /** The prefix for lookup up screen pages */
043    private static final String prefix = PREFIX + "/";
044
045    /** Injected service instance */
046    @TurbineService
047    private JspService jspService;
048
049    /** Injected service instance */
050    @TurbineService
051    private TemplateService templateService;
052
053    /**
054     * Method that sets up beans and forward the request to the JSP.
055     *
056     * @param pipelineData Turbine information.
057     * @return null - the JSP sends the information.
058     * @throws Exception a generic exception.
059     */
060    @Override
061    public String buildTemplate(PipelineData pipelineData)
062            throws Exception
063    {
064        RunData data = pipelineData.getRunData();
065        String screenTemplate = data.getTemplateInfo().getScreenTemplate();
066        // get the name of the template we want to use
067        String templateName
068            = templateService.getScreenTemplateName(screenTemplate);
069
070        // The Template Service could not find the Screen
071        if (StringUtils.isEmpty(templateName))
072        {
073            log.error("Screen " + screenTemplate + " not found!");
074            throw new Exception("Could not find screen for " + screenTemplate);
075        }
076
077        // let service know whether we are using a layout
078        jspService.handleRequest(pipelineData, prefix + templateName,
079                                 getLayout(pipelineData) == null);
080
081        return null;
082    }
083
084    /**
085     * Method to be overridden by subclasses to include data in beans, etc.
086     *
087     * @param pipelineData the PipelineData object
088     * @throws Exception a generic exception.
089     */
090    @Override
091    protected void doBuildTemplate(PipelineData pipelineData)
092        throws Exception
093    {
094        // abstract method
095    }
096}