BrightSide Workbench Full Report + Source Code
LoadLogged.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 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 package org.turro.contacts.util;
19 
20 import javax.persistence.Query;
21 import org.turro.contacts.Connector;
22 import org.turro.contacts.Contact;
23 import org.turro.contacts.db.ContactsPU;
24 import org.turro.elephant.context.Application;
25 import org.turro.elephant.security.IUser;
26 import org.turro.jpa.Dao;
27 
32 @Deprecated
33 public class LoadLogged {
34 
35  private Contact contact = null;
36 
37  public LoadLogged(Application app) {
38  Dao dao = new ContactsPU();
39  try {
40  Query q = dao.createNamedQuery("contactByLogin");
41  q.setParameter("login", app.getConstructor().getUser().getId());
42  contact = (Contact) q.getSingleResult();
43  if(contact != null) {
44  app.getHttpServletRequest().setAttribute("name", contact.getName());
45  app.getHttpServletRequest().setAttribute("email", getConnector(contact, IUser.CONNECTOR_EMAIL));
46  app.getHttpServletRequest().setAttribute("emailLogin", getConnector(contact, IUser.CONNECTOR_EMAIL_LOGIN));
47  }
48  } finally {
49  dao.close();
50  }
51  }
52 
53  public String getId() {
54  return contact != null ? contact.getId() : null;
55  }
56 
57  public Contact getContact() {
58  return contact;
59  }
60 
61  private String getConnector(Contact contact, String key) {
62  for(Connector connector : contact.getConnectors()) {
63  if(key.equalsIgnoreCase(connector.getDescription())) {
64  return connector.getValue();
65  }
66  }
67  return "";
68  }
69 
70 }
Set< Connector > getConnectors()
Definition: Contact.java:358
abstract HttpServletRequest getHttpServletRequest()
void close()
Definition: Dao.java:71
Query createNamedQuery(String name)
Definition: Dao.java:66
static final String CONNECTOR_EMAIL
Definition: IUser.java:32
static final String CONNECTOR_EMAIL_LOGIN
Definition: IUser.java:35