1 | |
package org.paneris.bibliomania; |
2 | |
|
3 | |
import java.util.Enumeration; |
4 | |
import java.util.Vector; |
5 | |
|
6 | |
import org.melati.Melati; |
7 | |
import org.melati.MelatiConfig; |
8 | |
import org.melati.poem.AccessToken; |
9 | |
import org.melati.poem.Field; |
10 | |
import org.melati.poem.GroupMembership; |
11 | |
import org.melati.poem.PoemThread; |
12 | |
import org.melati.template.TemplateContext; |
13 | |
import org.melati.template.TemplateEngine; |
14 | |
import org.melati.util.Email; |
15 | |
import org.melati.poem.util.EnumUtils; |
16 | |
import org.melati.poem.util.MappedEnumeration; |
17 | |
import org.melati.util.MelatiStringWriter; |
18 | |
import org.melati.poem.util.StringUtils; |
19 | |
import org.melati.util.UnexpectedExceptionException; |
20 | |
import org.paneris.bibliomania.generated.UserBase; |
21 | |
|
22 | |
public class User extends UserBase { |
23 | |
|
24 | 14 | TemplateEngine templateEngine = null; |
25 | 14 | MelatiConfig config = null; |
26 | 14 | Melati myMelati = null; |
27 | |
|
28 | |
public BibliomaniaDatabase getBibliomaniaDatabase () { |
29 | 0 | return (BibliomaniaDatabase)getDatabase(); |
30 | |
} |
31 | |
|
32 | 28 | public User() {} |
33 | |
|
34 | |
public void postInsert() { |
35 | 0 | if (getBibliomaniaDatabase().getRegisteredUserGroup() != null) { |
36 | 0 | GroupMembership membership = (GroupMembership)getBibliomaniaDatabase().getGroupMembershipTable().newPersistent(); |
37 | 0 | membership.setUser(this); |
38 | 0 | membership.setGroup(getBibliomaniaDatabase().getRegisteredUserGroup()); |
39 | 0 | getBibliomaniaDatabase().getGroupMembershipTable().create(membership); |
40 | |
} |
41 | |
|
42 | 0 | if (this != getUserTable().administratorUser() && |
43 | |
this != getUserTable().guestUser() && |
44 | |
this != ((UserTable)getUserTable()).boardManager() && |
45 | |
false) |
46 | |
|
47 | |
try { |
48 | |
if (templateEngine == null) { |
49 | |
config = new MelatiConfig(); |
50 | |
templateEngine = config.getTemplateEngine(); |
51 | |
templateEngine.init(config); |
52 | |
myMelati = new Melati(config,null); |
53 | |
myMelati.setTemplateEngine(templateEngine); |
54 | |
} |
55 | |
TemplateContext context = templateEngine.getTemplateContext(); |
56 | |
context.put("melati",myMelati); |
57 | |
context.put("user",this); |
58 | |
String templateName = "org/paneris/bibliomania/html/email/NewUser.wm"; |
59 | |
MelatiStringWriter sw = (MelatiStringWriter)myMelati.getStringWriter(); |
60 | |
templateEngine.expandTemplate(sw, templateName, context); |
61 | |
myMelati.write(); |
62 | |
String message = sw.toString(); |
63 | |
Email.send(getDatabase().getSettingTable().get(Email.SMTPSERVER), getBibliomaniaDatabase().getConfirmationEmailFrom(), |
64 | |
getEmail(), "", "Bibliomania registration details", message); |
65 | |
} catch (Exception e) { |
66 | |
throw new UnexpectedExceptionException("A confirmation email could not be sent",e); |
67 | |
} |
68 | 0 | } |
69 | |
|
70 | |
|
71 | |
|
72 | |
public void generateDefaults() { |
73 | 0 | super.generateDefaults(); |
74 | 0 | if (getWantemailalerts() == null) setWantemailalerts(Boolean.TRUE); |
75 | 0 | if (getWantspam() == null) setWantspam(Boolean.TRUE); |
76 | 0 | if (getFulltimeeducation() == null) setFulltimeeducation(Boolean.FALSE); |
77 | 0 | if (getDodgeyemail() == null) setDodgeyemail(Boolean.FALSE); |
78 | 0 | } |
79 | |
|
80 | |
public void assertCanRead(AccessToken token) { |
81 | 0 | if (token != this) |
82 | 0 | super.assertCanRead(token); |
83 | 0 | } |
84 | |
|
85 | |
public void assertCanWrite(AccessToken token) { |
86 | 0 | if (token != this && this != getDatabase().guestAccessToken()) |
87 | 0 | super.assertCanWrite(token); |
88 | 0 | } |
89 | |
|
90 | |
public boolean hasPurchased(Download download) { |
91 | 0 | return hasPurchased(download.getTroid()); |
92 | |
} |
93 | |
|
94 | |
public boolean hasPurchased(Integer troid) { |
95 | 0 | Vector where = new Vector(); |
96 | 0 | where.add("\"user\" = " + getTroid()); |
97 | 0 | where.add("authorised = true"); |
98 | 0 | where.add("product = product.id"); |
99 | 0 | where.add("product.download = " + troid); |
100 | 0 | String whereClause = EnumUtils.concatenated(" AND ", where.elements()); |
101 | 0 | return getBibliomaniaDatabase().getShopOrderItemTable().troidSelection(whereClause, null, false).hasMoreElements(); |
102 | |
} |
103 | |
|
104 | |
public boolean isRegisteredUser(BibliomaniaDatabase db) { |
105 | 0 | return PoemThread.accessToken().givesCapability(db.getRegisteredUserCapability()); |
106 | |
} |
107 | |
|
108 | |
|
109 | |
public String generateName() { |
110 | 0 | String generatedName = getEmail(); |
111 | |
|
112 | 0 | if (generatedName == null || generatedName.equals("")) return StringUtils.randomString(6); |
113 | |
|
114 | 0 | int at = generatedName.indexOf('@'); |
115 | 0 | int dot = generatedName.indexOf('.'); |
116 | 0 | if (dot != -1 && dot < at) at = dot; |
117 | 0 | if (at > 0) generatedName = generatedName.substring(0,at); |
118 | 0 | return generatedName; |
119 | |
} |
120 | |
|
121 | |
public String generateLogin() { |
122 | 0 | return getEmail(); |
123 | |
} |
124 | |
|
125 | |
public String generatePassword() { |
126 | 0 | return StringUtils.randomString(6); |
127 | |
} |
128 | |
|
129 | |
public Enumeration getUserDetailsFields() { |
130 | 0 | return |
131 | |
new MappedEnumeration(fieldsOfColumns(((org.paneris.bibliomania.UserTable) |
132 | 0 | getUserTable()).getUserDetailsColumns())) { |
133 | |
public Object mapped(Object field) { |
134 | 0 | Field f = (Field)field; |
135 | 0 | if (f.getName().equals("name")) |
136 | 0 | return f.withDescription("Your name"); |
137 | 0 | if (f.getName().equals("email")) |
138 | 0 | return f.withDescription("Your email address"); |
139 | 0 | if (f.getName().equals("password")) |
140 | 0 | return f.withDescription("Your password"); |
141 | 0 | return f; |
142 | |
} |
143 | |
}; |
144 | |
} |
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
} |
159 | |
|
160 | |
|
161 | |
|