View Javadoc

1   /**
2    * Copyright 2011-2013 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.push.dao;
17  
18  import java.sql.Timestamp;
19  import java.util.Iterator;
20  import java.util.List;
21  
22  import javax.persistence.EntityManager;
23  import javax.persistence.PersistenceContext;
24  import javax.persistence.Query;
25  
26  import org.apache.log4j.Logger;
27  import org.kuali.mobility.push.entity.Device;
28  import org.kuali.mobility.push.entity.Push;
29  import org.kuali.mobility.push.entity.PushDeviceTuple;
30  import org.kuali.mobility.push.service.PushDeviceTupleService;
31  import org.springframework.beans.factory.annotation.Autowired;
32  import org.springframework.stereotype.Repository;
33  import org.springframework.transaction.annotation.Transactional;
34  
35  /**
36   * Implementation of the Push Data Access Object for <code>Push</code> objects
37   * 
38   * @author Kuali Mobility Team (mobility.dev@kuali.org)
39   * @since 2.0.0
40   */
41  @Repository
42  public class PushDaoImpl implements PushDao {
43  
44  	/** A reference to a logger */
45  	private static final Logger LOG = Logger.getLogger(PushDaoImpl.class);
46  
47  	/** A reference to the <code>EntityManger</code> */
48  	@PersistenceContext
49  	private EntityManager entityManager;
50  
51  	@Autowired
52  	private PushDeviceTupleService pdtService;
53  	
54  	/**
55  	 * Creates a new instance of a <code>PushDaoImpl</code>
56  	 */
57  	public PushDaoImpl(){}
58  
59  	/*
60  	 * (non-Javadoc)
61  	 * @see org.kuali.mobility.push.dao.PushDao#findPushById(java.lang.Long)
62  	 */
63  	@SuppressWarnings("unchecked")
64  	public Push findPushById(Long id){
65  		Query query = entityManager.createNamedQuery("Push.find");
66  		query.setParameter("pushId", id);
67  		Push result;
68  		try{
69  			result = (Push) query.getSingleResult();
70  		}catch(Exception e){
71  			LOG.info("Exception: " + e.getMessage());
72  			result = null;
73  		}
74  		return result;
75  	}
76  
77  	/*
78  	 * (non-Javadoc)
79  	 * @see org.kuali.mobility.push.dao.PushDao#findAllPush()
80  	 */
81  	@SuppressWarnings("unchecked")
82  	public List<Push> findAllPush(){
83  		Query query = entityManager.createNamedQuery("Push.findAll");
84  		return query.getResultList();
85  	}
86  
87  	/*
88  	 * (non-Javadoc)
89  	 * @see org.kuali.mobility.push.dao.PushDao#countPushes()
90  	 */
91  	@SuppressWarnings("unchecked")
92  	public int countPushes(){
93  		Query query = entityManager.createNamedQuery("Push.countAll");
94  		return ((Long)query.getSingleResult()).intValue();
95  	}
96  
97  	/*
98  	 * (non-Javadoc)
99  	 * @see org.kuali.mobility.push.dao.PushDao#findUnsentPushTuples()
100 	 */
101 	@SuppressWarnings("unchecked")
102 	public List<PushDeviceTuple> findUnsentPushTuples(){
103 		Query query = entityManager.createNamedQuery("PushDeviceTuple.findUnsent");
104 		return query.getResultList();
105 	}
106 
107 	/*
108 	 * (non-Javadoc)
109 	 * @see org.kuali.mobility.push.dao.PushDao#savePush(org.kuali.mobility.push.entity.Push)
110 	 */
111 	@Transactional
112 	public void savePush(Push push){
113 		if(push == null){
114 			return;
115 		}
116 		if(push.getPushId() == null){
117 			entityManager.persist(push);
118 		}else{
119 			entityManager.merge(push);
120 		}
121 	}
122 
123 	/*
124 	 * (non-Javadoc)
125 	 * @see org.kuali.mobility.push.dao.PushDao#removePush(org.kuali.mobility.push.entity.Push)
126 	 */
127 	@Transactional
128 	public boolean removePush(Push push){
129 		boolean result = true;
130 		if(push == null){
131 			result = false;
132 		} else if(push.getPushId() == null) {
133 			result = false;
134 		} else {
135 			
136 			int removedTuples = pdtService.removeTuplesForPush(push);
137 			if(removedTuples > 0){
138 				LOG.info("Removed " + removedTuples + " tuples associated with Push notifications.");
139 			}else{
140 				LOG.info("No tuples associated with Push notifications removed.");				
141 			}
142 			
143 			try{
144 				getEntityManager().remove(getEntityManager().contains(push)?push:getEntityManager().merge(push));
145 			}catch(Exception e){
146 				LOG.info("Exception while trying to remove push notification.", e);
147 				result = false;
148 			}
149 		}
150 		return result;
151 	}	
152 	
153 	/*
154 	 * (non-Javadoc)
155 	 * @see org.kuali.mobility.push.dao.PushDao#savePush(org.kuali.mobility.push.entity.Push, java.util.List)
156 	 */
157 	@Transactional
158 	public void savePush(Push push, List<Device> devices){
159 		if(push == null){
160 			return;
161 		}
162 		if(push.getPushId() == null){
163 			entityManager.persist(push);
164 		}else{
165 			entityManager.merge(push);
166 		}
167 		Iterator<Device> i = devices.iterator();
168 		while(i.hasNext()){
169 			Device d = i.next();
170 			PushDeviceTuple pdt = new PushDeviceTuple();
171 			pdt.setPushId(push.getPushId());
172 			pdt.setDeviceId(d.getId());
173 			pdt.setPostedTimestamp(new Timestamp(System.currentTimeMillis()));
174 			pdt.setStatus(PushDeviceTuple.STATUS_PENDING);
175 			if(pdt.getId() == null){
176 				entityManager.persist(pdt);
177 			}else{
178 				entityManager.merge(pdt);
179 			}
180 		}
181 	}
182 
183 	/*
184 	 * (non-Javadoc)
185 	 * @see org.kuali.mobility.push.dao.PushDao#findDevicesForPush(org.kuali.mobility.push.entity.Push)
186 	 */
187 	@Transactional
188 	public List<Device> findDevicesForPush(Push push){
189 		Query query = entityManager.createNamedQuery("PushDeviceTuple.findPushDevices");
190 		query.setParameter("pushId", push.getPushId());
191 		return query.getResultList();
192 	}
193 
194 	@Transactional
195 	public void savePushDeviceTuples(List<PushDeviceTuple> tuples){
196 		if(tuples == null){
197 			return;
198 		}
199 		Iterator<PushDeviceTuple> i = tuples.iterator();
200 		while(i.hasNext()){
201 			PushDeviceTuple pdt = i.next();
202 			if(pdt.getId() == null){
203 				entityManager.persist(pdt);
204 			}else{
205 				entityManager.merge(pdt);
206 			}
207 		}
208 	}
209 
210 	public EntityManager getEntityManager() {
211 		return entityManager;
212 	}
213 
214 	public void setEntityManager(EntityManager entityManager) {
215 		this.entityManager = entityManager;
216 	}
217 
218 	/**
219 	 * @return the pdtService
220 	 */
221 	public PushDeviceTupleService getPdtService() {
222 		return pdtService;
223 	}
224 
225 	/**
226 	 * @param pdtService the pdtService to set
227 	 */
228 	public void setPdtService(PushDeviceTupleService pdtService) {
229 		this.pdtService = pdtService;
230 	}
231 
232 
233 }