View Javadoc

1   /*
2    * Copyright 2007 The International Moth Class Association (IMCA)
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Service for CRUD operations by the Data Admin Servlet.
34   *
35   * @author dougculnane
36   */
37  public class EditDataService extends Service {
38  
39      /**
40       * Apache Commons Logger specific to this class.
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      * Delete the Database entity object.
141      * @param entityObject
142      * @param id
143      * @return
144      * @throws Exception
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 }