BrightSide Workbench Full Report + Source Code
TellSomeoneControl.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2016 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 java.util.logging.Level;
23 import java.util.logging.Logger;
24 import org.apache.commons.mail.EmailException;
25 import org.turro.action.MailSenders;
26 import org.turro.auth.Authentication;
27 import org.turro.contacts.SignUp;
28 import org.turro.contacts.db.ContactsPU;
29 import org.turro.elephant.TemplateControl;
30 import org.turro.elephant.context.ElephantContext;
31 import org.turro.elephant.impl.util.StringParser;
32 import org.turro.elephant.util.Messages;
33 import org.turro.i18n.I_;
34 import org.turro.jpa.Dao;
35 import org.turro.plugin.contacts.IContact;
36 import org.zkoss.zk.ui.event.Event;
37 import org.zkoss.zk.ui.select.annotation.Listen;
38 import org.zkoss.zk.ui.select.annotation.Wire;
39 import org.zkoss.zk.ui.util.Clients;
40 import org.zkoss.zul.Textbox;
41 
46 public class TellSomeoneControl extends TemplateControl {
47 
48  @Wire("#name")
49  private Textbox name;
50 
51  @Wire("#email")
52  private Textbox email;
53 
54  @Wire("#comment")
55  private Textbox comment;
56 
57  private SignUp sue;
58 
59  @Listen("onClick = #register")
60  public void onRegister(Event event) {
61  SignUp su = new SignUp();
62  su.setName(name.getValue());
63  su.setEmail(email.getValue());
64  su.setComment(comment.getValue());
65  if(su.isValid()) {
66  if(existPending(su.getEmail())) {
67  Messages.confirmProcess().add(I_.get("Email pending to confirm"))
68  .paragraph().add(I_.get("Resend confirmation e-mail")).show(() -> {
69  sendEmail(sue);
70  Clients.showNotification(I_.format("Confirmation e-mail has been sent to %s", sue.getEmail()));
71  });
72  } else if(existEmail(su.getEmail())) {
73  Clients.showNotification(I_.format("%s already exists", su.getEmail()));
74  } else {
75  su = new ContactsPU().saveObject(su);
76  sendEmail(su);
77  Clients.showNotification(I_.format("Confirmation e-mail has been sent to %s", su.getEmail()));
78  }
79  } else {
80  Clients.showNotification("No valid values");
81  }
82  }
83 
84  private static boolean existEmail(String email) {
85  Dao dao = new ContactsPU();
86  List l = dao.getResultList(
87  "select c from Connector c where c.value = ?",
88  new Object[] { email }
89  );
90  if(l.isEmpty()) {
91  l = dao.getResultList(
92  "select c from SignUp c where c.email = ? and confirmed = TRUE",
93  new Object[] { email }
94  );
95  }
96  return !l.isEmpty();
97  }
98 
99  private boolean existPending(String email) {
100  Dao dao = new ContactsPU();
101  List l = dao.getResultList(
102  "select c from SignUp c where c.email = ? and confirmed = FALSE",
103  new Object[] { email }
104  );
105  if(!l.isEmpty()) {
106  sue = (SignUp) l.iterator().next();
107  }
108  return !l.isEmpty();
109  }
110 
111  private boolean sendEmail(SignUp signUp) {
112  try {
113  IContact contact = Authentication.getIContact();
114  if(contact == null || !contact.isValid()) {
115  return false;
116  }
117  MailSenders.getPool()
118  .addUser(signUp.getName(), signUp.getEmail())
119  .put("signUp", signUp)
120  .put("contact", contact)
121  .put("motive", I_.format("%s would like you to signup in %s", contact.getName(), ElephantContext.getSiteName()))
122  .put("comment", StringParser.toHTML(signUp.getComment()))
123  .sendTemplate("sign-tellsomeone",
124  I_.format("%s would like you to signup in %s", contact.getName(), ElephantContext.getSiteName()));
125  sendMailToAdmin(signUp, contact);
126  return true;
127  } catch (EmailException ex) {
128  Logger.getLogger(TellSomeoneControl.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
129  return false;
130  }
131  }
132 
133  private static void sendMailToAdmin(SignUp signUp, IContact contact) {
134  MailSenders.getPool()
135  .addAdministrators()
136  .send("User wants friend to register",
137  contact.getName() + " -> " + signUp.getName() +
138  ": " +
139  signUp.getEmail() +
140  "\n\n" +
141  signUp.getComment());
142 // SendMail.toAdmin("User wants friend to register",
143 // contact.getName() + " -> " + signUp.getName() +
144 // ": " +
145 // signUp.getEmail() +
146 // "\n\n" +
147 // signUp.getComment());
148  }
149 
150 }
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 Messages confirmProcess()
Definition: Messages.java:95
Messages add(String word)
Definition: Messages.java:50
static String format(String msg, Object... arguments)
Definition: I_.java:49
static String get(String msg)
Definition: I_.java:41