BrightSide Workbench Full Report + Source Code
AccountInput.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2012 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.contact;
20 
21 import org.turro.contacts.Contact;
22 import org.turro.contacts.db.ContactsPU;
23 import org.turro.elephant.db.WhereClause;
24 import org.turro.i18n.I_;
25 import org.zkoss.lang.Strings;
26 import org.zkoss.zk.ui.Component;
27 import org.zkoss.zk.ui.WrongValueException;
28 import org.zkoss.zk.ui.event.Event;
29 import org.zkoss.zk.ui.event.EventListener;
30 import org.zkoss.zk.ui.event.Events;
31 import org.zkoss.zul.Constraint;
32 import org.zkoss.zul.Textbox;
33 
38 public class AccountInput extends Textbox {
39 
40  private Contact contact;
41 
42  public AccountInput(final Contact contact) throws WrongValueException {
43  super(contact.getEmailAccount());
44  this.contact = contact;
45  setCols(30);
46  Constraint ctt = new Constraint() {
47  @Override
48  public void validate(Component comp, Object value) throws WrongValueException {
49  if(!isReadonly() && !Strings.isEmpty((String) value)) {
50  if(exists((String) value)) {
51  setText("");
52  throw new WrongValueException(AccountInput.this, I_.get("Login already exists"));
53  }
54  }
55  }
56  };
57  setConstraint(ctt);
58  addEventListener(Events.ON_CHANGE, new EventListener<Event>() {
59  @Override
60  public void onEvent(Event event) throws Exception {
61  contact.setEmailAccount(getValue());
62  }
63  });
64  }
65 
66  private boolean exists(String value) {
67  WhereClause wc = new WhereClause();
68  wc.addClause("select distinct c from Contact as c");
69  wc.addClause("where c.id <> :id and c.emailAccount = :email");
70  wc.addNamedValue("id", contact.getId());
71  wc.addNamedValue("email", value);
72  return !new ContactsPU().getResultList(wc).isEmpty();
73  }
74 
75 }
void addNamedValue(String name, Object value)
static String get(String msg)
Definition: I_.java:41