View Javadoc

1   /**
2    * Copyright 2011 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  
16  package org.kuali.mobility.emergencyinfo.dao;
17  
18  import java.util.List;
19  
20  import javax.persistence.EntityManager;
21  import javax.persistence.NoResultException;
22  import javax.persistence.OptimisticLockException;
23  import javax.persistence.PersistenceContext;
24  import javax.persistence.Query;
25  
26  import org.kuali.mobility.emergencyinfo.entity.EmergencyInfo;
27  import org.springframework.stereotype.Repository;
28  
29  @Repository
30  public class EmergencyInfoDaoImpl implements EmergencyInfoDao {
31  
32      @PersistenceContext
33      private EntityManager entityManager;
34      
35      @SuppressWarnings("unchecked")
36      public List<EmergencyInfo> findAllEmergencyInfo() {
37          Query query = entityManager.createQuery("select ei from EmergencyInfo ei order by ei.order");
38          try {
39              return query.getResultList();
40          } catch (NoResultException e) {
41              return null;
42          }
43      }
44  
45      public EmergencyInfo findEmergencyInfoById(Long id) {
46          Query query = entityManager.createQuery("select ei from EmergencyInfo ei where ei.emergencyInfoId = :id");
47          query.setParameter("id", id);
48          try {
49              return (EmergencyInfo) query.getSingleResult();
50          } catch (NoResultException e) {
51              return null;
52          }
53      }
54  
55      @SuppressWarnings("unchecked")
56      public List<EmergencyInfo> findAllEmergencyInfoByCampus(String campus) {
57          try {
58              Query query = entityManager.createQuery("select ei from EmergencyInfo ei where ei.campus like :campus order by ei.order");
59              query.setParameter("campus", campus);
60              return query.getResultList();
61          } catch (NoResultException e) {
62              return null;
63          }
64      }
65      
66      public Long saveEmergencyInfo(EmergencyInfo emergencyInfo) {
67          if (emergencyInfo == null) {
68              return null;
69          }
70          try {
71              if (emergencyInfo.getEmergencyInfoId() == null) {
72                  entityManager.persist(emergencyInfo);
73              } else {
74                  entityManager.merge(emergencyInfo);
75              }
76          } catch (OptimisticLockException oe) {
77              return null;
78          }
79          return emergencyInfo.getEmergencyInfoId();
80      }
81  
82      public void deleteEmergencyInfoById(Long id) {
83          Query query = entityManager.createQuery("delete from EmergencyInfo ei where ei.emergencyInfoId = :id");
84          query.setParameter("id", id);
85          query.executeUpdate();
86      }
87  
88      public void reorder(Long id, boolean up) {
89          List<EmergencyInfo> list = findAllEmergencyInfo();
90          EmergencyInfo last = null;
91          boolean flag = false;
92          int index = -1;
93          int count = list.get(0).getOrder();
94          for (EmergencyInfo emergencyInfo : list) {
95              index++;
96              if (emergencyInfo.getEmergencyInfoId().equals(id)) {            
97                  if (up && last != null) {
98                      swap(last, emergencyInfo);
99                      count = last.getOrder() + 1;
100                     continue;
101                 } else if (!up) {
102                     EmergencyInfo next = list.get(index + 1);
103                     swap(emergencyInfo, next);
104                     count = next.getOrder() + 1;
105                     continue;                    
106                 }
107                 flag = true;
108             }
109             if (flag) {
110                 emergencyInfo.setOrder(count);  
111                 entityManager.merge(emergencyInfo);
112             }
113             count++;
114             last = emergencyInfo;
115         }
116     }
117 
118     private void swap(EmergencyInfo one, EmergencyInfo two) {
119         int temp = one.getOrder();
120         one.setOrder(two.getOrder());
121         two.setOrder(temp);
122         entityManager.merge(one);
123         entityManager.merge(two);
124     }
125     
126     public EntityManager getEntityManager() {
127         return entityManager;
128     }
129 
130     public void setEntityManager(EntityManager entityManager) {
131         this.entityManager = entityManager;
132     }
133 
134 }