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 | 0 | public class AdminDaoImpl implements AdminDao { |
37 | |
|
38 | |
@PersistenceContext |
39 | |
private EntityManager entityManager; |
40 | |
|
41 | |
public EntityManager getEntityManager() { |
42 | 0 | return entityManager; |
43 | |
} |
44 | |
|
45 | |
public void setEntityManager(EntityManager entityManager) { |
46 | 0 | this.entityManager = entityManager; |
47 | 0 | } |
48 | |
|
49 | |
@SuppressWarnings("unchecked") |
50 | |
public List<HomeScreen> getAllHomeScreens(){ |
51 | 0 | Query query = entityManager.createQuery("select h from HomeScreen h"); |
52 | |
try { |
53 | 0 | return query.getResultList(); |
54 | 0 | } catch (Exception e) { |
55 | 0 | return null; |
56 | |
} |
57 | |
} |
58 | |
|
59 | |
@Override |
60 | |
public HomeScreen getHomeScreenById(long homeScreenId) { |
61 | 0 | Query query = entityManager.createQuery("select h from HomeScreen h where h.homeScreenId = :id"); |
62 | 0 | query.setParameter("id", homeScreenId); |
63 | |
try { |
64 | 0 | return (HomeScreen) query.getSingleResult(); |
65 | 0 | } catch (Exception e) { |
66 | 0 | return null; |
67 | |
} |
68 | |
} |
69 | |
|
70 | |
@Override |
71 | |
public HomeScreen getHomeScreenByAlias(String alias) { |
72 | 0 | Query query = entityManager.createQuery("select h from HomeScreen h where h.alias = :alias"); |
73 | 0 | query.setParameter("alias", alias); |
74 | |
try { |
75 | 0 | return (HomeScreen) query.getSingleResult(); |
76 | 0 | } catch (Exception e) { |
77 | 0 | return null; |
78 | |
} |
79 | |
} |
80 | |
|
81 | |
@Override |
82 | |
public Long saveHomeScreen(HomeScreen homeScreen) { |
83 | 0 | if (homeScreen == null) { |
84 | 0 | return null; |
85 | |
} |
86 | 0 | if (homeScreen.getAlias() != null) { |
87 | 0 | homeScreen.setAlias(homeScreen.getAlias().trim()); |
88 | |
} |
89 | 0 | for (HomeTool ht : homeScreen.getHomeTools()) { |
90 | 0 | ht.setHomeScreen(homeScreen); |
91 | |
} |
92 | |
try { |
93 | 0 | if (homeScreen.getHomeScreenId() == null) { |
94 | 0 | entityManager.persist(homeScreen); |
95 | |
} else { |
96 | 0 | deleteHomeToolsByHomeScreenId(homeScreen.getHomeScreenId()); |
97 | 0 | entityManager.merge(homeScreen); |
98 | |
} |
99 | 0 | } catch (OptimisticLockException oe) { |
100 | 0 | return null; |
101 | 0 | } |
102 | 0 | return homeScreen.getHomeScreenId(); |
103 | |
} |
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
private void deleteHomeToolsByHomeScreenId(long homeScreenId) { |
110 | 0 | Query query = entityManager.createQuery("delete from HomeTool ht where ht.homeScreenId = :id"); |
111 | 0 | query.setParameter("id", homeScreenId); |
112 | 0 | query.executeUpdate(); |
113 | 0 | } |
114 | |
|
115 | |
@Override |
116 | |
public void deleteHomeScreenById(long homeScreenId) { |
117 | 0 | Query query = entityManager.createQuery("delete from HomeScreen h where h.homeScreenId = :id"); |
118 | 0 | query.setParameter("id", homeScreenId); |
119 | 0 | query.executeUpdate(); |
120 | 0 | } |
121 | |
|
122 | |
@SuppressWarnings("unchecked") |
123 | |
public List<Tool> getAllTools(){ |
124 | 0 | Query query = entityManager.createQuery("select t from Tool t"); |
125 | |
try { |
126 | 0 | return query.getResultList(); |
127 | 0 | } catch (Exception e) { |
128 | 0 | return null; |
129 | |
} |
130 | |
} |
131 | |
|
132 | |
public Tool getToolById(long toolId) { |
133 | 0 | Query query = entityManager.createQuery("select t from Tool t where t.toolId = :id"); |
134 | 0 | query.setParameter("id", toolId); |
135 | |
try { |
136 | 0 | return (Tool) query.getSingleResult(); |
137 | 0 | } catch (Exception e) { |
138 | 0 | return null; |
139 | |
} |
140 | |
} |
141 | |
|
142 | |
public Long saveTool(Tool tool) { |
143 | 0 | if (tool == null) { |
144 | 0 | return null; |
145 | |
} |
146 | 0 | if (tool.getTitle() != null) { |
147 | 0 | tool.setTitle(tool.getTitle().trim()); |
148 | |
} |
149 | 0 | if (tool.getUrl() != null) { |
150 | 0 | tool.setUrl(tool.getUrl().trim()); |
151 | |
} |
152 | 0 | if (tool.getIconUrl() != null) { |
153 | 0 | tool.setIconUrl(tool.getIconUrl().trim()); |
154 | |
} |
155 | 0 | if (tool.getDescription() != null) { |
156 | 0 | tool.setDescription(tool.getDescription().trim()); |
157 | |
} |
158 | |
try { |
159 | 0 | if (tool.getToolId() == null) { |
160 | 0 | entityManager.persist(tool); |
161 | |
} else { |
162 | 0 | entityManager.merge(tool); |
163 | |
} |
164 | 0 | } catch (OptimisticLockException oe) { |
165 | 0 | return null; |
166 | 0 | } |
167 | 0 | return tool.getToolId(); |
168 | |
} |
169 | |
|
170 | |
public void deleteToolById(Long toolId) { |
171 | 0 | Query query = entityManager.createQuery("delete from Tool t where t.toolId = :toolId"); |
172 | 0 | query.setParameter("toolId", toolId); |
173 | 0 | query.executeUpdate(); |
174 | 0 | } |
175 | |
} |