Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
User |
|
| 2.625;2.625 | ||||
User$1 |
|
| 2.625;2.625 |
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 | 0 | public User() {} |
14 | ||
15 | public void generateDefaults() { | |
16 | 0 | if (getPassword() == null || getPassword().equals("")) setPassword(StringUtils.randomString(6)); |
17 | 0 | 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 | 0 | if (getName() == null || getName().equals("")) setName(generateName()); |
22 | 0 | } |
23 | ||
24 | public String generateName() { | |
25 | // by default - name = login | |
26 | 0 | 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 | 0 | String loginid = getName(); |
37 | ||
38 | // no name - try email | |
39 | 0 | if (loginid == null || loginid.equals("")) loginid = getEmail(); |
40 | // ahhh - still none - randomise | |
41 | 0 | if (loginid == null || loginid.equals("")) return StringUtils.randomString(6); |
42 | ||
43 | 0 | int space = loginid.indexOf(' '); |
44 | 0 | if (space > 0) { |
45 | 0 | loginid = loginid.substring(0,space); |
46 | 0 | space ++; |
47 | 0 | 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 | 0 | int at = loginid.indexOf('@'); |
51 | 0 | int dot = loginid.indexOf('.'); |
52 | 0 | if (dot != -1 && dot < at) at = dot; |
53 | 0 | if (at > 0) loginid = loginid.substring(0,at); |
54 | } | |
55 | ||
56 | // check to see if we already have this login id | |
57 | 0 | Column loginColumn = getJammyjoesDatabaseTables().getUserTable().getLoginColumn(); |
58 | 0 | boolean found = loginColumn.selectionWhereEq(loginid).hasMoreElements(); |
59 | 0 | String testId = new String(loginid); |
60 | 0 | int count = 0; |
61 | 0 | while (found) { |
62 | 0 | count++; |
63 | 0 | testId = new String(loginid); |
64 | 0 | String testIdString = "" + count; |
65 | 0 | for (int i=0; i < (2 - testIdString.length()); i++) { |
66 | 0 | testId += "0"; |
67 | } | |
68 | 0 | testId += count; |
69 | 0 | found = loginColumn.selectionWhereEq(testId).hasMoreElements(); |
70 | 0 | } |
71 | 0 | return testId.trim(); |
72 | } | |
73 | ||
74 | public Enumeration getUserDetailsFields() { | |
75 | 0 | 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 | 0 | final Persistent _this = this; |
84 | 0 | return |
85 | new MappedEnumeration(columns) { | |
86 | 0 | public Object mapped(Object column) { |
87 | 0 | return ((Column)column).asField(_this); |
88 | } | |
89 | }; | |
90 | } | |
91 | ||
92 | public void assertCanWrite(AccessToken token) { | |
93 | 0 | if (token != this) |
94 | 0 | super.assertCanWrite(token); |
95 | 0 | } |
96 | ||
97 | } |