1
2
3
4
5
6
7
8
9
10
11
12
13
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
32
33
34
35 @Repository
36 public class AdminDaoImpl implements AdminDao {
37
38
39
40
41 @PersistenceContext
42 private EntityManager entityManager;
43
44
45
46
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
60
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
75
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
90
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
118
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
128
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
139
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
153
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
167
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
208
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
219
220
221 public EntityManager getEntityManager() {
222 return entityManager;
223 }
224
225
226
227
228
229 public void setEntityManager(EntityManager entityManager) {
230 this.entityManager = entityManager;
231 }
232 }