View Javadoc

1   package org.paneris.melati.site.model;
2   
3   import org.paneris.melati.site.model.generated.PageTableBase;
4   import org.melati.poem.DefinitionSource;
5   import org.melati.poem.Database;
6   import org.melati.poem.PoemException;
7   
8   /**
9    * Melati POEM generated, programmer modifiable stub 
10   * for a <code>PageTable</code> object.
11   * <p>
12   * Description: 
13   *   A Page. 
14   * </p>
15   *
16   * 
17   * <table> 
18   * <tr><th colspan='3'>
19   * Field summary for SQL table <code>Page</code>
20   * </th></tr>
21   * <tr><th>Name</th><th>Type</th><th>Description</th></tr>
22   * <tr><td> id </td><td> Integer </td><td> &nbsp; </td></tr> 
23   * <tr><td> name </td><td> String </td><td> A file system name, no spaces or 
24   * special characters </td></tr> 
25   * <tr><td> displayname </td><td> String </td><td> The full name used in the 
26   * TITLE. </td></tr> 
27   * <tr><td> displayorder </td><td> Integer </td><td> A rank determining where 
28   * the page appears in a list </td></tr> 
29   * <tr><td> deleted </td><td> Boolean </td><td> Whether or not to include 
30   * this Unit </td></tr> 
31   * <tr><td> lastencached </td><td> Timestamp </td><td> When the unit was 
32   * encached; cleared when the unit is updated </td></tr> 
33   * <tr><td> parent </td><td> Page </td><td> The organisation to which this is 
34   * affiliated, if any.  </td></tr> 
35   * <tr><td> template </td><td> Template </td><td> The name of the layout for 
36   * this page </td></tr> 
37   * <tr><td> style </td><td> Style </td><td> The name of the CSS class for 
38   * this page </td></tr> 
39   * <tr><td> messageboard </td><td> Board </td><td> A messageboard on which 
40   * this unit can be discussed </td></tr> 
41   * </table> 
42   * 
43   * @generator  org.melati.poem.prepro.TableDef#generateTableMainJava 
44   */
45  public class PageTable extends PageTableBase {
46  
47   /**
48    * Constructor.
49    * 
50    * @generator org.melati.poem.prepro.TableDef#generateTableMainJava 
51    * @param database          the POEM database we are using
52    * @param name              the name of this <code>Table</code>
53    * @param definitionSource  which definition is being used
54    * @throws PoemException    if anything goes wrong
55    */
56    public PageTable(
57        Database database, String name,
58        DefinitionSource definitionSource) throws PoemException {
59      super(database, name, definitionSource);
60    }
61  
62    // programmer's domain-specific code here
63  
64    static int displayOrder = 0;
65  
66    /**
67     * Create or retrieve existing page by name.
68     * 
69     * @return the existing or newly created object
70     */
71    public Page ensurePage(String displayName){
72      String name = deSpace(displayName);
73      Page p = (Page)getNameColumn().firstWhereEq(name);
74      if (p == null) {
75        p = (Page)newPersistent();
76        p.setName(name);
77        p.setDisplayname(displayName);
78        p.setDisplayorder(displayOrder);
79        p.setDeleted(false);
80        p.setDisplay(true);
81        displayOrder = displayOrder + 1;
82        p.makePersistent();
83      }
84      return p;
85    }
86  
87    /**
88     * @param displayName long name of page
89     * @return existing or newly minted Page
90     */
91    public Page ensure(String displayName){
92      Page p = ensurePage(displayName);
93      SiteDatabaseTables db = ((SiteDatabaseTables)getDatabase());
94      db.getDivTable().ensure(p);
95      return p;
96    }
97  
98    /**
99     * @param displayName long name of page
100    * @param template special template
101    * @return existing or newly minted Page
102    */
103   public Page ensure(String displayName, Template template){
104     Page p = ensurePage(displayName);
105     SiteDatabaseTables db = ((SiteDatabaseTables)getDatabase());
106     db.getDivTable().ensure(p);
107     p.setTemplate(template);
108     return p;
109   }
110 
111   /**
112    * @param displayName long name of page
113    * @param parent the page's parent
114    * @return existing or newly minted Page
115    */
116   public Page ensure(String displayName, Page parent){
117     Page p = ensure(displayName);
118     p.setParent(parent);
119     return p;
120   }
121   
122   /**
123    * @param displayName long name of page
124    * @param parent the page's parent
125    * @param divSubject name of Div to create
126    * @param divContent Div content
127    * @return existing or newly minted Page
128    */
129   public Page ensure(String displayName, Page parent, 
130                      String divSubject, String divContent){
131     Page p = ensurePage(displayName);
132     p.setParent(parent);
133     ((SiteDatabaseTables)getDatabase()).
134         getDivTable().ensure(p, divSubject, divContent);
135     return p;
136   }
137 
138 
139   /**
140    * Remove spaces from a string.
141    * @param s the string
142    * @return without spaces
143    */
144   public static String deSpace(String s) {
145     int l = s.length();
146     for (int i = 0; i < l; ++i) {
147       char c = s.charAt(i);
148       if (c == ' ') {
149         // damn, found one; catch up to here ...
150         StringBuffer t = new StringBuffer(l -1);
151         for (int j = 0; j < i; ++j)
152           t.append(s.charAt(j));
153         // ... and continue
154         for (++i; i < l; ++i) {
155           c = s.charAt(i);
156           if (c != ' ')
157            t.append(c);
158         }
159         return t.toString();
160       }
161     }
162     return s;
163   }
164 }