View Javadoc

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    TemplateEngine templateEngine = null;
25    MelatiConfig config = null;
26    Melati myMelati = null;
27    
28    public BibliomaniaDatabase getBibliomaniaDatabase () {
29        return (BibliomaniaDatabase)getDatabase();
30    }
31  
32    public User() {}
33  
34    public void postInsert() {
35      if (getBibliomaniaDatabase().getRegisteredUserGroup() != null) {
36        GroupMembership membership = (GroupMembership)getBibliomaniaDatabase().getGroupMembershipTable().newPersistent();
37        membership.setUser(this);
38        membership.setGroup(getBibliomaniaDatabase().getRegisteredUserGroup());
39        getBibliomaniaDatabase().getGroupMembershipTable().create(membership);
40      }
41      // email the user
42      if (this != getUserTable().administratorUser() &&
43          this != getUserTable().guestUser() && 
44          this != ((UserTable)getUserTable()).boardManager() &&
45          false) //FIXME Sometime
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    }
69    
70    // if a user is auto created by mailing a messageboard, assume they want
71    // to be spammed
72    public void generateDefaults() {
73      super.generateDefaults();
74      if (getWantemailalerts() == null) setWantemailalerts(Boolean.TRUE);
75      if (getWantspam() == null) setWantspam(Boolean.TRUE);
76      if (getFulltimeeducation() == null) setFulltimeeducation(Boolean.FALSE);
77      if (getDodgeyemail() == null) setDodgeyemail(Boolean.FALSE);
78    }
79  
80    public void assertCanRead(AccessToken token) {
81      if (token != this)
82        super.assertCanRead(token);
83    }
84  
85    public void assertCanWrite(AccessToken token) {
86      if (token != this && this != getDatabase().guestAccessToken())
87        super.assertCanWrite(token);
88    }
89    
90   public boolean hasPurchased(Download download) {
91      return hasPurchased(download.getTroid());
92   }
93   
94   public boolean hasPurchased(Integer troid) {
95      Vector where = new Vector();
96      where.add("\"user\" = " + getTroid()); 
97      where.add("authorised = true"); 
98      where.add("product = product.id"); 
99      where.add("product.download = " + troid); 
100     String whereClause = EnumUtils.concatenated(" AND ", where.elements());
101     return getBibliomaniaDatabase().getShopOrderItemTable().troidSelection(whereClause, null, false).hasMoreElements();
102  }
103   
104   public boolean isRegisteredUser(BibliomaniaDatabase db) {
105     return PoemThread.accessToken().givesCapability(db.getRegisteredUserCapability());
106   }
107   
108     
109   public String generateName() {
110     String generatedName = getEmail();
111     // ahhh - still none - randomise
112     if (generatedName == null  || generatedName.equals("")) return StringUtils.randomString(6);
113     // try and make the best of it if we have a name that is actually an email address
114     int at = generatedName.indexOf('@');
115     int dot = generatedName.indexOf('.');
116     if (dot != -1 && dot < at) at = dot;
117     if (at > 0) generatedName = generatedName.substring(0,at);
118     return generatedName;
119   }
120     
121   public String generateLogin() {
122     return getEmail();
123   }
124     
125   public String generatePassword() {
126     return StringUtils.randomString(6);
127   }  
128   
129   public Enumeration getUserDetailsFields() {
130     return
131       new MappedEnumeration(fieldsOfColumns(((org.paneris.bibliomania.UserTable)
132                             getUserTable()).getUserDetailsColumns())) {
133         public Object mapped(Object field) {
134           Field f = (Field)field;
135           if (f.getName().equals("name")) 
136             return f.withDescription("Your name");
137           if (f.getName().equals("email")) 
138             return f.withDescription("Your email address");
139           if (f.getName().equals("password")) 
140             return f.withDescription("Your password");
141           return f;
142           }
143         };
144   }
145 /*
146    // this is now in Column.java
147   public Enumeration fieldsOfColumns(Enumeration columns) {
148     // return new FieldsEnumeration(this, columns);
149     final Persistent _this = this;
150     return
151         new MappedEnumeration(columns) {
152           public Object mapped(Object column) {
153             return ((Column)column).asField(_this);
154           }
155         };
156   }
157 */
158 }
159 
160 
161