View Javadoc

1   /**
2    * Copyright 2011-2012 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.library.dao;
17  
18  import java.util.List;
19  
20  import javax.persistence.EntityManager;
21  import javax.persistence.PersistenceContext;
22  import javax.persistence.Query;
23  
24  import org.apache.log4j.Logger;
25  import org.kuali.mobility.library.entity.Library;
26  import org.kuali.mobility.library.entity.LibraryHourPeriod;
27  import org.kuali.mobility.library.entity.LibraryHourSet;
28  import org.springframework.stereotype.Repository;
29  import org.springframework.transaction.annotation.Transactional;
30  
31  /**
32   * Implementation of te <code>LibraryDao</code>
33   * @author Kuali Mobility Team (mobility.collab@kuali.org)
34   * @since 2.3.0
35   */
36  @Repository
37  public class LibraryDaoImpl implements LibraryDao {
38  	
39  	/**
40  	 * A reference to a logger.
41  	 */
42  	private static final Logger LOG = Logger.getLogger( LibraryDaoImpl.class );
43  
44  	/**
45  	 * Reference to the <code>EntityManager</code>
46  	 */
47  	@PersistenceContext
48  	private EntityManager entityManager;
49  
50  
51  	/*
52  	 * (non-Javadoc)
53  	 * @see org.kuali.mobility.library.dao.LibraryDao#getLibraries()
54  	 */
55  	@Override
56  	public List<Library> getLibraries(){
57  		String nq = "Library.getLibraries";
58  		Query query = getEntityManager().createNamedQuery(nq);
59  		return query.getResultList();
60  	}
61  
62  	/*
63  	 * (non-Javadoc)
64  	 * @see org.kuali.mobility.library.dao.LibraryDao#getLibrary(long)
65  	 */
66  	@Override
67  	public Library getLibrary(long libraryId) {
68  		return this.entityManager.find(Library.class, libraryId);
69  	}
70  
71  	/*
72  	 * (non-Javadoc)
73  	 * @see org.kuali.mobility.library.dao.LibraryDao#saveLibrary(org.kuali.mobility.library.entity.Library)
74  	 */
75  	@Override
76  	@Transactional
77  	public Library saveLibrary(Library library) {
78  		if (library.getId() == null){
79  			this.entityManager.persist(library);
80  		}
81  		else {
82  			library = this.entityManager.merge(library);
83  		}
84  		return library;
85  	}
86  
87  	/*
88  	 * (non-Javadoc)
89  	 * @see org.kuali.mobility.library.dao.LibraryDao#getLibraryHourSets(long)
90  	 */
91  	@Override
92  	public List<LibraryHourSet> getLibraryHourSets(long libraryId) {
93  		Query q = this.entityManager.createNamedQuery("LibraryHourSet.getHourSets");
94  		q.setParameter("libraryId", libraryId);
95  		return q.getResultList();
96  	}
97  
98  	/*
99  	 * (non-Javadoc)
100 	 * @see org.kuali.mobility.library.dao.LibraryDao#saveLibraryHourSets(org.kuali.mobility.library.entity.LibraryHourSet)
101 	 */
102 	@Override
103 	@Transactional
104 	public LibraryHourSet saveLibraryHourSets(LibraryHourSet lhs) {
105 		if (lhs.getId() == null){
106 			this.entityManager.persist(lhs);
107 			return lhs;
108 		}
109 		else {
110 			return this.entityManager.merge(lhs);
111 		}
112 	}
113 	
114 	
115 
116 	/**
117 	 * Gets the reference to the <code>EntityManager</code>.
118 	 * @return The reference to the <code>EntityManager</code>.
119 	 */
120 	public EntityManager getEntityManager() {
121 		return entityManager;
122 	}
123 
124 	/**
125 	 * Sets the reference to the <code>EntityManager</code>.
126 	 * @param entityManager The reference to the <code>EntityManager</code>.
127 	 */
128 	public void setEntityManager(EntityManager entityManager) {
129 		this.entityManager = entityManager;
130 	}
131 
132 	/* (non-Javadoc)
133 	 * @see org.kuali.mobility.library.dao.LibraryDao#getLibariesForCampus(java.lang.String)
134 	 */
135 	@Override
136 	public List<Library> getLibariesForCampus(String campusCode) {
137 		Query query = getEntityManager().createNamedQuery("Library.getLibariesForCampus");
138 		query.setParameter("campusCode", campusCode);
139 		return query.getResultList();
140 	}
141 
142 	/* (non-Javadoc)
143 	 * @see org.kuali.mobility.library.dao.LibraryDao#getCampusWithLibraries()
144 	 */
145 	@Override
146 	public List<String> getCampusWithLibraries() {
147 		Query query = getEntityManager().createNamedQuery("Library.getCampusWithLibraries");
148 		return query.getResultList();
149 	}
150 
151 	/* (non-Javadoc)
152 	 * @see org.kuali.mobility.library.dao.LibraryDao#saveLibraryHourPeriod(org.kuali.mobility.library.entity.LibraryHourPeriod)
153 	 */
154 	@Override
155 	@Transactional
156 	public LibraryHourPeriod saveLibraryHourPeriod(LibraryHourPeriod libraryHourPeriod) {
157 		
158 		if (libraryHourPeriod.getId() == null){
159 			getEntityManager().persist(libraryHourPeriod);
160 			return libraryHourPeriod;
161 		}
162 		else {
163 			return getEntityManager().merge(libraryHourPeriod);
164 		}
165 	}
166 
167 }