View Javadoc
1   package org.apache.turbine.modules.pages;
2   
3   
4   /*
5    * Licensed to the Apache Software Foundation (ASF) under one
6    * or more contributor license agreements.  See the NOTICE file
7    * distributed with this work for additional information
8    * regarding copyright ownership.  The ASF licenses this file
9    * to you under the Apache License, Version 2.0 (the
10   * "License"); you may not use this file except in compliance
11   * with the License.  You may obtain a copy of the License at
12   *
13   *   http://www.apache.org/licenses/LICENSE-2.0
14   *
15   * Unless required by applicable law or agreed to in writing,
16   * software distributed under the License is distributed on an
17   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18   * KIND, either express or implied.  See the License for the
19   * specific language governing permissions and limitations
20   * under the License.
21   */
22  
23  
24  import org.apache.turbine.annotation.TurbineService;
25  import org.apache.turbine.pipeline.PipelineData;
26  import org.apache.turbine.services.template.TemplateService;
27  import org.apache.turbine.util.RunData;
28  import org.apache.turbine.util.TurbineException;
29  
30  /**
31   * When building sites using templates, Screens need only be defined
32   * for templates which require dynamic (database or object) data.
33   *
34   * <p>
35   *
36   * This page can be used on sites where the number of Screens can be
37   * much less than the number of templates.  The templates can be
38   * grouped in directories with common layouts.  Screen modules are
39   * then expected to be placed in packages corresponding with the
40   * templates' directories and follow a specific naming scheme.
41   *
42   * <p>
43   *
44   * The template parameter is parsed and and a Screen whose package
45   * matches the templates path and shares the same name minus any
46   * extension and beginning with a capital letter is searched for.  If
47   * not found, a Screen in a package matching the template's path with
48   * name Default is searched for.  If still not found, a Screen with
49   * name Default is looked for in packages corresponding to parent
50   * directories in the template's path until a match is found.
51   *
52   * <p>
53   *
54   * For example if data.getParameters().getString("template") returns
55   * /about_us/directions/driving.wm, the search follows
56   * about_us.directions.Driving, about_us.directions.Default,
57   * about_us.Default, Default, WebMacroSiteScreen (i.e. the default
58   * screen set in TurbineResources).
59   *
60   * <p>
61   *
62   * Only one Layout module is used, since it is expected that any
63   * dynamic content will be placed in navigations and screens.  The
64   * layout template to be used is found in a similar way to the Screen.
65   * For example the following paths will be searched in the layouts
66   * subdirectory: /about_us/directions/driving.wm,
67   * /about_us/directions/default.wm, /about_us/default.wm, /default.wm,
68   * where wm is the value of the template.default.extension property.
69   *
70   * <p>
71   *
72   * This approach allows a site with largely static content to be
73   * updated and added to regularly by those with little Java
74   * experience.
75   *
76   * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
77   * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
78   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
79   * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
80   * @version $Id$
81   */
82  public class TemplatePage
83      extends DefaultPage
84  {
85      /** Injected service instance */
86      @TurbineService
87      private TemplateService templateService;
88  
89      /**
90       * Works with TemplateService to set up default templates and
91       * corresponding class modules.
92       *
93       * @param pipelineData Turbine information.
94       * @throws Exception a generic exception.
95       */
96      @Override
97      protected void doBuildAfterAction(PipelineData pipelineData)
98          throws Exception
99      {
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 }