View Javadoc

1   package org.paneris.rimauresq.model;
2   
3   import org.paneris.rimauresq.model.generated.UserTableBase;
4   import org.melati.poem.AccessPoemException;
5   import org.melati.poem.DefinitionSource;
6   import org.melati.poem.Database;
7   import org.melati.poem.InitialisationPoemException;
8   import org.melati.poem.JdbcPersistent;
9   import org.melati.poem.Persistent;
10  import org.melati.poem.PoemException;
11  import org.melati.poem.PoemTask;
12  import org.melati.poem.PoemThread;
13  import org.melati.poem.ValidationPoemException;
14  
15  /**
16   * Melati POEM generated, programmer modifiable stub 
17   * for a <code>UserTable</code> object.
18   * <p>
19   * Description: 
20   *   A WVM User. 
21   * </p>
22   *
23   * 
24   * <table> 
25   * <tr><th colspan='3'>
26   * Field summary for SQL table <code>User</code>
27   * </th></tr>
28   * <tr><th>Name</th><th>Type</th><th>Description</th></tr>
29   * <tr><td> email </td><td> String </td><td> The user's email address 
30   * </td></tr> 
31   * <tr><td> title </td><td> String </td><td> User's title or role </td></tr> 
32   * <tr><td> address </td><td> String </td><td> &nbsp; </td></tr> 
33   * <tr><td> town </td><td> String </td><td> &nbsp; </td></tr> 
34   * <tr><td> postcode </td><td> String </td><td> Order postcode </td></tr> 
35   * <tr><td> country </td><td> String </td><td> &nbsp; </td></tr> 
36   * <tr><td> tel </td><td> String </td><td> User's telephone number </td></tr> 
37   * <tr><td> fax </td><td> String </td><td> User's fax number </td></tr> 
38   * <tr><td> mobile </td><td> String </td><td> User's mobile number </td></tr> 
39   * <tr><td> news </td><td> Boolean </td><td> Does the user want to receive 
40   * the  email newsletter? </td></tr> 
41   * </table> 
42   * 
43   * @generator  org.melati.poem.prepro.TableDef#generateTableMainJava 
44   */
45  public class UserTable extends UserTableBase {
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 UserTable(
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    public void init() throws PoemException {
65      super.init();
66  
67      // We can't put this in the constructor because the email
68      // column isn't defined until we've called UserTableBase.init()
69      getEmailColumn().setRaw_unsafe(guestUser, "");
70      getEmailColumn().setRaw_unsafe(administratorUser, "");
71    }
72  
73    public void postInitialise() {
74      super.postInitialise();
75      PoemThread.withAccessToken(
76          getDatabase().getUserTable().administratorUser(),
77          new PoemTask() {
78            public void run() {
79              if (((User)administratorUser()).getEmail().equals(""))
80                getEmailColumn().setRaw_unsafe(administratorUser, 
81                                               "rimauresq@paneris.co.uk");
82            }
83  
84            public String toString() {
85              return "Creating User defaults";
86            }
87      }
88      );
89    }
90  
91    /**
92     * @return the existing or newly minted user
93     */
94    public User ensure(String name, String title, String tel, 
95                       String email) {
96      User p = (User)getNameColumn().firstWhereEq(name);
97      if (p == null) {
98        p = (User)newPersistent();
99        p.setLogin(name);
100       p.setName(name);
101       p.setPassword(name);
102       p.setTitle(title);
103       p.setTel(tel);
104       p.setEmail(email);
105       p.makePersistent();
106     }
107     return p;
108   }
109   
110   protected JdbcPersistent _newPersistent() {
111     User u = (User)super._newPersistent();
112     u.setNews_unsafe(Boolean.TRUE);
113     return u;
114   }
115 
116   public void create(Persistent persistent)
117   throws AccessPoemException, ValidationPoemException,
118          InitialisationPoemException {
119     User u = (User)persistent; 
120     if (u.getLogin() == null)
121       u.setLogin("tmp");
122     if (u.getPassword() == null)
123       u.setPassword("tmp");
124     super.create(persistent);
125     if (u.getLogin() == "tmp")
126       u.setLogin(u.troid().toString());
127     if (u.getPassword() == "tmp")
128       u.setPassword(u.troid().toString());
129   }
130 }
131