View Javadoc

1   /*
2    * $Source: /usr/cvsroot/MelatiSite/src/main/java/org/paneris/melati/site/Display.java,v $
3    * $Revision: 1.6 $
4    *
5    *
6    * Contact details for copyright holder:
7    * 
8    *  Tim Pizey (timp@paneris.org)
9    */
10  
11  package org.paneris.melati.site;
12  
13  import org.melati.Melati;
14  import org.melati.poem.Persistent;
15  import org.melati.poem.Column;
16  import org.melati.PoemContext;
17  import org.melati.template.ServletTemplateContext;
18  import org.melati.util.StringUtils;
19  import org.paneris.melati.site.model.Template;
20  import org.paneris.melati.site.model.Templated;
21  import org.paneris.melati.site.model.HasGetTemplateUrl;
22  
23  /**
24   * Display an object using its Template.
25   * 
26   * This is the main servlet for the site, so it needs to handle 
27   * a number of request types.
28   * 
29   * 1. Display of a record from the db
30   * 2. Display a selection of records from a table in the db
31   * 3. Display images or files from the DOCUMENT_ROOT
32   * 
33   * This last is a trick to enable an image or page to be 
34   * displayed instead of a db record/table.
35   * 
36   * 
37   *
38   *  http://localhost/db/Display/table/record.html
39   * 
40   *
41   * Table Record Method         URL
42   * null  null   null           Display - Error
43   * null  null   present        Display/AllUrls.html
44   * valid null   present        Display/page/Summary.html
45   * valid valid  null           Display/page/1.html
46   * valid valid  null           Display/page/About.melati.site.html 
47   * valid valid  present        Display/page/1/Special.html
48   * valid valid  present        Display/page/About.Us/intranet.html 
49   * 
50   * 
51   */
52  public class Display extends SiteServlet {
53  
54    /**
55     * Stop eclipse wingeing.
56     */
57    private static final long serialVersionUID = 1L;
58  
59    protected String reallyDoTemplateRequest(Melati melati, 
60                                             ServletTemplateContext context)
61        throws Exception {
62  
63      PoemContext pc = melati.getPoemContext();
64      String method = pc.getMethod();
65  
66      Persistent object = melati.getObject();
67  
68      if (object != null) {
69        object.assertCanRead();
70        context.put("object", object);
71      }
72      if (method != null && !method.equals("") && 
73          melati.getTable() != null) {
74        if (method.lastIndexOf(".htm") != -1) {
75          method = method.substring(0,method.lastIndexOf(".htm")); 
76        }
77        
78        if (melati.getObject() == null) {
79          Integer id = null;
80          try {
81            id = new Integer(method);
82          }
83          catch (NumberFormatException e) {
84            if (melati.getTable() != null) {
85              String value = StringUtils.tr(method,'.', ' '); 
86              value = StringUtils.tr(method,'_', ' '); 
87              Column search = melati.getTable().primaryCriterionColumn();
88              Persistent o = search.firstWhereEq(value);
89              if (o != null) id = o.troid();
90            }
91          }
92          if (id != null) {
93            melati.getPoemContext().setTroid(id); 
94            melati.getPoemContext().setMethod(null);
95          } 
96          melati.loadTableAndObject();
97          if (melati.getObject() != null) {
98            object = melati.getObject();
99            object.assertCanRead();
100           context.put("object", object);
101           if (object instanceof Templated) {
102             Template t = ((Templated)object).getTemplate();
103             String url = null;
104             if (t != null) url = t.getUrl();
105             if (url != null) {
106               System.err.println("returning url:" + url);
107               return url;
108             } else {
109               System.err.println("url null");              
110             }
111           } else {
112             System.err.println("not templated:" +object.getClass().getName());              
113           }
114           System.err.println("return 1 - really");
115           return StringUtils.capitalised(melati.getTable().getName());
116         // StringUtils.tr(melati.getObject().getClass().getName(), '.', '/');
117         } else {
118           System.err.println("return 2");
119           return method;
120         }
121       } else {
122         System.err.println("return 3");
123         return method;
124       }
125     }  
126     if (context.getForm("template") != null) {
127       System.err.println("return 4");
128       return context.getForm("template");
129     }
130     if (object != null && object instanceof Templated) {
131       Template t = ((Templated)object).getTemplate();
132       String url = null;
133       if(t != null) url = t.getUrl();
134       if (url != null) return url;
135       return StringUtils.capitalised(melati.getTable().getName());
136     }
137     if (object != null && object instanceof HasGetTemplateUrl) {
138       String t = ((HasGetTemplateUrl)melati.getObject()).getTemplateUrl();
139       if (t != null) return t;
140       return StringUtils.capitalised(melati.getTable().getName());
141     }
142     return "Display";
143   }
144 
145 }