|
1 | | -package org.woehlke.simpleworklist.oodm.services.impl; |
2 | | - |
3 | | - |
4 | | -import java.util.Date; |
5 | | -import java.util.HashMap; |
6 | | -import java.util.List; |
7 | | -import java.util.Map; |
8 | | - |
9 | | -import org.springframework.beans.factory.annotation.Autowired; |
10 | | - |
11 | | -import org.slf4j.Logger; |
12 | | -import org.slf4j.LoggerFactory; |
13 | | -import org.springframework.data.domain.Page; |
14 | | -import org.springframework.data.domain.Pageable; |
15 | | -import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; |
16 | | -import org.springframework.security.crypto.password.PasswordEncoder; |
17 | | -import org.springframework.stereotype.Service; |
18 | | -import org.springframework.transaction.annotation.Propagation; |
19 | | -import org.springframework.transaction.annotation.Transactional; |
20 | | -import org.woehlke.simpleworklist.model.beans.UserAccountForm; |
21 | | -import org.woehlke.simpleworklist.oodm.entities.Context; |
22 | | -import org.woehlke.simpleworklist.oodm.entities.UserAccount; |
23 | | -import org.woehlke.simpleworklist.oodm.entities.User2UserMessage; |
24 | | -import org.woehlke.simpleworklist.oodm.enumerations.Language; |
25 | | -import org.woehlke.simpleworklist.oodm.repository.ContextRepository; |
26 | | -import org.woehlke.simpleworklist.oodm.repository.UserAccountRepository; |
27 | | -import org.woehlke.simpleworklist.oodm.repository.User2UserMessageRepository; |
28 | | -import org.woehlke.simpleworklist.oodm.services.UserAccountService; |
29 | | - |
30 | | -@Service("userAccountService") |
31 | | -@Transactional(propagation = Propagation.REQUIRED, readOnly = true) |
32 | | -public class UserAccountServiceImpl implements UserAccountService { |
33 | | - |
34 | | - private static final Logger LOGGER = LoggerFactory.getLogger(UserAccountServiceImpl.class); |
35 | | - |
36 | | - private final UserAccountRepository userAccountRepository; |
37 | | - |
38 | | - private final User2UserMessageRepository userMessageRepository; |
39 | | - |
40 | | - private final ContextRepository contextRepository; |
41 | | - |
42 | | - private final PasswordEncoder encoder; |
43 | | - |
44 | | - @Autowired |
45 | | - public UserAccountServiceImpl(UserAccountRepository userAccountRepository, User2UserMessageRepository userMessageRepository, ContextRepository contextRepository) { |
46 | | - this.userAccountRepository = userAccountRepository; |
47 | | - this.userMessageRepository = userMessageRepository; |
48 | | - this.contextRepository = contextRepository; |
49 | | - int strength = 10; |
50 | | - this.encoder = new BCryptPasswordEncoder(strength); |
51 | | - } |
52 | | - |
53 | | - public boolean isEmailAvailable(String email) { |
54 | | - return userAccountRepository.findByUserEmail(email) == null; |
55 | | - } |
56 | | - |
57 | | - @Override |
58 | | - @Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = false) |
59 | | - public void createUser(UserAccountForm userAccount) { |
60 | | - UserAccount u = new UserAccount(); |
61 | | - u.setUserEmail(userAccount.getUserEmail()); |
62 | | - u.setUserFullname(userAccount.getUserFullname()); |
63 | | - u.setUserPassword(encoder.encode(userAccount.getUserPassword())); |
64 | | - u.setDefaultLanguage(Language.EN); |
65 | | - Date now = new Date(); |
66 | | - u.setLastLoginTimestamp(now); |
67 | | - u.setAccountNonExpired(true); |
68 | | - u.setAccountNonLocked(true); |
69 | | - u.setCredentialsNonExpired(true); |
70 | | - u.setEnabled(true); |
71 | | - LOGGER.info("About to save " + u.toString()); |
72 | | - u = userAccountRepository.save(u); |
73 | | - Context workContext = new Context("Arbeit","Work"); |
74 | | - Context privContext = new Context("Privat","Private"); |
75 | | - workContext.setUserAccount(u); |
76 | | - privContext.setUserAccount(u); |
77 | | - LOGGER.info("About to save " + workContext.toString()); |
78 | | - contextRepository.save(workContext); |
79 | | - LOGGER.info("About to save " + privContext.toString()); |
80 | | - contextRepository.save(privContext); |
81 | | - u.setDefaultContext(workContext); |
82 | | - u = userAccountRepository.save(u); |
83 | | - LOGGER.info("Saved " + u.toString()); |
84 | | - } |
85 | | - |
86 | | - @Override |
87 | | - @Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = false) |
88 | | - public UserAccount saveAndFlush(UserAccount u) { |
89 | | - return userAccountRepository.saveAndFlush(u); |
90 | | - } |
91 | | - |
92 | | - @Override |
93 | | - public UserAccount findByUserEmail(String userEmail) { |
94 | | - return userAccountRepository.findByUserEmail(userEmail); |
95 | | - } |
96 | | - |
97 | | - @Override |
98 | | - public Page<UserAccount> findAll(Pageable request) { |
99 | | - return userAccountRepository.findAll(request); |
100 | | - } |
101 | | - |
102 | | - @Override |
103 | | - @Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = false) |
104 | | - public void changeUsersPassword(UserAccountForm userAccount) { |
105 | | - UserAccount ua = userAccountRepository.findByUserEmail(userAccount.getUserEmail()); |
106 | | - if(ua != null) { |
107 | | - String pwEncoded = encoder.encode(userAccount.getUserPassword()); |
108 | | - ua.setUserPassword(pwEncoded); |
109 | | - userAccountRepository.saveAndFlush(ua); |
110 | | - } |
111 | | - } |
112 | | - |
113 | | - @Override |
114 | | - public UserAccount findUserById(long userId) { |
115 | | - return userAccountRepository.getOne(userId); |
116 | | - } |
117 | | - |
118 | | - @Override |
119 | | - public Map<Long, Integer> getNewIncomingMessagesForEachOtherUser(UserAccount receiver) { |
120 | | - Map<Long, Integer> newIncomingMessagesForEachOtherUser = new HashMap<>(); |
121 | | - List<UserAccount> allUsers = userAccountRepository.findAll(); |
122 | | - for(UserAccount sender :allUsers ){ |
123 | | - if(receiver.getId().longValue() == sender.getId().longValue()){ |
124 | | - newIncomingMessagesForEachOtherUser.put(sender.getId(),0); |
125 | | - } else { |
126 | | - List<User2UserMessage> user2UserMessages = userMessageRepository.findBySenderAndReceiverAndReadByReceiver(sender,receiver,false); |
127 | | - newIncomingMessagesForEachOtherUser.put(sender.getId(), user2UserMessages.size()); |
128 | | - } |
129 | | - } |
130 | | - return newIncomingMessagesForEachOtherUser; |
131 | | - } |
132 | | - |
133 | | -} |
| 1 | +package org.woehlke.simpleworklist.oodm.services.impl; |
| 2 | + |
| 3 | + |
| 4 | +import java.util.Date; |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Map; |
| 8 | + |
| 9 | +import org.springframework.beans.factory.annotation.Autowired; |
| 10 | + |
| 11 | +import org.slf4j.Logger; |
| 12 | +import org.slf4j.LoggerFactory; |
| 13 | +import org.springframework.data.domain.Page; |
| 14 | +import org.springframework.data.domain.Pageable; |
| 15 | +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; |
| 16 | +import org.springframework.security.crypto.password.PasswordEncoder; |
| 17 | +import org.springframework.stereotype.Service; |
| 18 | +import org.springframework.transaction.annotation.Propagation; |
| 19 | +import org.springframework.transaction.annotation.Transactional; |
| 20 | +import org.woehlke.simpleworklist.user.UserAccountForm; |
| 21 | +import org.woehlke.simpleworklist.oodm.entities.Context; |
| 22 | +import org.woehlke.simpleworklist.oodm.entities.UserAccount; |
| 23 | +import org.woehlke.simpleworklist.oodm.entities.User2UserMessage; |
| 24 | +import org.woehlke.simpleworklist.oodm.enumerations.Language; |
| 25 | +import org.woehlke.simpleworklist.oodm.repository.ContextRepository; |
| 26 | +import org.woehlke.simpleworklist.oodm.repository.UserAccountRepository; |
| 27 | +import org.woehlke.simpleworklist.oodm.repository.User2UserMessageRepository; |
| 28 | +import org.woehlke.simpleworklist.oodm.services.UserAccountService; |
| 29 | + |
| 30 | +@Service("userAccountService") |
| 31 | +@Transactional(propagation = Propagation.REQUIRED, readOnly = true) |
| 32 | +public class UserAccountServiceImpl implements UserAccountService { |
| 33 | + |
| 34 | + private static final Logger LOGGER = LoggerFactory.getLogger(UserAccountServiceImpl.class); |
| 35 | + |
| 36 | + private final UserAccountRepository userAccountRepository; |
| 37 | + |
| 38 | + private final User2UserMessageRepository userMessageRepository; |
| 39 | + |
| 40 | + private final ContextRepository contextRepository; |
| 41 | + |
| 42 | + private final PasswordEncoder encoder; |
| 43 | + |
| 44 | + @Autowired |
| 45 | + public UserAccountServiceImpl(UserAccountRepository userAccountRepository, User2UserMessageRepository userMessageRepository, ContextRepository contextRepository) { |
| 46 | + this.userAccountRepository = userAccountRepository; |
| 47 | + this.userMessageRepository = userMessageRepository; |
| 48 | + this.contextRepository = contextRepository; |
| 49 | + int strength = 10; |
| 50 | + this.encoder = new BCryptPasswordEncoder(strength); |
| 51 | + } |
| 52 | + |
| 53 | + public boolean isEmailAvailable(String email) { |
| 54 | + return userAccountRepository.findByUserEmail(email) == null; |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + @Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = false) |
| 59 | + public void createUser(UserAccountForm userAccount) { |
| 60 | + UserAccount u = new UserAccount(); |
| 61 | + u.setUserEmail(userAccount.getUserEmail()); |
| 62 | + u.setUserFullname(userAccount.getUserFullname()); |
| 63 | + u.setUserPassword(encoder.encode(userAccount.getUserPassword())); |
| 64 | + u.setDefaultLanguage(Language.EN); |
| 65 | + Date now = new Date(); |
| 66 | + u.setLastLoginTimestamp(now); |
| 67 | + u.setAccountNonExpired(true); |
| 68 | + u.setAccountNonLocked(true); |
| 69 | + u.setCredentialsNonExpired(true); |
| 70 | + u.setEnabled(true); |
| 71 | + LOGGER.info("About to save " + u.toString()); |
| 72 | + u = userAccountRepository.save(u); |
| 73 | + Context workContext = new Context("Arbeit","Work"); |
| 74 | + Context privContext = new Context("Privat","Private"); |
| 75 | + workContext.setUserAccount(u); |
| 76 | + privContext.setUserAccount(u); |
| 77 | + LOGGER.info("About to save " + workContext.toString()); |
| 78 | + contextRepository.save(workContext); |
| 79 | + LOGGER.info("About to save " + privContext.toString()); |
| 80 | + contextRepository.save(privContext); |
| 81 | + u.setDefaultContext(workContext); |
| 82 | + u = userAccountRepository.save(u); |
| 83 | + LOGGER.info("Saved " + u.toString()); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + @Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = false) |
| 88 | + public UserAccount saveAndFlush(UserAccount u) { |
| 89 | + return userAccountRepository.saveAndFlush(u); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public UserAccount findByUserEmail(String userEmail) { |
| 94 | + return userAccountRepository.findByUserEmail(userEmail); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public Page<UserAccount> findAll(Pageable request) { |
| 99 | + return userAccountRepository.findAll(request); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + @Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = false) |
| 104 | + public void changeUsersPassword(UserAccountForm userAccount) { |
| 105 | + UserAccount ua = userAccountRepository.findByUserEmail(userAccount.getUserEmail()); |
| 106 | + if(ua != null) { |
| 107 | + String pwEncoded = encoder.encode(userAccount.getUserPassword()); |
| 108 | + ua.setUserPassword(pwEncoded); |
| 109 | + userAccountRepository.saveAndFlush(ua); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public UserAccount findUserById(long userId) { |
| 115 | + return userAccountRepository.getOne(userId); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public Map<Long, Integer> getNewIncomingMessagesForEachOtherUser(UserAccount receiver) { |
| 120 | + Map<Long, Integer> newIncomingMessagesForEachOtherUser = new HashMap<>(); |
| 121 | + List<UserAccount> allUsers = userAccountRepository.findAll(); |
| 122 | + for(UserAccount sender :allUsers ){ |
| 123 | + if(receiver.getId().longValue() == sender.getId().longValue()){ |
| 124 | + newIncomingMessagesForEachOtherUser.put(sender.getId(),0); |
| 125 | + } else { |
| 126 | + List<User2UserMessage> user2UserMessages = userMessageRepository.findBySenderAndReceiverAndReadByReceiver(sender,receiver,false); |
| 127 | + newIncomingMessagesForEachOtherUser.put(sender.getId(), user2UserMessages.size()); |
| 128 | + } |
| 129 | + } |
| 130 | + return newIncomingMessagesForEachOtherUser; |
| 131 | + } |
| 132 | + |
| 133 | +} |
0 commit comments