001package org.apache.turbine.modules.pages;
002
003
004/*
005 * Licensed to the Apache Software Foundation (ASF) under one
006 * or more contributor license agreements.  See the NOTICE file
007 * distributed with this work for additional information
008 * regarding copyright ownership.  The ASF licenses this file
009 * to you under the Apache License, Version 2.0 (the
010 * "License"); you may not use this file except in compliance
011 * with the License.  You may obtain a copy of the License at
012 *
013 *   http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing,
016 * software distributed under the License is distributed on an
017 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
018 * KIND, either express or implied.  See the License for the
019 * specific language governing permissions and limitations
020 * under the License.
021 */
022
023
024import org.apache.turbine.annotation.TurbineService;
025import org.apache.turbine.pipeline.PipelineData;
026import org.apache.turbine.services.template.TemplateService;
027import org.apache.turbine.util.RunData;
028import org.apache.turbine.util.TurbineException;
029
030/**
031 * When building sites using templates, Screens need only be defined
032 * for templates which require dynamic (database or object) data.
033 *
034 * <p>
035 *
036 * This page can be used on sites where the number of Screens can be
037 * much less than the number of templates.  The templates can be
038 * grouped in directories with common layouts.  Screen modules are
039 * then expected to be placed in packages corresponding with the
040 * templates' directories and follow a specific naming scheme.
041 *
042 * <p>
043 *
044 * The template parameter is parsed and and a Screen whose package
045 * matches the templates path and shares the same name minus any
046 * extension and beginning with a capital letter is searched for.  If
047 * not found, a Screen in a package matching the template's path with
048 * name Default is searched for.  If still not found, a Screen with
049 * name Default is looked for in packages corresponding to parent
050 * directories in the template's path until a match is found.
051 *
052 * <p>
053 *
054 * For example if data.getParameters().getString("template") returns
055 * /about_us/directions/driving.wm, the search follows
056 * about_us.directions.Driving, about_us.directions.Default,
057 * about_us.Default, Default, WebMacroSiteScreen (i.e. the default
058 * screen set in TurbineResources).
059 *
060 * <p>
061 *
062 * Only one Layout module is used, since it is expected that any
063 * dynamic content will be placed in navigations and screens.  The
064 * layout template to be used is found in a similar way to the Screen.
065 * For example the following paths will be searched in the layouts
066 * subdirectory: /about_us/directions/driving.wm,
067 * /about_us/directions/default.wm, /about_us/default.wm, /default.wm,
068 * where wm is the value of the template.default.extension property.
069 *
070 * <p>
071 *
072 * This approach allows a site with largely static content to be
073 * updated and added to regularly by those with little Java
074 * experience.
075 *
076 * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
077 * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
078 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
079 * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
080 * @version $Id$
081 */
082public class TemplatePage
083    extends DefaultPage
084{
085    /** Injected service instance */
086    @TurbineService
087    private TemplateService templateService;
088
089    /**
090     * Works with TemplateService to set up default templates and
091     * corresponding class modules.
092     *
093     * @param pipelineData Turbine information.
094     * @throws Exception a generic exception.
095     */
096    @Override
097    protected void doBuildAfterAction(PipelineData pipelineData)
098        throws Exception
099    {
100        RunData data = pipelineData.getRunData();
101        // The Template Service at this point must fetch the Screen class
102        // to match a given template. If the Screen class has already been
103        // set by an action, skip this, because the user has the already
104        // specified the Screen class he wants to use.
105        if (!data.hasScreen())
106        {
107            // This is effectively getting the "template" parameter
108            // from the parameter parser in rundata. This is coming
109            // from the request for a template.
110            String template = data.getTemplateInfo().getScreenTemplate();
111
112            // Get the layout template and the correct Screen.
113            String layoutTemplate =
114                    templateService.getLayoutTemplateName(template);
115            data.getTemplateInfo().setLayoutTemplate(layoutTemplate);
116
117            String screen = templateService.getScreenName(template);
118
119            if (screen == null)
120            {
121                String errMsg = "Couldn't map Template "
122                    + template + " to any Screen class!";
123                log.error(errMsg);
124                throw new TurbineException(errMsg);
125            }
126            data.setScreen(screen);
127        }
128    }
129}