added final modifiers where possible in service layer

This commit is contained in:
Rohan Sircar 2019-12-12 10:52:32 +05:30
parent dc2a5504fb
commit 53d89dc36f
5 changed files with 93 additions and 98 deletions

View File

@ -14,7 +14,7 @@ public class CaptchaService {
webCaptcha = WebCaptcha.builder().captchaBehaviour(new SimpleCaptchaBehavior()).build();
}
public BufferedImage createCaptchaImage(String captchaText)
public BufferedImage createCaptchaImage(final String captchaText)
{
return webCaptcha.generateCaptcha(captchaText);
}

View File

@ -31,11 +31,12 @@ public class ChatServiceImpl implements ChatService {
@Autowired
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);
ChatUser fromUser = userRepository.findByUserName(fromUserName);
ChatUser toUser = userRepository.findByUserName(toUserName);
final ChatUser fromUser = userRepository.findByUserName(fromUserName);
final ChatUser toUser = userRepository.findByUserName(toUserName);
ChatMessage chatMessage = new ChatMessage();
messageCipher = messageCipherRepository.save(messageCipher);
@ -50,45 +51,51 @@ public class ChatServiceImpl implements ChatService {
@Override
@Transactional(readOnly = true)
public List<ChatMessageDTO> getAllMessages(String fromUser, String toUser) {
List<ChatMessage> chatMessages = chatMessageRepository.getAllMessages(fromUser, toUser);
List<ChatMessageDTO> chatMessageDTOs = myConversionService.convertToChatMessageDTOs(chatMessages);
public List<ChatMessageDTO> getAllMessages(final String fromUser, final String toUser) {
final List<ChatMessage> chatMessages = chatMessageRepository.getAllMessages(fromUser, toUser);
final List<ChatMessageDTO> chatMessageDTOs = myConversionService.convertToChatMessageDTOs(chatMessages);
return chatMessageDTOs;
}
@Override
@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));
}
@Override
@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,
final int size) {
// Sort sort = Sort
// Page<ChatMessage> chatMessages = chatMessageRepository.getAllMessages(fromUser, toUser,PageRequest.of(page, size));
// List<ChatMessageDTO> chatMessageDTOs = myConversionService.convertToChatMessageDTOs(chatMessages);
// Page<ChatMessage> chatMessages =
// chatMessageRepository.getAllMessages(fromUser, toUser,PageRequest.of(page,
// size));
// List<ChatMessageDTO> chatMessageDTOs =
// myConversionService.convertToChatMessageDTOs(chatMessages);
// return chatMessageDTOs;
return myConversionService.convertToChatMessageDTOs(chatMessageRepository.getAllMessages(fromUser, toUser,PageRequest.of(page, size)));
return myConversionService.convertToChatMessageDTOs(
chatMessageRepository.getAllMessages(fromUser, toUser, PageRequest.of(page, size)));
}
@Override
@Transactional(readOnly = true)
public List<ChatMessageDTO> getNewMessages(String fromUser, String toUser, Date lastMessageTime) {
List<ChatMessage> chatMessages = chatMessageRepository.getNewMessages(fromUser, toUser, lastMessageTime);
public List<ChatMessageDTO> getNewMessages(final String fromUser, final String toUser, final Date lastMessageTime) {
final List<ChatMessage> chatMessages = chatMessageRepository.getNewMessages(fromUser, toUser, lastMessageTime);
// List<ChatMessageDTO> chatMessageDTOs
return myConversionService.convertToChatMessageDTOs(chatMessages);
}
@Override
public void reencryptMessages(List<ReencryptionDTO> reencryptionDTOs) {
List<MessageCipher> messageCiphers = reencryptionDTOs.stream().map(reencryptionDTO -> reencryptionDTO.getMessageCipher()).collect(Collectors.toList());
public void reencryptMessages(final List<ReencryptionDTO> reencryptionDTOs) {
final List<MessageCipher> messageCiphers = reencryptionDTOs.stream()
.map(reencryptionDTO -> reencryptionDTO.getMessageCipher()).collect(Collectors.toList());
messageCipherRepository.saveAll(messageCiphers);
}
@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
return null;
}

View File

@ -46,12 +46,12 @@ public class DBInitializerService {
private EntityManager entityManager;
public int getNumTables(Connection connection) throws SQLException {
PreparedStatement preparedStatement = connection.prepareStatement(numTablesQuery);
public int getNumTables(final Connection connection) throws SQLException {
final PreparedStatement preparedStatement = connection.prepareStatement(numTablesQuery);
preparedStatement.setString(1, dbName);
ResultSet resultSet = preparedStatement.executeQuery();
final ResultSet resultSet = preparedStatement.executeQuery();
resultSet.next();
int numTables = resultSet.getInt("num_tables");
final int numTables = resultSet.getInt("num_tables");
return numTables;
}
@ -60,14 +60,14 @@ public class DBInitializerService {
public void doSomethingAfterStartup() throws SQLException, IOException {
logger.info("Application Started - running initializer service");
Session session = entityManager.unwrap(Session.class);
final Session session = entityManager.unwrap(Session.class);
session.doWork(connection -> {
if (getNumTables(connection) == 0) {
logger.info("Database is empty. Populating tables and roles");
try {
populateDB(connection);
} catch (IOException e) {
} catch (final IOException 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,
new EncodedResource(new ClassPathResource("scheme.sql"), StandardCharsets.UTF_8));
ScriptUtils.executeSqlScript(connection,
@ -87,10 +87,8 @@ public class DBInitializerService {
}
private void resetAllUserSessions(Connection connection) throws SQLException
{
PreparedStatement preparedStatement = connection.prepareStatement(resetSessionsQuery);
private void resetAllUserSessions(final Connection connection) throws SQLException {
final PreparedStatement preparedStatement = connection.prepareStatement(resetSessionsQuery);
preparedStatement.executeUpdate();
}

View File

@ -39,59 +39,48 @@ public class MyConversionService {
@Autowired
ModelMapper modelMapper;
public ChatMessageDTO convertToChatMessageDTO(ChatMessage chatMessage)
{
ChatMessageDTO chatMessageDTO = modelMapper.map(chatMessage, ChatMessageDTO.class);
public ChatMessageDTO convertToChatMessageDTO(final ChatMessage chatMessage) {
final ChatMessageDTO chatMessageDTO = modelMapper.map(chatMessage, ChatMessageDTO.class);
return chatMessageDTO;
}
public ChatMessage convertToChatMessage(ChatMessageDTO chatMessageDTO)
{
ChatMessage chatMessage = modelMapper.map(chatMessageDTO, ChatMessage.class);
public ChatMessage convertToChatMessage(final ChatMessageDTO chatMessageDTO) {
final ChatMessage chatMessage = modelMapper.map(chatMessageDTO, ChatMessage.class);
return chatMessage;
}
public MessageCipher convertToMessageCipher(MessageCipherDTO messageCipherDTO)
{
MessageCipher messageCipher = modelMapper.map(messageCipherDTO, MessageCipher.class);
public MessageCipher convertToMessageCipher(final MessageCipherDTO messageCipherDTO) {
final MessageCipher messageCipher = modelMapper.map(messageCipherDTO, MessageCipher.class);
return messageCipher;
}
public List<ChatMessageDTO> convertToChatMessageDTOs(List<ChatMessage> chatMessages)
{
return chatMessages.stream()
.map(chatMessage -> convertToChatMessageDTO(chatMessage))
public List<ChatMessageDTO> convertToChatMessageDTOs(final List<ChatMessage> chatMessages) {
return chatMessages.stream().map(chatMessage -> convertToChatMessageDTO(chatMessage))
.collect(Collectors.toList());
}
public ReencryptionDTO convertToReencryptionDTO(ChatMessage chatMessage)
{
ReencryptionDTO reencryptionDTO = modelMapper.map(chatMessage, ReencryptionDTO.class);
public ReencryptionDTO convertToReencryptionDTO(final ChatMessage chatMessage) {
final ReencryptionDTO reencryptionDTO = modelMapper.map(chatMessage, ReencryptionDTO.class);
return reencryptionDTO;
}
public ChatMessage convertToChatMessage(ReencryptionDTO reencryptionDTO)
{
ChatMessage chatMessage = modelMapper.map(reencryptionDTO, ChatMessage.class);
public ChatMessage convertToChatMessage(final ReencryptionDTO reencryptionDTO) {
final ChatMessage chatMessage = modelMapper.map(reencryptionDTO, ChatMessage.class);
return chatMessage;
}
public List<ReencryptionDTO> convertToReencryptionDTOs(List<ChatMessage> chatMessages)
{
return chatMessages.stream()
.map(chatMessage -> convertToReencryptionDTO(chatMessage))
public List<ReencryptionDTO> convertToReencryptionDTOs(final List<ChatMessage> chatMessages) {
return chatMessages.stream().map(chatMessage -> convertToReencryptionDTO(chatMessage))
.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()
.map(chatMessage -> convertToChatMessageDTO(chatMessage))

View File

@ -35,9 +35,10 @@ public class UserServiceImpl implements UserService {
private final UserSessionRepository userSessionRepository;
public UserServiceImpl(@Autowired UserRepository userRepository, @Autowired UserRoleRepository userRoleRepository,
@Autowired PasswordEncoder passwordEncoder, @Autowired RoleRepository roleRepository,
@Autowired UserSessionRepository userSessionRepository) {
public UserServiceImpl(@Autowired final UserRepository userRepository,
@Autowired final UserRoleRepository userRoleRepository, @Autowired final PasswordEncoder passwordEncoder,
@Autowired final RoleRepository roleRepository,
@Autowired final UserSessionRepository userSessionRepository) {
this.userRepository = userRepository;
this.userRoleRepository = userRoleRepository;
this.passwordEncoder = passwordEncoder;
@ -46,13 +47,13 @@ public class UserServiceImpl implements UserService {
}
@Override
public UserRole registerUser(UserRegistrationDTO userRegistrationDTO) {
ChatUser user = new ChatUser();
public UserRole registerUser(final UserRegistrationDTO userRegistrationDTO) {
final ChatUser user = new ChatUser();
user.setUserName(userRegistrationDTO.getUserName());
user.setPassword(passwordEncoder.encode(userRegistrationDTO.getPassword()));
ChatUser changedUser = userRepository.save(user);
UserRole userRole = new UserRole();
Role role = roleRepository.findByName("USER");
final ChatUser changedUser = userRepository.save(user);
final UserRole userRole = new UserRole();
final Role role = roleRepository.findByName("USER");
userRole.setRole(role);
userRole.setUser(changedUser);
userRoleRepository.save(userRole);
@ -61,7 +62,7 @@ public class UserServiceImpl implements UserService {
@Override
@Transactional(readOnly = true)
public List<String> findAllOtherUsers(String userName) {
public List<String> findAllOtherUsers(final String userName) {
return userRepository.findAllOtherUserNames(userName);
}
@ -72,16 +73,16 @@ public class UserServiceImpl implements UserService {
}
@Transactional(readOnly = true)
public List<ActiveUserDTO> getOtherActiveUsers(String userName) {
List<String> userList = findAllOtherUsers(userName);
public List<ActiveUserDTO> getOtherActiveUsers(final String userName) {
final List<String> userList = findAllOtherUsers(userName);
List<UserSession> userSessionsList = userSessionRepository.findAllUserSessions();
Map<String, UserSession> lastActiveMap = convertToMap(userSessionsList);
final List<UserSession> userSessionsList = userSessionRepository.findAllUserSessions();
final Map<String, UserSession> lastActiveMap = convertToMap(userSessionsList);
List<ActiveUserDTO> activeUserDTOs = new ArrayList<ActiveUserDTO>();
final List<ActiveUserDTO> activeUserDTOs = new ArrayList<ActiveUserDTO>();
userList.forEach(u -> {
ActiveUserDTO activeUserDTO = new ActiveUserDTO();
UserSession us = lastActiveMap.get(u);
final ActiveUserDTO activeUserDTO = new ActiveUserDTO();
final UserSession us = lastActiveMap.get(u);
activeUserDTO.setUserName(u);
activeUserDTO.setOnline(false);
@ -99,25 +100,25 @@ public class UserServiceImpl implements UserService {
@Transactional(readOnly = true)
@Override
public ChatUser findByUserName(String userName) {
public ChatUser findByUserName(final String userName) {
return userRepository.findByUserName(userName);
}
private Map<String, UserSession> convertToMap(List<UserSession> userSessionList) {
Map<String, UserSession> userMap = new HashMap<>();
private Map<String, UserSession> convertToMap(final List<UserSession> userSessionList) {
final Map<String, UserSession> userMap = new HashMap<>();
userSessionList.forEach(us -> {
userMap.put(us.getUser().getUserName(), us);
});
return userMap;
}
private String toLastActiveString(Instant lastActive) {
private String toLastActiveString(final Instant lastActive) {
if (lastActive == null)
return null;
Duration duration = Duration.between(lastActive, Instant.now());
long hours = duration.toHours();
long minutes = duration.toMinutes();
long days = duration.toDays();
final Duration duration = Duration.between(lastActive, Instant.now());
final long hours = duration.toHours();
final long minutes = duration.toMinutes();
final long days = duration.toDays();
// @formatter:off
if(minutes < 60)
{
@ -150,7 +151,7 @@ public class UserServiceImpl implements UserService {
@Override
@Transactional(readOnly = true)
public List<UserRole> getUserWithRole(String userName) {
public List<UserRole> getUserWithRole(final String userName) {
return userRoleRepository.findByUser(userName);
}
}