1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.mobility.user.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.kuali.mobility.user.entity.User; |
25 | |
import org.kuali.mobility.user.entity.UserPreference; |
26 | |
import org.springframework.stereotype.Repository; |
27 | |
|
28 | |
@Repository |
29 | |
public class UserDaoImpl implements UserDao { |
30 | |
|
31 | |
@PersistenceContext |
32 | |
private EntityManager entityManager; |
33 | |
|
34 | 0 | public UserDaoImpl() {} |
35 | |
|
36 | |
public User findUserByDeviceId(String deviceId) { |
37 | 0 | Query query = entityManager.createQuery("select u from User u where u.deviceId = :deviceId"); |
38 | 0 | query.setParameter("deviceId", deviceId); |
39 | |
try { |
40 | 0 | return (User) query.getSingleResult(); |
41 | 0 | } catch (Exception e) { |
42 | 0 | return null; |
43 | |
} |
44 | |
} |
45 | |
|
46 | |
public User findUserByPrincipalName(String principalName) { |
47 | 0 | Query query = entityManager.createQuery("select u from User u where u.principalName = :principalName"); |
48 | 0 | query.setParameter("principalName", principalName); |
49 | |
try { |
50 | 0 | return (User) query.getSingleResult(); |
51 | 0 | } catch (Exception e) { |
52 | 0 | return null; |
53 | |
} |
54 | |
} |
55 | |
|
56 | |
public User findUserByPrincipalId(Long principalId) { |
57 | 0 | Query query = entityManager.createQuery("select u from User u where u.principalId = :principalId"); |
58 | 0 | query.setParameter("principalId", principalId); |
59 | |
try { |
60 | 0 | return (User) query.getSingleResult(); |
61 | 0 | } catch (Exception e) { |
62 | 0 | return null; |
63 | |
} |
64 | |
} |
65 | |
|
66 | |
public void saveUser(User user) { |
67 | 0 | if (user == null) { |
68 | 0 | return; |
69 | |
} |
70 | 0 | if (user.getPrincipalId() == null) { |
71 | 0 | entityManager.persist(user); |
72 | |
} else { |
73 | 0 | entityManager.merge(user); |
74 | |
} |
75 | 0 | } |
76 | |
|
77 | |
public void saveUserPreference(UserPreference userPreference) { |
78 | 0 | if (userPreference == null) { |
79 | 0 | return; |
80 | |
} |
81 | 0 | if (userPreference.getPreferenceId() == null) { |
82 | 0 | entityManager.persist(userPreference); |
83 | |
} else { |
84 | 0 | entityManager.merge(userPreference); |
85 | |
} |
86 | 0 | } |
87 | |
|
88 | |
public void deleteUserPreferenceById(Long preferenceId) { |
89 | 0 | Query query = entityManager.createQuery("delete from UserPreference up where up.preferenceId = :preferenceId"); |
90 | 0 | query.setParameter("preferenceId", preferenceId); |
91 | 0 | query.executeUpdate(); |
92 | 0 | } |
93 | |
|
94 | |
@SuppressWarnings("unchecked") |
95 | |
public List<UserPreference> findAllUserPreferencesByPrincipalId(Long principalId) { |
96 | 0 | Query query = entityManager.createQuery("select up from UserPreference up where up.principalId = :principalId"); |
97 | 0 | query.setParameter("principalId", principalId); |
98 | |
try { |
99 | 0 | return (List<UserPreference>) query.getResultList(); |
100 | 0 | } catch (Exception e) { |
101 | 0 | return null; |
102 | |
} |
103 | |
} |
104 | |
|
105 | |
public UserPreference findUserPreferenceByPreferenceId(Long preferenceId) { |
106 | 0 | Query query = entityManager.createQuery("select up from UserPreference up where up.preferenceId = :preferenceId"); |
107 | 0 | query.setParameter("preferenceId", preferenceId); |
108 | |
try { |
109 | 0 | return (UserPreference) query.getSingleResult(); |
110 | 0 | } catch (Exception e) { |
111 | 0 | return null; |
112 | |
} |
113 | |
} |
114 | |
|
115 | |
public EntityManager getEntityManager() { |
116 | 0 | return entityManager; |
117 | |
} |
118 | |
|
119 | |
public void setEntityManager(EntityManager entityManager) { |
120 | 0 | this.entityManager = entityManager; |
121 | 0 | } |
122 | |
|
123 | |
} |