View Javadoc

1   package br.com.ibnetwork.guara.pipeline.valve.view;
2   
3   import java.util.ArrayList;
4   import java.util.Iterator;
5   import java.util.List;
6   
7   import org.apache.avalon.framework.configuration.Configurable;
8   import org.apache.avalon.framework.configuration.Configuration;
9   import org.apache.avalon.framework.configuration.ConfigurationException;
10  import org.apache.avalon.framework.service.ServiceException;
11  import org.apache.avalon.framework.service.ServiceManager;
12  import org.apache.avalon.framework.service.Serviceable;
13  import org.apache.commons.logging.Log;
14  import org.apache.commons.logging.LogFactory;
15  
16  import br.com.ibnetwork.guara.pipeline.PipelineException;
17  import br.com.ibnetwork.guara.pipeline.valve.ValveSupport;
18  import br.com.ibnetwork.guara.rundata.Outcome;
19  import br.com.ibnetwork.guara.rundata.RunData;
20  import br.com.ibnetwork.guara.view.Context;
21  import br.com.ibnetwork.guara.view.TemplateEngine;
22  import br.com.ibnetwork.guara.view.TemplateUtils;
23  
24  /***
25   * @author leandro
26   */
27  public class RenderScreen
28      extends ValveSupport
29      implements Serviceable, Configurable
30  {
31      private Log logger;
32      
33      private TemplateEngine engine;
34      
35      private String onTemplateNotFound;
36  
37      private String defaultTemplate;
38  
39      OutcomeMapping[] mappings;
40      
41      public static final String SCREEN_CONTEXT_KEY = "screen_placeholder";
42      
43      public void service(ServiceManager manager) 
44      	throws ServiceException
45      {
46          logger = LogFactory.getLog(RenderScreen.class);
47          engine = (TemplateEngine) manager.lookup(TemplateEngine.ROLE);
48      }
49  
50      public void configure(Configuration root)
51      	throws ConfigurationException
52      {
53          onTemplateNotFound = "screens." + root.getChild("onTemplateNotFound").getAttribute("template");
54          defaultTemplate = root.getChild("defaultTemplate").getAttribute("template");
55          Configuration[] modules = root.getChild("templateMapping").getChildren("module");
56          mappings = new OutcomeMapping[modules.length];
57          for (int i = 0; i < modules.length; i++)
58          {
59              Configuration conf = modules[i];
60              String name = conf.getAttribute("name");
61              List outcomes = getMapping(conf);
62              mappings[i] = new OutcomeMapping(name,outcomes);
63          }
64      }
65  
66      private List getMapping(Configuration root)
67      {
68          Configuration[] conf = root.getChildren("outcome");
69          List result = new ArrayList();
70          for (int j = 0; j < conf.length; j++)
71          {
72              Configuration outcome = conf[j];
73              result.add(outcome);
74          }
75          return result;
76      }
77  
78      public boolean execute(RunData data) 
79      	throws PipelineException
80      {
81          String templateName = getTemplateName(data);
82          Context ctx = data.getContext();
83          if(!engine.templateExists(templateName))
84          {
85              if(logger.isDebugEnabled()) 
86              {
87                  logger.debug("Template ["+templateName+"] not found. Will render ["
88                          +onTemplateNotFound+"]");
89              }
90              ctx.put("templateName",templateName);
91              templateName = onTemplateNotFound;
92          }
93          String result = TemplateUtils.renderTemplate(engine, data.getParameters().getEncoding(), data.getContext(), templateName);
94          ctx.put(SCREEN_CONTEXT_KEY, result);
95          return true;
96      }
97  
98      private String getTemplateName(RunData data)
99      {
100         Outcome outcome = data.getOutcome();
101         String templateName = null;
102         if(outcome == null || outcome.equals(Outcome.UNKNOWN))
103         {
104             //UNKNOWN outcome, use template from RunData
105             templateName = data.getPageInfo().getTemplate();
106         }
107         else
108         {
109             for (int i = 0; i < mappings.length; i++)
110             {
111                 OutcomeMapping mapping = mappings[i];
112                 String moduleName = outcome.getSource().getName();
113                 if(mapping.moduleName.equals(moduleName))
114                 {
115                     templateName = mapping.getTemplateName(outcome);
116                 }
117             }
118         }
119         if(templateName == null)
120         {
121             templateName = defaultTemplate;
122         }
123         templateName = "screens." + templateName;
124         return templateName;
125     }
126 }
127 
128 class OutcomeMapping
129 {
130     List outcomes;
131     
132     String moduleName;
133 
134     public OutcomeMapping(String name, List outcomes)
135     {
136         this.moduleName = name;
137         this.outcomes = outcomes;
138     }
139 
140     public String getTemplateName(Outcome outcome)
141     {
142     	String defaultTemplate = null;
143         for (Iterator iter = outcomes.iterator(); iter.hasNext();) 
144         {
145 			Configuration conf = (Configuration) iter.next();
146 			String template = conf.getAttribute("template",null);
147 			String code = conf.getAttribute("code","DEFAULT");
148             String method = conf.getAttribute("method",null);
149             if(code.equals(outcome.getCode()) 
150                     && (method == null ? outcome.getMethod() == null : method.equals(outcome.getMethod())))
151             {
152                 return template;
153             }
154             else if("DEFAULT".equals(code))
155 			{
156 				defaultTemplate = template;
157 			}
158 		}
159         return defaultTemplate;
160     }
161 }