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
19
20
21 if (getName() == null || getName().equals("")) setName(generateName());
22 }
23
24 public String generateName() {
25
26 return getLogin();
27 }
28
29
30
31
32
33
34
35 public String generateLogin() {
36 String loginid = getName();
37
38
39 if (loginid == null || loginid.equals("")) loginid = getEmail();
40
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
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
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
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 }