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.push.dao;
17  
18  import java.util.List;
19  
20  import javax.persistence.EntityManager;
21  import javax.persistence.NoResultException;
22  import javax.persistence.PersistenceContext;
23  import javax.persistence.Query;
24  
25  import org.kuali.mobility.push.entity.Sender;
26  import org.springframework.stereotype.Repository;
27  import org.springframework.transaction.annotation.Transactional;
28  
29  /**
30   * Implementation of the Sender Data Access Object.
31   * 
32   * @author Kuali Mobility Team (mobility.dev@kuali.org)
33   * @since 2.0.0
34   */
35  @Repository
36  public class SenderDaoImpl implements SenderDao{
37  
38  	/** A reference to a logger */
39  	private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DeviceDaoImpl.class);		
40  	
41  	@PersistenceContext
42      private EntityManager entityManager;
43  	
44  	/**
45  	 * Creates new instance of the <code>DeviceDaoImpl</code>.
46  	 */
47  	public SenderDaoImpl(){}
48  	
49  	/*
50  	 * (non-Javadoc)
51  	 * @see org.kuali.mobility.push.dao.SenderDao#findAllSenders()
52  	 */
53  	public List<Sender> findAllSenders(){
54          Query query = entityManager.createQuery("select s from Sender s order by s.name");
55          return query.getResultList();	
56  	}
57  
58  	/*
59  	 * (non-Javadoc)
60  	 * @see org.kuali.mobility.push.dao.SenderDao#findAllUnhiddenSenders()
61  	 */
62  	public List<Sender> findAllUnhiddenSenders(){
63          Query query = entityManager.createQuery("select s from Sender s where s.hidden = 0 order by s.name");
64          return query.getResultList();	
65  	}
66  	
67  	/*
68  	 * (non-Javadoc)
69  	 * @see org.kuali.mobility.push.dao.SenderDao#findSenderById(long)
70  	 */
71  	public Sender findSenderById(long id){
72          Query query = entityManager.createQuery("select s from Sender s where s.id = :id");
73          query.setParameter("id", id);        
74          Sender s;
75          try{
76          	s = (Sender)query.getSingleResult();
77          }catch(NoResultException e){
78          	LOG.info("Sender with id " + id + " was not found or was invalid.");
79          	return null;
80          }
81          return s;	}
82  	
83  	/*
84  	 * (non-Javadoc)
85  	 * @see org.kuali.mobility.push.dao.SenderDao#findSenderByName(java.lang.String)
86  	 */
87  	public Sender findSenderByName(String name){
88          Query query = entityManager.createQuery("select s from Sender s where s.name = :name");
89          query.setParameter("name", name);        
90          Sender s;
91          try{
92          	s = (Sender)query.getSingleResult();
93          }catch(NoResultException e){
94          	LOG.info("Sender with name " + name + " was not found or was invalid.");
95          	return null;
96          }
97          return s;	
98  	}
99  
100 	/*
101 	 * (non-Javadoc)
102 	 * @see org.kuali.mobility.push.dao.SenderDao#findSenderBySenderKey(java.lang.String)
103 	 */
104 	public Sender findSenderBySenderKey(String senderKey){
105         Query query = entityManager.createQuery("select s from Sender s where s.senderKey = :senderKey");
106         query.setParameter("senderKey", senderKey);  
107         Sender s;
108         try{
109         	s = (Sender)query.getSingleResult();
110         }catch(NoResultException e){
111         	LOG.info("Sender with senderKey " + senderKey + " was not found or was invalid.");
112         	return null;
113         }
114         return s;
115 	}
116 	
117 	/*
118 	 * (non-Javadoc)
119 	 * @see org.kuali.mobility.push.dao.SenderDao#findSenderByShortName(java.lang.String)
120 	 */
121 	public Sender findSenderByShortName(String shortName){
122         Query query = entityManager.createQuery("select s from Sender s where s.shortName = :shortName");
123         query.setParameter("shortName", shortName); 
124         Sender s;
125         try{
126         	s = (Sender)query.getSingleResult();
127         }catch(NoResultException e){
128         	LOG.info("Sender with Shortname " + shortName + " was not found or was invalid.");
129         	return null;
130         }
131         return s;
132 	}
133 	
134 	/*
135 	 * (non-Javadoc)
136 	 * @see org.kuali.mobility.push.dao.SenderDao#isValidSenderKey(java.lang.String)
137 	 */
138 	public 	boolean isValidSenderKey(String senderKey){
139         Query query = entityManager.createQuery("select s from Sender s where s.senderKey = :senderKey");
140         query.setParameter("senderKey", senderKey);        
141         try{
142         	query.getSingleResult();
143         }catch(Exception e){
144         	LOG.info("SenderKey " + senderKey + " was not found or was invalid.");
145         	return false;
146         }
147         return true;        
148 	}
149 	
150 	/*
151 	 * (non-Javadoc)
152 	 * @see org.kuali.mobility.push.dao.SenderDao#saveSender(org.kuali.mobility.push.entity.Sender)
153 	 */
154 	public void saveSender(Sender sender){
155 		LOG.info("SenderDaoImpl.saveSender");
156 		if(sender == null){
157     		return;
158     	}
159 		LOG.info("SenderDaoImpl.saveSender : id = " + sender.getId());
160 		if(sender.getId() == null){
161     		LOG.info("SenderDaoImpl.saveSender : persist");
162     		entityManager.persist(sender);
163     	}else{
164     		LOG.info("SenderDaoImpl.saveSender : merge");
165     		entityManager.merge(sender);
166     	}
167 	}
168 	
169 	/*
170 	 * (non-Javadoc)
171 	 * @see org.kuali.mobility.push.dao.SenderDao#removeSender(org.kuali.mobility.push.entity.Sender)
172 	 */
173 	@Transactional
174 	public boolean removeSender(Sender sender){
175     	boolean result = true;
176     	if(sender == null){
177     		return false;
178     	}
179     	if(sender.getId() != null){
180     		try{
181     			Sender s = entityManager.find(Sender.class, sender.getId());
182     			entityManager.remove(s);
183     		}catch(Exception e){
184     			LOG.info("Exception Caught: " + e.getMessage());
185     			result = false;
186     		}
187     	}
188     	return result;
189 	}
190 	
191 	/** A reference to the <code>EntityManager</code> */ /**
192 	 * Returns the reference to the <code>EntityManager</code>
193 	 * @return The reference to the <code>EntityManager</code>
194 	 */
195     public EntityManager getEntityManager() {
196         return entityManager;
197     }
198 
199 	/**
200 	 * Sets the reference to the <code>EntityManager</code>
201 	 * @param entityManager The reference to the <code>EntityManager</code>
202 	 */
203     public void setEntityManager(EntityManager entityManager) {
204         this.entityManager = entityManager;
205     }
206 }