View Javadoc

1   package org.paneris.rimauresq;
2   
3   import org.melati.Melati;
4   import org.melati.PoemContext;
5   import org.melati.poem.Column;
6   import org.melati.poem.Persistent;
7   import org.melati.poem.Table;
8   import org.melati.servlet.PathInfoException;
9   import org.melati.template.ServletTemplateContext;
10  import org.melati.util.StringUtils;
11  import org.paneris.melati.site.SiteServlet;
12  
13  import org.paneris.rimauresq.model.RimauresqDatabase;
14  
15  /**
16   * @author timp
17   *
18   */
19  public abstract class RimauresqServlet extends SiteServlet {
20  
21    /** Where to find static content. */
22    private static final String STATIC_ROOT = "/dist/rimauresq/rimauresq-webapp/src/main/webapp/";
23    /** The DB we are using. */
24    private static String DB_NAME = "rimauresq";
25  
26    /**
27     * @return the root
28     */
29    public String getSTATIC_ROOT() {
30      return STATIC_ROOT;
31    }
32  
33    /**
34     * @return Returns the dB_NAME.
35     */
36    public String getDB_NAME() {
37      return DB_NAME;
38    }
39  
40    
41    /**
42     * Fullfill promise from SiteServlet, setup context.
43     * 
44     * @return Template name without path or extension
45     */
46    protected String reallyDoTemplateRequest(Melati melati,
47        ServletTemplateContext templateContext)
48        throws Exception {
49      templateContext.put("homePage", ((RimauresqDatabase) melati.getDatabase())
50          .getHomePage());
51      String newsAdminName = getSetting(melati, "NewsAdminName");
52      String newsAdminEmail = getSetting(melati, "NewsAdminEmail");
53      templateContext.put("newsAdminName", newsAdminName);
54      templateContext.put("newsAdminEmail", newsAdminEmail);
55      return doRimauresqTemplateRequest(melati, templateContext);
56    }
57  
58    protected PoemContext poemContext(Melati melati)
59        throws PathInfoException {
60  
61      PoemContext it = new PoemContext();
62  
63      String initParameterPathInfo = getInitParameter("pathinfo");
64      String[] parts;
65      if (initParameterPathInfo != null) {
66        parts = StringUtils.split(initParameterPathInfo, '/');
67      } else { 
68        parts = melati.getPathInfoParts();
69      }
70      // set it to something in order to provoke meaningful error
71      it.setLogicalDatabase("");
72      if (parts.length > 0) {
73        it.setLogicalDatabase(parts[0]);
74        // Display/en/table.html - might be needed
75        // Display/en/ATemplate.html - template that is DB specific
76        // or contains no template specific info at all
77        if (parts.length == 2)
78          it.setMethod(parts[1]);
79        // Display/en/page/1.html
80        // Display/en/page/TableSpecificSummaryTemplate.html
81        // Display/en/page/Primary.Search.Criterion.content.html
82        if (parts.length == 3) {
83          String r = parts[2];
84          if (r.lastIndexOf(".htm") != -1) {
85            r = r.substring(0, r.lastIndexOf(".htm"));
86            // it.method = "html";
87          }
88          it.setTable(parts[1]);
89          try {
90            it.setTroid(new Integer(r));
91          } catch (NumberFormatException e) {
92            it.setMethod(parts[2]);
93          }
94          
95        
96        }
97  
98        // Display/en/page/1/SpecialTemplate.html
99        // Display/en/page/Primary.Search.Criterion.content/SpecialTemplate.html
100       // Display/en/page/Primary_Search_Criterion_content/SpecialTemplate.html
101       
102       if (parts.length == 4) {
103         it.setTable(parts[1]);
104         try {
105           it.setTroid(new Integer(parts[2]));
106         } catch (NumberFormatException e) {
107           String r = parts[2];
108           if (r.lastIndexOf(".htm") != -1) {
109             r = r.substring(0, r.lastIndexOf(".htm"));
110           }
111           String value = StringUtils.tr(r, '.', ' ');
112           value = StringUtils.tr(r, '_', ' ');
113           Table t = melati.getTable();
114           if (t != null) {
115             Column c = t.primaryCriterionColumn();
116             if (c == null)
117               throw new NullPointerException("primaryCriterionColumn null");
118             Persistent o = c.firstWhereEq(value);
119             if (o != null)
120               it.setTroid(o.troid());
121           }
122         }
123         it.setMethod(parts[3]);
124       }
125       /*
126        * if (parts.length > 4) { 
127        * String pathInfo = melati.getRequest().getPathInfo(); 
128        * pathInfo = pathInfo.substring(1);
129        * for (int i = 0; i< 2; i++) { 
130        *   pathInfo = pathInfo.substring(pathInfo.indexOf("/") + 1); 
131        * } 
132        * it.method = pathInfo; 
133        * }
134        */
135     }
136     return it;
137   }
138 
139   /**
140    * Override this method to build up output in individual servlets.
141    * 
142    * @return Template name without path or extension
143    */
144   protected abstract String doRimauresqTemplateRequest(Melati melati,
145       ServletTemplateContext templateContext)
146       throws Exception;
147 
148 }