1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.imca.services;
17
18 import java.io.UnsupportedEncodingException;
19 import java.security.NoSuchAlgorithmException;
20 import java.util.List;
21
22 import javax.persistence.Query;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 import net.sf.imca.model.PersonBO;
28 import net.sf.imca.model.entities.AddressEntity;
29 import net.sf.imca.model.entities.PersonEntity;
30 import net.sf.imca.model.exceptions.DataCheckingException;
31
32
33
34
35
36
37 public class EditDataService extends Service {
38
39
40
41
42 private Log log = LogFactory.getLog(EditDataService.class);
43
44 public Object saveEntity(Object entityObject, String id) throws Exception {
45 startTransaction();
46
47 Object dbObject;
48 if (id == null || "null".equals(id) || "".equals(id) || "0".equals(id)) {
49 dbObject = ReflectionUtil.getObject(entityObject.getClass().getName());
50 } else {
51 long dbId = new Long(id).longValue();
52 dbObject = em.find(entityObject.getClass(), dbId);
53 }
54 ReflectionUtil.replaceDBObject(dbObject, entityObject);
55
56 em.persist(dbObject);
57 log.info("Saved Entity: (" + dbObject.getClass().getName() + ") "
58 + dbObject.toString());
59
60 endTransaction();
61 return dbObject;
62 }
63
64 public PersonBO saveMyData(PersonEntity personEntity, String password, String confirmPassword)
65 throws DataCheckingException {
66
67 if (!confirmPassword.equals(password)) {
68 throw new DataCheckingException("Password and Confirm Password do not match");
69 }
70
71 startTransaction();
72 PersonBO person = new PersonBO(em, personEntity.getId());
73 if (!"".equals(password)) {
74 try {
75 person.setPasswordEncodeAndSet(password);
76 } catch (UnsupportedEncodingException e) {
77 throw new DataCheckingException("Passwords encoding problem");
78 } catch (NoSuchAlgorithmException e) {
79 throw new DataCheckingException("Passwords encoding problem");
80 }
81 }
82 person.replaceEmail(em, personEntity.getEmail());
83 person.getEntity().setFirstName(personEntity.getFirstName());
84 person.getEntity().setLastName(personEntity.getLastName());
85 person.getEntity().setTel(personEntity.getTel());
86 person.getEntity().setMobile(personEntity.getMobile());
87 person.getEntity().setClub(personEntity.getClub());
88 person.getEntity().setLanguageCode(personEntity.getLanguageCode());
89 if (person.getEntity().getAddress() == null) {
90 person.getEntity().setAddress(new AddressEntity());
91 }
92 person.getEntity().getAddress().setStreet1(personEntity.getAddress().getStreet1());
93 person.getEntity().getAddress().setStreet2(personEntity.getAddress().getStreet2());
94 person.getEntity().getAddress().setArea(personEntity.getAddress().getArea());
95 person.getEntity().getAddress().setPostCode(personEntity.getAddress().getPostCode());
96 person.getEntity().getAddress().setCity(personEntity.getAddress().getCity());
97 person.getEntity().getAddress().setCountryCode(personEntity.getAddress().getCountryCode());
98
99 em.persist(person.getEntity().getAddress());
100 em.persist(person.getEntity());
101 log.info("Saved PersonEntity: " + person.getEntity().toString());
102 try {
103 this.endTransaction();
104 } catch (Exception ex) {
105 log.error(ex);
106 return null;
107 }
108 return person;
109 }
110
111 @SuppressWarnings("unchecked")
112 public Object findEntity(Class clazz, long goToId) {
113 startTransaction();
114 Object object = em.find(clazz, goToId);
115 try {
116 this.endTransaction();
117 } catch (Exception ex) {
118 log.error(ex);
119 return null;
120 }
121 return object;
122 }
123
124 @SuppressWarnings("unchecked")
125 public List search(String entityName, String search) {
126 startTransaction();
127 Query query = em.createNamedQuery(entityName + "BasicSearch");
128 query.setParameter("search", "%" + search + "%");
129 List list = query.getResultList();
130 try {
131 this.endTransaction();
132 } catch (Exception ex) {
133 log.error(ex);
134 return null;
135 }
136 return list;
137 }
138
139
140
141
142
143
144
145
146 public boolean deleteEntity(Object entityObject, String id) throws Exception {
147
148 startTransaction();
149 Object object = em.find(entityObject.getClass(), new Long(id).longValue());
150 em.remove(object);
151 endTransaction();
152 log.info("Deleted object: " + object.toString());
153 return true;
154 }
155
156 }