BrightSide Workbench Full Report + Source Code
AdminRegisterComposer.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.io.UnsupportedEncodingException;
22 import java.util.List;
23 import java.util.Set;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
26 import org.amic.util.string.Strings;
27 import org.turro.action.Actions;
28 import org.turro.action.MailSenders;
29 import org.turro.action.PushSenders;
30 import org.turro.command.Command;
31 import org.turro.command.Context;
32 import org.turro.contacts.Connector;
33 import org.turro.contacts.Contact;
34 import org.turro.contacts.SignUp;
35 import org.turro.contacts.db.ContactsPU;
36 import org.turro.contacts.form.ContactWrapper;
37 import org.turro.elephant.context.Application;
38 import org.turro.elephant.context.ElephantContext;
39 import org.turro.elephant.context.HeadlessApplication;
40 import org.turro.elephant.context.IConstructor;
41 import org.turro.elephant.impl.context.ContextFactory;
42 import org.turro.elephant.security.IUser;
43 import org.turro.elephant.util.ZkossUtils;
44 import org.turro.i18n.I_;
45 import org.turro.jpa.Dao;
46 import org.turro.mail.impl.MailPool;
47 import org.turro.mail.queue.QueueManager;
48 import org.turro.marker.ElephantMarker;
49 import org.turro.security.SocialGroups;
50 import org.zkoss.zk.ui.Component;
51 import org.zkoss.zk.ui.event.Event;
52 import org.zkoss.zk.ui.select.SelectorComposer;
53 import org.zkoss.zk.ui.select.annotation.Listen;
54 import org.zkoss.zk.ui.select.annotation.Wire;
55 import org.zkoss.zk.ui.util.Clients;
56 import org.zkoss.zul.Textbox;
57 
62 @Deprecated
63 public class AdminRegisterComposer extends SelectorComposer<Component> {
64 
65  @Wire("#name")
66  private Textbox name;
67 
68  @Wire("#email")
69  private Textbox email;
70 
71  private SignUp sue;
72 
73  @Listen("onClick = #register")
74  public void onRegister(Event event) {
75  SignUp su = new SignUp();
76  su.setName(name.getValue());
77  su.setEmail(email.getValue());
78  su.setComment("");
79  if(su.isValid()) {
80  if(existPending(su.getEmail())) {
81  ZkossUtils.confirmProcess(I_.get("Email pending to confirm") +
82  "\n\n" +
83  I_.get("Resend confirmation e-mail"), new Command() {
84  @Override
85  public Object execute(Context context) {
86  sendEmail(sue);
87  Clients.showNotification(I_.format("Confirmation e-mail has been sent to %s", sue.getEmail()));
88  return true;
89  }
90  });
91  } else if(existEmail(su.getEmail())) {
92  Clients.showNotification(I_.format("%s already exists", su.getEmail()));
93  } else {
94  try {
95  if(createContact(su)) {
96  su.setConfirmed(true);
97  su = new ContactsPU().saveObject(su);
98  sendMailToAdmin(su);
99  sendWelcomeMailToUser(su);
100  Clients.showNotification(I_.get("Created"));
101  }
102  } catch (Exception ex) {
103  Logger.getLogger(AdminRegisterComposer.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
104  }
105  }
106  } else {
107  Clients.showNotification("No valid values");
108  }
109  }
110 
111  private static boolean existEmail(String email) {
112  Dao dao = new ContactsPU();
113  List l = dao.getResultList(
114  "select c from Connector c where c.value = ?",
115  new Object[] { email }
116  );
117  if(l.isEmpty()) {
118  l = dao.getResultList(
119  "select c from SignUp c where c.email = ? and confirmed = TRUE",
120  new Object[] { email }
121  );
122  }
123  return !l.isEmpty();
124  }
125 
126  private boolean existPending(String email) {
127  Dao dao = new ContactsPU();
128  List l = dao.getResultList(
129  "select c from SignUp c where c.email = ? and confirmed = FALSE",
130  new Object[] { email }
131  );
132  if(!l.isEmpty()) {
133  sue = (SignUp) l.iterator().next();
134  }
135  return !l.isEmpty();
136  }
137 
138  private boolean sendEmail(SignUp signUp) {
139  IConstructor constructor = HeadlessApplication.getInstance().getConstructor();
140  MailPool mp = (MailPool) ContextFactory.getImplementation(constructor, "IMailPool");
141  mp.setEncoding(ElephantContext.getEncoding());
142  mp.addCssFile(ElephantContext.getRealPath("/_internal/css/mail.css"));
143  mp.addToPool(null, signUp.getEmail(), null,
144  I_.get("Email pending to confirm") + " : " + ElephantContext.getSiteName(),
145  getMessage(signUp, constructor), "text/html");
146  mp.sendPool();
147  return true;
148  }
149 
150  private String getMessage(SignUp signUp, IConstructor constructor) {
151  ElephantMarker em = new ElephantMarker(constructor);
152  em.put("signUp", signUp);
153  return em.parse("signup", "pending");
154  }
155 
156  public static boolean createContact(SignUp signUp) throws Exception {
157  if(Application.getApplication().isInRole("contact:new") && !existEmail(signUp.getEmail())) {
158  Contact contact = new Contact();
159  contact.setName(signUp.getName());
160  Connector email = new Connector();
161  email.setContact(contact);
163  email.setValue(signUp.getEmail());
164  contact.getConnectors().add(email);
165  if(!Strings.isBlank(ElephantContext.getSiteSyndicate())) {
166  String groups[] = ElephantContext.getSiteSyndicate().split(",");
167  SocialGroups.syndicate(List.of(contact), Set.of(groups), null);
168  contact = new ContactWrapper(contact).save();
169  }
170  new QueueManager().subscribeDefaults(contact.getIContact());
171  return true;
172  }
173  return false;
174  }
175 
176  private static void sendMailToAdmin(SignUp signUp) {
177  String message = signUp.getName() + ": " + signUp.getEmail();
178  if(!Strings.isBlank(signUp.getComment())) message += "\n" + signUp.getComment();
179  if(!Strings.isBlank(signUp.getValueMap())) message += "\n" + signUp.getValueMap();
180  MailSenders.getPool().addAdministrators().send("New user", message);
181  PushSenders.getPool().addAdministrators().send("New user", message);
182  }
183 
184  private static void sendWelcomeMailToUser(SignUp signUp) throws UnsupportedEncodingException {
185  IConstructor constructor = HeadlessApplication.getInstance().getConstructor();
186 
187  ElephantMarker em = new ElephantMarker(constructor);
188  em.put("signUp", signUp);
189  try {
190  em.put("link", ElephantContext.getServerUrl("http") + "?" + Actions.createAction(signUp.getEmail(), "/user/"));
191  } catch (Exception ex) {
192  Logger.getLogger(AdminRegisterComposer.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
193  }
194 
195  MailPool mp = (MailPool) ContextFactory.getImplementation(constructor, "IMailPool");
196  mp.setEncoding(ElephantContext.getEncoding());
197  mp.addCssFile(ElephantContext.getRealPath("/_internal/css/mail.css"));
198  mp.addToPool(null, signUp.getEmail(), null,
199  I_.get("Welcome") + " : " + ElephantContext.getSiteName(),
200  em.parse("signup", "welcome"), "text/html");
201  mp.sendPool();
202  }
203 
204 }
205 
static IMailSender getPool()
static IPushSender getPool()
void setContact(org.turro.contacts.Contact contact)
Definition: Connector.java:103
void setDescription(String description)
Definition: Connector.java:87
void setValue(String value)
Definition: Connector.java:95
void setName(String name)
Definition: Contact.java:208
Set< Connector > getConnectors()
Definition: Contact.java:358
void setComment(String comment)
Definition: SignUp.java:82
void setEmail(String email)
Definition: SignUp.java:74
void setConfirmed(boolean confirmed)
Definition: SignUp.java:90
void setName(String name)
Definition: SignUp.java:66
static String getServerUrl(String scheme)
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
void subscribeDefaults(IContact contact)
static void syndicate(List< Contact > contacts, Set< String > socialGroupIds, Dao dao)
static final String CONNECTOR_EMAIL
Definition: IUser.java:32