View Javadoc

1   package org.paneris.jammyjoes.model;
2   
3   import java.util.Enumeration;
4   
5   import org.melati.poem.AccessToken;
6   import org.melati.poem.Column;
7   import org.melati.poem.Persistent;
8   import org.melati.poem.util.MappedEnumeration;
9   import org.melati.util.StringUtils;
10  import org.paneris.jammyjoes.model.generated.UserBase;
11  
12  public class User extends UserBase {
13    public User() {}
14    
15    public void generateDefaults() {
16      if (getPassword() == null || getPassword().equals("")) setPassword(StringUtils.randomString(6));
17      if (getLogin() == null || getLogin().equals("")) setLogin(generateLogin());
18      // we must have a name, but it should not be the email address as it would 
19      // not be fair to expose the user's email address on systems where this should
20      // be kept hidden.
21      if (getName() == null || getName().equals("")) setName(generateName());
22    }
23  
24     public String generateName() {
25       // by default - name = login
26       return getLogin();
27     }
28  
29    /*
30     * this calculates the login id from the user name.  the string before the 
31     * 1st ' ', '@' or '.' is extracted, and then made unique.
32     * 
33     * override this to do your own thing
34     */
35    public String generateLogin() {
36      String loginid = getName();
37      
38      // no name - try email
39      if (loginid == null || loginid.equals("")) loginid = getEmail();
40      // ahhh - still none - randomise
41      if (loginid == null || loginid.equals("")) return StringUtils.randomString(6);
42      
43      int space = loginid.indexOf(' ');
44      if (space > 0) {
45        loginid = loginid.substring(0,space);
46        space ++;
47        if (space < loginid.length()) loginid += loginid.charAt(space);
48      } else {
49        // try and make the best of it if we have a name that is actually an email address
50        int at = loginid.indexOf('@');
51        int dot = loginid.indexOf('.');
52        if (dot != -1 && dot < at) at = dot;
53        if (at > 0) loginid = loginid.substring(0,at);
54      }
55      
56      // check to see if we already have this login id
57      Column loginColumn = getJammyjoesDatabaseTables().getUserTable().getLoginColumn();
58      boolean found = loginColumn.selectionWhereEq(loginid).hasMoreElements();
59      String testId = new String(loginid);
60      int count = 0;
61      while (found) {
62        count++;
63        testId = new String(loginid);
64        String testIdString = "" + count;
65        for (int i=0; i < (2 - testIdString.length()); i++) {
66  	testId += "0";
67        }
68        testId += count;
69        found = loginColumn.selectionWhereEq(testId).hasMoreElements();
70      }
71      return testId.trim();
72    }
73    
74    public Enumeration getUserDetailsFields() {
75      return
76        fieldsOfColumns(((org.paneris.jammyjoes.model.UserTable)
77                              getUserTable()).getUserDetailsColumns());
78    }
79    
80  
81    public Enumeration fieldsOfColumns(Enumeration columns) {
82      // return new FieldsEnumeration(this, columns);
83      final Persistent _this = this;
84      return
85          new MappedEnumeration(columns) {
86            public Object mapped(Object column) {
87              return ((Column)column).asField(_this);
88            }
89          };
90    }
91    
92    public void assertCanWrite(AccessToken token) {
93      if (token != this)
94        super.assertCanWrite(token);
95    }
96  
97  }