BrightSide Workbench Full Report + Source Code
TellSomeoneComposer.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2015 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.contacts.zul.register;
20 
21 import java.util.List;
22 import org.turro.auth.Authentication;
23 import org.turro.command.Command;
24 import org.turro.command.Context;
25 import org.turro.contacts.SignUp;
26 import org.turro.contacts.db.ContactsPU;
27 import org.turro.elephant.context.ElephantContext;
28 import org.turro.elephant.context.HeadlessApplication;
29 import org.turro.elephant.context.IConstructor;
30 import org.turro.elephant.impl.context.ContextFactory;
31 import org.turro.elephant.impl.util.StringParser;
32 import org.turro.elephant.util.ZkossUtils;
33 import org.turro.i18n.I_;
34 import org.turro.jpa.Dao;
35 import org.turro.mail.impl.MailPool;
36 import org.turro.marker.ElephantMarker;
37 import org.turro.plugin.contacts.IContact;
38 import org.zkoss.zk.ui.Component;
39 import org.zkoss.zk.ui.event.Event;
40 import org.zkoss.zk.ui.select.SelectorComposer;
41 import org.zkoss.zk.ui.select.annotation.Listen;
42 import org.zkoss.zk.ui.select.annotation.Wire;
43 import org.zkoss.zk.ui.util.Clients;
44 import org.zkoss.zul.Textbox;
45 
50 public class TellSomeoneComposer extends SelectorComposer<Component> {
51 
52  @Wire("#name")
53  private Textbox name;
54 
55  @Wire("#email")
56  private Textbox email;
57 
58  @Wire("#comment")
59  private Textbox comment;
60 
61  private SignUp sue;
62 
63  @Listen("onClick = #register")
64  public void onRegister(Event event) {
65  SignUp su = new SignUp();
66  su.setName(name.getValue());
67  su.setEmail(email.getValue());
68  su.setComment(comment.getValue());
69  if(su.isValid()) {
70  if(existPending(su.getEmail())) {
71  ZkossUtils.confirmProcess(I_.get("Email pending to confirm") +
72  "\n\n" +
73  I_.get("Resend confirmation e-mail"), new Command() {
74  @Override
75  public Object execute(Context context) {
76  sendEmail(sue);
77  Clients.showNotification(I_.format("Confirmation e-mail has been sent to %s", sue.getEmail()));
78  return true;
79  }
80  });
81  } else if(existEmail(su.getEmail())) {
82  Clients.showNotification(I_.format("%s already exists", su.getEmail()));
83  } else {
84  su = new ContactsPU().saveObject(su);
85  sendEmail(su);
86  Clients.showNotification(I_.format("Confirmation e-mail has been sent to %s", su.getEmail()));
87  }
88  } else {
89  Clients.showNotification("No valid values");
90  }
91  }
92 
93  private static boolean existEmail(String email) {
94  Dao dao = new ContactsPU();
95  List l = dao.getResultList(
96  "select c from Connector c where c.value = ?",
97  new Object[] { email }
98  );
99  if(l.isEmpty()) {
100  l = dao.getResultList(
101  "select c from SignUp c where c.email = ? and confirmed = TRUE",
102  new Object[] { email }
103  );
104  }
105  return !l.isEmpty();
106  }
107 
108  private boolean existPending(String email) {
109  Dao dao = new ContactsPU();
110  List l = dao.getResultList(
111  "select c from SignUp c where c.email = ? and confirmed = FALSE",
112  new Object[] { email }
113  );
114  if(!l.isEmpty()) {
115  sue = (SignUp) l.iterator().next();
116  }
117  return !l.isEmpty();
118  }
119 
120  private boolean sendEmail(SignUp signUp) {
121  IConstructor constructor = HeadlessApplication.getInstance().getConstructor();
122  IContact contact = Authentication.getIContact();
123  if(contact == null || !contact.isValid()) {
124  return false;
125  }
126  MailPool mp = (MailPool) ContextFactory.getImplementation(constructor, "IMailPool");
127  mp.setEncoding(ElephantContext.getEncoding());
128  mp.addCssFile(ElephantContext.getRealPath("/_internal/css/mail.css"));
129  mp.addToPool(null, signUp.getEmail(), null,
130  I_.format("%s would like you to signup in %s", contact.getName(), ElephantContext.getSiteName()),
131  getMessage(signUp, constructor, contact), "text/html");
132  mp.sendPool();
133  sendMailToAdmin(signUp, contact);
134  return true;
135  }
136 
137  private String getMessage(SignUp signUp, IConstructor constructor, IContact contact) {
138  ElephantMarker em = new ElephantMarker(constructor);
139  em.put("signUp", signUp);
140  em.put("contact", contact);
141  em.put("motive", I_.format("%s would like you to signup in %s", contact.getName(), ElephantContext.getSiteName()));
142  em.put("comment", StringParser.toHTML(signUp.getComment()));
143  return em.parse("signup", "tellsomeone");
144  }
145 
146  private static void sendMailToAdmin(SignUp signUp, IContact contact) {
147  MailPool.sendMailToAdmin("User wants friend to register",
148  contact.getName() + " -> " + signUp.getName() +
149  ": " +
150  signUp.getEmail() +
151  "\n\n" +
152  signUp.getComment());
153  }
154 
155 }
void setComment(String comment)
Definition: SignUp.java:82
void setEmail(String email)
Definition: SignUp.java:74
void setName(String name)
Definition: SignUp.java:66
static void confirmProcess(String message, Command command)
Definition: ZkossUtils.java:74
static String format(String msg, Object... arguments)
Definition: I_.java:49
static String get(String msg)
Definition: I_.java:41