From aa10e64e65296f32834de6aa9f7aab70124d0c9e Mon Sep 17 00:00:00 2001 From: Madhavan7 Date: Fri, 2 Dec 2022 06:35:49 -0500 Subject: [PATCH] Massive Refactoring, implemented Login Use case according to the MVP pattern --- .../UserChatsPresenter.java | 53 +++++++++++++++++++ .../UserLoginPresenter.java | 32 +++++++++++ .../UserLoginViewI.java | 8 +++ .../UserExistsOutputView.java} | 4 +- .../UserExistsPresenter.java | 8 +-- .../UserVerificationOutputView.java | 7 +++ .../UserVerificationPresenter.java | 6 +-- .../userRegCredentialsRetriever.java | 2 +- .../login_screen/AppScreenCreator.java | 37 +++++++++++++ .../screens/login_screen/UserLoginUI.java | 33 +++++++++--- .../UserRegistrationUI.java | 12 +++-- .../UserVerificationScreen.java | 5 +- .../UserLoginInputBoundary.java | 5 +- .../UserLoginInteractor2.java | 34 ++++++++++++ .../UserLoginOutputBoundary.java | 15 ++++++ .../UserLoginPresenter.java | 36 ------------- .../UserVerificationOutputBoundary.java | 7 --- .../TestUserExistsInteractor.java | 6 +-- .../TestUserVerificationInteractor.java | 6 +-- 19 files changed, 242 insertions(+), 74 deletions(-) create mode 100644 src/main/java/interface_adapters/login_interface_adapters/UserChatsPresenter.java create mode 100644 src/main/java/interface_adapters/login_interface_adapters/UserLoginPresenter.java create mode 100644 src/main/java/interface_adapters/login_interface_adapters/UserLoginViewI.java rename src/main/java/{use_cases/user_registration_use_cases/UserExistsOutputBoundary.java => interface_adapters/user_registration_interface_adapters/UserExistsOutputView.java} (87%) rename src/main/java/{use_cases/user_registration_use_cases => interface_adapters/user_registration_interface_adapters}/UserExistsPresenter.java (86%) create mode 100644 src/main/java/interface_adapters/user_registration_interface_adapters/UserVerificationOutputView.java rename src/main/java/{use_cases/user_registration_use_cases => interface_adapters/user_registration_interface_adapters}/UserVerificationPresenter.java (88%) rename src/main/java/{use_cases/user_registration_use_cases => interface_adapters/user_registration_interface_adapters}/userRegCredentialsRetriever.java (55%) create mode 100644 src/main/java/screens/login_screen/AppScreenCreator.java create mode 100644 src/main/java/use_cases/user_login_use_cases/UserLoginInteractor2.java create mode 100644 src/main/java/use_cases/user_login_use_cases/UserLoginOutputBoundary.java delete mode 100644 src/main/java/use_cases/user_login_use_cases/UserLoginPresenter.java delete mode 100644 src/main/java/use_cases/user_registration_use_cases/UserVerificationOutputBoundary.java diff --git a/src/main/java/interface_adapters/login_interface_adapters/UserChatsPresenter.java b/src/main/java/interface_adapters/login_interface_adapters/UserChatsPresenter.java new file mode 100644 index 00000000..2bc4876f --- /dev/null +++ b/src/main/java/interface_adapters/login_interface_adapters/UserChatsPresenter.java @@ -0,0 +1,53 @@ +package interface_adapters.login_interface_adapters; + +import use_cases.user_login_use_cases.UserLoginOutputBoundary; + +import java.util.ArrayList; +import java.util.List; + +public class UserChatsPresenter implements UserLoginOutputBoundary { + private List chats; + private boolean notExists; + private boolean notMatched; + private String username; + + public UserChatsPresenter(){ + this.chats = new ArrayList<>(); + } + + @Override + public void setUsername(String username) { + this.username = username; + } + + @Override + public void setChats(List chats) { + this.chats = chats; + } + public List getChats(){ + return this.chats; + } + + @Override + public void setUserNotExists(boolean notExists) { + this.notExists = notExists; + } + + public boolean isNotExists() { + return notExists; + } + + @Override + public void setPasswordNotMatched(boolean notMatched) { + this.notMatched = notMatched; + } + + @Override + public String getUsername() { + return this.username; + } + + public boolean isNotMatched() { + return notMatched; + } +} diff --git a/src/main/java/interface_adapters/login_interface_adapters/UserLoginPresenter.java b/src/main/java/interface_adapters/login_interface_adapters/UserLoginPresenter.java new file mode 100644 index 00000000..1007a623 --- /dev/null +++ b/src/main/java/interface_adapters/login_interface_adapters/UserLoginPresenter.java @@ -0,0 +1,32 @@ +package interface_adapters.login_interface_adapters; + +import data_access.Database; +import use_cases.user_login_use_cases.UserLoginInputBoundary; + +public class UserLoginPresenter { + private final UserLoginInputBoundary loginGuard; + private String username; + private String password; + Database database; + private UserLoginViewI loginView; + + public UserLoginPresenter(Database database, UserLoginInputBoundary loginGuard){ + this.database = database; + this.loginGuard = loginGuard; + } + + public void tryLogin() { + loginGuard.login(this.username, this.password); + loginView.setChatsPresenter(loginGuard.getChatsPresenter()); + loginView.display(); + } + + public void setLoginCredentials(String username, String password) { + this.username = username; + this.password = password; + } + public void setLoginView(UserLoginViewI loginView){ + this.loginView = loginView; + } + +} diff --git a/src/main/java/interface_adapters/login_interface_adapters/UserLoginViewI.java b/src/main/java/interface_adapters/login_interface_adapters/UserLoginViewI.java new file mode 100644 index 00000000..5c585f10 --- /dev/null +++ b/src/main/java/interface_adapters/login_interface_adapters/UserLoginViewI.java @@ -0,0 +1,8 @@ +package interface_adapters.login_interface_adapters; + +import use_cases.user_login_use_cases.UserLoginOutputBoundary; + +public interface UserLoginViewI { + void display(); + void setChatsPresenter(UserLoginOutputBoundary outputBoundary); +} diff --git a/src/main/java/use_cases/user_registration_use_cases/UserExistsOutputBoundary.java b/src/main/java/interface_adapters/user_registration_interface_adapters/UserExistsOutputView.java similarity index 87% rename from src/main/java/use_cases/user_registration_use_cases/UserExistsOutputBoundary.java rename to src/main/java/interface_adapters/user_registration_interface_adapters/UserExistsOutputView.java index a2cab656..5f4ab589 100644 --- a/src/main/java/use_cases/user_registration_use_cases/UserExistsOutputBoundary.java +++ b/src/main/java/interface_adapters/user_registration_interface_adapters/UserExistsOutputView.java @@ -1,8 +1,8 @@ -package use_cases.user_registration_use_cases; +package interface_adapters.user_registration_interface_adapters; /** * Presenter interface that presents information, or gets input from user * */ -public interface UserExistsOutputBoundary { +public interface UserExistsOutputView { /** * Gets the verification code from the user * */ diff --git a/src/main/java/use_cases/user_registration_use_cases/UserExistsPresenter.java b/src/main/java/interface_adapters/user_registration_interface_adapters/UserExistsPresenter.java similarity index 86% rename from src/main/java/use_cases/user_registration_use_cases/UserExistsPresenter.java rename to src/main/java/interface_adapters/user_registration_interface_adapters/UserExistsPresenter.java index 685f296a..203ab6dc 100644 --- a/src/main/java/use_cases/user_registration_use_cases/UserExistsPresenter.java +++ b/src/main/java/interface_adapters/user_registration_interface_adapters/UserExistsPresenter.java @@ -1,6 +1,8 @@ -package use_cases.user_registration_use_cases; +package interface_adapters.user_registration_interface_adapters; import data_access.Database; +import use_cases.user_registration_use_cases.VerificationCodeDeliveryManager; +import use_cases.user_registration_use_cases.createMailMan; /** * This is the class responsible for getting processing the input given by user, and either allowing verification, @@ -9,9 +11,9 @@ public class UserExistsPresenter { private final VerificationCodeDeliveryManager verCodeDeliveryManager; Database database; - UserExistsOutputBoundary existsOutputBoundary; + UserExistsOutputView existsOutputBoundary; - public UserExistsPresenter(Database database, UserExistsOutputBoundary existsOutputBoundary, createMailMan mailMan){ + public UserExistsPresenter(Database database, UserExistsOutputView existsOutputBoundary, createMailMan mailMan){ this.database = database; this.existsOutputBoundary = existsOutputBoundary; //The responsibility of dealing with verification is passed onto this class diff --git a/src/main/java/interface_adapters/user_registration_interface_adapters/UserVerificationOutputView.java b/src/main/java/interface_adapters/user_registration_interface_adapters/UserVerificationOutputView.java new file mode 100644 index 00000000..cee1b432 --- /dev/null +++ b/src/main/java/interface_adapters/user_registration_interface_adapters/UserVerificationOutputView.java @@ -0,0 +1,7 @@ +package interface_adapters.user_registration_interface_adapters; + +public interface UserVerificationOutputView { + void getLoginCredentials(); + void cannotVerify(); + +} diff --git a/src/main/java/use_cases/user_registration_use_cases/UserVerificationPresenter.java b/src/main/java/interface_adapters/user_registration_interface_adapters/UserVerificationPresenter.java similarity index 88% rename from src/main/java/use_cases/user_registration_use_cases/UserVerificationPresenter.java rename to src/main/java/interface_adapters/user_registration_interface_adapters/UserVerificationPresenter.java index c8cf615b..b194f2ec 100644 --- a/src/main/java/use_cases/user_registration_use_cases/UserVerificationPresenter.java +++ b/src/main/java/interface_adapters/user_registration_interface_adapters/UserVerificationPresenter.java @@ -1,4 +1,4 @@ -package use_cases.user_registration_use_cases; +package interface_adapters.user_registration_interface_adapters; import data_access.Database; @@ -8,11 +8,11 @@ public class UserVerificationPresenter { private String password; private String email; - private final UserVerificationOutputBoundary verificationOutputBoundary; + private final UserVerificationOutputView verificationOutputBoundary; private int code; - public UserVerificationPresenter(Database database, UserVerificationOutputBoundary verificationOutputBoundary){ + public UserVerificationPresenter(Database database, UserVerificationOutputView verificationOutputBoundary){ this.database = database; this.verificationOutputBoundary = verificationOutputBoundary; } diff --git a/src/main/java/use_cases/user_registration_use_cases/userRegCredentialsRetriever.java b/src/main/java/interface_adapters/user_registration_interface_adapters/userRegCredentialsRetriever.java similarity index 55% rename from src/main/java/use_cases/user_registration_use_cases/userRegCredentialsRetriever.java rename to src/main/java/interface_adapters/user_registration_interface_adapters/userRegCredentialsRetriever.java index 1d4951cb..cf2e0fd6 100644 --- a/src/main/java/use_cases/user_registration_use_cases/userRegCredentialsRetriever.java +++ b/src/main/java/interface_adapters/user_registration_interface_adapters/userRegCredentialsRetriever.java @@ -1,4 +1,4 @@ -package use_cases.user_registration_use_cases; +package interface_adapters.user_registration_interface_adapters; public interface userRegCredentialsRetriever { void getUserCredentials(); diff --git a/src/main/java/screens/login_screen/AppScreenCreator.java b/src/main/java/screens/login_screen/AppScreenCreator.java new file mode 100644 index 00000000..91a66a42 --- /dev/null +++ b/src/main/java/screens/login_screen/AppScreenCreator.java @@ -0,0 +1,37 @@ +package screens.login_screen; +import interface_adapters.login_interface_adapters.UserLoginViewI; +import screens.appscreen.AppScreen; +import use_cases.user_login_use_cases.UserLoginOutputBoundary; + +import java.util.ArrayList; + +public class AppScreenCreator implements UserLoginViewI { + private String username ; + private ArrayList chats; + private boolean userNotExists; + private boolean passNotMatched; + AppScreen appScreen; + public AppScreenCreator(){ + } + @Override + public void display() { + if(userNotExists|| passNotMatched){ + showUnableToLogin(); + }else{ + /*this.appScreen = new AppScreen(username, chats);*/ + System.out.println(username); + } + } + + private void showUnableToLogin() { + System.out.println("unable to login"); + } + @Override + public void setChatsPresenter(UserLoginOutputBoundary chatsPresenter){ + this.username = chatsPresenter.getUsername(); + this.chats = (ArrayList) chatsPresenter.getChats(); + this.userNotExists = chatsPresenter.isNotExists(); + this.passNotMatched = chatsPresenter.isNotMatched(); + } + +} diff --git a/src/main/java/screens/login_screen/UserLoginUI.java b/src/main/java/screens/login_screen/UserLoginUI.java index 444c2b9f..19c3f99c 100644 --- a/src/main/java/screens/login_screen/UserLoginUI.java +++ b/src/main/java/screens/login_screen/UserLoginUI.java @@ -1,20 +1,37 @@ package screens.login_screen; -import use_cases.user_login_use_cases.UserLoginPresenter; -import use_cases.user_registration_use_cases.UserVerificationOutputBoundary; +import data_access.Database; +import data_access.UserDatabase; +import interface_adapters.login_interface_adapters.UserChatsPresenter; +import interface_adapters.login_interface_adapters.UserLoginViewI; +import use_cases.user_login_use_cases.UserLoginInputBoundary; +import interface_adapters.login_interface_adapters.UserLoginPresenter; +import use_cases.user_login_use_cases.UserLoginInteractor2; +import interface_adapters.user_registration_interface_adapters.UserVerificationOutputView; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.io.File; /** This is the screen on which the user enters his credentials in order to login **/ -public class UserLoginUI implements ActionListener, UserVerificationOutputBoundary { +public class UserLoginUI implements ActionListener, UserVerificationOutputView { - private final UserLoginPresenter loginInteractor; + private final UserLoginPresenter loginPresenter; JTextField credentialText; JPasswordField passwordText; - public UserLoginUI(UserLoginPresenter loginInteractor){ - this.loginInteractor = loginInteractor; + public UserLoginUI(UserLoginPresenter loginPresenter){ + UserLoginViewI loginViewI = new AppScreenCreator(); + this.loginPresenter = loginPresenter; + this.loginPresenter.setLoginView(loginViewI); + } + //For Testing Purposes + public static void main(String[] args){ + Database userDB = new UserDatabase(new File("new")); + UserLoginInputBoundary inputBoundary = new UserLoginInteractor2(userDB, new UserChatsPresenter()); + UserLoginPresenter loginPresenter1 = new UserLoginPresenter(userDB, inputBoundary); + UserLoginUI loginUI = new UserLoginUI(loginPresenter1); + loginUI.getLoginCredentials(); } @Override public void getLoginCredentials(){ @@ -69,7 +86,7 @@ public void cannotVerify() { public void actionPerformed(ActionEvent e) { String username = credentialText.getText(); String password = passwordText.getText(); - loginInteractor.setLoginCredentials(username, password); - loginInteractor.tryLogin(); + loginPresenter.setLoginCredentials(username, password); + loginPresenter.tryLogin(); } } diff --git a/src/main/java/screens/user_registration_screen/UserRegistrationUI.java b/src/main/java/screens/user_registration_screen/UserRegistrationUI.java index cfab2d00..84ea9bc9 100644 --- a/src/main/java/screens/user_registration_screen/UserRegistrationUI.java +++ b/src/main/java/screens/user_registration_screen/UserRegistrationUI.java @@ -1,8 +1,11 @@ package screens.user_registration_screen; import data_access.Database; import data_access.UserDatabase; +import interface_adapters.login_interface_adapters.UserChatsPresenter; +import interface_adapters.user_registration_interface_adapters.*; import screens.login_screen.UserLoginUI; -import use_cases.user_login_use_cases.UserLoginPresenter; +import use_cases.user_login_use_cases.UserLoginInteractor2; +import interface_adapters.login_interface_adapters.UserLoginPresenter; import use_cases.user_registration_use_cases.*; import javax.swing.*; @@ -84,10 +87,11 @@ public void getUserCredentials(){ public static void main(String[] args){ Database testDB = new UserDatabase(new File("newAccounts2")); - UserLoginPresenter userLoginInteractor = new UserLoginPresenter(testDB); - UserVerificationOutputBoundary loginUI = new UserLoginUI(userLoginInteractor); + UserLoginInteractor2 userLoginInteractor2 = new UserLoginInteractor2(testDB, new UserChatsPresenter()); + UserLoginPresenter userLoginPresenter = new UserLoginPresenter(testDB, userLoginInteractor2); + UserVerificationOutputView loginUI = new UserLoginUI(userLoginPresenter); UserVerificationPresenter verificationInteractor = new UserVerificationPresenter(testDB, loginUI); - UserExistsOutputBoundary verificationScreen = new UserVerificationScreen(verificationInteractor); + UserExistsOutputView verificationScreen = new UserVerificationScreen(verificationInteractor); UserExistsPresenter existsInteractor = new UserExistsPresenter(testDB, verificationScreen, new verificationMethodFactory()); UserRegistrationUI testUI = new UserRegistrationUI(existsInteractor); testUI.getUserCredentials(); diff --git a/src/main/java/screens/user_registration_screen/UserVerificationScreen.java b/src/main/java/screens/user_registration_screen/UserVerificationScreen.java index 26338b54..d5f5bfc9 100644 --- a/src/main/java/screens/user_registration_screen/UserVerificationScreen.java +++ b/src/main/java/screens/user_registration_screen/UserVerificationScreen.java @@ -1,5 +1,6 @@ package screens.user_registration_screen; -import use_cases.user_registration_use_cases.*; +import interface_adapters.user_registration_interface_adapters.UserExistsOutputView; +import interface_adapters.user_registration_interface_adapters.UserVerificationPresenter; import javax.swing.*; import java.awt.event.ActionEvent; @@ -8,7 +9,7 @@ * This is the class that is responsible for retrieving the verification code form the user, or presenting if * verification is not possible * */ -public class UserVerificationScreen implements UserExistsOutputBoundary, ActionListener { +public class UserVerificationScreen implements UserExistsOutputView, ActionListener { private final JTextField verText = new JTextField(20); private final UserVerificationPresenter verificationInputBoundary; private int code; diff --git a/src/main/java/use_cases/user_login_use_cases/UserLoginInputBoundary.java b/src/main/java/use_cases/user_login_use_cases/UserLoginInputBoundary.java index d0ff589b..74fa86a7 100644 --- a/src/main/java/use_cases/user_login_use_cases/UserLoginInputBoundary.java +++ b/src/main/java/use_cases/user_login_use_cases/UserLoginInputBoundary.java @@ -1,7 +1,8 @@ package use_cases.user_login_use_cases; public interface UserLoginInputBoundary { - void tryLogin(); + void login(String username, String password); + + UserLoginOutputBoundary getChatsPresenter(); - void setLoginCredentials(String username, String password); } diff --git a/src/main/java/use_cases/user_login_use_cases/UserLoginInteractor2.java b/src/main/java/use_cases/user_login_use_cases/UserLoginInteractor2.java new file mode 100644 index 00000000..e9853ea1 --- /dev/null +++ b/src/main/java/use_cases/user_login_use_cases/UserLoginInteractor2.java @@ -0,0 +1,34 @@ +package use_cases.user_login_use_cases; + +import data_access.Database; +import entities.user_entities.User; + +import java.util.ArrayList; + +public class UserLoginInteractor2 implements UserLoginInputBoundary { + private final UserLoginOutputBoundary chatPresenter; + private final Database database; + + private User user; + + public UserLoginInteractor2(Database database, UserLoginOutputBoundary chatPresenter){ + this.database = database; + this.chatPresenter = chatPresenter; + } + + @Override + public void login(String username, String password) { + //will update this.chatPresenter + //TODO: change below, as its just temporary + chatPresenter.setChats(new ArrayList<>()); + chatPresenter.setUsername(username); + chatPresenter.setUserNotExists(false); + chatPresenter.setPasswordNotMatched(false); + + } + + @Override + public UserLoginOutputBoundary getChatsPresenter() { + return this.chatPresenter; + } +} diff --git a/src/main/java/use_cases/user_login_use_cases/UserLoginOutputBoundary.java b/src/main/java/use_cases/user_login_use_cases/UserLoginOutputBoundary.java new file mode 100644 index 00000000..f37fc22f --- /dev/null +++ b/src/main/java/use_cases/user_login_use_cases/UserLoginOutputBoundary.java @@ -0,0 +1,15 @@ +package use_cases.user_login_use_cases; + +import java.util.List; + +public interface UserLoginOutputBoundary { + void setUsername(String username); + void setChats(List chats); + void setUserNotExists(boolean notExists); + void setPasswordNotMatched(boolean notMatched); + + String getUsername(); + List getChats(); + boolean isNotExists(); + boolean isNotMatched(); +} diff --git a/src/main/java/use_cases/user_login_use_cases/UserLoginPresenter.java b/src/main/java/use_cases/user_login_use_cases/UserLoginPresenter.java deleted file mode 100644 index 670f6cf3..00000000 --- a/src/main/java/use_cases/user_login_use_cases/UserLoginPresenter.java +++ /dev/null @@ -1,36 +0,0 @@ -package use_cases.user_login_use_cases; - -import data_access.Database; -import entities.user_entities.User; - -public class UserLoginPresenter { - private String username; - private String password; - private User user; - Database database; - public UserLoginPresenter(Database database){ - this.database = database; - } - - public void tryLogin() { - try{ - //TODO: issues here with serialization, and dependency inversion - user = database.getUser(username); - if(user.PasswordMatch(this.password)){ - user.login(); - }else{ - System.out.println("the password or username is incorrect"); - } - }catch(NullPointerException e){ - //TODO: implement login output boundary - System.out.println("An account with these credentials do not exist"); - } - - } - - public void setLoginCredentials(String username, String password) { - this.username = username; - this.password = password; - } - -} diff --git a/src/main/java/use_cases/user_registration_use_cases/UserVerificationOutputBoundary.java b/src/main/java/use_cases/user_registration_use_cases/UserVerificationOutputBoundary.java deleted file mode 100644 index cde3797f..00000000 --- a/src/main/java/use_cases/user_registration_use_cases/UserVerificationOutputBoundary.java +++ /dev/null @@ -1,7 +0,0 @@ -package use_cases.user_registration_use_cases; - -public interface UserVerificationOutputBoundary { - void getLoginCredentials(); - void cannotVerify(); - -} diff --git a/src/test/java/test_user_registration/TestUserExistsInteractor.java b/src/test/java/test_user_registration/TestUserExistsInteractor.java index f17e4640..865d8b19 100644 --- a/src/test/java/test_user_registration/TestUserExistsInteractor.java +++ b/src/test/java/test_user_registration/TestUserExistsInteractor.java @@ -5,8 +5,8 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import use_cases.user_registration_use_cases.ISendVerificationCode; -import use_cases.user_registration_use_cases.UserExistsPresenter; -import use_cases.user_registration_use_cases.UserExistsOutputBoundary; +import interface_adapters.user_registration_interface_adapters.UserExistsPresenter; +import interface_adapters.user_registration_interface_adapters.UserExistsOutputView; import use_cases.user_registration_use_cases.createMailMan; public class TestUserExistsInteractor { @@ -55,7 +55,7 @@ public boolean UserExists(String username) { } }; //This output boundary used in test cases - private class TestOutputBoundary implements UserExistsOutputBoundary{ + private class TestOutputBoundary implements UserExistsOutputView { public int x = 0; @Override public void getVerificationCredentials() { diff --git a/src/test/java/test_user_registration/TestUserVerificationInteractor.java b/src/test/java/test_user_registration/TestUserVerificationInteractor.java index bc5fba0d..f2c38f34 100644 --- a/src/test/java/test_user_registration/TestUserVerificationInteractor.java +++ b/src/test/java/test_user_registration/TestUserVerificationInteractor.java @@ -4,8 +4,8 @@ import entities.user_entities.User; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -import use_cases.user_registration_use_cases.UserVerificationPresenter; -import use_cases.user_registration_use_cases.UserVerificationOutputBoundary; +import interface_adapters.user_registration_interface_adapters.UserVerificationPresenter; +import interface_adapters.user_registration_interface_adapters.UserVerificationOutputView; public class TestUserVerificationInteractor { private class testDatabase implements Database{ @@ -30,7 +30,7 @@ public boolean UserExists(String username) { return false; } } - private class testUserVerificationOutputBdy implements UserVerificationOutputBoundary{ + private class testUserVerificationOutputBdy implements UserVerificationOutputView { public int x; @Override public void getLoginCredentials() {