added final modifiers where possible in service layer
This commit is contained in:
parent
dc2a5504fb
commit
53d89dc36f
@ -14,7 +14,7 @@ public class CaptchaService {
|
|||||||
webCaptcha = WebCaptcha.builder().captchaBehaviour(new SimpleCaptchaBehavior()).build();
|
webCaptcha = WebCaptcha.builder().captchaBehaviour(new SimpleCaptchaBehavior()).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
public BufferedImage createCaptchaImage(String captchaText)
|
public BufferedImage createCaptchaImage(final String captchaText)
|
||||||
{
|
{
|
||||||
return webCaptcha.generateCaptcha(captchaText);
|
return webCaptcha.generateCaptcha(captchaText);
|
||||||
}
|
}
|
||||||
|
@ -31,12 +31,13 @@ public class ChatServiceImpl implements ChatService {
|
|||||||
@Autowired
|
@Autowired
|
||||||
MyConversionService myConversionService;
|
MyConversionService myConversionService;
|
||||||
|
|
||||||
public ChatMessageDTO saveNewMessage(String fromUserName, String toUserName, MessageCipherDTO messageCipherDTO) {
|
public ChatMessageDTO saveNewMessage(final String fromUserName, final String toUserName,
|
||||||
|
final MessageCipherDTO messageCipherDTO) {
|
||||||
|
|
||||||
MessageCipher messageCipher = myConversionService.convertToMessageCipher(messageCipherDTO);
|
MessageCipher messageCipher = myConversionService.convertToMessageCipher(messageCipherDTO);
|
||||||
ChatUser fromUser = userRepository.findByUserName(fromUserName);
|
final ChatUser fromUser = userRepository.findByUserName(fromUserName);
|
||||||
ChatUser toUser = userRepository.findByUserName(toUserName);
|
final ChatUser toUser = userRepository.findByUserName(toUserName);
|
||||||
|
|
||||||
ChatMessage chatMessage = new ChatMessage();
|
ChatMessage chatMessage = new ChatMessage();
|
||||||
messageCipher = messageCipherRepository.save(messageCipher);
|
messageCipher = messageCipherRepository.save(messageCipher);
|
||||||
chatMessage.setMessageCipher(messageCipher);
|
chatMessage.setMessageCipher(messageCipher);
|
||||||
@ -50,45 +51,51 @@ public class ChatServiceImpl implements ChatService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
public List<ChatMessageDTO> getAllMessages(String fromUser, String toUser) {
|
public List<ChatMessageDTO> getAllMessages(final String fromUser, final String toUser) {
|
||||||
List<ChatMessage> chatMessages = chatMessageRepository.getAllMessages(fromUser, toUser);
|
final List<ChatMessage> chatMessages = chatMessageRepository.getAllMessages(fromUser, toUser);
|
||||||
List<ChatMessageDTO> chatMessageDTOs = myConversionService.convertToChatMessageDTOs(chatMessages);
|
final List<ChatMessageDTO> chatMessageDTOs = myConversionService.convertToChatMessageDTOs(chatMessages);
|
||||||
return chatMessageDTOs;
|
return chatMessageDTOs;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
public List<ReencryptionDTO> getAllMessagesForReencryption(String fromUser, String toUser) {
|
public List<ReencryptionDTO> getAllMessagesForReencryption(final String fromUser, final String toUser) {
|
||||||
return myConversionService.convertToReencryptionDTOs(chatMessageRepository.getAllMessages(fromUser, toUser));
|
return myConversionService.convertToReencryptionDTOs(chatMessageRepository.getAllMessages(fromUser, toUser));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
public List<ChatMessageDTO> getMessagePage(String fromUser, String toUser, int page, int size) {
|
public List<ChatMessageDTO> getMessagePage(final String fromUser, final String toUser, final int page,
|
||||||
// Sort sort = Sort
|
final int size) {
|
||||||
// Page<ChatMessage> chatMessages = chatMessageRepository.getAllMessages(fromUser, toUser,PageRequest.of(page, size));
|
// Sort sort = Sort
|
||||||
// List<ChatMessageDTO> chatMessageDTOs = myConversionService.convertToChatMessageDTOs(chatMessages);
|
// Page<ChatMessage> chatMessages =
|
||||||
// return chatMessageDTOs;
|
// chatMessageRepository.getAllMessages(fromUser, toUser,PageRequest.of(page,
|
||||||
return myConversionService.convertToChatMessageDTOs(chatMessageRepository.getAllMessages(fromUser, toUser,PageRequest.of(page, size)));
|
// size));
|
||||||
|
// List<ChatMessageDTO> chatMessageDTOs =
|
||||||
|
// myConversionService.convertToChatMessageDTOs(chatMessages);
|
||||||
|
// return chatMessageDTOs;
|
||||||
|
return myConversionService.convertToChatMessageDTOs(
|
||||||
|
chatMessageRepository.getAllMessages(fromUser, toUser, PageRequest.of(page, size)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
public List<ChatMessageDTO> getNewMessages(String fromUser, String toUser, Date lastMessageTime) {
|
public List<ChatMessageDTO> getNewMessages(final String fromUser, final String toUser, final Date lastMessageTime) {
|
||||||
List<ChatMessage> chatMessages = chatMessageRepository.getNewMessages(fromUser, toUser, lastMessageTime);
|
final List<ChatMessage> chatMessages = chatMessageRepository.getNewMessages(fromUser, toUser, lastMessageTime);
|
||||||
// List<ChatMessageDTO> chatMessageDTOs
|
// List<ChatMessageDTO> chatMessageDTOs
|
||||||
return myConversionService.convertToChatMessageDTOs(chatMessages);
|
return myConversionService.convertToChatMessageDTOs(chatMessages);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void reencryptMessages(List<ReencryptionDTO> reencryptionDTOs) {
|
public void reencryptMessages(final List<ReencryptionDTO> reencryptionDTOs) {
|
||||||
List<MessageCipher> messageCiphers = reencryptionDTOs.stream().map(reencryptionDTO -> reencryptionDTO.getMessageCipher()).collect(Collectors.toList());
|
final List<MessageCipher> messageCiphers = reencryptionDTOs.stream()
|
||||||
|
.map(reencryptionDTO -> reencryptionDTO.getMessageCipher()).collect(Collectors.toList());
|
||||||
messageCipherRepository.saveAll(messageCiphers);
|
messageCipherRepository.saveAll(messageCiphers);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<ChatMessageDTO> getAllMessages(String name, String userName, PageRequest pageRequest) {
|
public List<ChatMessageDTO> getAllMessages(final String name, final String userName,
|
||||||
|
final PageRequest pageRequest) {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -46,12 +46,12 @@ public class DBInitializerService {
|
|||||||
private EntityManager entityManager;
|
private EntityManager entityManager;
|
||||||
|
|
||||||
|
|
||||||
public int getNumTables(Connection connection) throws SQLException {
|
public int getNumTables(final Connection connection) throws SQLException {
|
||||||
PreparedStatement preparedStatement = connection.prepareStatement(numTablesQuery);
|
final PreparedStatement preparedStatement = connection.prepareStatement(numTablesQuery);
|
||||||
preparedStatement.setString(1, dbName);
|
preparedStatement.setString(1, dbName);
|
||||||
ResultSet resultSet = preparedStatement.executeQuery();
|
final ResultSet resultSet = preparedStatement.executeQuery();
|
||||||
resultSet.next();
|
resultSet.next();
|
||||||
int numTables = resultSet.getInt("num_tables");
|
final int numTables = resultSet.getInt("num_tables");
|
||||||
return numTables;
|
return numTables;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,14 +60,14 @@ public class DBInitializerService {
|
|||||||
public void doSomethingAfterStartup() throws SQLException, IOException {
|
public void doSomethingAfterStartup() throws SQLException, IOException {
|
||||||
logger.info("Application Started - running initializer service");
|
logger.info("Application Started - running initializer service");
|
||||||
|
|
||||||
Session session = entityManager.unwrap(Session.class);
|
final Session session = entityManager.unwrap(Session.class);
|
||||||
|
|
||||||
session.doWork(connection -> {
|
session.doWork(connection -> {
|
||||||
if (getNumTables(connection) == 0) {
|
if (getNumTables(connection) == 0) {
|
||||||
logger.info("Database is empty. Populating tables and roles");
|
logger.info("Database is empty. Populating tables and roles");
|
||||||
try {
|
try {
|
||||||
populateDB(connection);
|
populateDB(connection);
|
||||||
} catch (IOException e) {
|
} catch (final IOException e) {
|
||||||
log.error("IO error", e);
|
log.error("IO error", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -79,7 +79,7 @@ public class DBInitializerService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void populateDB(Connection connection) throws SQLException, IOException {
|
private void populateDB(final Connection connection) throws SQLException, IOException {
|
||||||
ScriptUtils.executeSqlScript(connection,
|
ScriptUtils.executeSqlScript(connection,
|
||||||
new EncodedResource(new ClassPathResource("scheme.sql"), StandardCharsets.UTF_8));
|
new EncodedResource(new ClassPathResource("scheme.sql"), StandardCharsets.UTF_8));
|
||||||
ScriptUtils.executeSqlScript(connection,
|
ScriptUtils.executeSqlScript(connection,
|
||||||
@ -87,10 +87,8 @@ public class DBInitializerService {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void resetAllUserSessions(final Connection connection) throws SQLException {
|
||||||
private void resetAllUserSessions(Connection connection) throws SQLException
|
final PreparedStatement preparedStatement = connection.prepareStatement(resetSessionsQuery);
|
||||||
{
|
|
||||||
PreparedStatement preparedStatement = connection.prepareStatement(resetSessionsQuery);
|
|
||||||
preparedStatement.executeUpdate();
|
preparedStatement.executeUpdate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,59 +39,48 @@ public class MyConversionService {
|
|||||||
@Autowired
|
@Autowired
|
||||||
ModelMapper modelMapper;
|
ModelMapper modelMapper;
|
||||||
|
|
||||||
public ChatMessageDTO convertToChatMessageDTO(ChatMessage chatMessage)
|
public ChatMessageDTO convertToChatMessageDTO(final ChatMessage chatMessage) {
|
||||||
{
|
final ChatMessageDTO chatMessageDTO = modelMapper.map(chatMessage, ChatMessageDTO.class);
|
||||||
ChatMessageDTO chatMessageDTO = modelMapper.map(chatMessage, ChatMessageDTO.class);
|
|
||||||
return chatMessageDTO;
|
return chatMessageDTO;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ChatMessage convertToChatMessage(ChatMessageDTO chatMessageDTO)
|
public ChatMessage convertToChatMessage(final ChatMessageDTO chatMessageDTO) {
|
||||||
{
|
final ChatMessage chatMessage = modelMapper.map(chatMessageDTO, ChatMessage.class);
|
||||||
ChatMessage chatMessage = modelMapper.map(chatMessageDTO, ChatMessage.class);
|
|
||||||
return chatMessage;
|
return chatMessage;
|
||||||
}
|
}
|
||||||
|
|
||||||
public MessageCipher convertToMessageCipher(MessageCipherDTO messageCipherDTO)
|
public MessageCipher convertToMessageCipher(final MessageCipherDTO messageCipherDTO) {
|
||||||
{
|
final MessageCipher messageCipher = modelMapper.map(messageCipherDTO, MessageCipher.class);
|
||||||
MessageCipher messageCipher = modelMapper.map(messageCipherDTO, MessageCipher.class);
|
|
||||||
return messageCipher;
|
return messageCipher;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<ChatMessageDTO> convertToChatMessageDTOs(List<ChatMessage> chatMessages)
|
public List<ChatMessageDTO> convertToChatMessageDTOs(final List<ChatMessage> chatMessages) {
|
||||||
{
|
return chatMessages.stream().map(chatMessage -> convertToChatMessageDTO(chatMessage))
|
||||||
return chatMessages.stream()
|
|
||||||
.map(chatMessage -> convertToChatMessageDTO(chatMessage))
|
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
public ReencryptionDTO convertToReencryptionDTO(ChatMessage chatMessage)
|
public ReencryptionDTO convertToReencryptionDTO(final ChatMessage chatMessage) {
|
||||||
{
|
final ReencryptionDTO reencryptionDTO = modelMapper.map(chatMessage, ReencryptionDTO.class);
|
||||||
ReencryptionDTO reencryptionDTO = modelMapper.map(chatMessage, ReencryptionDTO.class);
|
|
||||||
return reencryptionDTO;
|
return reencryptionDTO;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ChatMessage convertToChatMessage(ReencryptionDTO reencryptionDTO)
|
public ChatMessage convertToChatMessage(final ReencryptionDTO reencryptionDTO) {
|
||||||
{
|
final ChatMessage chatMessage = modelMapper.map(reencryptionDTO, ChatMessage.class);
|
||||||
ChatMessage chatMessage = modelMapper.map(reencryptionDTO, ChatMessage.class);
|
|
||||||
return chatMessage;
|
return chatMessage;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<ReencryptionDTO> convertToReencryptionDTOs(List<ChatMessage> chatMessages)
|
public List<ReencryptionDTO> convertToReencryptionDTOs(final List<ChatMessage> chatMessages) {
|
||||||
{
|
return chatMessages.stream().map(chatMessage -> convertToReencryptionDTO(chatMessage))
|
||||||
return chatMessages.stream()
|
|
||||||
.map(chatMessage -> convertToReencryptionDTO(chatMessage))
|
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Iterable<ChatMessage> convertoToChatMessages(final List<ChatMessageDTO> chatMessageDTOs) {
|
||||||
public Iterable<ChatMessage> convertoToChatMessages(List<ChatMessageDTO> chatMessageDTOs)
|
|
||||||
{
|
return chatMessageDTOs.stream().map(chatMessageDTO -> convertToChatMessage(chatMessageDTO))
|
||||||
|
.collect(Collectors.toList());
|
||||||
return chatMessageDTOs.stream()
|
|
||||||
.map(chatMessageDTO -> convertToChatMessage(chatMessageDTO)).collect(Collectors.toList());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<ChatMessageDTO> convertToChatMessageDTOs(Page<ChatMessage> chatMessages)
|
public List<ChatMessageDTO> convertToChatMessageDTOs(final Page<ChatMessage> chatMessages)
|
||||||
{
|
{
|
||||||
return chatMessages.stream()
|
return chatMessages.stream()
|
||||||
.map(chatMessage -> convertToChatMessageDTO(chatMessage))
|
.map(chatMessage -> convertToChatMessageDTO(chatMessage))
|
||||||
|
@ -35,9 +35,10 @@ public class UserServiceImpl implements UserService {
|
|||||||
|
|
||||||
private final UserSessionRepository userSessionRepository;
|
private final UserSessionRepository userSessionRepository;
|
||||||
|
|
||||||
public UserServiceImpl(@Autowired UserRepository userRepository, @Autowired UserRoleRepository userRoleRepository,
|
public UserServiceImpl(@Autowired final UserRepository userRepository,
|
||||||
@Autowired PasswordEncoder passwordEncoder, @Autowired RoleRepository roleRepository,
|
@Autowired final UserRoleRepository userRoleRepository, @Autowired final PasswordEncoder passwordEncoder,
|
||||||
@Autowired UserSessionRepository userSessionRepository) {
|
@Autowired final RoleRepository roleRepository,
|
||||||
|
@Autowired final UserSessionRepository userSessionRepository) {
|
||||||
this.userRepository = userRepository;
|
this.userRepository = userRepository;
|
||||||
this.userRoleRepository = userRoleRepository;
|
this.userRoleRepository = userRoleRepository;
|
||||||
this.passwordEncoder = passwordEncoder;
|
this.passwordEncoder = passwordEncoder;
|
||||||
@ -46,13 +47,13 @@ public class UserServiceImpl implements UserService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public UserRole registerUser(UserRegistrationDTO userRegistrationDTO) {
|
public UserRole registerUser(final UserRegistrationDTO userRegistrationDTO) {
|
||||||
ChatUser user = new ChatUser();
|
final ChatUser user = new ChatUser();
|
||||||
user.setUserName(userRegistrationDTO.getUserName());
|
user.setUserName(userRegistrationDTO.getUserName());
|
||||||
user.setPassword(passwordEncoder.encode(userRegistrationDTO.getPassword()));
|
user.setPassword(passwordEncoder.encode(userRegistrationDTO.getPassword()));
|
||||||
ChatUser changedUser = userRepository.save(user);
|
final ChatUser changedUser = userRepository.save(user);
|
||||||
UserRole userRole = new UserRole();
|
final UserRole userRole = new UserRole();
|
||||||
Role role = roleRepository.findByName("USER");
|
final Role role = roleRepository.findByName("USER");
|
||||||
userRole.setRole(role);
|
userRole.setRole(role);
|
||||||
userRole.setUser(changedUser);
|
userRole.setUser(changedUser);
|
||||||
userRoleRepository.save(userRole);
|
userRoleRepository.save(userRole);
|
||||||
@ -61,7 +62,7 @@ public class UserServiceImpl implements UserService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
public List<String> findAllOtherUsers(String userName) {
|
public List<String> findAllOtherUsers(final String userName) {
|
||||||
return userRepository.findAllOtherUserNames(userName);
|
return userRepository.findAllOtherUserNames(userName);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -72,16 +73,16 @@ public class UserServiceImpl implements UserService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
public List<ActiveUserDTO> getOtherActiveUsers(String userName) {
|
public List<ActiveUserDTO> getOtherActiveUsers(final String userName) {
|
||||||
List<String> userList = findAllOtherUsers(userName);
|
final List<String> userList = findAllOtherUsers(userName);
|
||||||
|
|
||||||
List<UserSession> userSessionsList = userSessionRepository.findAllUserSessions();
|
final List<UserSession> userSessionsList = userSessionRepository.findAllUserSessions();
|
||||||
Map<String, UserSession> lastActiveMap = convertToMap(userSessionsList);
|
final Map<String, UserSession> lastActiveMap = convertToMap(userSessionsList);
|
||||||
|
|
||||||
List<ActiveUserDTO> activeUserDTOs = new ArrayList<ActiveUserDTO>();
|
final List<ActiveUserDTO> activeUserDTOs = new ArrayList<ActiveUserDTO>();
|
||||||
userList.forEach(u -> {
|
userList.forEach(u -> {
|
||||||
ActiveUserDTO activeUserDTO = new ActiveUserDTO();
|
final ActiveUserDTO activeUserDTO = new ActiveUserDTO();
|
||||||
UserSession us = lastActiveMap.get(u);
|
final UserSession us = lastActiveMap.get(u);
|
||||||
|
|
||||||
activeUserDTO.setUserName(u);
|
activeUserDTO.setUserName(u);
|
||||||
activeUserDTO.setOnline(false);
|
activeUserDTO.setOnline(false);
|
||||||
@ -99,25 +100,25 @@ public class UserServiceImpl implements UserService {
|
|||||||
|
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
@Override
|
@Override
|
||||||
public ChatUser findByUserName(String userName) {
|
public ChatUser findByUserName(final String userName) {
|
||||||
return userRepository.findByUserName(userName);
|
return userRepository.findByUserName(userName);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Map<String, UserSession> convertToMap(List<UserSession> userSessionList) {
|
private Map<String, UserSession> convertToMap(final List<UserSession> userSessionList) {
|
||||||
Map<String, UserSession> userMap = new HashMap<>();
|
final Map<String, UserSession> userMap = new HashMap<>();
|
||||||
userSessionList.forEach(us -> {
|
userSessionList.forEach(us -> {
|
||||||
userMap.put(us.getUser().getUserName(), us);
|
userMap.put(us.getUser().getUserName(), us);
|
||||||
});
|
});
|
||||||
return userMap;
|
return userMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String toLastActiveString(Instant lastActive) {
|
private String toLastActiveString(final Instant lastActive) {
|
||||||
if (lastActive == null)
|
if (lastActive == null)
|
||||||
return null;
|
return null;
|
||||||
Duration duration = Duration.between(lastActive, Instant.now());
|
final Duration duration = Duration.between(lastActive, Instant.now());
|
||||||
long hours = duration.toHours();
|
final long hours = duration.toHours();
|
||||||
long minutes = duration.toMinutes();
|
final long minutes = duration.toMinutes();
|
||||||
long days = duration.toDays();
|
final long days = duration.toDays();
|
||||||
// @formatter:off
|
// @formatter:off
|
||||||
if(minutes < 60)
|
if(minutes < 60)
|
||||||
{
|
{
|
||||||
@ -150,7 +151,7 @@ public class UserServiceImpl implements UserService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
public List<UserRole> getUserWithRole(String userName) {
|
public List<UserRole> getUserWithRole(final String userName) {
|
||||||
return userRoleRepository.findByUser(userName);
|
return userRoleRepository.findByUser(userName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user