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.writer.dao;
17  
18  import org.kuali.mobility.writer.entity.Topic;
19  import org.springframework.stereotype.Repository;
20  import org.springframework.transaction.annotation.Transactional;
21  
22  import javax.persistence.EntityManager;
23  import javax.persistence.PersistenceContext;
24  import javax.persistence.Query;
25  import java.util.List;
26  
27  /**
28   * Implementation for the TopicDao
29   * @author Kuali Mobility Team (mobility.collab@kuali.org)
30   * @since 3.0.0
31   */
32  @Repository
33  public class TopicDaoImpl implements TopicDao{
34  
35  	@PersistenceContext
36  	private EntityManager entityManager;
37  
38  	public EntityManager getEntityManager() {
39  		return entityManager;
40  	}
41  
42  	public void setEntityManager(EntityManager entityManager) {
43  		this.entityManager = entityManager;
44  	}
45  
46  
47  	@Override
48  	public List<Topic> getTopics() {
49  		Query query = getEntityManager().createNamedQuery("Topic.getTopics");
50  		return query.getResultList();
51  	}
52  
53  
54  	@Override
55  	public Topic getTopic(long topicId) {
56  		return getEntityManager().find(Topic.class, topicId);
57  	}
58  
59      @Override
60      @Transactional
61      public Topic saveTopic(Topic topic) {
62  
63          if(topic.getId() == null){
64              entityManager.persist(topic);
65              return topic;
66          }
67          else{
68              return entityManager.merge(topic);
69          }
70  
71      }
72  
73  
74  }