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.admin.dao;
17  
18  import java.util.List;
19  
20  import javax.persistence.EntityManager;
21  import javax.persistence.OptimisticLockException;
22  import javax.persistence.PersistenceContext;
23  import javax.persistence.Query;
24  
25  import org.kuali.mobility.admin.entity.HomeScreen;
26  import org.kuali.mobility.admin.entity.HomeTool;
27  import org.kuali.mobility.admin.entity.Tool;
28  import org.springframework.stereotype.Repository;
29  
30  /**
31   * The DAO for actually performing administrative tasks on the data store.
32   * @author Kuali Mobility Team (mobility.dev@kuali.org)
33   * @since 1.0.0
34   */
35  @Repository
36  public class AdminDaoImpl implements AdminDao {
37  
38  	/**
39  	 * A reference to the <code>EntityManager</code>.
40  	 */
41  	@PersistenceContext
42  	private EntityManager entityManager;
43  
44  	/*
45  	 * (non-Javadoc)
46  	 * @see org.kuali.mobility.admin.dao.AdminDao#getAllHomeScreens()
47  	 */
48  	@SuppressWarnings("unchecked")
49  	public List<HomeScreen> getAllHomeScreens(){
50  		Query query = entityManager.createNamedQuery("HomeScreen.getAllHomeScreens");
51  		try { 
52  			return query.getResultList();
53  		} catch (Exception e) {        	
54  			return null;
55  		}
56  	}
57  
58  	/*
59  	 * (non-Javadoc)
60  	 * @see org.kuali.mobility.admin.dao.AdminDao#getHomeScreenById(long)
61  	 */
62  	@Override
63  	public HomeScreen getHomeScreenById(long homeScreenId) {
64  		Query query = entityManager.createNamedQuery("HomeScreen.getHomeScreenById");
65  		query.setParameter("id", homeScreenId);
66  		try {
67  			return (HomeScreen) query.getSingleResult();
68  		} catch (Exception e) {
69  			return null;
70  		}
71  	}
72  
73  	/*
74  	 * (non-Javadoc)
75  	 * @see org.kuali.mobility.admin.dao.AdminDao#getHomeScreenByAlias(java.lang.String)
76  	 */
77  	@Override
78  	public HomeScreen getHomeScreenByAlias(String alias) {
79  		Query query = entityManager.createNamedQuery("HomeScreen.getHomeScreenByAlias");
80  		query.setParameter("alias", alias);
81  		try {
82  			return (HomeScreen) query.getSingleResult();
83  		} catch (Exception e) {
84  			return null;
85  		}
86  	}
87  
88  	/*
89  	 * (non-Javadoc)
90  	 * @see org.kuali.mobility.admin.dao.AdminDao#saveHomeScreen(org.kuali.mobility.admin.entity.HomeScreen)
91  	 */
92  	@Override
93  	public Long saveHomeScreen(HomeScreen homeScreen) {
94  		if (homeScreen == null) {
95  			return null;
96  		}
97  		if (homeScreen.getAlias() != null) {
98  			homeScreen.setAlias(homeScreen.getAlias().trim());
99  		}
100 		for (HomeTool ht : homeScreen.getHomeTools()) {
101 			ht.setHomeScreen(homeScreen);
102 		}
103 		try {
104 			if (homeScreen.getHomeScreenId() == null) {
105 				entityManager.persist(homeScreen);
106 			} else {
107 				deleteHomeToolsByHomeScreenId(homeScreen.getHomeScreenId());
108 				entityManager.merge(homeScreen);
109 			}
110 		} catch (OptimisticLockException oe) {
111 			return null;
112 		}
113 		return homeScreen.getHomeScreenId();
114 	}
115 
116 	/**
117 	 * Deletes all the HomeTool objects associated with the given HomeScreen.  This effectively removes all Tool associations from a HomeScreen.
118 	 * @param homeScreenId
119 	 */
120 	private void deleteHomeToolsByHomeScreenId(long homeScreenId) {
121 		Query query = entityManager.createNamedQuery("HomeScreen.deleteHomeToolsByHomeScreenId");
122 		query.setParameter("id", homeScreenId);
123 		query.executeUpdate();
124 	}
125 
126 	/*
127 	 * (non-Javadoc)
128 	 * @see org.kuali.mobility.admin.dao.AdminDao#deleteHomeScreenById(long)
129 	 */
130 	@Override
131 	public void deleteHomeScreenById(long homeScreenId) {
132 		Query query = entityManager.createNamedQuery("HomeScreen.deleteHomeScreenById");
133 		query.setParameter("id", homeScreenId);
134 		query.executeUpdate();
135 	}
136 
137 	/*
138 	 * (non-Javadoc)
139 	 * @see org.kuali.mobility.admin.dao.AdminDao#getAllTools()
140 	 */
141 	@SuppressWarnings("unchecked")
142 	public List<Tool> getAllTools(){
143 		Query query = entityManager.createNamedQuery("Tool.getAllTools");
144 		try { 
145 			return query.getResultList();
146 		} catch (Exception e) {        	
147 			return null;
148 		}
149 	}
150 
151 	/*
152 	 * (non-Javadoc)
153 	 * @see org.kuali.mobility.admin.dao.AdminDao#getToolById(long)
154 	 */
155 	public Tool getToolById(long toolId) {
156 		Query query = entityManager.createNamedQuery("Tool.getToolById");
157 		query.setParameter("id", toolId);
158 		try {
159 			return (Tool) query.getSingleResult();
160 		} catch (Exception e) {
161 			return null;
162 		}
163 	}
164 
165 	/*
166 	 * (non-Javadoc)
167 	 * @see org.kuali.mobility.admin.dao.AdminDao#saveTool(org.kuali.mobility.admin.entity.Tool)
168 	 */
169 	public Long saveTool(Tool tool) {
170 		if (tool == null) {
171 			return null;
172 		}
173 		if (tool.getTitle() != null) {
174 			tool.setTitle(tool.getTitle().trim());
175 		}
176 		if (tool.getSubtitle() != null) {
177 			tool.setSubtitle(tool.getSubtitle().trim());
178 		}
179 		if (tool.getUrl() != null) {
180 			tool.setUrl(tool.getUrl().trim());
181 		}
182 		if (tool.getIconUrl() != null) {
183 			tool.setIconUrl(tool.getIconUrl().trim());
184 		}
185 		if (tool.getDescription() != null) {
186 			tool.setDescription(tool.getDescription().trim());
187 		}
188 		if (tool.getContacts() != null) {
189 			tool.setContacts(tool.getContacts().trim());
190 		}
191 		if (tool.getKeywords() != null) {
192 			tool.setKeywords(tool.getKeywords().trim());
193 		}
194 		try {
195 			if (tool.getToolId() == null) {
196 				entityManager.persist(tool);
197 			} else {
198 				entityManager.merge(tool);
199 			}
200 		} catch (OptimisticLockException oe) {
201 			return null;
202 		}
203 		return tool.getToolId();
204 	}
205 
206 	/*
207 	 * (non-Javadoc)
208 	 * @see org.kuali.mobility.admin.dao.AdminDao#deleteToolById(java.lang.Long)
209 	 */
210 	public void deleteToolById(Long toolId) {
211 		Query query = entityManager.createNamedQuery("Tool.deleteToolById");
212 		query.setParameter("toolId", toolId);
213 		query.executeUpdate();
214 	}
215 	
216 
217 	/**
218 	 * Gets the reference to the <code>EntityManager</code>.
219 	 * @return The reference to the <code>EntityManager</code>.
220 	 */
221 	public EntityManager getEntityManager() {
222 		return entityManager;
223 	}
224 
225 	/**
226 	 * Sets the reference to the <code>EntityManager</code>.
227 	 * @param entityManager The reference to the <code>EntityManager</code>.
228 	 */
229 	public void setEntityManager(EntityManager entityManager) {
230 		this.entityManager = entityManager;
231 	}
232 }