diff --git a/build.gradle b/build.gradle index 074c8187..e879019c 100644 --- a/build.gradle +++ b/build.gradle @@ -18,9 +18,9 @@ dependencies { implementation 'commons-validator:commons-validator:1.7' implementation 'com.toedter:jcalendar:1.4' implementation 'com.fasterxml.jackson.core:jackson-core:2.12.3' - implementation 'com.fasterxml.jackson.core:jackson-databind:2.12.3' + implementation 'com.fasterxml.jackson.core:jackson-databind:2.12.7.1' implementation 'commons-io:commons-io:2.8.0' - implementation 'cn.hutool:hutool-core:5.7.16' + implementation 'cn.hutool:hutool-core:5.8.11' implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.12.5' } diff --git a/src/main/java/Main.java b/src/main/java/Main.java index fbd04236..885f07ac 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,19 +1,18 @@ -import adapter.controller.*; -import adapter.presenter.*; -import businessrule.UIFactory; -import businessrule.gateway.*; -import businessrule.inputboundary.*; -import businessrule.outputboundary.*; -import businessrule.responsemodel.BaseResponseModel; -import businessrule.usecase.*; -import driver.database.*; -import driver.screen.UIDesign; -import driver.screen.UIManager; -import driver.screen.WelcomeUI; -import entity.Attorney; -import entity.factory.ClientFactory; -import entity.factory.PostFactory; -import entity.factory.QuestionFactory; +import adapters.controllers.*; +import adapters.presenters.*; +import entities.factories.AttorneyFactory; +import infrastructure.screens.UIFactory; +import usecases.gateway.*; +import usecases.inputboundary.*; +import usecases.outputboundary.*; +import usecases.responses.BaseResponseModel; +import usecases.interactors.*; +import infrastructure.database.*; +import infrastructure.screens.utils.UIDesign; +import infrastructure.screens.utils.UIManager; +import entities.factories.ClientFactory; +import entities.factories.PostFactory; +import entities.factories.QuestionFactory; import javax.swing.*; import java.awt.*; @@ -25,6 +24,7 @@ public static void main(String[] args) { ClientGateway clientGateway = new ClientRepository(); AttorneyGateway attorneyGateway = new AttorneyRepository(); ClientFactory clientFactory = new ClientFactory(); + AttorneyFactory attorneyFactory = new AttorneyFactory(); QuestionFactory questionFactory = new QuestionFactory(); QuestionGateway questionGateway = new QuestionRepo(); @@ -33,14 +33,14 @@ public static void main(String[] args) { System.out.println("System - finished set up repo"); - AttorneyRepository attorneyRepo = new AttorneyRepository(); - attorneyRepo.delete(12345678); - Attorney attorney = new Attorney(); - attorney.setUserName("Kaxi"); - attorney.setPassword("12345678"); - attorney.setUserId(12345678); - attorney.setEmail("123455@gmail.com"); - attorneyRepo.save(attorney); +// AttorneyRepository attorneyRepo = new AttorneyRepository(); +// attorneyRepo.delete(12345678); +// Attorney attorney = new Attorney(); +// attorney.setUserName("Kaxi"); +// attorney.setPassword("12345678"); +// attorney.setUserId(12345678); +// attorney.setEmail("123455@gmail.com"); +// attorneyRepo.save(attorney); //set up Jframe JFrame application = new JFrame("Legal Consultation Platform"); @@ -64,8 +64,10 @@ public static void main(String[] args) { UserLoginInputBoundary userLoginInteractor = new UserLoginInteractor(gatewayFactory, homePageOutputBoundary); UserLoginControl loginControl = new UserLoginControl(userLoginInteractor); - UserRegisterInputBoundary clientRegisterInteractor = new ClientRegisterInteractor(clientGateway, clientFactory, registerOutputBoundary); - ClientRegisterControl registerControl = new ClientRegisterControl(clientRegisterInteractor); + UserRegisterInputBoundary clientRegisterInteractor = new ClientRegisterInteractor(clientGateway, registerOutputBoundary, clientFactory); + UserRegisterInputBoundary attorneyRegisterInteractor = new AttorneyRegisterInteractor(attorneyGateway, registerOutputBoundary, attorneyFactory); + RegisterControl registerControl = new RegisterControl(clientRegisterInteractor); + RegisterControl attorneyRegisterControl = new RegisterControl(attorneyRegisterInteractor); QuestionInputBoundary questionInteractor = new AskQuestionInteractor(questionGateway, theQuestionOutputBoundary, questionFactory, clientGateway); @@ -93,7 +95,7 @@ public static void main(String[] args) { postFactory, gatewayFactory); PostControl postControl = new PostControl(replyInteractor); - RateInputBoundary rateInteractor = new RateInteractor(questionGateway, homePageOutputBoundary, clientGateway, + RateInputBoundary rateInteractor = new RateInteractor(questionGateway, homePageOutputBoundary, attorneyGateway); RateControl rateControl = new RateControl(rateInteractor); System.out.println("System - finished set up use case"); @@ -101,7 +103,7 @@ public static void main(String[] args) { //control container ControlContainer controlContainer = new ControlContainer(registerControl, closeQuestionControl, postControl, questionControl, rateControl, selectQuestionControl, loginControl, viewQuestionControl, - browseQuestionControl, recommendationControl); + browseQuestionControl, recommendationControl, attorneyRegisterControl); //feed control container into the response formatter UIManager.setControlContainer(controlContainer); diff --git a/src/main/java/adapter/controller/ClientRegisterControl.java b/src/main/java/adapter/controller/ClientRegisterControl.java deleted file mode 100644 index bd948da0..00000000 --- a/src/main/java/adapter/controller/ClientRegisterControl.java +++ /dev/null @@ -1,22 +0,0 @@ -package adapter.controller; - -import businessrule.inputboundary.UserRegisterInputBoundary; -import businessrule.requestmodel.RegistrationData; -import businessrule.responsemodel.BaseResponseModel; - -public class ClientRegisterControl { - private final UserRegisterInputBoundary inputBoundary; - - public ClientRegisterControl(UserRegisterInputBoundary inputBoundary) { - this.inputBoundary = inputBoundary; - } - - public BaseResponseModel create(String userName, String email, String password1, String password2, - String StateAbb, String PostalCode, - String ethnicity, int age, String gender, String maritalStatus, - int numberOfHousehold, float annualIncome){ - RegistrationData requestModel = new RegistrationData(userName, email, password1, password2, StateAbb, - PostalCode, ethnicity, age, gender, maritalStatus, numberOfHousehold, annualIncome); - return inputBoundary.create(requestModel); - } -} diff --git a/src/main/java/adapter/controller/CloseQuestionControl.java b/src/main/java/adapter/controller/CloseQuestionControl.java deleted file mode 100644 index beccc0bf..00000000 --- a/src/main/java/adapter/controller/CloseQuestionControl.java +++ /dev/null @@ -1,18 +0,0 @@ -package adapter.controller; - -import businessrule.inputboundary.CloseInputBoundary; -import businessrule.requestmodel.CloseRequestModel; -import businessrule.responsemodel.UserResponseModel; - -public class CloseQuestionControl { - private final CloseInputBoundary closeInputBoundary; - - public CloseQuestionControl(CloseInputBoundary closeInputBoundary) { - this.closeInputBoundary = closeInputBoundary; - } - public UserResponseModel closeQuestion(int questionId){ - CloseRequestModel closeRequestModel = new CloseRequestModel(questionId); - return closeInputBoundary.closeQuestion(closeRequestModel); - } -} - diff --git a/src/main/java/adapter/controller/PostControl.java b/src/main/java/adapter/controller/PostControl.java deleted file mode 100644 index 58eb0825..00000000 --- a/src/main/java/adapter/controller/PostControl.java +++ /dev/null @@ -1,20 +0,0 @@ -package adapter.controller; - -import businessrule.inputboundary.PostInputBoundary; -import businessrule.requestmodel.PostRequestModel; -import businessrule.responsemodel.TheQuestionResponseModel; -import businessrule.responsemodel.UserResponseModel; - -public class PostControl { - private final PostInputBoundary postInput; - - public PostControl(PostInputBoundary postInput){ - this.postInput = postInput; - } - - public TheQuestionResponseModel createPost(int questionId, String postText){ - PostRequestModel postRequestModel = new PostRequestModel(questionId, postText); - - return postInput.createPost(postRequestModel); - } -} diff --git a/src/main/java/adapter/controller/QuestionControl.java b/src/main/java/adapter/controller/QuestionControl.java deleted file mode 100644 index 0d1f6ed9..00000000 --- a/src/main/java/adapter/controller/QuestionControl.java +++ /dev/null @@ -1,21 +0,0 @@ -package adapter.controller; - -import businessrule.responsemodel.TheQuestionResponseModel; -import businessrule.inputboundary.QuestionInputBoundary; -import businessrule.requestmodel.QuestionRequestModel; -import businessrule.responsemodel.UserResponseModel; - -import java.time.LocalDate; - -public class QuestionControl { - private final QuestionInputBoundary questionInput; - - public QuestionControl(QuestionInputBoundary questionInput){ - this.questionInput = questionInput; - } - - public TheQuestionResponseModel createQuestion(String questionCategory, String title, LocalDate createAt, LocalDate legalDeadline){ - QuestionRequestModel requestModel = new QuestionRequestModel(questionCategory, title, createAt, legalDeadline); - return questionInput.createQuestion(requestModel); - } -} diff --git a/src/main/java/adapter/controller/RateControl.java b/src/main/java/adapter/controller/RateControl.java deleted file mode 100644 index dcc4036c..00000000 --- a/src/main/java/adapter/controller/RateControl.java +++ /dev/null @@ -1,19 +0,0 @@ -package adapter.controller; - -import businessrule.inputboundary.RateInputBoundary; -import businessrule.requestmodel.RateRequestModel; -import businessrule.responsemodel.UserResponseModel; - -public class RateControl { - private final RateInputBoundary rateInput; - - public RateControl(RateInputBoundary input) { - this.rateInput = input; - } - - public UserResponseModel rateAnswer(int rate, int questionId) { - RateRequestModel rating = new RateRequestModel(rate, questionId); - return rateInput.rateAnswer(rating); - } - -} diff --git a/src/main/java/adapter/controller/SelectQuestionControl.java b/src/main/java/adapter/controller/SelectQuestionControl.java deleted file mode 100644 index e09f8eb9..00000000 --- a/src/main/java/adapter/controller/SelectQuestionControl.java +++ /dev/null @@ -1,18 +0,0 @@ -package adapter.controller; - -import businessrule.inputboundary.SelectInputBoundary; -import businessrule.requestmodel.SelectRequestModel; -import businessrule.responsemodel.TheQuestionResponseModel; -import businessrule.responsemodel.UserResponseModel; - -public class SelectQuestionControl { - private final SelectInputBoundary selectInputBoundary; - - public SelectQuestionControl(SelectInputBoundary selectInputBoundary) { - this.selectInputBoundary = selectInputBoundary; - } - public TheQuestionResponseModel selectQuestion(int questionId) { - SelectRequestModel selectRequestModel = new SelectRequestModel(questionId); - return selectInputBoundary.selectQuestion(selectRequestModel); - } -} diff --git a/src/main/java/adapter/controller/UserLoginControl.java b/src/main/java/adapter/controller/UserLoginControl.java deleted file mode 100644 index d88b3812..00000000 --- a/src/main/java/adapter/controller/UserLoginControl.java +++ /dev/null @@ -1,18 +0,0 @@ -package adapter.controller; - -import businessrule.inputboundary.UserLoginInputBoundary; -import businessrule.requestmodel.UserLoginRequestModel; -import businessrule.responsemodel.UserResponseModel; - -public class UserLoginControl { - private final UserLoginInputBoundary inputBoundary; - - public UserLoginControl(UserLoginInputBoundary inputBoundary) { - this.inputBoundary = inputBoundary; - } - - public UserResponseModel login(int userId, String password){ - UserLoginRequestModel requestModel = new UserLoginRequestModel(userId, password); - return inputBoundary.login(requestModel); - } -} diff --git a/src/main/java/adapter/controller/ViewQuestionControl.java b/src/main/java/adapter/controller/ViewQuestionControl.java deleted file mode 100644 index 07cefcaf..00000000 --- a/src/main/java/adapter/controller/ViewQuestionControl.java +++ /dev/null @@ -1,20 +0,0 @@ -package adapter.controller; - -import businessrule.responsemodel.UserResponseModel; -import businessrule.inputboundary.ViewInputBoundary; -import businessrule.responsemodel.ViewResponseModel; - -import javax.swing.text.View; - -public class ViewQuestionControl{ - - private final ViewInputBoundary viewInputBoundary; - - public ViewQuestionControl(ViewInputBoundary viewInputBoundary) { - this.viewInputBoundary = viewInputBoundary; - } - - public ViewResponseModel viewQuestion(){ - return viewInputBoundary.viewQuestion(); - } -} diff --git a/src/main/java/adapter/presenter/HomePageResponseFormatter.java b/src/main/java/adapter/presenter/HomePageResponseFormatter.java deleted file mode 100644 index 85ece989..00000000 --- a/src/main/java/adapter/presenter/HomePageResponseFormatter.java +++ /dev/null @@ -1,37 +0,0 @@ -package adapter.presenter; -import businessrule.UIFactory; -import businessrule.outputboundary.UserOutputBoundary; -import businessrule.responsemodel.UserResponseModel; -import driver.screen.HomePageUI; -import driver.screen.UIManager; -import entity.ApplicationException; - -import javax.swing.*; -import java.awt.*; - -public class HomePageResponseFormatter implements UserOutputBoundary { - UIManager UIManager; - - public HomePageResponseFormatter(UIManager UIManager) { - this.UIManager = UIManager; - } - - @Override - public UserResponseModel prepareFail(String msg) { - throw new ApplicationException(msg); - } - - @Override - public UserResponseModel prepareSuccess(UserResponseModel response) { - pageConstructor(response); - return response; - } - - protected void pageConstructor(UserResponseModel response){ - JPanel screens = UIManager.getScreens(); - CardLayout cardLayout = UIManager.getCardLayout(); - JPanel homePage = UIFactory.getUI(UIFactory.UIType.HOME_PAGE, UIManager, response); - screens.add(homePage, "Home Page"); - cardLayout.show(screens, "Home Page"); - } -} diff --git a/src/main/java/adapter/presenter/RegisterResponseFormatter.java b/src/main/java/adapter/presenter/RegisterResponseFormatter.java deleted file mode 100644 index e3cee226..00000000 --- a/src/main/java/adapter/presenter/RegisterResponseFormatter.java +++ /dev/null @@ -1,41 +0,0 @@ -package adapter.presenter; - -import adapter.controller.ControlContainer; -import businessrule.UIFactory; -import businessrule.outputboundary.BaseOutputBoundary; -import businessrule.responsemodel.BaseResponseModel; -import driver.screen.UIManager; -import entity.ApplicationException; -//import driver.screen.WelcomeUI; -import javax.swing.*; -import java.awt.*; -import java.awt.datatransfer.Clipboard; -import java.awt.datatransfer.StringSelection; -import java.awt.datatransfer.Transferable; - -public class RegisterResponseFormatter implements BaseOutputBoundary { - UIManager UIManager; - - public RegisterResponseFormatter(UIManager UIManager) { - this.UIManager = UIManager; - } - - @Override - public BaseResponseModel prepareFail(String msg) { - throw new ApplicationException(msg); - } - @Override - public BaseResponseModel prepareSuccess(String msg) { - JPanel screens = UIManager.getScreens(); - CardLayout cardLayout = UIManager.getCardLayout(); - JPanel welcomeUI = UIFactory.getUI(UIFactory.UIType.WELCOME_UI, UIManager, new BaseResponseModel()); - screens.add(welcomeUI, "Welcome"); - cardLayout.show(screens, "Welcome"); - Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard(); - Transferable tText = new StringSelection(msg+""); - clip.setContents(tText, null); - String message = "Important: Your unique User ID: " + msg + " has been generated and copied to your clipboard. \nPlease save it securely, as it will not be shown again."; - JOptionPane.showMessageDialog(null, message,"Notice",JOptionPane.YES_OPTION); - return new BaseResponseModel(); - } -} diff --git a/src/main/java/adapter/presenter/TheQuestionResponseFormatter.java b/src/main/java/adapter/presenter/TheQuestionResponseFormatter.java deleted file mode 100644 index 0872c247..00000000 --- a/src/main/java/adapter/presenter/TheQuestionResponseFormatter.java +++ /dev/null @@ -1,41 +0,0 @@ -package adapter.presenter; - - -import businessrule.UIFactory; -import businessrule.outputboundary.TheQuestionOutputBoundary; -import businessrule.responsemodel.TheQuestionResponseModel; -import businessrule.responsemodel.UserResponseModel; -import businessrule.usecase.util.PostDisplayFormatter; -import driver.screen.UIManager; -import entity.ApplicationException; - -import javax.swing.*; -import java.awt.*; -import java.util.Map; - -public class TheQuestionResponseFormatter implements TheQuestionOutputBoundary { - driver.screen.UIManager UIManager; - - public TheQuestionResponseFormatter(UIManager UIManager) { - this.UIManager = UIManager; - } - @Override - public TheQuestionResponseModel prepareFail(String msg) { - throw new ApplicationException(msg); - } - - @Override - public TheQuestionResponseModel prepareSuccess(TheQuestionResponseModel response) { - JPanel screens = UIManager.getScreens(); - CardLayout cardLayout = UIManager.getCardLayout(); - Map postMap= response.getPostMap(); - if (postMap.size() == 1){ - cardLayout.show(screens, "Home Page"); - } else { - JPanel questionUI = UIFactory.getUI(UIFactory.UIType.QUESTION_UI, UIManager, response); - screens.add(questionUI, "Question"); - cardLayout.show(screens, "Question"); - } - return response; - } -} diff --git a/src/main/java/adapter/presenter/ViewResponseFormatter.java b/src/main/java/adapter/presenter/ViewResponseFormatter.java deleted file mode 100644 index 2a0cc9a2..00000000 --- a/src/main/java/adapter/presenter/ViewResponseFormatter.java +++ /dev/null @@ -1,33 +0,0 @@ -package adapter.presenter; - -import businessrule.UIFactory; -import businessrule.outputboundary.ViewOutputBoundary; -import businessrule.responsemodel.UserResponseModel; -import businessrule.responsemodel.ViewResponseModel; -import driver.screen.UIManager; -import entity.ApplicationException; -import javax.swing.*; -import java.awt.*; - -public class ViewResponseFormatter implements ViewOutputBoundary { - UIManager UIManager; - - public ViewResponseFormatter(driver.screen.UIManager UIManager) { - this.UIManager = UIManager; - } - @Override - public ViewResponseModel prepareFail(String msg) { - throw new ApplicationException(msg); - } - - @Override - public ViewResponseModel prepareSuccess(ViewResponseModel response) { - JPanel screens = UIManager.getScreens(); - CardLayout cardLayout = UIManager.getCardLayout(); - JPanel questionListUI = UIFactory.getUI(UIFactory.UIType.QUESTION_LIST_UI, UIManager, response); - screens.add(questionListUI, "Question List"); - cardLayout.show(screens, "Question List"); - - return response; - } -} diff --git a/src/main/java/adapters/controllers/CloseQuestionControl.java b/src/main/java/adapters/controllers/CloseQuestionControl.java new file mode 100644 index 00000000..b1b5a443 --- /dev/null +++ b/src/main/java/adapters/controllers/CloseQuestionControl.java @@ -0,0 +1,34 @@ +package adapters.controllers; + +import usecases.inputboundary.CloseInputBoundary; +import usecases.requests.CloseRequestModel; +import usecases.responses.UserResponseModel; + +/** + * This class represents a controller responsible for closing a question. It utilizes the CloseInputBoundary + * to handle the process of closing a question. + */ +public class CloseQuestionControl { + private final CloseInputBoundary closeInputBoundary; + + /** + * Constructs a CloseQuestionControl instance with the provided CloseInputBoundary. + * + * @param closeInputBoundary The CloseInputBoundary implementation to be used for closing questions. + */ + public CloseQuestionControl(CloseInputBoundary closeInputBoundary) { + this.closeInputBoundary = closeInputBoundary; + } + + /** + * Initiates the process of closing a question. + * + * @param questionId The ID of the question to be closed. + * @return A UserResponseModel containing the result of the closing operation. + */ + public UserResponseModel closeQuestion(int questionId){ + CloseRequestModel closeRequestModel = new CloseRequestModel(questionId); + return closeInputBoundary.closeQuestion(closeRequestModel); + } +} + diff --git a/src/main/java/adapter/controller/ControlContainer.java b/src/main/java/adapters/controllers/ControlContainer.java similarity index 57% rename from src/main/java/adapter/controller/ControlContainer.java rename to src/main/java/adapters/controllers/ControlContainer.java index 3e0c00b8..f20186e5 100644 --- a/src/main/java/adapter/controller/ControlContainer.java +++ b/src/main/java/adapters/controllers/ControlContainer.java @@ -1,7 +1,12 @@ -package adapter.controller; +package adapters.controllers; +/** + * This class represents that holds instances of various control classes. + * It is responsible for managing different aspects of the application. + */ public class ControlContainer { - ClientRegisterControl clientRegisterControl; + RegisterControl clientRegisterControl; + RegisterControl attorneyRegisterControl; CloseQuestionControl closeQuestionControl; PostControl postControl; QuestionControl questionControl; @@ -12,12 +17,28 @@ public class ControlContainer { ViewQuestionControl browseQuestionControl; ViewQuestionControl recommendationControl; - public ControlContainer(ClientRegisterControl clientRegisterControl, CloseQuestionControl closeQuestionControl, + /** + * Constructor to initialize the ControlContainer with instances of various control classes. + * + * @param clientRegisterControl The control class for client registration. + * @param closeQuestionControl The control class for closing questions. + * @param postControl The control class for handling posts. + * @param questionControl The control class for question management. + * @param rateControl The control class for rating questions. + * @param selectQuestionControl The control class for selecting questions. + * @param userLoginControl The control class for user login. + * @param viewQuestionControl The control class for viewing questions. + * @param browseQuestionControl The control class for browsing questions. + * @param recommendationControl The control class for viewing recommended questions. + * @param attorneyRegisterControl The control class for attorney registration + */ + public ControlContainer(RegisterControl clientRegisterControl, CloseQuestionControl closeQuestionControl, PostControl postControl, QuestionControl questionControl, RateControl rateControl, SelectQuestionControl selectQuestionControl, UserLoginControl userLoginControl, ViewQuestionControl viewQuestionControl, ViewQuestionControl browseQuestionControl, - ViewQuestionControl recommendationControl) { + ViewQuestionControl recommendationControl, RegisterControl attorneyRegisterControl) { this.clientRegisterControl = clientRegisterControl; + this.attorneyRegisterControl = attorneyRegisterControl; this.closeQuestionControl = closeQuestionControl; this.postControl = postControl; this.questionControl = questionControl; @@ -29,18 +50,22 @@ public ControlContainer(ClientRegisterControl clientRegisterControl, CloseQuesti this.recommendationControl = recommendationControl; } - public ClientRegisterControl getClientRegisterControl() { + + public RegisterControl getClientRegisterControl() { return clientRegisterControl; } + public CloseQuestionControl getCloseQuestionControl() { return closeQuestionControl; } + public PostControl getPostControl() { return postControl; } + public QuestionControl getQuestionControl() { return questionControl; } @@ -64,4 +89,6 @@ public ViewQuestionControl getViewQuestionControl() { public ViewQuestionControl getBrowseQuestionControl() {return browseQuestionControl;} public ViewQuestionControl getRecommendationControl() {return recommendationControl;} + + public RegisterControl getAttorneyRegisterControl() {return attorneyRegisterControl;} } diff --git a/src/main/java/adapters/controllers/PostControl.java b/src/main/java/adapters/controllers/PostControl.java new file mode 100644 index 00000000..91585d37 --- /dev/null +++ b/src/main/java/adapters/controllers/PostControl.java @@ -0,0 +1,34 @@ +package adapters.controllers; + +import usecases.inputboundary.PostInputBoundary; +import usecases.requests.PostRequestModel; +import usecases.responses.TheQuestionResponseModel; + +/** + * This class represents the controller responsible for handling interactions related to creating posts. + */ +public class PostControl { + private final PostInputBoundary postInput; + + /** + * Constructs a new PostControl instance. + * + * @param postInput The input boundary for post-related operations. + */ + public PostControl(PostInputBoundary postInput){ + this.postInput = postInput; + } + + /** + * Creates a new post and associates it with a question. + * + * @param questionId The ID of the question the post is associated with. + * @param postText The content of the post. + * @return A TheQuestionResponseModel containing information about the updated question UI. + */ + public TheQuestionResponseModel createPost(int questionId, String postText){ + PostRequestModel postRequestModel = new PostRequestModel(questionId, postText); + + return postInput.createPost(postRequestModel); + } +} diff --git a/src/main/java/adapters/controllers/QuestionControl.java b/src/main/java/adapters/controllers/QuestionControl.java new file mode 100644 index 00000000..3b88059f --- /dev/null +++ b/src/main/java/adapters/controllers/QuestionControl.java @@ -0,0 +1,39 @@ +package adapters.controllers; + +import usecases.responses.TheQuestionResponseModel; +import usecases.inputboundary.QuestionInputBoundary; +import usecases.requests.QuestionRequestModel; + +import java.time.LocalDate; + +/** + * This class represents a controller for handling questions. + * It interacts with the business logic to create questions and retrieve responses. + */ + +public class QuestionControl { + private final QuestionInputBoundary questionInput; + + /** + * Constructor for QuestionControl. + * + * @param questionInput The input boundary for handling questions. + */ + public QuestionControl(QuestionInputBoundary questionInput){ + this.questionInput = questionInput; + } + + /** + * Creates a new question based on the provided information. + * + * @param questionCategory The category of the question. + * @param title The title of the question. + * @param createAt The creation date of the question. + * @param legalDeadline The legal deadline for the question. + * @return The response model containing the result of creating the question. + */ + public TheQuestionResponseModel createQuestion(String questionCategory, String title, LocalDate createAt, LocalDate legalDeadline){ + QuestionRequestModel requestModel = new QuestionRequestModel(questionCategory, title, createAt, legalDeadline); + return questionInput.createQuestion(requestModel); + } +} diff --git a/src/main/java/adapters/controllers/RateControl.java b/src/main/java/adapters/controllers/RateControl.java new file mode 100644 index 00000000..d52d887f --- /dev/null +++ b/src/main/java/adapters/controllers/RateControl.java @@ -0,0 +1,35 @@ +package adapters.controllers; + +import usecases.inputboundary.RateInputBoundary; +import usecases.requests.RateRequestModel; +import usecases.responses.UserResponseModel; + +/** + * This class represents a controller responsible for handling rating-related actions. + * It interacts with the business logic layer to handle rating answers and related operations. + */ +public class RateControl { + private final RateInputBoundary rateInput; + + /** + * Constructs a RateControl instance with the specified RateInputBoundary implementation. + * + * @param input The RateInputBoundary implementation used for handling rate-related operations. + */ + public RateControl(RateInputBoundary input) { + this.rateInput = input; + } + + /** + * Rates an answer and updates the corresponding information. + * + * @param rate The rating value given by the user. + * @param questionId The ID of the question associated with the answer. + * @return A UserResponseModel containing updated homepage information after rating. + */ + public UserResponseModel rateAnswer(int rate, int questionId) { + RateRequestModel rating = new RateRequestModel(rate, questionId); + return rateInput.rateAnswer(rating); + } + +} diff --git a/src/main/java/adapters/controllers/RegisterControl.java b/src/main/java/adapters/controllers/RegisterControl.java new file mode 100644 index 00000000..94625c3d --- /dev/null +++ b/src/main/java/adapters/controllers/RegisterControl.java @@ -0,0 +1,30 @@ +package adapters.controllers; + +import usecases.inputboundary.UserRegisterInputBoundary; +import usecases.requests.RegistrationData; +import usecases.responses.BaseResponseModel; + +/** + * This class represents responsible for handling user registration. + */ +public class RegisterControl { + private final UserRegisterInputBoundary inputBoundary; + + /** + * Constructs a new instance of ClientRegisterControl. + * + * @param inputBoundary The input boundary for user registration. + */ + public RegisterControl(UserRegisterInputBoundary inputBoundary) { + this.inputBoundary = inputBoundary; + } + + /** + * Creates a new user registration based on the provided information. + * + * @param requestModel @return A RegisterResponseModel indicating the result of the registration attempt. + */ + public BaseResponseModel create(RegistrationData requestModel){ + return inputBoundary.create(requestModel); + } +} diff --git a/src/main/java/adapters/controllers/SelectQuestionControl.java b/src/main/java/adapters/controllers/SelectQuestionControl.java new file mode 100644 index 00000000..9bf1f5e9 --- /dev/null +++ b/src/main/java/adapters/controllers/SelectQuestionControl.java @@ -0,0 +1,33 @@ +package adapters.controllers; + +import usecases.inputboundary.SelectInputBoundary; +import usecases.requests.SelectRequestModel; +import usecases.responses.TheQuestionResponseModel; + +/** + * This class represents a controller responsible for selecting a question. + * It interacts with the business logic through the SelectInputBoundary and handles the input/output for question selection. + */ +public class SelectQuestionControl { + private final SelectInputBoundary selectInputBoundary; + + /** + * Constructs a SelectQuestionControl instance with the given SelectInputBoundary. + * + * @param selectInputBoundary The input boundary responsible for selecting questions. + */ + public SelectQuestionControl(SelectInputBoundary selectInputBoundary) { + this.selectInputBoundary = selectInputBoundary; + } + + /** + * Selects a question based on the provided question ID and user ID. + * + * @param questionId The ID of the question to be selected. + * @return The response model containing the selected question's information. + */ + public TheQuestionResponseModel selectQuestion(int questionId) { + SelectRequestModel selectRequestModel = new SelectRequestModel(questionId); + return selectInputBoundary.selectQuestion(selectRequestModel); + } +} diff --git a/src/main/java/adapters/controllers/UserLoginControl.java b/src/main/java/adapters/controllers/UserLoginControl.java new file mode 100644 index 00000000..fbb39b93 --- /dev/null +++ b/src/main/java/adapters/controllers/UserLoginControl.java @@ -0,0 +1,34 @@ +package adapters.controllers; + +import usecases.inputboundary.UserLoginInputBoundary; +import usecases.requests.UserLoginRequestModel; +import usecases.responses.UserResponseModel; + +/** + * This class represents the controller responsible for handling user login functionality. + * It communicates with the business logic through the UserLoginInputBoundary. + */ +public class UserLoginControl { + private final UserLoginInputBoundary inputBoundary; + + /** + * Constructs a new UserLoginControl instance with the provided UserLoginInputBoundary. + * + * @param inputBoundary The input boundary to communicate with the business logic. + */ + public UserLoginControl(UserLoginInputBoundary inputBoundary) { + this.inputBoundary = inputBoundary; + } + + /** + * Attempts to log in the user with the given userId and password. + * + * @param userId The ID of the user attempting to log in. + * @param password The password of the user attempting to log in. + * @return A UserResponseModel containing information about the home page after login. + */ + public UserResponseModel login(int userId, String password){ + UserLoginRequestModel requestModel = new UserLoginRequestModel(userId, password); + return inputBoundary.login(requestModel); + } +} diff --git a/src/main/java/adapters/controllers/ViewQuestionControl.java b/src/main/java/adapters/controllers/ViewQuestionControl.java new file mode 100644 index 00000000..020622e4 --- /dev/null +++ b/src/main/java/adapters/controllers/ViewQuestionControl.java @@ -0,0 +1,33 @@ +package adapters.controllers; + +import usecases.inputboundary.ViewInputBoundary; +import usecases.responses.ViewResponseModel; + +/** + * Controller class responsible for viewing questions. + * This class acts as an interface between the presentation layer and the business logic for viewing questions. + * It takes a user's ID as input, creates a request model, and delegates the request to the corresponding input boundary. + * The response model from the input boundary is returned to the caller. + */ +public class ViewQuestionControl{ + + private final ViewInputBoundary viewInputBoundary; + + /** + * Constructor to initialize the ViewQuestionControl with the corresponding input boundary. + * + * @param viewInputBoundary The input boundary for viewing questions. + */ + public ViewQuestionControl(ViewInputBoundary viewInputBoundary) { + this.viewInputBoundary = viewInputBoundary; + } + + /** + * View a question based on the user's ID. + * + * @return A response model containing the information about the viewed question. + */ + public ViewResponseModel viewQuestion(){ + return viewInputBoundary.viewQuestion(); + } +} diff --git a/src/main/java/adapters/presenters/HomePageResponseFormatter.java b/src/main/java/adapters/presenters/HomePageResponseFormatter.java new file mode 100644 index 00000000..faf43e8d --- /dev/null +++ b/src/main/java/adapters/presenters/HomePageResponseFormatter.java @@ -0,0 +1,58 @@ +package adapters.presenters; +import infrastructure.screens.UIFactory; +import usecases.outputboundary.UserOutputBoundary; +import usecases.responses.UserResponseModel; +import infrastructure.screens.utils.UIManager; +import entities.ApplicationException; + +import javax.swing.*; +import java.awt.*; + +/** + * This class represents a formatter for generating UI responses for the home page. + * It prepares and formats the response data for display on the user interface. + */ +public class HomePageResponseFormatter implements UserOutputBoundary { + UIManager UIManager; + + /** + * Constructs a HomePageResponseFormatter. + * + * @param UIManager A UIManager class that contains the screens and cardlayouts. + */ + public HomePageResponseFormatter(UIManager UIManager) { + this.UIManager = UIManager; + } + + /** + * Prepares a failure response with the specified message. + * + * @param msg The error message. + * @throws ApplicationException An exception indicating a failure in the application. + */ + @Override + public UserResponseModel prepareFail(String msg) { + throw new ApplicationException(msg); + } + + + /** + * Prepares a successful response for the home page. + * + * @param response The response model containing the data to be displayed. + * @return The prepared response model. + */ + @Override + public UserResponseModel prepareSuccess(UserResponseModel response) { + pageConstructor(response); + return response; + } + + protected void pageConstructor(UserResponseModel response){ + JPanel screens = UIManager.getScreens(); + CardLayout cardLayout = UIManager.getCardLayout(); + JPanel homePage = UIFactory.getUI(UIFactory.UIType.HOME_PAGE, UIManager, response); + screens.add(homePage, "Home Page"); + cardLayout.show(screens, "Home Page"); + } +} diff --git a/src/main/java/adapters/presenters/RegisterResponseFormatter.java b/src/main/java/adapters/presenters/RegisterResponseFormatter.java new file mode 100644 index 00000000..6bf51989 --- /dev/null +++ b/src/main/java/adapters/presenters/RegisterResponseFormatter.java @@ -0,0 +1,62 @@ +package adapters.presenters; + +import infrastructure.screens.UIFactory; +import usecases.outputboundary.BaseOutputBoundary; +import usecases.responses.BaseResponseModel; +import infrastructure.screens.utils.UIManager; +import entities.ApplicationException; +import javax.swing.*; +import java.awt.*; +import java.awt.datatransfer.Clipboard; +import java.awt.datatransfer.StringSelection; +import java.awt.datatransfer.Transferable; + +/** + * This class represents a formatter for register response data and controlling the display of UI + * elements based on the response. + */ +public class RegisterResponseFormatter implements BaseOutputBoundary { + UIManager UIManager; + + /** + * Constructs a RegisterResponseFormatter with the specified card layout and screens panel. + * + * @param UIManager UIManager class that contains cardlayout, screens, and control container. + */ + public RegisterResponseFormatter(UIManager UIManager) { + this.UIManager = UIManager; + } + + /** + * Prepares a failure response for registration. + * + * @param msg The error message to be displayed. + * @return A BaseResponseModel representing the failure response. + * @throws ApplicationException An exception indicating the application encountered an error. + */ + @Override + public BaseResponseModel prepareFail(String msg) { + throw new ApplicationException(msg); + } + + /** + * Prepares a success response for registration. + * + * @param msg The success message to be displayed. + * @return A RegisterResponseModel representing the success response. + */ + @Override + public BaseResponseModel prepareSuccess(String msg) { + JPanel screens = UIManager.getScreens(); + CardLayout cardLayout = UIManager.getCardLayout(); + JPanel welcomeUI = UIFactory.getUI(UIFactory.UIType.WELCOME_UI, UIManager, new BaseResponseModel()); + screens.add(welcomeUI, "Welcome"); + cardLayout.show(screens, "Welcome"); + Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard(); + Transferable tText = new StringSelection(msg); + clip.setContents(tText, null); + String message = "Important: Your unique User ID: " + msg + " has been generated and copied to your clipboard. \nPlease save it securely, as it will not be shown again."; + JOptionPane.showMessageDialog(null, message, "Notice", JOptionPane.INFORMATION_MESSAGE); + return new BaseResponseModel(); + } +} diff --git a/src/main/java/adapters/presenters/TheQuestionResponseFormatter.java b/src/main/java/adapters/presenters/TheQuestionResponseFormatter.java new file mode 100644 index 00000000..a9c6522e --- /dev/null +++ b/src/main/java/adapters/presenters/TheQuestionResponseFormatter.java @@ -0,0 +1,64 @@ +package adapters.presenters; + + +import infrastructure.screens.UIFactory; +import usecases.outputboundary.TheQuestionOutputBoundary; +import usecases.responses.TheQuestionResponseModel; +import usecases.dto.PostDisplay; +import infrastructure.screens.utils.UIManager; +import entities.ApplicationException; + +import javax.swing.*; +import java.awt.*; +import java.util.Map; + +/** + * This class represents the formatting the response model of a question into a user interface representation + * for display. + */ + +public class TheQuestionResponseFormatter implements TheQuestionOutputBoundary { + infrastructure.screens.utils.UIManager UIManager; + + /** + * Constructs a TheQuestionResponseFormatter with the specified CardLayout and JPanel. + * + * @param UIManager UIManager class contains screens, cardlayouts, and control container + */ + public TheQuestionResponseFormatter(UIManager UIManager) { + this.UIManager = UIManager; + } + + /** + * Prepares a failure response by throwing an ApplicationException with the specified message. + * + * @param msg The error message. + * @throws ApplicationException Always thrown with the specified error message. + */ + @Override + public TheQuestionResponseModel prepareFail(String msg) { + throw new ApplicationException(msg); + } + + /** + * Prepares a success response by formatting the response model into a user interface representation + * for display in TheQuestionUI. + * + * @param response The response model. + * @return The formatted response model suitable for display. + */ + @Override + public TheQuestionResponseModel prepareSuccess(TheQuestionResponseModel response) { + JPanel screens = UIManager.getScreens(); + CardLayout cardLayout = UIManager.getCardLayout(); + Map postMap= response.getPostMap(); + if (postMap.size() == 1){ + cardLayout.show(screens, "Home Page"); + } else { + JPanel questionUI = UIFactory.getUI(UIFactory.UIType.QUESTION_UI, UIManager, response); + screens.add(questionUI, "Question"); + cardLayout.show(screens, "Question"); + } + return response; + } +} diff --git a/src/main/java/adapters/presenters/ViewResponseFormatter.java b/src/main/java/adapters/presenters/ViewResponseFormatter.java new file mode 100644 index 00000000..ae965207 --- /dev/null +++ b/src/main/java/adapters/presenters/ViewResponseFormatter.java @@ -0,0 +1,54 @@ +package adapters.presenters; + +import infrastructure.screens.UIFactory; +import usecases.outputboundary.ViewOutputBoundary; +import usecases.responses.ViewResponseModel; +import infrastructure.screens.utils.UIManager; +import entities.ApplicationException; +import javax.swing.*; +import java.awt.*; + +/** + * The ViewResponseFormatter class is responsible for formatting and displaying view responses in the user interface. + * It implements the ViewOutputBoundary interface to handle success and failure scenarios for presenting data to the user. + */ +public class ViewResponseFormatter implements ViewOutputBoundary { + UIManager UIManager; + + /** + * Constructs a ViewResponseFormatter with the specified card layout and screen panel. + * + * @param UIManager UIManager class contains screens, cardlayouts, and control container. + */ + public ViewResponseFormatter(infrastructure.screens.utils.UIManager UIManager) { + this.UIManager = UIManager; + } + + /** + * Throws an ApplicationException with the specified error message. + * + * @param msg The error message to be included in the exception. + * @throws ApplicationException Always throws an ApplicationException with the provided message. + */ + @Override + public ViewResponseModel prepareFail(String msg) { + throw new ApplicationException(msg); + } + + /** + * Prepares and displays a successful view response in the user interface. + * + * @param response The ViewResponseModel containing the data to be displayed. + * @return The original response model passed as a parameter. + */ + @Override + public ViewResponseModel prepareSuccess(ViewResponseModel response) { + JPanel screens = UIManager.getScreens(); + CardLayout cardLayout = UIManager.getCardLayout(); + JPanel questionListUI = UIFactory.getUI(UIFactory.UIType.QUESTION_LIST_UI, UIManager, response); + screens.add(questionListUI, "Question List"); + cardLayout.show(screens, "Question List"); + + return response; + } +} diff --git a/src/main/java/businessrule/gateway/AttorneyGateway.java b/src/main/java/businessrule/gateway/AttorneyGateway.java deleted file mode 100644 index 71208687..00000000 --- a/src/main/java/businessrule/gateway/AttorneyGateway.java +++ /dev/null @@ -1,14 +0,0 @@ -package businessrule.gateway; - -import entity.Attorney; -import entity.Question; - -public interface AttorneyGateway extends UserGateway { - - @Override - Attorney get(int id); - - void clearAllRecommendations(); - - void addRecommendation (int Userid, Question question); -} diff --git a/src/main/java/businessrule/gateway/ClientGateway.java b/src/main/java/businessrule/gateway/ClientGateway.java deleted file mode 100644 index 1d2a33a4..00000000 --- a/src/main/java/businessrule/gateway/ClientGateway.java +++ /dev/null @@ -1,9 +0,0 @@ -package businessrule.gateway; - -import entity.Client; - -public interface ClientGateway extends UserGateway { - - @Override - Client get(int id); -} diff --git a/src/main/java/businessrule/gateway/GenericGateway.java b/src/main/java/businessrule/gateway/GenericGateway.java deleted file mode 100644 index e9785bf9..00000000 --- a/src/main/java/businessrule/gateway/GenericGateway.java +++ /dev/null @@ -1,14 +0,0 @@ -package businessrule.gateway; - -import java.util.List; - -public interface GenericGateway { - - void save(T entity); - Object get(int id); - boolean existsById(int id); - void delete(int id); - void deleteAll(); - List getAll(); - -} diff --git a/src/main/java/businessrule/gateway/PostGateway.java b/src/main/java/businessrule/gateway/PostGateway.java deleted file mode 100644 index 4acae53a..00000000 --- a/src/main/java/businessrule/gateway/PostGateway.java +++ /dev/null @@ -1,12 +0,0 @@ -package businessrule.gateway; - -import entity.Post; -import entity.Question; -import entity.User; - -public interface PostGateway extends GenericGateway{ - - @Override - Post get(int id); - -} diff --git a/src/main/java/businessrule/gateway/QuestionGateway.java b/src/main/java/businessrule/gateway/QuestionGateway.java deleted file mode 100644 index 9b47d006..00000000 --- a/src/main/java/businessrule/gateway/QuestionGateway.java +++ /dev/null @@ -1,20 +0,0 @@ -package businessrule.gateway; - -import entity.Post; -import entity.Question; -import java.time.LocalDate; -import java.util.List; - -public interface QuestionGateway extends GenericGateway{ - List getNotTakenQuestion(); - List getNotClosedQuestion(); - List getAllPostOfQuestion(int questionId); - void updateIsTaken(int questionId, boolean iaTaken); - void updateTakenByAttorney(int questionId, int attorneyId); - void updateIsClose(int questionId, boolean isClose); - void updateRating(int questionId, int rating); - void updateTakenAt(int questionId, LocalDate time); - void updatePosts(int id, Post post); - @Override - Question get(int id); -} diff --git a/src/main/java/businessrule/gateway/UserGateway.java b/src/main/java/businessrule/gateway/UserGateway.java deleted file mode 100644 index 9a56ee0f..00000000 --- a/src/main/java/businessrule/gateway/UserGateway.java +++ /dev/null @@ -1,11 +0,0 @@ -package businessrule.gateway; - -import entity.Question; -import entity.User; - -public interface UserGateway extends GenericGateway{ - boolean existsByName(String inputUserName); - void updateQuestionList(int id, Question question); - @Override - User get(int id); -} diff --git a/src/main/java/businessrule/inputboundary/CloseInputBoundary.java b/src/main/java/businessrule/inputboundary/CloseInputBoundary.java deleted file mode 100644 index a214d73c..00000000 --- a/src/main/java/businessrule/inputboundary/CloseInputBoundary.java +++ /dev/null @@ -1,8 +0,0 @@ -package businessrule.inputboundary; - -import businessrule.requestmodel.CloseRequestModel; -import businessrule.responsemodel.UserResponseModel; - -public interface CloseInputBoundary { - UserResponseModel closeQuestion(CloseRequestModel closeRequestModel); -} diff --git a/src/main/java/businessrule/inputboundary/PostInputBoundary.java b/src/main/java/businessrule/inputboundary/PostInputBoundary.java deleted file mode 100644 index 5f72f887..00000000 --- a/src/main/java/businessrule/inputboundary/PostInputBoundary.java +++ /dev/null @@ -1,9 +0,0 @@ -package businessrule.inputboundary; - -import businessrule.requestmodel.PostRequestModel; -import businessrule.responsemodel.TheQuestionResponseModel; -import businessrule.responsemodel.UserResponseModel; - -public interface PostInputBoundary { - TheQuestionResponseModel createPost(PostRequestModel postRequestModel); -} diff --git a/src/main/java/businessrule/inputboundary/QuestionInputBoundary.java b/src/main/java/businessrule/inputboundary/QuestionInputBoundary.java deleted file mode 100644 index 11d8c800..00000000 --- a/src/main/java/businessrule/inputboundary/QuestionInputBoundary.java +++ /dev/null @@ -1,9 +0,0 @@ -package businessrule.inputboundary; - -import businessrule.responsemodel.TheQuestionResponseModel; -import businessrule.requestmodel.QuestionRequestModel; -import businessrule.responsemodel.UserResponseModel; - -public interface QuestionInputBoundary { - TheQuestionResponseModel createQuestion(QuestionRequestModel questionRequestModel); -} diff --git a/src/main/java/businessrule/inputboundary/RateInputBoundary.java b/src/main/java/businessrule/inputboundary/RateInputBoundary.java deleted file mode 100644 index 207da852..00000000 --- a/src/main/java/businessrule/inputboundary/RateInputBoundary.java +++ /dev/null @@ -1,10 +0,0 @@ -package businessrule.inputboundary; - -import businessrule.requestmodel.RateRequestModel; -import businessrule.responsemodel.UserResponseModel; - -public interface RateInputBoundary { - - UserResponseModel rateAnswer(RateRequestModel rateRequestModel); - -} diff --git a/src/main/java/businessrule/inputboundary/SelectInputBoundary.java b/src/main/java/businessrule/inputboundary/SelectInputBoundary.java deleted file mode 100644 index a18f912d..00000000 --- a/src/main/java/businessrule/inputboundary/SelectInputBoundary.java +++ /dev/null @@ -1,9 +0,0 @@ -package businessrule.inputboundary; - -import businessrule.responsemodel.TheQuestionResponseModel; -import businessrule.requestmodel.SelectRequestModel; -import businessrule.responsemodel.UserResponseModel; - -public interface SelectInputBoundary { - TheQuestionResponseModel selectQuestion(SelectRequestModel selectRequestModel); -} diff --git a/src/main/java/businessrule/inputboundary/UserLoginInputBoundary.java b/src/main/java/businessrule/inputboundary/UserLoginInputBoundary.java deleted file mode 100644 index 6f9361e6..00000000 --- a/src/main/java/businessrule/inputboundary/UserLoginInputBoundary.java +++ /dev/null @@ -1,8 +0,0 @@ -package businessrule.inputboundary; - -import businessrule.requestmodel.UserLoginRequestModel; -import businessrule.responsemodel.UserResponseModel; - -public interface UserLoginInputBoundary { - UserResponseModel login(UserLoginRequestModel requestModel); -} diff --git a/src/main/java/businessrule/inputboundary/UserRegisterInputBoundary.java b/src/main/java/businessrule/inputboundary/UserRegisterInputBoundary.java deleted file mode 100644 index 70c52d65..00000000 --- a/src/main/java/businessrule/inputboundary/UserRegisterInputBoundary.java +++ /dev/null @@ -1,9 +0,0 @@ -package businessrule.inputboundary; - -import businessrule.requestmodel.RegistrationData; -import businessrule.responsemodel.BaseResponseModel; -import businessrule.responsemodel.UserResponseModel; - -public interface UserRegisterInputBoundary { - BaseResponseModel create(RegistrationData requestModel); -} diff --git a/src/main/java/businessrule/inputboundary/ViewInputBoundary.java b/src/main/java/businessrule/inputboundary/ViewInputBoundary.java deleted file mode 100644 index 6f0122a1..00000000 --- a/src/main/java/businessrule/inputboundary/ViewInputBoundary.java +++ /dev/null @@ -1,8 +0,0 @@ -package businessrule.inputboundary; - -import businessrule.responsemodel.UserResponseModel; -import businessrule.responsemodel.ViewResponseModel; - -public interface ViewInputBoundary { - ViewResponseModel viewQuestion(); -} diff --git a/src/main/java/businessrule/outputboundary/TheQuestionOutputBoundary.java b/src/main/java/businessrule/outputboundary/TheQuestionOutputBoundary.java deleted file mode 100644 index a22ba684..00000000 --- a/src/main/java/businessrule/outputboundary/TheQuestionOutputBoundary.java +++ /dev/null @@ -1,10 +0,0 @@ -package businessrule.outputboundary; - -import adapter.controller.ControlContainer; -import businessrule.responsemodel.TheQuestionResponseModel; -import businessrule.responsemodel.UserResponseModel; - -public interface TheQuestionOutputBoundary { - TheQuestionResponseModel prepareFail(String msg); - TheQuestionResponseModel prepareSuccess(TheQuestionResponseModel response); -} diff --git a/src/main/java/businessrule/outputboundary/ViewOutputBoundary.java b/src/main/java/businessrule/outputboundary/ViewOutputBoundary.java deleted file mode 100644 index 015bbac5..00000000 --- a/src/main/java/businessrule/outputboundary/ViewOutputBoundary.java +++ /dev/null @@ -1,11 +0,0 @@ -package businessrule.outputboundary; - -import adapter.controller.ControlContainer; -import businessrule.responsemodel.UserResponseModel; -import businessrule.responsemodel.ViewResponseModel; -import entity.User; - -public interface ViewOutputBoundary { - ViewResponseModel prepareFail(String msg); - ViewResponseModel prepareSuccess(ViewResponseModel response); -} diff --git a/src/main/java/businessrule/requestmodel/CloseRequestModel.java b/src/main/java/businessrule/requestmodel/CloseRequestModel.java deleted file mode 100644 index cf3747b2..00000000 --- a/src/main/java/businessrule/requestmodel/CloseRequestModel.java +++ /dev/null @@ -1,13 +0,0 @@ -package businessrule.requestmodel; - -public class CloseRequestModel { - private final int questionId; - - public CloseRequestModel(int questionId) { - this.questionId = questionId; - } - - public int getQuestionId() { - return questionId; - } -} diff --git a/src/main/java/businessrule/requestmodel/PostRequestModel.java b/src/main/java/businessrule/requestmodel/PostRequestModel.java deleted file mode 100644 index ec2307ee..00000000 --- a/src/main/java/businessrule/requestmodel/PostRequestModel.java +++ /dev/null @@ -1,19 +0,0 @@ -package businessrule.requestmodel; - -public class PostRequestModel { - private final int questionId; - private final String postText; - - public PostRequestModel(int questionId, String postText){ - this.questionId = questionId; - this.postText = postText; - } - - public int getQuestionId() { - return questionId; - } - - public String getPostText() { - return postText; - } -} diff --git a/src/main/java/businessrule/requestmodel/QuestionRequestModel.java b/src/main/java/businessrule/requestmodel/QuestionRequestModel.java deleted file mode 100644 index b2b59ee8..00000000 --- a/src/main/java/businessrule/requestmodel/QuestionRequestModel.java +++ /dev/null @@ -1,33 +0,0 @@ -package businessrule.requestmodel; - -import java.time.LocalDate; - -public class QuestionRequestModel { - private final String questionCategory; - private final String title; - private final LocalDate createAt; - private final LocalDate legalDeadline; - - public QuestionRequestModel(String questionCategory, String title, LocalDate createAt, LocalDate legalDeadline) { - this.questionCategory = questionCategory; - this.title = title; - this.createAt = createAt; - this.legalDeadline = legalDeadline; - } - - public String getQuestionCategory() { - return questionCategory; - } - - public String getTitle() { - return title; - } - - public LocalDate getCreateAt() { - return createAt; - } - - public LocalDate getLegalDeadline() { - return legalDeadline; - } -} diff --git a/src/main/java/businessrule/requestmodel/RateRequestModel.java b/src/main/java/businessrule/requestmodel/RateRequestModel.java deleted file mode 100644 index 61018a6d..00000000 --- a/src/main/java/businessrule/requestmodel/RateRequestModel.java +++ /dev/null @@ -1,20 +0,0 @@ -package businessrule.requestmodel; - -public class RateRequestModel { - - private final int rating; - private final int answerId; - - public RateRequestModel(int rating, int answerId) { - this.rating = rating; - this.answerId = answerId; - } - - public int getRating() { - return rating; - } - - public int getAnswerId() { - return answerId; - } -} diff --git a/src/main/java/businessrule/requestmodel/RegistrationData.java b/src/main/java/businessrule/requestmodel/RegistrationData.java deleted file mode 100644 index f32a6aab..00000000 --- a/src/main/java/businessrule/requestmodel/RegistrationData.java +++ /dev/null @@ -1,64 +0,0 @@ -package businessrule.requestmodel; - -import java.util.Set; - -public class RegistrationData { - // common fields for user registration - public int userId; - public String userName; - public String email; - public String password; - public String stateAbb; - public String postalCode; - - // fields for client registration - public String ethnicity; - public int age; - public String gender; - public String maritalStatus; - public int numberOfHousehold; - public float annualIncome; - public String password2; - - // fields for attorney registration - public Set professionals; - - // for user - public RegistrationData(String userName, String email, String password, String password2, String stateAbb, String postalCode) { - this.userName = userName; - this.email = email; - this.password = password; - this.password2 = password2; - this.stateAbb = stateAbb; - this.postalCode = postalCode; - } - - // for client registration - public RegistrationData(String userName, String email, String password, String password2, String stateAbb, String postalCode, String ethnicity, int age, String gender, String maritalStatus, int numberOfHousehold, float annualIncome) { - this.userName = userName; - this.email = email; - this.password = password; - this.password2 = password2; - this.stateAbb = stateAbb; - this.postalCode = postalCode; - this.ethnicity = ethnicity; - this.age = age; - this.gender = gender; - this.maritalStatus = maritalStatus; - this.numberOfHousehold = numberOfHousehold; - this.annualIncome = annualIncome; - } - - public RegistrationData(String userName, String email, String password, String password2, String stateAbb, String postalCode, Set professionals) { - this.userName = userName; - this.email = email; - this.password = password; - this.password2 = password2; - this.stateAbb = stateAbb; - this.postalCode = postalCode; - this.professionals = professionals; - } - - - public void setUserId(int userId) {this.userId = userId;} -} diff --git a/src/main/java/businessrule/requestmodel/SelectRequestModel.java b/src/main/java/businessrule/requestmodel/SelectRequestModel.java deleted file mode 100644 index 4007fc89..00000000 --- a/src/main/java/businessrule/requestmodel/SelectRequestModel.java +++ /dev/null @@ -1,13 +0,0 @@ -package businessrule.requestmodel; - -public class SelectRequestModel { - private final int questionId; - - public SelectRequestModel(int questionId) { - this.questionId = questionId; - } - - public int getQuestionId() { - return questionId; - } -} diff --git a/src/main/java/businessrule/requestmodel/UserLoginRequestModel.java b/src/main/java/businessrule/requestmodel/UserLoginRequestModel.java deleted file mode 100644 index 9840e4fc..00000000 --- a/src/main/java/businessrule/requestmodel/UserLoginRequestModel.java +++ /dev/null @@ -1,19 +0,0 @@ -package businessrule.requestmodel; - -public class UserLoginRequestModel { - private final int userId; - private final String password; - - public UserLoginRequestModel(int userId, String password) { - this.userId = userId; - this.password = password; - } - - public int getUserId() { - return userId; - } - - public String getPassword() { - return password; - } -} diff --git a/src/main/java/businessrule/responsemodel/BaseResponseModel.java b/src/main/java/businessrule/responsemodel/BaseResponseModel.java deleted file mode 100644 index 0170aca1..00000000 --- a/src/main/java/businessrule/responsemodel/BaseResponseModel.java +++ /dev/null @@ -1,4 +0,0 @@ -package businessrule.responsemodel; - -public class BaseResponseModel { -} diff --git a/src/main/java/businessrule/usecase/AttorneyRecommendInteractor.java b/src/main/java/businessrule/usecase/AttorneyRecommendInteractor.java deleted file mode 100644 index 39c1c6c8..00000000 --- a/src/main/java/businessrule/usecase/AttorneyRecommendInteractor.java +++ /dev/null @@ -1,31 +0,0 @@ -package businessrule.usecase; - -import businessrule.gateway.AttorneyGateway; -import businessrule.gateway.QuestionGateway; -import businessrule.outputboundary.TheQuestionOutputBoundary; -import businessrule.outputboundary.UserOutputBoundary; -import businessrule.outputboundary.ViewOutputBoundary; -import entity.Attorney; -import entity.Question; -import entity.User; -import java.util.List; - -public class AttorneyRecommendInteractor extends ViewQuestionInteractorBase { - private final AttorneyGateway attorneyGateway; - - public AttorneyRecommendInteractor(ViewOutputBoundary outputBoundary, QuestionGateway questionGateway, AttorneyGateway attorneyGateway) { - super(outputBoundary, questionGateway); - this.attorneyGateway = attorneyGateway; - } - - @Override - protected List fetchQuestions(int userId) { - Attorney attorney = (Attorney) fetchUser(userId); - return attorney.getRecommendations(); - } - - @Override - protected User fetchUser(int userId) { - return attorneyGateway.get(userId); - } -} diff --git a/src/main/java/businessrule/usecase/ClientRegisterInteractor.java b/src/main/java/businessrule/usecase/ClientRegisterInteractor.java deleted file mode 100644 index 71bf5c77..00000000 --- a/src/main/java/businessrule/usecase/ClientRegisterInteractor.java +++ /dev/null @@ -1,57 +0,0 @@ -package businessrule.usecase; - - -import businessrule.gateway.ClientGateway; -import businessrule.outputboundary.BaseOutputBoundary; -import businessrule.responsemodel.BaseResponseModel; -import entity.ApplicationException; -import entity.Client; -import entity.factory.ClientFactory; -import businessrule.usecase.util.CredentialChecker; -import businessrule.requestmodel.RegistrationData; - -public class ClientRegisterInteractor extends UserRegisterInteractor { - - public ClientRegisterInteractor(ClientGateway userGateway, ClientFactory userFactory, BaseOutputBoundary outputBoundary) { - super(userGateway, userFactory, outputBoundary); - } - - @Override - public BaseResponseModel create(RegistrationData requestModel) { - try {checkCredential(requestModel);} - catch (ApplicationException e) { - return outputBoundary.prepareFail(e.getMessage()); - } - int userId = generateId(); - requestModel.setUserId(userId); - Client client = userFactory.createUser(requestModel); - userGateway.save(client); - return outputBoundary.prepareSuccess(String.valueOf(userId)); - } - - private void checkCredential(RegistrationData requestModel) throws ApplicationException{ - // prepare input data - String inputUserName = requestModel.userName; - String inputEmail = requestModel.email; - String inputPassword1 = requestModel.password; - String inputPassword2 = requestModel.password2; - String inputPostalCode = requestModel.postalCode; - int inputAge = requestModel.age; - - // validate input data - CredentialChecker checker = new CredentialChecker(); - if (userGateway.existsByName(inputUserName)) { - throw new ApplicationException("User name already exists"); - } else if (!inputPassword1.equals(inputPassword2)) { - throw new ApplicationException("Passwords does not match"); - } else if (inputPassword1.length() < 8) { - throw new ApplicationException("Password is less than 8 characters"); - } else if (!checker.checkEmail(inputEmail)) { - throw new ApplicationException("Email is invalid"); - } else if (!checker.checkAge(inputAge)) { - throw new ApplicationException("Age is invalid"); - } else if (!checker.checkPostalCode(inputPostalCode)) { - throw new ApplicationException("Postal Code is invalid"); - } - } -} \ No newline at end of file diff --git a/src/main/java/businessrule/usecase/CloseQuestionInteractor.java b/src/main/java/businessrule/usecase/CloseQuestionInteractor.java deleted file mode 100644 index a546d963..00000000 --- a/src/main/java/businessrule/usecase/CloseQuestionInteractor.java +++ /dev/null @@ -1,44 +0,0 @@ -package businessrule.usecase; - -import businessrule.SessionManager; -import businessrule.UserSession; -import businessrule.gateway.ClientGateway; -import businessrule.inputboundary.CloseInputBoundary; -import businessrule.outputboundary.UserOutputBoundary; -import businessrule.requestmodel.CloseRequestModel; -import businessrule.responsemodel.UserResponseModel; -import entity.Client; -import entity.Question; -import businessrule.gateway.QuestionGateway; - -public class CloseQuestionInteractor implements CloseInputBoundary { - final QuestionGateway questionGateway; - final UserOutputBoundary outputBoundary; - final ClientGateway clientGateway; - - public CloseQuestionInteractor(QuestionGateway questionGateway, UserOutputBoundary outputBoundary, ClientGateway clientGateway) { - this.questionGateway = questionGateway; - this.outputBoundary = outputBoundary; - this.clientGateway = clientGateway; - } - - @Override - public UserResponseModel closeQuestion(CloseRequestModel closeRequestModel) { - // get input data - UserSession session = SessionManager.getSession(); - UserResponseModel response = session.getUserResponseModel(); - int questionId = closeRequestModel.getQuestionId(); - Client client = clientGateway.get(response.getUserId()); - Question question = questionGateway.get(questionId); - - - // handle close logic and prepare response model - if(question.isTaken()){ - questionGateway.updateIsClose(questionId, true); - return outputBoundary.prepareSuccess(response); - } - else{ - return outputBoundary.prepareFail("You can only close this question once it was taken by attorney."); - } - } -} diff --git a/src/main/java/businessrule/usecase/UserRegisterInteractor.java b/src/main/java/businessrule/usecase/UserRegisterInteractor.java deleted file mode 100644 index 6f6710b7..00000000 --- a/src/main/java/businessrule/usecase/UserRegisterInteractor.java +++ /dev/null @@ -1,35 +0,0 @@ -package businessrule.usecase; - -import businessrule.gateway.UserGateway; -import businessrule.inputboundary.UserRegisterInputBoundary; -import businessrule.outputboundary.BaseOutputBoundary; -import businessrule.responsemodel.BaseResponseModel; -import businessrule.usecase.util.RandomNumberGenerator; -import entity.User; -import businessrule.requestmodel.RegistrationData; -import entity.factory.UserFactory; - -public abstract class UserRegisterInteractor, F extends UserFactory, U extends User> implements UserRegisterInputBoundary { - protected final T userGateway; - protected final F userFactory; - protected final BaseOutputBoundary outputBoundary; - - public UserRegisterInteractor(T userGateway, F userFactory, BaseOutputBoundary outputBoundary) { - this.userGateway = userGateway; - this.userFactory = userFactory; - this.outputBoundary = outputBoundary; - } - - @Override - public abstract BaseResponseModel create(RegistrationData requestModel); - - protected int generateId() { - RandomNumberGenerator generator = new RandomNumberGenerator(); - int randomUserId = generator.generateClientId(8); - boolean exists = userGateway.existsById(randomUserId); - while (exists) { - randomUserId = generator.generateClientId(8); - exists = userGateway.existsById(randomUserId); - } return randomUserId; - } -} diff --git a/src/main/java/businessrule/usecase/ViewQuestionInteractor.java b/src/main/java/businessrule/usecase/ViewQuestionInteractor.java deleted file mode 100644 index d3586277..00000000 --- a/src/main/java/businessrule/usecase/ViewQuestionInteractor.java +++ /dev/null @@ -1,33 +0,0 @@ -package businessrule.usecase; - - -import businessrule.gateway.QuestionGateway; -import businessrule.gateway.UserGateway; -import businessrule.gateway.UserGatewayFactory; -import businessrule.outputboundary.TheQuestionOutputBoundary; -import businessrule.outputboundary.ViewOutputBoundary; -import entity.Question; -import entity.User; -import java.util.List; - -public class ViewQuestionInteractor extends ViewQuestionInteractorBase{ - final UserGatewayFactory userGatewayFactory; - - public ViewQuestionInteractor(ViewOutputBoundary outputBoundary, QuestionGateway questionGateway, UserGatewayFactory userGatewayFactory) { - super(outputBoundary, questionGateway); - this.userGatewayFactory = userGatewayFactory; - } - - @Override - protected List fetchQuestions(int userId) { - User user = fetchUser(userId); - return user.getQuestionsList(); - } - - @Override - protected User fetchUser(int userId) { - UserGateway userGateway = userGatewayFactory.createUserGateway(userId); - return userGateway.get(userId); - } - -} \ No newline at end of file diff --git a/src/main/java/businessrule/usecase/ViewQuestionInteractorBase.java b/src/main/java/businessrule/usecase/ViewQuestionInteractorBase.java deleted file mode 100644 index 3af46878..00000000 --- a/src/main/java/businessrule/usecase/ViewQuestionInteractorBase.java +++ /dev/null @@ -1,49 +0,0 @@ -package businessrule.usecase; - -import businessrule.SessionManager; -import businessrule.UserSession; -import businessrule.gateway.QuestionGateway; -import businessrule.inputboundary.ViewInputBoundary; -import businessrule.outputboundary.TheQuestionOutputBoundary; -import businessrule.outputboundary.UserOutputBoundary; -import businessrule.outputboundary.ViewOutputBoundary; -import businessrule.responsemodel.UserResponseModel; -import businessrule.responsemodel.ViewResponseModel; -import businessrule.usecase.util.BuilderService; -import businessrule.usecase.util.QuestionDisplayFormatter; -import businessrule.usecase.util.QuestionMapConstructor; -import entity.Question; -import entity.User; - -import javax.swing.text.View; -import java.util.List; -import java.util.Map; - -public abstract class ViewQuestionInteractorBase implements ViewInputBoundary { - protected final ViewOutputBoundary outputBoundary; - protected final QuestionGateway questionGateway; - - public ViewQuestionInteractorBase(ViewOutputBoundary outputBoundary, QuestionGateway questionGateway) { - this.outputBoundary = outputBoundary; - this.questionGateway = questionGateway; - } - - @Override - public ViewResponseModel viewQuestion() { - // get input - UserSession session = SessionManager.getSession(); - UserResponseModel response = session.getUserResponseModel(); - int userId = response.getUserId(); - - // strategy - List questionList = fetchQuestions(userId); - User user = fetchUser(userId); - - QuestionMapConstructor questionMapConstructor = new QuestionMapConstructor(); - Map questionMap = questionMapConstructor.constructQuestionMap(questionList); - return outputBoundary.prepareSuccess(BuilderService.getInstance().constructViewResponse(response, questionMap)); - } - - protected abstract List fetchQuestions(int userId); - protected abstract User fetchUser(int userId); -} diff --git a/src/main/java/businessrule/usecase/util/Matching.java b/src/main/java/businessrule/usecase/util/Matching.java deleted file mode 100644 index 5fddfa5b..00000000 --- a/src/main/java/businessrule/usecase/util/Matching.java +++ /dev/null @@ -1,19 +0,0 @@ -package businessrule.usecase.util; - -public class Matching { - private final int questionId; - private final int attorneyId; - - public Matching(int questionId, int attorneyId) { - this.questionId = questionId; - this.attorneyId = attorneyId; - } - - public int getQuestionId() { - return questionId; - } - - public int getAttorneyId() { - return attorneyId; - } -} diff --git a/src/main/java/businessrule/usecase/util/PostDisplayFormatter.java b/src/main/java/businessrule/usecase/util/PostDisplayFormatter.java deleted file mode 100644 index b131c9bc..00000000 --- a/src/main/java/businessrule/usecase/util/PostDisplayFormatter.java +++ /dev/null @@ -1,36 +0,0 @@ -package businessrule.usecase.util; - -import entity.Post; - -import java.time.LocalDate; - -public class PostDisplayFormatter { - private final String postText; - private final String userType; - private final String name; - private final LocalDate createAt; - - public PostDisplayFormatter(String postText,String userType, String name, LocalDate createAt) { - this.postText = postText; - this.userType = userType; - - this.name = name; - this.createAt = createAt; - } - - public String getPostText() { - return postText; - } - - public String getUserType() { - return userType; - } - - public String getName() { - return name; - } - - public LocalDate getCreateAt() { - return createAt; - } -} diff --git a/src/main/java/businessrule/usecase/util/PostMapConstructor.java b/src/main/java/businessrule/usecase/util/PostMapConstructor.java deleted file mode 100644 index 88d0c314..00000000 --- a/src/main/java/businessrule/usecase/util/PostMapConstructor.java +++ /dev/null @@ -1,44 +0,0 @@ -package businessrule.usecase.util; - -import businessrule.gateway.UserGateway; -import businessrule.gateway.UserGatewayFactory; -import entity.Post; -import entity.Question; -import entity.User; - -import java.time.LocalDate; -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - -public class PostMapConstructor { - private final UserGatewayFactory userGatewayFactory; - - public PostMapConstructor(UserGatewayFactory userGatewayFactory) {this.userGatewayFactory = userGatewayFactory;} - - public Map constructPostMap(Question question) { - List postList = question.getPosts(); - - // initialize post map - Map postMap = new LinkedHashMap<>(); - - // handle the empty post list - if (postList.isEmpty()) {return postMap;} - - // for each post list, find the data needed - for (Post post: postList) { - int postId = post.getPostId(); - UserGateway userGateway = userGatewayFactory.createUserGateway(post.getBelongsTo()); - User user = userGateway.get(post.getBelongsTo()); - String userType = user.getUserType(); - String name = user.getUserName(); - String postText = post.getPostText(); - LocalDate createAt = post.getCreateAt(); - PostDisplayFormatter postDisplayFormatter = new PostDisplayFormatter(postText, userType, name, createAt); - // put every data needed to the post map - postMap.put(postId, postDisplayFormatter); - } - return postMap; - } -} diff --git a/src/main/java/businessrule/usecase/util/QuestionDisplayFormatter.java b/src/main/java/businessrule/usecase/util/QuestionDisplayFormatter.java deleted file mode 100644 index 309a7879..00000000 --- a/src/main/java/businessrule/usecase/util/QuestionDisplayFormatter.java +++ /dev/null @@ -1,31 +0,0 @@ -package businessrule.usecase.util; - -import java.time.LocalDate; - -public class QuestionDisplayFormatter { - private final String title; - private final String type; - private final LocalDate legalDeadline; - private final boolean isClose; - - public QuestionDisplayFormatter(String title, String type, LocalDate legalDeadline, boolean isClose) { - this.title = title; - this.type = type; - this.legalDeadline = legalDeadline; - this.isClose = isClose; - } - - public String getTitle() { - return title; - } - - public String getType() { - return type; - } - - public LocalDate getLegalDeadline() { - return legalDeadline; - } - - public boolean isClose() {return isClose;} -} diff --git a/src/main/java/businessrule/usecase/util/QuestionMapConstructor.java b/src/main/java/businessrule/usecase/util/QuestionMapConstructor.java deleted file mode 100644 index 28270c9e..00000000 --- a/src/main/java/businessrule/usecase/util/QuestionMapConstructor.java +++ /dev/null @@ -1,28 +0,0 @@ -package businessrule.usecase.util; - -import entity.Question; - -import java.time.LocalDate; -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - -public class QuestionMapConstructor { - public Map constructQuestionMap(List questionList) { - Map questionMap = new LinkedHashMap<>(); - if (questionList.isEmpty()) { - return questionMap; - } - for (Question question: questionList) { - int questionId = question.getQuestionId(); - String title = question.getTitle(); - String type = question.getType(); - LocalDate legalDeadline = question.getLegalDeadline(); - boolean isClose = question.isClose(); - QuestionDisplayFormatter questionDisplayFormatter = new QuestionDisplayFormatter(title, type, legalDeadline, isClose); - questionMap.put(questionId, questionDisplayFormatter); - } - return questionMap; - } -} diff --git a/src/main/java/businessrule/usecase/util/RandomNumberGenerator.java b/src/main/java/businessrule/usecase/util/RandomNumberGenerator.java deleted file mode 100644 index 13893b3b..00000000 --- a/src/main/java/businessrule/usecase/util/RandomNumberGenerator.java +++ /dev/null @@ -1,32 +0,0 @@ -package businessrule.usecase.util; -import java.util.Random; - -public class RandomNumberGenerator { - public static int generate(int digit){ - Random random = new Random(); - int min = (int) Math.pow(10, digit - 1); - int max = (int) Math.pow(10, digit) - 1; - - return random.nextInt(max - min+1); - } - - public static int addDigitToFront(int number, int digit){ - String numAsStr = String.valueOf(number); - String digAsStr = String.valueOf(digit); - String res = digAsStr + numAsStr; - return Integer.parseInt(res); - } - - public int generateAttorneyId(int digit){ - return addDigitToFront(generate(digit-1), 1); - } - public int generateClientId(int digit){ - return addDigitToFront(generate(digit-1), 2); - } - public int generateQuestionId(int digit){ - return addDigitToFront(generate(digit-1), 3); - } - public int generatePostId(int digit){ - return addDigitToFront(generate(digit-1), 4); - } -} diff --git a/src/main/java/driver/database/ClientRepository.java b/src/main/java/driver/database/ClientRepository.java deleted file mode 100644 index 0d00d87e..00000000 --- a/src/main/java/driver/database/ClientRepository.java +++ /dev/null @@ -1,18 +0,0 @@ -package driver.database; - -import businessrule.gateway.ClientGateway; -import entity.Client; - -public class ClientRepository extends UserRepository implements ClientGateway { - - public ClientRepository() { - super(Client.class); - } - - @Override - public Client get(int id) { - return (Client) super.get(id); - } - -} - diff --git a/src/main/java/driver/database/PostRepo.java b/src/main/java/driver/database/PostRepo.java deleted file mode 100644 index 1ba95283..00000000 --- a/src/main/java/driver/database/PostRepo.java +++ /dev/null @@ -1,17 +0,0 @@ -package driver.database; - -import businessrule.gateway.PostGateway; -import entity.Post; - -public class PostRepo extends GenericRepository implements PostGateway { - - public PostRepo() { - super(Post.class); - } - - @Override - public Post get(int id) { - return (Post) super.get(id); - } - -} diff --git a/src/main/java/entities/ApplicationException.java b/src/main/java/entities/ApplicationException.java new file mode 100644 index 00000000..7f713644 --- /dev/null +++ b/src/main/java/entities/ApplicationException.java @@ -0,0 +1,16 @@ +package entities; + +/** + * This class represents a custom runtime exception that can be thrown during application execution. + */ +public class ApplicationException extends RuntimeException { + + /** + * Constructs an ApplicationException with the specified error message. + * + * @param msg The error message associated with the exception. + */ + public ApplicationException(String msg) { + super(msg); + } +} \ No newline at end of file diff --git a/src/main/java/entity/Post.java b/src/main/java/entities/Post.java similarity index 50% rename from src/main/java/entity/Post.java rename to src/main/java/entities/Post.java index ff04f6aa..892c4ebc 100644 --- a/src/main/java/entity/Post.java +++ b/src/main/java/entities/Post.java @@ -1,4 +1,4 @@ -package entity; +package entities; import com.fasterxml.jackson.annotation.JsonProperty; @@ -7,6 +7,13 @@ import java.time.LocalDate; import java.util.Objects; +/** + * This class represents a post in a question thread. + * A "Post" is a message or response posted in the context of a specific question thread. + * Each post contains information such as the ID of the associated question, the post ID, + * the creation date, the text of the post, and an identifier indicating which user the post + * belongs to. + */ @Entity public class Post { @JsonProperty(required = true) @@ -24,6 +31,15 @@ public class Post { public Post() { } + /** + * Constructs a new Post instance. + * + * @param questionId The ID of the associated question. + * @param postId The ID of the post. + * @param createAt The creation date of the post. + * @param postText The text of the post. + * @param belongsTo The identifier of the user to whom the post belongs. + */ public Post(int questionId, int postId, LocalDate createAt, String postText, int belongsTo){ this.questionId = questionId; this.postId = postId; @@ -32,37 +48,93 @@ public Post(int questionId, int postId, LocalDate createAt, String postText, int this.belongsTo = belongsTo; } + /** + * Gets the ID of the associated question. + * + * @return The question ID. + */ public int getQuestionId() { return questionId; } + /** + * Gets the ID of the post. + * + * @return The post ID. + */ public int getPostId() { return postId; } + /** + * Gets the creation date of the post. + * + * @return The creation date. + */ public LocalDate getCreateAt() { return createAt; } + /** + * Gets the text of the post. + * + * @return The post text. + */ public String getPostText() { return postText; } + /** + * Gets the identifier of the user to whom the post belongs. + * + * @return The user identifier. + */ public int getBelongsTo() {return belongsTo;} + /** + * Sets the ID of the associated question. + * + * @param questionId The question ID to set. + */ public void setQuestionId(int questionId) {this.questionId = questionId;} + /** + * Sets the ID of the post. + * + * @param postId The post ID to set. + */ public void setPostId(int postId) {this.postId = postId;} + /** + * Sets the creation date of the post. + * + * @param createAt The creation date to set. + */ public void setCreateAt(LocalDate createAt) {this.createAt = createAt;} + /** + * Sets the text of the post. + * + * @param postText The post text to set. + */ public void setPostText(String postText) {this.postText = postText;} + /** + * Sets the identifier of the user to whom the post belongs. + * + * @param belongsTo The user identifier to set. + */ public void setBelongsTo(int belongsTo) {this.belongsTo = belongsTo;} + /** + * {@inheritDoc} + */ @Override public int hashCode() {return Objects.hashCode(postId);} + /** + * {@inheritDoc} + */ @Override public boolean equals(Object obj) { if (!(obj instanceof Post)) return false; @@ -70,8 +142,11 @@ public boolean equals(Object obj) { return postId == otherPost.postId; } + /** + * {@inheritDoc} + */ @Override public String toString() { return String.format("This is a post in %d belongs to %d", questionId, belongsTo); } -} +} \ No newline at end of file diff --git a/src/main/java/entities/Question.java b/src/main/java/entities/Question.java new file mode 100644 index 00000000..21ec1ab3 --- /dev/null +++ b/src/main/java/entities/Question.java @@ -0,0 +1,325 @@ +package entities; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.Id; +import javax.persistence.OneToMany; +import java.time.LocalDate; +import java.util.List; +import java.util.ArrayList; +import java.util.Objects; + +/** + * This class represents a question thread. + * Each question thread contains information about the type of the question, the client who asked the question, + * whether the question has been taken and by whom, and a list of posts belonging to the question. + */ +@Entity +public class Question { + @Id + @JsonProperty(required = true) + private int questionId; + @JsonProperty(required = true) + private String type; + private String title; + @JsonProperty(required = true) + private LocalDate createAt; + @JsonProperty(required = true) + private int askedByClient; + @JsonProperty(required = true) + private LocalDate legalDeadline; + @JsonProperty(value = "isTaken", required = true) + private boolean isTaken; + @JsonProperty(required = true) + private int takenByAttorney; + @JsonProperty(required = true) + private LocalDate takenAt; + @JsonProperty(value = "isClose", required = true) + private boolean isClose; + @JsonProperty(required = true) + private int rating; + @OneToMany(targetEntity = Post.class, fetch = FetchType.EAGER) + @JsonProperty(required = true) + private List posts; + public static final int MISSING_RATING = -1; + + public Question() { + rating = MISSING_RATING; + posts = new ArrayList(); + } + + /** + * Constructs a new Question instance. + * + * @param questionId The ID of the question. + * @param type The type of the question. + * @param title The title of the question. + * @param createAt The creation date of the question. + * @param askedByClient The ID of the client who asked the question. + * @param legalDeadline The legal deadline for the question. + */ + public Question(int questionId, String type, String title, LocalDate createAt, int askedByClient, LocalDate legalDeadline) { + this.questionId = questionId; + this.type = type; + this.title = title; + this.createAt = createAt; + this.askedByClient = askedByClient; + this.legalDeadline = legalDeadline; + this.rating = MISSING_RATING; + this.posts = new ArrayList(); + } + + /** + * Gets the ID of the question. + * + * @return The question ID. + */ + public int getQuestionId() { + return questionId; + } + + /** + * Gets the type of the question. + * + * @return The question type. + */ + public String getType() { + return type; + } + + /** + * Gets the title of the question. + * + * @return The question title. + */ + public String getTitle() { + return title; + } + + /** + * Gets the creation date of the question. + * + * @return The creation date. + */ + public LocalDate getCreateAt() { + return createAt; + } + + /** + * Gets the ID of the client who asked the question. + * + * @return The client ID. + */ + public int getAskedByClient() { + return askedByClient; + } + + /** + * Gets the legal deadline for the question. + * + * @return The legal deadline. + */ + public LocalDate getLegalDeadline() { + return legalDeadline; + } + + /** + * Checks if the question has been taken. + * + * @return True if the question is taken, false otherwise. + */ + public boolean isTaken() { + return isTaken; + } + + /** + * Gets the ID of the attorney who took the question. + * + * @return The attorney ID. + */ + public int getTakenByAttorney() { + return takenByAttorney; + } + + /** + * Gets the date when the question was taken. + * + * @return The taken date. + */ + public LocalDate getTakenAt() { + return takenAt; + } + + /** + * Checks if the question is closed. + * + * @return True if the question is closed, false otherwise. + */ + public boolean isClose() { + return isClose; + } + + /** + * Gets the rating of the question. + * + * @return The question rating. + */ + public int getRating() { + return rating; + } + + /** + * Gets the list of posts belonging to the question. + * + * @return The list of posts. + */ + public List getPosts() { + return posts; + } + + /** + * Sets the ID of the question. + * + * @param questionId The question ID to set. + */ + public void setQuestionId(int questionId) { + this.questionId = questionId; + } + + /** + * Sets the type of the question. + * + * @param type The question type to set. + */ + public void setType(String type) { + this.type = type; + } + + /** + * Sets the title of the question. + * + * @param title The question title to set. + */ + public void setTitle(String title) { + this.title = title; + } + + /** + * Sets the creation date of the question. + * + * @param createAt The creation date to set. + */ + public void setCreateAt(LocalDate createAt) { + this.createAt = createAt; + } + + /** + * Sets the ID of the client who asked the question. + * + * @param askedByClient The client ID to set. + */ + public void setAskedByClient(int askedByClient) { + this.askedByClient = askedByClient; + } + + /** + * Sets the legal deadline for the question. + * + * @param legalDeadline The legal deadline to set. + */ + public void setLegalDeadline(LocalDate legalDeadline) { + this.legalDeadline = legalDeadline; + } + + /** + * Sets whether the question is taken or not. + * + * @param taken True if taken, false otherwise. + */ + public void setTaken(boolean taken) { + isTaken = taken; + } + + /** + * Sets the ID of the attorney who took the question. + * + * @param takenByAttorney The attorney ID to set. + */ + public void setTakenByAttorney(int takenByAttorney) { + this.takenByAttorney = takenByAttorney; + } + + /** + * Sets the date when the question was taken. + * + * @param takenAt The taken date to set. + */ + public void setTakenAt(LocalDate takenAt) { + this.takenAt = takenAt; + } + + /** + * Sets whether the question is closed or not. + * + * @param close True if closed, false otherwise. + */ + public void setClose(boolean close) { + isClose = close; + } + + /** + * Sets the rating of the question. + * + * @param rating The question rating to set. + */ + public void setRating(int rating) { + this.rating = rating; + } + + /** + * Adds a post to the list of posts belonging to the question. + * + * @param post The post to add. + */ + public void addPosts(Post post) { + if (!posts.contains(post)) { + posts.add(post); + } + } + + + /** + * Generates a hash code for the Question object based on its questionId. + * + * @return The hash code value. + */ + @Override + public int hashCode() { + return Objects.hashCode(questionId); + } + + /** + * Compares this Question object with another object to determine equality. + * + * @param obj The object to compare with. + * @return True if the objects are equal, false otherwise. + */ + @Override + public boolean equals(Object obj) { + if (!(obj instanceof Question)) return false; + Question otherQuestion = (Question) obj; + return questionId == otherQuestion.questionId; + } + + /** + * Returns a string representation of the Question object. + * + * @return The formatted string. + */ + @Override + public String toString() { + return String.format("This is a %s type question asked by %d", type, askedByClient); + } +} \ No newline at end of file diff --git a/src/main/java/entities/factories/AttorneyFactory.java b/src/main/java/entities/factories/AttorneyFactory.java new file mode 100644 index 00000000..3a7ceef0 --- /dev/null +++ b/src/main/java/entities/factories/AttorneyFactory.java @@ -0,0 +1,22 @@ +package entities.factories; + +import usecases.requests.AttorneyRegistrationData; +import usecases.requests.RegistrationData; +import entities.user.Attorney; + +/** + * This class represents creating Attorney instances. + */ +public class AttorneyFactory implements UserFactory { + + /** + * Creates an Attorney instance based on the provided registration data. + * + * @param data The registration data for creating the Attorney. + * @return A new Attorney instance. + */ + @Override + public Attorney createUser(RegistrationData data) { + return new Attorney.Builder(data).build(); + } +} \ No newline at end of file diff --git a/src/main/java/entities/factories/ClientFactory.java b/src/main/java/entities/factories/ClientFactory.java new file mode 100644 index 00000000..b904fcf7 --- /dev/null +++ b/src/main/java/entities/factories/ClientFactory.java @@ -0,0 +1,22 @@ +package entities.factories; + +import usecases.requests.ClientRegistrationData; +import usecases.requests.RegistrationData; +import entities.user.Client; + +/** + * This class represents creating Client instances. + */ +public class ClientFactory implements UserFactory { + + /** + * Creates a Client instance based on the provided registration data. + * + * @param data The registration data for creating the Client. + * @return A new Client instance. + */ + @Override + public Client createUser(RegistrationData data) { + return new Client.Builder(data).build(); + } +} \ No newline at end of file diff --git a/src/main/java/entities/factories/PostFactory.java b/src/main/java/entities/factories/PostFactory.java new file mode 100644 index 00000000..01f2a076 --- /dev/null +++ b/src/main/java/entities/factories/PostFactory.java @@ -0,0 +1,16 @@ +package entities.factories; + +import entities.Post; + +import java.time.LocalDate; + +/** + * This is a factory class for creating Post objects. + * The "PostFactory" class provides a convenient way to create instances of the "Post" class by encapsulating + * the construction logic. It serves as a central point for creating posts associated with questions in the application. + */ +public class PostFactory { + public Post create(int postId, int questionId, LocalDate createAt, String postText, int belongsTo){ + return new Post(questionId, postId, createAt, postText, belongsTo); + } +} diff --git a/src/main/java/entity/factory/QuestionFactory.java b/src/main/java/entities/factories/QuestionFactory.java similarity index 68% rename from src/main/java/entity/factory/QuestionFactory.java rename to src/main/java/entities/factories/QuestionFactory.java index 2b5d5a80..2b1cca82 100644 --- a/src/main/java/entity/factory/QuestionFactory.java +++ b/src/main/java/entities/factories/QuestionFactory.java @@ -1,9 +1,12 @@ -package entity.factory; +package entities.factories; -import entity.Question; +import entities.Question; import java.time.LocalDate; +/** + * This is class represnts for creating instances of the "Question" class. + */ public class QuestionFactory { public Question create(int questionId, String type, String title, LocalDate createAt, int askedByClient, LocalDate legalDeadline){ return new Question(questionId, type, title, createAt, askedByClient, legalDeadline); diff --git a/src/main/java/entities/factories/UserFactory.java b/src/main/java/entities/factories/UserFactory.java new file mode 100644 index 00000000..0061135f --- /dev/null +++ b/src/main/java/entities/factories/UserFactory.java @@ -0,0 +1,21 @@ +package entities.factories; + +import entities.user.User; +import usecases.requests.RegistrationData; +import entities.user.UserImp; + +/** + * This is a interface represents creating user instances. + * + * @param The type of user to create, extending UserImp. + */ +public interface UserFactory { + + /** + * Creates a user instance based on the provided registration data. + * + * @param data The registration data for creating the user. + * @return A new user instance. + */ + T createUser(RegistrationData data); +} \ No newline at end of file diff --git a/src/main/java/entity/Attorney.java b/src/main/java/entities/user/Attorney.java similarity index 74% rename from src/main/java/entity/Attorney.java rename to src/main/java/entities/user/Attorney.java index c2098368..58938367 100644 --- a/src/main/java/entity/Attorney.java +++ b/src/main/java/entities/user/Attorney.java @@ -1,8 +1,8 @@ -package entity; +package entities.user; import com.fasterxml.jackson.annotation.JsonProperty; -import businessrule.requestmodel.RegistrationData; -import driver.database.AttorneyRepository; +import entities.Question; +import usecases.requests.RegistrationData; import javax.persistence.*; import java.time.LocalDate; @@ -11,6 +11,12 @@ import java.util.ArrayList; import java.util.Set; +/** + * This is a class representing attorney user. + * An Attorney is a registered user who is a legal professional, capable of answering legal questions + * posted by clients. The Attorney has attributes such as user ID, name, email, password, state abbreviation, + * postal code, and a list of questions associated with them. + */ @Entity public class Attorney extends UserImp { @OneToMany(targetEntity = Question.class, fetch = FetchType.EAGER) @@ -19,24 +25,49 @@ public class Attorney extends UserImp { @ElementCollection(fetch = FetchType.EAGER) private Set professionals; + /** + * Default constructor for creating an Attorney instance. + */ public Attorney() { super(); recommendations = new ArrayList<>(); professionals = new HashSet<>(); } + /** + * Constructor for creating an Attorney instance using a builder. + * + * @param builder The builder instance used for constructing the Attorney. + */ public Attorney(Builder builder) { super(builder); } + + /** + * Builder class for constructing Attorney instances. + */ public static class Builder extends UserImp.Builder { + + /** + * Constructor for the Attorney builder. + * + * @param data The registration data for creating the Attorney. + */ public Builder(RegistrationData data) { super(data); } + /** + * Set the list of professional specialties for the Attorney. + * + * @param professionals The set of professional specialties. + * @return The builder instance. + */ public Attorney.Builder professionals(Set professionals) { this.data.professionals = professionals; return this; } + @Override protected Attorney.Builder self() { return this; diff --git a/src/main/java/entity/Client.java b/src/main/java/entities/user/Client.java similarity index 50% rename from src/main/java/entity/Client.java rename to src/main/java/entities/user/Client.java index 801781ba..6ae9f83d 100644 --- a/src/main/java/entity/Client.java +++ b/src/main/java/entities/user/Client.java @@ -1,7 +1,8 @@ -package entity; +package entities.user; import com.fasterxml.jackson.annotation.JsonProperty; -import businessrule.requestmodel.RegistrationData; +import entities.Question; +import usecases.requests.RegistrationData; import javax.persistence.*; import java.util.ArrayList; @@ -21,8 +22,16 @@ public class Client extends UserImp { @JsonProperty(required = true) private float annualIncome; + /** + * Default constructor for the Client class. + */ public Client() {super();} + /** + * Constructor to create a Client instance using the provided Builder. + * + * @param builder The Builder instance containing client data. + */ private Client(Builder builder) { super(builder); this.ethnicity = builder.data.ethnicity; @@ -31,75 +40,157 @@ private Client(Builder builder) { this.maritalStatus = builder.data.maritalStatus; this.numberOfHousehold = builder.data.numberOfHousehold; this.annualIncome = builder.data.annualIncome; - this.questionsList = new ArrayList(); + this.questionsList = new ArrayList<>(); } + /** + * Builder class for creating instances of the Client class. + */ public static class Builder extends UserImp.Builder { + /** + * Constructor for the Builder class. + * + * @param data The registration data for the client. + */ public Builder(RegistrationData data) { super(data); } + /** + * Set the ethnicity for the client. + * + * @param ethnicity The ethnicity of the client. + * @return The Builder instance. + */ public Builder ethnicity(String ethnicity) { this.data.ethnicity = ethnicity; return this; } + /** + * Set the age for the client. + * + * @param age The age of the client. + * @return The Builder instance. + */ public Builder age(int age) { this.data.age = age; return this; } + /** + * Set the gender for the client. + * + * @param gender The gender of the client. + * @return The Builder instance. + */ public Builder gender(String gender) { this.data.gender = gender; return this; } + /** + * Set the marital status for the client. + * + * @param maritalStatus The marital status of the client. + * @return The Builder instance. + */ public Builder maritalStatus(String maritalStatus) { this.data.maritalStatus = maritalStatus; return this; } + /** + * Set the number of household members for the client. + * + * @param numberOfHousehold The number of household members of the client. + * @return The Builder instance. + */ public Builder numberOfHousehold(int numberOfHousehold) { this.data.numberOfHousehold = numberOfHousehold; return this; } + /** + * Set the annual income for the client. + * + * @param annualIncome The annual income of the client. + * @return The Builder instance. + */ public Builder annualIncome(float annualIncome) { this.data.annualIncome = annualIncome; return this; } + /** + * {@inheritDoc} + */ @Override protected Builder self() { return this; } + /** + * Build and return a Client instance based on the Builder configuration. + * + * @return A new Client instance. + */ @Override public Client build() { return new Client(this); } } + /** + * Get the ethnicity of the client. + * + * @return The ethnicity of the client. + */ public String getEthnicity() { return ethnicity; } + /** + * Get the age of the client. + * + * @return The age of the client. + */ public int getAge() { return age; } + /** + * Get the gender of the client. + * + * @return The gender of the client. + */ public String getGender() { return gender; } + /** + * Get the marital status of the client. + * + * @return The marital status of the client. + */ public String getMaritalStatus() { return maritalStatus; } + /** + * Get the number of household members of the client. + * + * @return The number of household members of the client. + */ public int getNumberOfHousehold() { return numberOfHousehold; } + /** + * Get the annual income of the client. + * + * @return The annual income of the client. + */ public float getAnnualIncome() { return annualIncome; } @@ -116,12 +207,23 @@ public float getAnnualIncome() { public void setAnnualIncome(float annualIncome) {this.annualIncome = annualIncome;} + /** + * Provide the string representation of the user type. + * + * @return Client + */ @Override @Transient public String getUserType() { return "Client"; } + /** + * Check if a question can be closed for the client. + * + * @param question The question to be checked. + * @return True if the question can be closed, otherwise false. + */ @Override public boolean isQuestionCloseable(Question question) { boolean isClose = question.isClose(); @@ -129,16 +231,34 @@ public boolean isQuestionCloseable(Question question) { return (isTaken && !isClose); } + /** + * Check if a question is selectable for the client. + * + * @param question The question to be checked. + * @return Always returns true, indicating that the question is selectable. + */ @Override public boolean isQuestionSelectable(Question question) { return true; } + /** + * Check if a question can be replied to by the client. + * + * @param question The question to be checked. + * @return True if the question can be replied to, otherwise false. + */ @Override public boolean isQuestionReplyable(Question question) { return !question.isClose(); } + /** + * Check if this client instance is equal to another object. + * + * @param obj The object to compare with this client instance. + * @return True if the objects are equal, otherwise false. + */ @Override public boolean equals(Object obj) { if (!(obj instanceof Client)) return false; @@ -146,11 +266,22 @@ public boolean equals(Object obj) { return userId == otherClient.userId; } + /** + * Get a string representation of the client. + * + * @return A formatted string representing the client. + */ @Override public String toString() { return String.format("[Client]: %s", name); } + /** + * Check if a question is rateable for the client. + * + * @param question The question to be checked. + * @return True if the question is rateable, otherwise false. + */ @Override public boolean isQuestionRateable(Question question) { return question.isClose(); diff --git a/src/main/java/entity/User.java b/src/main/java/entities/user/User.java similarity index 74% rename from src/main/java/entity/User.java rename to src/main/java/entities/user/User.java index 24cd89df..d2631646 100644 --- a/src/main/java/entity/User.java +++ b/src/main/java/entities/user/User.java @@ -1,9 +1,13 @@ -package entity; +package entities.user; -import java.util.List; +import entities.Question; -import javax.persistence.Entity; +import java.util.List; +/** + * The "User" interface represents a generic user in the system. + * This interface defines common methods that are shared by different types of users in the application, + */ public interface User { int getUserId(); String getUserName(); diff --git a/src/main/java/entity/UserImp.java b/src/main/java/entities/user/UserImp.java similarity index 75% rename from src/main/java/entity/UserImp.java rename to src/main/java/entities/user/UserImp.java index 8f7faeeb..aa741b94 100644 --- a/src/main/java/entity/UserImp.java +++ b/src/main/java/entities/user/UserImp.java @@ -1,7 +1,8 @@ -package entity; +package entities.user; import com.fasterxml.jackson.annotation.JsonProperty; -import businessrule.requestmodel.RegistrationData; +import entities.Question; +import usecases.requests.RegistrationData; import javax.persistence.Entity; import javax.persistence.FetchType; @@ -11,8 +12,11 @@ import java.util.List; import java.util.Objects; +/** + * This class respresents a user entity with common properties and behaviors. + */ @Entity -public abstract class UserImp implements User{ +public abstract class UserImp implements User { @Id @JsonProperty(required = true) protected int userId; @@ -26,7 +30,7 @@ public abstract class UserImp implements User{ @JsonProperty(required = true) protected List questionsList; - public UserImp() {questionsList = new ArrayList();} + public UserImp() {questionsList = new ArrayList<>();} protected UserImp(Builder builder) { this.userId = builder.data.userId; @@ -124,13 +128,42 @@ public void addQuestion(Question question) { @Override public int hashCode() {return Objects.hashCode(userId);} + /** + * Return the string representation of the user type + * + * @return user type + */ public abstract String getUserType(); + /** + * Check if a question can be closed for the user. + * + * @param question The question to be checked. + * @return True if the question can be closed, otherwise false. + */ public abstract boolean isQuestionCloseable(Question question); + /** + * Check if a question is selectable for the user. + * + * @param question The question to be checked. + * @return True if the question is selectable, otherwise false. + */ public abstract boolean isQuestionSelectable(Question question); + /** + * Check if a question can be replied to by the user. + * + * @param question The question to be checked. + * @return True if the question can be replied to, otherwise false. + */ public abstract boolean isQuestionReplyable(Question question); + /** + * Check if a question is rateable by the user. + * + * @param question The question to be checked. + * @return True if the question is rateable, otherwise false. + */ public abstract boolean isQuestionRateable(Question question); } diff --git a/src/main/java/entity/ApplicationException.java b/src/main/java/entity/ApplicationException.java deleted file mode 100644 index e526ba31..00000000 --- a/src/main/java/entity/ApplicationException.java +++ /dev/null @@ -1,7 +0,0 @@ -package entity; - -public class ApplicationException extends RuntimeException{ - public ApplicationException(String msg){ - super(msg); - } -} diff --git a/src/main/java/entity/Question.java b/src/main/java/entity/Question.java deleted file mode 100644 index 586aacf8..00000000 --- a/src/main/java/entity/Question.java +++ /dev/null @@ -1,134 +0,0 @@ -package entity; - -import com.fasterxml.jackson.annotation.JsonProperty; - -import javax.persistence.Entity; -import javax.persistence.FetchType; -import javax.persistence.Id; -import javax.persistence.OneToMany; -import java.time.LocalDate; -import java.util.List; -import java.util.ArrayList; -import java.util.Objects; - -/** - * This is a class representing a question thread. - * - * Each question thread contain information about the type of the question, the client who asked the question, - * whether the question has been taken and who took the question, and a list of post belongs to the question. - */ -@Entity -public class Question { - @Id - @JsonProperty(required = true) - private int questionId; - @JsonProperty(required = true) - private String type; - private String title; - @JsonProperty(required = true) - private LocalDate createAt; - @JsonProperty(required = true) - private int askedByClient; - @JsonProperty(required = true) - private LocalDate legalDeadline; - @JsonProperty(value = "isTaken", required = true) - private boolean isTaken; - @JsonProperty(required = true) - private int takenByAttorney; - @JsonProperty(required = true) - private LocalDate takenAt; - @JsonProperty(value = "isClose", required = true) - private boolean isClose; - @JsonProperty(required = true) - private int rating; - @OneToMany(targetEntity = Post.class, fetch = FetchType.EAGER) - @JsonProperty(required = true) - private List posts; - public static final int MISSING_RATING = -1; - - - public Question() { - rating = MISSING_RATING; - posts = new ArrayList(); - } - - public Question(int questionId, String type, String title, LocalDate createAt, int askedByClient, LocalDate legalDeadline) { - this.questionId = questionId; - this.type = type; - this.title = title; - this.createAt = createAt; - this.askedByClient = askedByClient; - this.legalDeadline = legalDeadline; - this.rating = MISSING_RATING; - this.posts = new ArrayList(); - } - - public int getQuestionId() {return questionId;} - - public String getType() {return type;} - - public String getTitle() { - return title; - } - - public LocalDate getCreateAt() {return createAt;} - - public int getAskedByClient() {return askedByClient;} - - public LocalDate getLegalDeadline() {return legalDeadline;} - - public boolean isTaken() {return isTaken;} - - public int getTakenByAttorney() {return takenByAttorney;} - - public LocalDate getTakenAt() {return takenAt;} - - public boolean isClose() {return isClose;} - - public int getRating() {return rating;} - - public List getPosts() {return posts;} - - public void setQuestionId(int questionId) {this.questionId = questionId;} - - public void setType(String type) {this.type = type;} - - public void setTitle(String title) {this.title = title;} - - public void setCreateAt(LocalDate createAt) {this.createAt = createAt;} - - public void setAskedByClient(int askedByClient) {this.askedByClient = askedByClient;} - - public void setLegalDeadline(LocalDate legalDeadline) {this.legalDeadline = legalDeadline;} - - public void setTaken(boolean taken) {isTaken = taken;} - - public void setTakenByAttorney(int takenByAttorney) {this.takenByAttorney = takenByAttorney;} - - public void setTakenAt(LocalDate takenAt) {this.takenAt = takenAt;} - - public void setClose(boolean close) {isClose = close;} - - public void setRating(int rating) {this.rating = rating;} - - public void addPosts(Post post) { - if (! posts.contains(post)) { - posts.add(post); - } - } - - @Override - public int hashCode() {return Objects.hashCode(questionId);} - - @Override - public boolean equals(Object obj) { - if (!(obj instanceof Question)) return false; - Question otherQuestion = (Question) obj; - return questionId == otherQuestion.questionId; - } - - @Override - public String toString() { - return String.format("This is a %s type question asked by %d", type, askedByClient); - } -} diff --git a/src/main/java/entity/factory/AttorneyFactory.java b/src/main/java/entity/factory/AttorneyFactory.java deleted file mode 100644 index d121c3ca..00000000 --- a/src/main/java/entity/factory/AttorneyFactory.java +++ /dev/null @@ -1,11 +0,0 @@ -package entity.factory; - -import businessrule.requestmodel.RegistrationData; -import entity.Attorney; - -public class AttorneyFactory implements UserFactory { - @Override - public Attorney createUser(RegistrationData data) { - return new Attorney.Builder(data).build(); - } -} diff --git a/src/main/java/entity/factory/ClientFactory.java b/src/main/java/entity/factory/ClientFactory.java deleted file mode 100644 index 39fac52d..00000000 --- a/src/main/java/entity/factory/ClientFactory.java +++ /dev/null @@ -1,10 +0,0 @@ -package entity.factory; -import businessrule.requestmodel.RegistrationData; -import entity.Client; - -public class ClientFactory implements UserFactory { - @Override - public Client createUser(RegistrationData data) { - return new Client.Builder(data).build(); - } -} diff --git a/src/main/java/entity/factory/PostFactory.java b/src/main/java/entity/factory/PostFactory.java deleted file mode 100644 index 07488c11..00000000 --- a/src/main/java/entity/factory/PostFactory.java +++ /dev/null @@ -1,11 +0,0 @@ -package entity.factory; - -import entity.Post; - -import java.time.LocalDate; - -public class PostFactory { - public Post create(int postId, int questionId, LocalDate createAt, String postText, int belongsTo){ - return new Post(questionId, postId, createAt, postText, belongsTo); - } -} diff --git a/src/main/java/entity/factory/UserFactory.java b/src/main/java/entity/factory/UserFactory.java deleted file mode 100644 index fea07144..00000000 --- a/src/main/java/entity/factory/UserFactory.java +++ /dev/null @@ -1,8 +0,0 @@ -package entity.factory; - -import businessrule.requestmodel.RegistrationData; -import entity.UserImp; - -public interface UserFactory { - T createUser(RegistrationData data); -} diff --git a/src/main/java/driver/database/AttorneyRepository.java b/src/main/java/infrastructure/database/AttorneyRepository.java similarity index 64% rename from src/main/java/driver/database/AttorneyRepository.java rename to src/main/java/infrastructure/database/AttorneyRepository.java index 39f3ce80..93d39297 100644 --- a/src/main/java/driver/database/AttorneyRepository.java +++ b/src/main/java/infrastructure/database/AttorneyRepository.java @@ -1,7 +1,8 @@ -package driver.database; +package infrastructure.database; -import businessrule.gateway.AttorneyGateway; -import entity.*; +import entities.user.Attorney; +import usecases.gateway.AttorneyGateway; +import entities.*; import javax.jdo.JDOHelper; import javax.persistence.EntityManager; @@ -9,17 +10,34 @@ import java.util.ArrayList; import java.util.List; +/** + * This class represents managing Attorney entities in the database. + */ public class AttorneyRepository extends UserRepository implements AttorneyGateway { + /** + * Constructs an AttorneyRepository instance. + * Initializes the superclass with the Attorney class. + */ public AttorneyRepository() { super(Attorney.class); } + /** + * Retrieves an Attorney entity with the specified ID from the database. + * + * @param id The ID of the Attorney entity to retrieve. + * @return The Attorney entity associated with the specified ID, or null if not found. + */ @Override public Attorney get(int id) { return (Attorney) super.get(id); } + /** + * Clears all recommendations associated with Attorneys in the database. + * This method removes all recommendations from each Attorney entity. + */ @Override public void clearAllRecommendations() { EntityManager em = DatabaseConnection.getEntityManager(); @@ -43,6 +61,12 @@ public void clearAllRecommendations() { } } + /** + * Adds a recommendation to an Attorney entity in the database. + * + * @param Userid The ID of the Attorney entity to which the recommendation will be added. + * @param question The Question entity representing the recommendation. + */ @Override public void addRecommendation(int Userid, Question question) { EntityManager em = DatabaseConnection.getEntityManager(); @@ -62,4 +86,4 @@ public void addRecommendation(int Userid, Question question) { em.close(); } } -} +} \ No newline at end of file diff --git a/src/main/java/infrastructure/database/ClientRepository.java b/src/main/java/infrastructure/database/ClientRepository.java new file mode 100644 index 00000000..d6230541 --- /dev/null +++ b/src/main/java/infrastructure/database/ClientRepository.java @@ -0,0 +1,29 @@ +package infrastructure.database; + +import usecases.gateway.ClientGateway; +import entities.user.Client; + +/** + * This class represents managing Client entities in the database. + */ +public class ClientRepository extends UserRepository implements ClientGateway { + + /** + * Constructs a ClientRepository instance. + * Initializes the superclass with the Client class. + */ + public ClientRepository() { + super(Client.class); + } + + /** + * Retrieves a Client entity with the specified ID from the database. + * + * @param id The ID of the Client entity to retrieve. + * @return The Client entity associated with the specified ID, or null if not found. + */ + @Override + public Client get(int id) { + return (Client) super.get(id); + } +} \ No newline at end of file diff --git a/src/main/java/driver/database/DatabaseConnection.java b/src/main/java/infrastructure/database/DatabaseConnection.java similarity index 68% rename from src/main/java/driver/database/DatabaseConnection.java rename to src/main/java/infrastructure/database/DatabaseConnection.java index 7459a36e..229e47d1 100644 --- a/src/main/java/driver/database/DatabaseConnection.java +++ b/src/main/java/infrastructure/database/DatabaseConnection.java @@ -1,13 +1,22 @@ -package driver.database; +package infrastructure.database; + import javax.persistence.EntityManagerFactory; import javax.persistence.EntityManager; import javax.persistence.Persistence; +/** + * This class represents a utility for managing the Database Connection and Entity Manager. + */ public class DatabaseConnection { private static final String URL = "objectdb:myapp.odb;admin"; private static EntityManagerFactory entityManagerFactory; + /** + * Retrieves an instance of EntityManager for database interaction. + * + * @return An EntityManager instance. + */ public static EntityManager getEntityManager() { if (entityManagerFactory == null) { entityManagerFactory = Persistence.createEntityManagerFactory(URL); @@ -15,10 +24,12 @@ public static EntityManager getEntityManager() { return entityManagerFactory.createEntityManager(); } + /** + * Closes the EntityManagerFactory if it is open. + */ public static void closeEntityManagerFactory() { if (entityManagerFactory != null && entityManagerFactory.isOpen()) { entityManagerFactory.close(); } } -} - +} \ No newline at end of file diff --git a/src/main/java/driver/database/GenericRepository.java b/src/main/java/infrastructure/database/GenericRepository.java similarity index 80% rename from src/main/java/driver/database/GenericRepository.java rename to src/main/java/infrastructure/database/GenericRepository.java index ca7fde75..feda2e95 100644 --- a/src/main/java/driver/database/GenericRepository.java +++ b/src/main/java/infrastructure/database/GenericRepository.java @@ -1,13 +1,23 @@ -package driver.database; +package infrastructure.database; -import businessrule.gateway.GenericGateway; +import usecases.gateway.GenericGateway; import javax.persistence.EntityManager; import javax.persistence.EntityTransaction; import java.util.List; +/** + * This class represents managing entities in the database. + * + * @param The type of entity managed by the repository. + */ public class GenericRepository implements GenericGateway { final Class entityType; + /** + * Constructs a GenericRepository instance. + * + * @param entityType The class representing the type of entity managed by the repository. + */ public GenericRepository(Class entityType) { this.entityType = entityType; } @@ -77,12 +87,19 @@ R executeTransactionWithResult(DatabaseOperationWithResult operation) { } } + /** + * Represents an operation that accepts an EntityManager and performs an action. + */ interface DatabaseOperation { void accept(EntityManager entityManager); } + /** + * Represents an operation that accepts an EntityManager and returns a result. + * + * @param The type of the result returned by the operation. + */ interface DatabaseOperationWithResult { R apply(EntityManager entityManager); } - -} +} \ No newline at end of file diff --git a/src/main/java/infrastructure/database/PostRepo.java b/src/main/java/infrastructure/database/PostRepo.java new file mode 100644 index 00000000..b8ff3d6e --- /dev/null +++ b/src/main/java/infrastructure/database/PostRepo.java @@ -0,0 +1,29 @@ +package infrastructure.database; + +import usecases.gateway.PostGateway; +import entities.Post; + +/** + * This class representsmanaging Post entities in the database. + */ +public class PostRepo extends GenericRepository implements PostGateway { + + /** + * Constructs a PostRepo instance. + * Initializes the superclass with the Post class. + */ + public PostRepo() { + super(Post.class); + } + + /** + * Retrieves a Post entity with the specified ID from the database. + * + * @param id The ID of the Post entity to retrieve. + * @return The Post entity associated with the specified ID, or null if not found. + */ + @Override + public Post get(int id) { + return (Post) super.get(id); + } +} diff --git a/src/main/java/driver/database/QuestionRepo.java b/src/main/java/infrastructure/database/QuestionRepo.java similarity index 94% rename from src/main/java/driver/database/QuestionRepo.java rename to src/main/java/infrastructure/database/QuestionRepo.java index a853e25f..82b1be82 100644 --- a/src/main/java/driver/database/QuestionRepo.java +++ b/src/main/java/infrastructure/database/QuestionRepo.java @@ -1,8 +1,8 @@ -package driver.database; +package infrastructure.database; -import businessrule.gateway.QuestionGateway; -import entity.Post; -import entity.Question; +import usecases.gateway.QuestionGateway; +import entities.Post; +import entities.Question; import javax.jdo.JDOHelper; import javax.persistence.EntityManager; import javax.persistence.EntityTransaction; @@ -10,8 +10,15 @@ import java.util.ArrayList; import java.util.List; +/** + * This class represents managing Question entities in the database. + */ public class QuestionRepo extends GenericRepository implements QuestionGateway { + /** + * Constructs a QuestionRepo instance. + * Initializes the superclass with the Question class. + */ public QuestionRepo() { super(Question.class); } @@ -169,4 +176,5 @@ public void updatePosts(int questionId, Post post) { public Question get(int id) { return (Question) super.get(id); } + } diff --git a/src/main/java/businessrule/gateway/UserGatewayFactory.java b/src/main/java/infrastructure/database/UserGatewayFactory.java similarity index 53% rename from src/main/java/businessrule/gateway/UserGatewayFactory.java rename to src/main/java/infrastructure/database/UserGatewayFactory.java index 0cf3f433..2bab0680 100644 --- a/src/main/java/businessrule/gateway/UserGatewayFactory.java +++ b/src/main/java/infrastructure/database/UserGatewayFactory.java @@ -1,12 +1,20 @@ -package businessrule.gateway; +package infrastructure.database; -import driver.database.AttorneyRepository; -import driver.database.ClientRepository; -import entity.ApplicationException; -import entity.User; +import entities.ApplicationException; +import entities.user.User; +import usecases.gateway.UserGateway; +/** + * This class represents for creating instances of UserGateway based on the user's role. + */ public class UserGatewayFactory { + /** + * Creates a UserGateway based on the user's role. + * + * @param userId The ID of the user for whom the gateway is to be created. + * @return An instance of UserGateway for the specified user. + */ public UserGateway createUserGateway(int userId) { try { UserGateway userGateway; @@ -20,6 +28,14 @@ public UserGateway createUserGateway(int userId) { throw new ApplicationException("UserId does not exist"); } } + + /** + * Checks if the given user ID belongs to a client. + * + * @param userId The ID of the user to check. + * @return True if the user is a client, false if the user is an attorney. + * @throws ApplicationException If the user ID format is invalid or if it doesn't exist. + */ private static boolean isClient(int userId) throws ApplicationException { if (Integer.toString(userId).startsWith("1")) { return false; diff --git a/src/main/java/driver/database/UserRepository.java b/src/main/java/infrastructure/database/UserRepository.java similarity index 65% rename from src/main/java/driver/database/UserRepository.java rename to src/main/java/infrastructure/database/UserRepository.java index ba5799e1..66edba61 100644 --- a/src/main/java/driver/database/UserRepository.java +++ b/src/main/java/infrastructure/database/UserRepository.java @@ -1,13 +1,23 @@ -package driver.database; +package infrastructure.database; -import businessrule.gateway.UserGateway; -import entity.Question; -import entity.User; +import usecases.gateway.UserGateway; +import entities.Question; +import entities.user.User; import javax.jdo.JDOHelper; import java.util.List; +/** + * This class represents managing User entities in the database. + * + * @param The type of User entity managed by the repository. + */ public class UserRepository extends GenericRepository implements UserGateway { + /** + * Constructs a UserRepository instance. + * + * @param entityType The class representing the type of User entity managed by the repository. + */ public UserRepository(Class entityType) { super(entityType); } @@ -16,7 +26,7 @@ public UserRepository(Class entityType) { public boolean existsByName(String inputUserName) { return executeTransactionWithResult(entityManager -> { List users = entityManager.createQuery("SELECT u FROM " + entityType.getSimpleName() + - " u WHERE u.name =: name", entityType).setParameter("name", inputUserName).getResultList(); + " u WHERE u.name = :name", entityType).setParameter("name", inputUserName).getResultList(); return !users.isEmpty(); }); } @@ -34,5 +44,4 @@ public void updateQuestionList(int id, Question question) { public User get(int id) { return (User) super.get(id); } - } diff --git a/src/main/java/driver/screen/AskQuestionUI.java b/src/main/java/infrastructure/screens/AskQuestionUI.java similarity index 91% rename from src/main/java/driver/screen/AskQuestionUI.java rename to src/main/java/infrastructure/screens/AskQuestionUI.java index 3a75a932..ed0fa517 100644 --- a/src/main/java/driver/screen/AskQuestionUI.java +++ b/src/main/java/infrastructure/screens/AskQuestionUI.java @@ -1,7 +1,7 @@ -package driver.screen; +package infrastructure.screens; -import adapter.controller.ControlContainer; -import adapter.controller.QuestionControl; +import adapters.controllers.ControlContainer; +import adapters.controllers.QuestionControl; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; @@ -16,14 +16,15 @@ import javax.swing.JPanel; import com.toedter.calendar.JDateChooser; +import infrastructure.screens.utils.UIDesign; +import infrastructure.screens.utils.UIManager; -import static driver.screen.UIDesign.*; -import static driver.screen.UIDrawer.*; +import static infrastructure.screens.utils.UIDesign.*; +import static infrastructure.screens.utils.UIDrawer.*; /** - * - * @author joseph + * This class represents the user interface for asking a new question. */ public class AskQuestionUI extends UserUI implements ActionListener { JComboBox questionType = new JComboBox<>(QUESTION_TYPE_LIST); diff --git a/src/main/java/driver/screen/AttorneyHomePageUI.java b/src/main/java/infrastructure/screens/AttorneyHomePageUI.java similarity index 79% rename from src/main/java/driver/screen/AttorneyHomePageUI.java rename to src/main/java/infrastructure/screens/AttorneyHomePageUI.java index e913e4ed..ac3342a8 100644 --- a/src/main/java/driver/screen/AttorneyHomePageUI.java +++ b/src/main/java/infrastructure/screens/AttorneyHomePageUI.java @@ -1,10 +1,9 @@ -package driver.screen; +package infrastructure.screens; -import adapter.controller.ControlContainer; -import adapter.controller.ViewQuestionControl; -import businessrule.SessionManager; -import businessrule.UIFactory; -import cn.hutool.core.net.LocalPortGenerater; +import adapters.controllers.ControlContainer; +import adapters.controllers.ViewQuestionControl; +import infrastructure.screens.utils.UIManager; +import usecases.session.SessionManager; import javax.swing.*; import java.awt.*; @@ -13,11 +12,24 @@ import java.util.ArrayList; import java.util.List; -import static driver.screen.UIDrawer.*; +import static infrastructure.screens.utils.UIDrawer.*; + + +/** + * This class represents a JPanel for the Attorney's home page user interface. + */ public class AttorneyHomePageUI extends HomePageUI implements ActionListener { static final String BROWSE_AVAILABLE_QUESTIONS_BUTTON_NAME = "Browse Available Questions"; static final String RECOMMENDED_QUESTIONS_BUTTON_NAME = "Recommended Questions"; + + /** + * Constructs an AttorneyHomePageUI instance. + * + * @param UIManager UIManager contains screens, cardlayouts, and control containers. + * @param userId The ID of the logged-in attorney user. + * @param userName The name of the logged-in attorney user. + */ public AttorneyHomePageUI(String userName, int userId, UIManager UIManager) { super(userName, userId, UIManager); diff --git a/src/main/java/driver/screen/BaseUI.java b/src/main/java/infrastructure/screens/BaseUI.java similarity index 59% rename from src/main/java/driver/screen/BaseUI.java rename to src/main/java/infrastructure/screens/BaseUI.java index 875e2ed6..6a2a5428 100644 --- a/src/main/java/driver/screen/BaseUI.java +++ b/src/main/java/infrastructure/screens/BaseUI.java @@ -1,11 +1,16 @@ -package driver.screen; +package infrastructure.screens; + +import infrastructure.screens.utils.UIManager; import javax.swing.*; import java.awt.event.ActionListener; -import static driver.screen.UIDesign.*; +import static infrastructure.screens.utils.UIDesign.*; +/** + * This absr=tract class represents the base for all other user interface. + */ public abstract class BaseUI extends JPanel implements ActionListener { - UIManager uiManager; + infrastructure.screens.utils.UIManager uiManager; public BaseUI(UIManager UIManager) { setBackgroundFrame(this); setBackground(lightGreenColor); diff --git a/src/main/java/driver/screen/ClientHomePageUI.java b/src/main/java/infrastructure/screens/ClientHomePageUI.java similarity index 79% rename from src/main/java/driver/screen/ClientHomePageUI.java rename to src/main/java/infrastructure/screens/ClientHomePageUI.java index 7b13dff7..4028acaf 100644 --- a/src/main/java/driver/screen/ClientHomePageUI.java +++ b/src/main/java/infrastructure/screens/ClientHomePageUI.java @@ -1,9 +1,8 @@ -package driver.screen; +package infrastructure.screens; -import adapter.controller.*; -import businessrule.SessionManager; -import businessrule.UIFactory; -import businessrule.UserSession; +import adapters.controllers.*; +import infrastructure.screens.utils.UIManager; +import usecases.session.SessionManager; import javax.swing.*; import java.awt.*; @@ -12,12 +11,21 @@ import java.util.ArrayList; import java.util.List; -import static driver.screen.UIDrawer.*; - - +import static infrastructure.screens.utils.UIDrawer.*; +/** + * This class represents a JPanel for the Client's home page user interface. + */ public class ClientHomePageUI extends HomePageUI implements ActionListener { static final String ASK_NEW_QUESTION_BUTTON_NAME = "Ask new question"; + + /** + * Constructs a new ClientHomePageUI instance. + * + * @param UIManager UIManager contains screens, cardlayouts, and control containers. + * @param userId The ID of the logged-in client user. + * @param userName The name of the logged-in client user. + */ public ClientHomePageUI(String userName, int userId, UIManager UIManager) { super(userName, userId, UIManager); diff --git a/src/main/java/driver/screen/HomePageUI.java b/src/main/java/infrastructure/screens/HomePageUI.java similarity index 71% rename from src/main/java/driver/screen/HomePageUI.java rename to src/main/java/infrastructure/screens/HomePageUI.java index 1c1e68c5..405e503a 100644 --- a/src/main/java/driver/screen/HomePageUI.java +++ b/src/main/java/infrastructure/screens/HomePageUI.java @@ -1,10 +1,15 @@ -package driver.screen; +package infrastructure.screens; + +import infrastructure.screens.utils.UIManager; import javax.swing.*; import java.awt.*; -import static driver.screen.UIDesign.setTitleFont; +import static infrastructure.screens.utils.UIDesign.setTitleFont; +/** + * This abstract class represents a base for all home page user interface. + */ public abstract class HomePageUI extends UserUI { protected JLabel title; static final String TITLE_NAME = "Home"; diff --git a/src/main/java/driver/screen/LoginUI.java b/src/main/java/infrastructure/screens/LoginUI.java similarity index 87% rename from src/main/java/driver/screen/LoginUI.java rename to src/main/java/infrastructure/screens/LoginUI.java index d651480a..1494c0e7 100644 --- a/src/main/java/driver/screen/LoginUI.java +++ b/src/main/java/infrastructure/screens/LoginUI.java @@ -1,7 +1,8 @@ -package driver.screen; +package infrastructure.screens; -import adapter.controller.ControlContainer; -import adapter.controller.UserLoginControl; +import adapters.controllers.ControlContainer; +import adapters.controllers.UserLoginControl; +import infrastructure.screens.utils.UIManager; import javax.swing.*; import java.awt.*; @@ -10,13 +11,12 @@ import java.util.ArrayList; import java.util.List; -import static driver.screen.UIDesign.*; -import static driver.screen.UIDrawer.*; +import static infrastructure.screens.utils.UIDesign.*; +import static infrastructure.screens.utils.UIDrawer.*; /** - * - * @author kaxi + * This class represents a user interface for logging in as both a client and an attorney. */ public class LoginUI extends BaseUI implements ActionListener { JTextField userId = new JTextField(15); diff --git a/src/main/java/driver/screen/QuestionCloseUI.java b/src/main/java/infrastructure/screens/QuestionCloseUI.java similarity index 73% rename from src/main/java/driver/screen/QuestionCloseUI.java rename to src/main/java/infrastructure/screens/QuestionCloseUI.java index b4e6aa13..8b6590ec 100644 --- a/src/main/java/driver/screen/QuestionCloseUI.java +++ b/src/main/java/infrastructure/screens/QuestionCloseUI.java @@ -1,18 +1,21 @@ -package driver.screen; -import adapter.controller.ControlContainer; -import businessrule.usecase.util.PostDisplayFormatter; +package infrastructure.screens; +import adapters.controllers.ControlContainer; +import infrastructure.screens.utils.UIManager; +import usecases.dto.PostDisplay; import javax.swing.*; import java.awt.*; import java.time.LocalDate; import java.util.Map; -import static driver.screen.UIDrawer.*; - +import static infrastructure.screens.utils.UIDrawer.*; +/** + * This class provides a UI for client to view the question that was closed. + */ public class QuestionCloseUI extends QuestionUI{ public QuestionCloseUI(String userName, int userId, UIManager UIManager, int questionId, String title, - String questionType, LocalDate deadline, Map postMap) { + String questionType, LocalDate deadline, Map postMap) { super(userName, userId, UIManager, questionId, title, questionType, deadline, postMap); //Spacers diff --git a/src/main/java/driver/screen/QuestionListUI.java b/src/main/java/infrastructure/screens/QuestionListUI.java similarity index 67% rename from src/main/java/driver/screen/QuestionListUI.java rename to src/main/java/infrastructure/screens/QuestionListUI.java index 05e11ae2..cfe32f6e 100644 --- a/src/main/java/driver/screen/QuestionListUI.java +++ b/src/main/java/infrastructure/screens/QuestionListUI.java @@ -1,7 +1,9 @@ -package driver.screen; -import adapter.controller.ControlContainer; -import adapter.controller.SelectQuestionControl; -import businessrule.usecase.util.QuestionDisplayFormatter; +package infrastructure.screens; +import adapters.controllers.ControlContainer; +import adapters.controllers.SelectQuestionControl; +import infrastructure.screens.utils.UIDesign; +import infrastructure.screens.utils.UIManager; +import usecases.dto.QuestionDisplay; import javax.swing.*; import java.awt.*; @@ -10,16 +12,26 @@ import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.Map; +import static infrastructure.screens.utils.UIDesign.*; +import static infrastructure.screens.utils.UIDrawer.*; -import static driver.screen.UIDesign.*; -import static driver.screen.UIDrawer.*; - +/** + * This class represents a JPanel for a list of questions displayed in the user interface. + */ public class QuestionListUI extends UserUI implements ActionListener { - Map questionMap; + Map questionMap; static final String HOME_PAGE_BUTTON_NAME = "Home Page"; + /** + * Constructs a new QuestionListUI instance. + * + * @param userId The ID of the logged-in user. + * @param userName The name of the logged-in user. + * @param UIManager A UIManager class that contains screen, cardlayout, and control container + * @param questionMap A map of question IDs to their display formatters. + */ public QuestionListUI(String userName, int userId, UIManager UIManager, - Map questionMap) { + Map questionMap) { super(userName, userId, UIManager); this.questionMap = questionMap; @@ -34,7 +46,7 @@ public QuestionListUI(String userName, int userId, UIManager UIManager, questionScrollPanel.setLayout(new BoxLayout(questionScrollPanel, BoxLayout.Y_AXIS)); for (Integer questionId : questionMap.keySet()) { //read all variables from displayFormatter - QuestionDisplayFormatter question = questionMap.get(questionId); + QuestionDisplay question = questionMap.get(questionId); String title = question.getTitle(); String type = question.getType(); @@ -48,14 +60,11 @@ public QuestionListUI(String userName, int userId, UIManager UIManager, JButton questionButton = new JButton(); UIDesign.setSizeInLayout(questionButton, new Dimension(340, 60)); questionButton.add(questionPanel); - questionButton.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - System.out.println("Question selected"); - ControlContainer controlContainer = UIManager.getControlContainer(); - SelectQuestionControl selectQuestionControl = controlContainer.getSelectQuestionControl(); - selectQuestionControl.selectQuestion(questionId); - } + questionButton.addActionListener(e -> { + System.out.println("Question selected"); + ControlContainer controlContainer = UIManager.getControlContainer(); + SelectQuestionControl selectQuestionControl = controlContainer.getSelectQuestionControl(); + selectQuestionControl.selectQuestion(questionId); }); questionScrollPanel.add(questionButton); } diff --git a/src/main/java/driver/screen/QuestionNewUI.java b/src/main/java/infrastructure/screens/QuestionNewUI.java similarity index 70% rename from src/main/java/driver/screen/QuestionNewUI.java rename to src/main/java/infrastructure/screens/QuestionNewUI.java index 4ace4157..3dd1cd85 100644 --- a/src/main/java/driver/screen/QuestionNewUI.java +++ b/src/main/java/infrastructure/screens/QuestionNewUI.java @@ -1,19 +1,23 @@ -package driver.screen; +package infrastructure.screens; -import adapter.controller.ControlContainer; -import adapter.controller.PostControl; -import businessrule.usecase.util.PostDisplayFormatter; -import entity.ApplicationException; +import adapters.controllers.ControlContainer; +import adapters.controllers.PostControl; +import infrastructure.screens.utils.UIManager; +import usecases.dto.PostDisplay; +import entities.ApplicationException; import javax.swing.*; import java.awt.*; import java.time.LocalDate; import java.util.Map; -import static driver.screen.UIDrawer.addSpacer; +import static infrastructure.screens.utils.UIDrawer.addSpacer; +/** + * This class provides a UI for users to compose the initial content of a newly initiated question. + */ public class QuestionNewUI extends QuestionUI{ - public QuestionNewUI(String userName, int userId, UIManager UIManager, int questionId, String title, String questionType, LocalDate deadline, Map postMap) { + public QuestionNewUI(String userName, int userId, UIManager UIManager, int questionId, String title, String questionType, LocalDate deadline, Map postMap) { super(userName, userId, UIManager, questionId, title, questionType, deadline, postMap); JPanel spacer = addSpacer(20); diff --git a/src/main/java/driver/screen/QuestionOpenAttorneyUI.java b/src/main/java/infrastructure/screens/QuestionOpenAttorneyUI.java similarity index 67% rename from src/main/java/driver/screen/QuestionOpenAttorneyUI.java rename to src/main/java/infrastructure/screens/QuestionOpenAttorneyUI.java index b7d9eb87..d30c1aed 100644 --- a/src/main/java/driver/screen/QuestionOpenAttorneyUI.java +++ b/src/main/java/infrastructure/screens/QuestionOpenAttorneyUI.java @@ -1,21 +1,21 @@ -package driver.screen; +package infrastructure.screens; -import adapter.controller.ControlContainer; -import adapter.controller.PostControl; -import businessrule.usecase.util.PostDisplayFormatter; -import entity.ApplicationException; +import infrastructure.screens.utils.UIManager; +import usecases.dto.PostDisplay; import javax.swing.*; -import java.awt.*; import java.time.LocalDate; import java.util.Map; -import static driver.screen.UIDrawer.*; +import static infrastructure.screens.utils.UIDrawer.*; +/** + * This class provides a UI for attorney to post their answer for a specific question. + */ public class QuestionOpenAttorneyUI extends QuestionNewUI { public QuestionOpenAttorneyUI(String userName, int userId, UIManager UIManager, int questionId, String title, - String questionType, LocalDate deadline, Map postMap) { + String questionType, LocalDate deadline, Map postMap) { super(userName, userId, UIManager, questionId, title, questionType, deadline, postMap); //spacers diff --git a/src/main/java/driver/screen/QuestionOpenClientUI.java b/src/main/java/infrastructure/screens/QuestionOpenClientUI.java similarity index 78% rename from src/main/java/driver/screen/QuestionOpenClientUI.java rename to src/main/java/infrastructure/screens/QuestionOpenClientUI.java index 9e53d306..b12b050d 100644 --- a/src/main/java/driver/screen/QuestionOpenClientUI.java +++ b/src/main/java/infrastructure/screens/QuestionOpenClientUI.java @@ -1,21 +1,25 @@ -package driver.screen; +package infrastructure.screens; -import adapter.controller.CloseQuestionControl; -import adapter.controller.ControlContainer; -import businessrule.usecase.util.PostDisplayFormatter; -import entity.ApplicationException; +import adapters.controllers.CloseQuestionControl; +import adapters.controllers.ControlContainer; +import infrastructure.screens.utils.UIManager; +import usecases.dto.PostDisplay; +import entities.ApplicationException; import javax.swing.*; import java.awt.*; import java.time.LocalDate; import java.util.Map; -import static driver.screen.UIDrawer.*; +import static infrastructure.screens.utils.UIDrawer.*; +/** + * This class provides a UI for client to their follow-up. + */ public class QuestionOpenClientUI extends QuestionOpenAttorneyUI{ public QuestionOpenClientUI(String userName, int userId, UIManager UIManager, int questionId, String title, - String questionType, LocalDate deadline, Map postMap) { + String questionType, LocalDate deadline, Map postMap) { super(userName, userId ,UIManager,questionId, title, questionType, deadline, postMap); //The spacer diff --git a/src/main/java/driver/screen/QuestionUI.java b/src/main/java/infrastructure/screens/QuestionUI.java similarity index 93% rename from src/main/java/driver/screen/QuestionUI.java rename to src/main/java/infrastructure/screens/QuestionUI.java index 00f42d19..d0485698 100644 --- a/src/main/java/driver/screen/QuestionUI.java +++ b/src/main/java/infrastructure/screens/QuestionUI.java @@ -1,6 +1,7 @@ -package driver.screen; -import adapter.controller.ControlContainer; -import businessrule.usecase.util.PostDisplayFormatter; +package infrastructure.screens; +import adapters.controllers.ControlContainer; +import infrastructure.screens.utils.UIManager; +import usecases.dto.PostDisplay; import javax.swing.*; import javax.swing.event.DocumentEvent; @@ -15,9 +16,12 @@ import java.util.List; import java.util.Map; -import static driver.screen.UIDesign.*; -import static driver.screen.UIDrawer.*; +import static infrastructure.screens.utils.UIDesign.*; +import static infrastructure.screens.utils.UIDrawer.*; +/** + * This abstract class represents a base for all question-specific user interface. + */ public abstract class QuestionUI extends UserUI { int questionId; JPanel questionTitle = new JPanel(); @@ -35,7 +39,7 @@ public abstract class QuestionUI extends UserUI { static final String CLOSE_BUTTON_NAME = "Close Question"; public QuestionUI(String userName, int userId, UIManager UIManager, int questionId, String title, - String questionType, LocalDate deadline, Map postMap) { + String questionType, LocalDate deadline, Map postMap) { super(userName, userId, UIManager); this.questionId = questionId; @@ -66,7 +70,7 @@ public QuestionUI(String userName, int userId, UIManager UIManager, int question //Set post scroll pane JPanel postScrollPanel = new JPanel(); postScrollPanel.setLayout(new BoxLayout(postScrollPanel, BoxLayout.Y_AXIS)); - for (PostDisplayFormatter post : postMap.values()) { + for (PostDisplay post : postMap.values()) { //read all variables from displayFormatter String name = post.getName(); String postText = post.getPostText(); diff --git a/src/main/java/driver/screen/RatePanel.java b/src/main/java/infrastructure/screens/RatePanel.java similarity index 90% rename from src/main/java/driver/screen/RatePanel.java rename to src/main/java/infrastructure/screens/RatePanel.java index 438941fd..d6e2555b 100644 --- a/src/main/java/driver/screen/RatePanel.java +++ b/src/main/java/infrastructure/screens/RatePanel.java @@ -1,7 +1,8 @@ -package driver.screen; +package infrastructure.screens; -import adapter.controller.ControlContainer; -import adapter.controller.RateControl; +import adapters.controllers.ControlContainer; +import adapters.controllers.RateControl; +import infrastructure.screens.utils.UIDesign; import javax.swing.*; import java.awt.*; @@ -9,8 +10,11 @@ import java.awt.event.ActionListener; import java.util.Objects; -import static driver.screen.UIDesign.*; +import static infrastructure.screens.utils.UIDesign.*; +/** + * This class provides a UI for client to rate their experience. + */ public class RatePanel extends JPanel implements ActionListener { ControlContainer controlContainer; JFrame rateFrame; @@ -29,8 +33,6 @@ public RatePanel(ControlContainer controlContainer, JFrame rateFrame, int userId dropdownContainer.setBackground(UIDesign.whiteColor); UIDesign.setSizeInLayout(dropdownContainer, new Dimension(350, 50)); - String[] rateList = {"Satisfied", "Not satisfied"}; - UIDesign.setSizeInLayout(rate, new Dimension(250, 40)); rate.setRenderer(new MyComboBoxRenderer()); diff --git a/src/main/java/driver/screen/RegisterUI.java b/src/main/java/infrastructure/screens/RegisterUI.java similarity index 79% rename from src/main/java/driver/screen/RegisterUI.java rename to src/main/java/infrastructure/screens/RegisterUI.java index 4370d819..b2c08e42 100644 --- a/src/main/java/driver/screen/RegisterUI.java +++ b/src/main/java/infrastructure/screens/RegisterUI.java @@ -1,7 +1,10 @@ -package driver.screen; +package infrastructure.screens; -import adapter.controller.ClientRegisterControl; -import adapter.controller.ControlContainer; +import adapters.controllers.RegisterControl; +import adapters.controllers.ControlContainer; +import infrastructure.screens.utils.UIManager; +import usecases.requests.ClientRegistrationData; +import usecases.requests.RegistrationData; import javax.swing.*; import java.awt.*; @@ -9,9 +12,12 @@ import java.util.ArrayList; import java.util.List; -import static driver.screen.UIDrawer.*; -import static driver.screen.UIDesign.*; +import static infrastructure.screens.utils.UIDrawer.*; +import static infrastructure.screens.utils.UIDesign.*; +/** + * This class represents a JPanel for the user registration interface. + */ public class RegisterUI extends BaseUI{ JTextField userName = new JTextField(15); @@ -49,6 +55,11 @@ public class RegisterUI extends BaseUI{ static final String GENDER_PROMPT = "Gender"; static final String MARITAL_STATUS_PROMPT = "Marital Status"; + /** + * Constructs a new RegisterUI instance. + * + * @param UIManager UIManager class contains screens, cadlayouts, and control container + */ public RegisterUI(UIManager UIManager) { super(UIManager); @@ -138,15 +149,20 @@ public void actionPerformed(ActionEvent e) { ControlContainer controlContainer = uiManager.getControlContainer(); JPanel screens = uiManager.getScreens(); CardLayout cardLayout = uiManager.getCardLayout(); - ClientRegisterControl clientRegisterControl = controlContainer.getClientRegisterControl(); + RegisterControl registerControl = controlContainer.getClientRegisterControl(); switch (actionCommand) { case REGISTER_BUTTON_NAME: try { - clientRegisterControl.create(userName.getText(), email.getText(), String.valueOf(password1.getPassword()), - String.valueOf(password2.getPassword()), stateAbb.getText(), postalCode.getText(), - (String) ethnicity.getSelectedItem(), Integer.parseInt(age.getText()), (String) gender.getSelectedItem(), - (String) maritalStatus.getSelectedItem(), Integer.parseInt(numberOfHousehold.getText()), - Float.parseFloat(annualIncome.getText())); + RegistrationData registrationData = new RegistrationData(userName.getText(), email.getText(), String.valueOf(password1.getPassword()), String.valueOf(password2.getPassword()), stateAbb.getText(), postalCode.getText()); + ClientRegistrationData clientRegistrationData = new ClientRegistrationData.Builder(registrationData) + .ethnicity((String) ethnicity.getSelectedItem()) + .age(Integer.parseInt(age.getText())) + .gender((String) gender.getSelectedItem()) + .maritalStatus((String) maritalStatus.getSelectedItem()) + .numberOfHousehold(Integer.parseInt(numberOfHousehold.getText())) + .annualIncome(Float.parseFloat(annualIncome.getText())) + .build(); + registerControl.create(clientRegistrationData); } catch (Exception ex) { JOptionPane.showMessageDialog(this, ex.getMessage()); } @@ -157,4 +173,4 @@ public void actionPerformed(ActionEvent e) { break; } } -} \ No newline at end of file +} diff --git a/src/main/java/businessrule/UIFactory.java b/src/main/java/infrastructure/screens/UIFactory.java similarity index 84% rename from src/main/java/businessrule/UIFactory.java rename to src/main/java/infrastructure/screens/UIFactory.java index 42dc834c..26a0e8c2 100644 --- a/src/main/java/businessrule/UIFactory.java +++ b/src/main/java/infrastructure/screens/UIFactory.java @@ -1,14 +1,17 @@ -package businessrule; - -import businessrule.responsemodel.BaseResponseModel; -import businessrule.responsemodel.TheQuestionResponseModel; -import businessrule.responsemodel.UserResponseModel; -import businessrule.responsemodel.ViewResponseModel; -import businessrule.usecase.util.PostDisplayFormatter; -import driver.screen.*; +package infrastructure.screens; + +import infrastructure.screens.utils.UIManager; +import usecases.responses.BaseResponseModel; +import usecases.responses.TheQuestionResponseModel; +import usecases.responses.UserResponseModel; +import usecases.responses.ViewResponseModel; +import usecases.dto.PostDisplay; import java.util.HashMap; import java.util.Map; +/** + * This class represents a factory that create UI based on specific needs and situations. + */ public class UIFactory { private static final String CLIENT_TYPE = "Client"; private static final String ATTORNEY_TYPE = "Attorney"; @@ -24,6 +27,14 @@ public enum UIType { QUESTION_UI } + /** + * + * @param uiType type of the target UI. + * @param uiManager UIManager that contains screens, cardlayouts, and control containers. + * @param userResponseModel a response model contains basic user information. + * @return the specific UI needed. + * @throws IllegalArgumentException when the UI type is invalid. + */ private static UserUI getUserUI(UIType uiType, UIManager uiManager, UserResponseModel userResponseModel) throws IllegalArgumentException { int userId = userResponseModel.getUserId(); String userName = userResponseModel.getUserName(); @@ -52,8 +63,8 @@ private static QuestionListUI getQuestionListUI(UIManager uiManager, ViewRespons private static QuestionUI getQuestionUI(UIManager uiManager, TheQuestionResponseModel questionResponseModel) { String userType = questionResponseModel.getUserType(); - Boolean isClose = questionResponseModel.isClose(); - Map postMap = questionResponseModel.getPostMap(); + boolean isClose = questionResponseModel.isClose(); + Map postMap = questionResponseModel.getPostMap(); if (isClose) { return new QuestionCloseUI(questionResponseModel.getUserName(), questionResponseModel.getUserId(), uiManager, questionResponseModel.getQuestionId(), questionResponseModel.getTitle(), questionResponseModel.getType(), diff --git a/src/main/java/driver/screen/UserUI.java b/src/main/java/infrastructure/screens/UserUI.java similarity index 65% rename from src/main/java/driver/screen/UserUI.java rename to src/main/java/infrastructure/screens/UserUI.java index c6f8af63..57892e69 100644 --- a/src/main/java/driver/screen/UserUI.java +++ b/src/main/java/infrastructure/screens/UserUI.java @@ -1,9 +1,14 @@ -package driver.screen; +package infrastructure.screens; + +import infrastructure.screens.utils.UIManager; import javax.swing.*; -import static driver.screen.UIDesign.*; -import static driver.screen.UIDrawer.*; +import static infrastructure.screens.utils.UIDrawer.*; + +/** + * This abstract class serve as the base for all user-specific UI. + */ public abstract class UserUI extends BaseUI{ protected String userName; protected int userId; diff --git a/src/main/java/driver/screen/WelcomeUI.java b/src/main/java/infrastructure/screens/WelcomeUI.java similarity index 78% rename from src/main/java/driver/screen/WelcomeUI.java rename to src/main/java/infrastructure/screens/WelcomeUI.java index 62714d56..cf18ab7d 100644 --- a/src/main/java/driver/screen/WelcomeUI.java +++ b/src/main/java/infrastructure/screens/WelcomeUI.java @@ -1,6 +1,6 @@ -package driver.screen; +package infrastructure.screens; -import adapter.controller.ControlContainer; +import infrastructure.screens.utils.UIManager; import javax.swing.*; import java.awt.*; @@ -9,14 +9,23 @@ import java.util.ArrayList; import java.util.List; -import static driver.screen.UIDesign.*; -import static driver.screen.UIDrawer.*; +import static infrastructure.screens.utils.UIDesign.*; +import static infrastructure.screens.utils.UIDrawer.*; +/** + * This class represents the initial screen of the application where users can choose to register or log in. + */ public class WelcomeUI extends BaseUI implements ActionListener { static final String TITLE_TEXT = "WELCOME"; static final String REGISTER_BUTTON_NAME = "Register"; static final String LOGIN_BUTTON_NAME = "Login"; + /** + * Constructs a WelcomeUI instance with the provided control container, + * card layout, and screens panel. + * + * @param UIManager UIManager that contains screens, cardlayouts, and control container + */ public WelcomeUI(UIManager UIManager) { super(UIManager); diff --git a/src/main/java/driver/screen/UIDesign.java b/src/main/java/infrastructure/screens/utils/UIDesign.java similarity index 94% rename from src/main/java/driver/screen/UIDesign.java rename to src/main/java/infrastructure/screens/utils/UIDesign.java index 00a35653..fbaa5fd2 100644 --- a/src/main/java/driver/screen/UIDesign.java +++ b/src/main/java/infrastructure/screens/utils/UIDesign.java @@ -1,11 +1,11 @@ -package driver.screen; +package infrastructure.screens.utils; import javax.swing.*; -import javax.swing.border.EmptyBorder; -import javax.swing.border.LineBorder; import java.awt.*; -import java.util.concurrent.Flow; +/** + * This class represents a consistent design settings and helper methods for UI components. + */ public class UIDesign { //All colors used in the UI public static Color lightGreenColor = new Color(202,216,209); diff --git a/src/main/java/driver/screen/UIDrawer.java b/src/main/java/infrastructure/screens/utils/UIDrawer.java similarity index 98% rename from src/main/java/driver/screen/UIDrawer.java rename to src/main/java/infrastructure/screens/utils/UIDrawer.java index 700187f2..d08393a9 100644 --- a/src/main/java/driver/screen/UIDrawer.java +++ b/src/main/java/infrastructure/screens/utils/UIDrawer.java @@ -1,11 +1,11 @@ -package driver.screen; +package infrastructure.screens.utils; import com.toedter.calendar.JDateChooser; import javax.swing.*; import javax.swing.border.LineBorder; import java.awt.*; -import static driver.screen.UIDesign.*; +import static infrastructure.screens.utils.UIDesign.*; import java.awt.event.ActionListener; import java.util.List; @@ -250,7 +250,7 @@ public static JPanel questionTitleDrawer(String questionType, String title){ return titlePanel; } - protected static JPanel replyAreaDrawer(JTextArea inputPostArea, String buttonName, ActionListener parent) { + public static JPanel replyAreaDrawer(JTextArea inputPostArea, String buttonName, ActionListener parent) { inputPostArea.setLineWrap(true); inputPostArea.setWrapStyleWord(true); diff --git a/src/main/java/driver/screen/UIManager.java b/src/main/java/infrastructure/screens/utils/UIManager.java similarity index 88% rename from src/main/java/driver/screen/UIManager.java rename to src/main/java/infrastructure/screens/utils/UIManager.java index f55a54a6..002ff566 100644 --- a/src/main/java/driver/screen/UIManager.java +++ b/src/main/java/infrastructure/screens/utils/UIManager.java @@ -1,6 +1,6 @@ -package driver.screen; +package infrastructure.screens.utils; -import adapter.controller.ControlContainer; +import adapters.controllers.ControlContainer; import javax.swing.*; import java.awt.*; diff --git a/src/main/java/usecases/dto/Matching.java b/src/main/java/usecases/dto/Matching.java new file mode 100644 index 00000000..553f651c --- /dev/null +++ b/src/main/java/usecases/dto/Matching.java @@ -0,0 +1,29 @@ +package usecases.dto; + +/** + * This class represents a matching between a question and an attorney. + * It stores information about the question ID and the attorney ID that are matched. + */ +public class Matching { + private final int questionId; + private final int attorneyId; + + /** + * Constructs a new `Matching` object with the specified question ID and attorney ID. + * + * @param questionId The ID of the question to be matched. + * @param attorneyId The ID of the attorney to be matched. + */ + public Matching(int questionId, int attorneyId) { + this.questionId = questionId; + this.attorneyId = attorneyId; + } + + public int getQuestionId() { + return questionId; + } + + public int getAttorneyId() { + return attorneyId; + } +} diff --git a/src/main/java/businessrule/usecase/util/MatchingResult.java b/src/main/java/usecases/dto/MatchingResult.java similarity index 58% rename from src/main/java/businessrule/usecase/util/MatchingResult.java rename to src/main/java/usecases/dto/MatchingResult.java index c03be3b2..b66d12a5 100644 --- a/src/main/java/businessrule/usecase/util/MatchingResult.java +++ b/src/main/java/usecases/dto/MatchingResult.java @@ -1,7 +1,11 @@ -package businessrule.usecase.util; +package usecases.dto; import java.util.List; +/** + * This class represents the result of the matching process. + * It contains a list of `Matching` objects that represent the matches between questions and attorneys. + */ public class MatchingResult { private final List matchingResult; diff --git a/src/main/java/usecases/dto/PostDisplay.java b/src/main/java/usecases/dto/PostDisplay.java new file mode 100644 index 00000000..95c90119 --- /dev/null +++ b/src/main/java/usecases/dto/PostDisplay.java @@ -0,0 +1,66 @@ +package usecases.dto; + +import java.time.LocalDate; + +/** + * This class represents the formatted display information for a post. + * It contains attributes such as the post text, whether it's from a client, the name of the poster, + * and the creation date. + */ +public class PostDisplay { + private final String postText; + private final String userType; + private final String name; + private final LocalDate createAt; + + /** + * Constructs a `PostDisplayFormatter` object with the specified attributes. + * + * @param postText The text content of the post. + * @param userType Indicates the user type. + * @param name The name of the poster. + * @param createAt The creation date of the post. + */ + public PostDisplay(String postText, String userType, String name, LocalDate createAt) { + this.postText = postText; + this.userType = userType; + this.name = name; + this.createAt = createAt; + } + + /** + * Gets the text content of the post. + * + * @return The post text. + */ + public String getPostText() { + return postText; + } + + /** + * Checks the user type of the post from. + * + * @return `true` if the post is from a client, otherwise `false`. + */ + public String getUserType() { + return userType; + } + + /** + * Gets the name of the poster. + * + * @return The name of the poster. + */ + public String getName() { + return name; + } + + /** + * Gets the creation date of the post. + * + * @return The creation date. + */ + public LocalDate getCreateAt() { + return createAt; + } +} diff --git a/src/main/java/usecases/dto/QuestionDisplay.java b/src/main/java/usecases/dto/QuestionDisplay.java new file mode 100644 index 00000000..d48ba6fc --- /dev/null +++ b/src/main/java/usecases/dto/QuestionDisplay.java @@ -0,0 +1,65 @@ +package usecases.dto; + +import java.time.LocalDate; + +/** + * This class represents the formatted display information for a question. + * It encapsulates attributes such as the question title, type, legal deadline, and whether the question is closed. + */ +public class QuestionDisplay { + private final String title; + private final String type; + private final LocalDate legalDeadline; + private final boolean isClose; + + /** + * Constructs a `QuestionDisplayFormatter` object with the specified attributes. + * + * @param title The title of the question. + * @param type The type of the question. + * @param legalDeadline The legal deadline for the question. + * @param isClose Indicates if the question is closed. + */ + public QuestionDisplay(String title, String type, LocalDate legalDeadline, boolean isClose) { + this.title = title; + this.type = type; + this.legalDeadline = legalDeadline; + this.isClose = isClose; + } + + /** + * Gets the title of the question. + * + * @return The question title. + */ + public String getTitle() { + return title; + } + + /** + * Gets the type of the question. + * + * @return The question type. + */ + public String getType() { + return type; + } + + /** + * Gets the legal deadline for the question. + * + * @return The legal deadline. + */ + public LocalDate getLegalDeadline() { + return legalDeadline; + } + + /** + * Checks if the question is closed. + * + * @return `true` if the question is closed, otherwise `false`. + */ + public boolean isClose() { + return isClose; + } +} \ No newline at end of file diff --git a/src/main/java/usecases/gateway/AttorneyGateway.java b/src/main/java/usecases/gateway/AttorneyGateway.java new file mode 100644 index 00000000..d8768096 --- /dev/null +++ b/src/main/java/usecases/gateway/AttorneyGateway.java @@ -0,0 +1,34 @@ +package usecases.gateway; + +import entities.user.Attorney; +import entities.Question; + +/** + * This interface provides methods for retrieving an attorney, clearing recommendations, and + * adding recommendations for questions. + */ +public interface AttorneyGateway extends UserGateway { + + /** + * Get an attorney by their ID. + * + * @param id The ID of the attorney to retrieve. + * @return The attorney object associated with the provided ID. + */ + @Override + Attorney get(int id); + + /** + * Clear all recommendations associated with attorneys. + */ + void clearAllRecommendations(); + + + /** + * Add a recommendation for a question to an attorney. + * + * @param userId The ID of the user (attorney) receiving the recommendation. + * @param question The question to be recommended. + */ + void addRecommendation(int userId, Question question); +} diff --git a/src/main/java/usecases/gateway/ClientGateway.java b/src/main/java/usecases/gateway/ClientGateway.java new file mode 100644 index 00000000..3376c74b --- /dev/null +++ b/src/main/java/usecases/gateway/ClientGateway.java @@ -0,0 +1,18 @@ +package usecases.gateway; + +import entities.user.Client; + +/** + * This interface provides a method for retrieving a client by their ID. + */ +public interface ClientGateway extends UserGateway { + + /** + * Get a client by their ID. + * + * @param id The ID of the client to retrieve. + * @return The client object associated with the provided ID. + */ + @Override + Client get(int id); +} diff --git a/src/main/java/usecases/gateway/GenericGateway.java b/src/main/java/usecases/gateway/GenericGateway.java new file mode 100644 index 00000000..141776c6 --- /dev/null +++ b/src/main/java/usecases/gateway/GenericGateway.java @@ -0,0 +1,53 @@ +package usecases.gateway; + +import java.util.List; + +/** + * This interface provides a generic gateway for accessing data and performing common operations. + * + * @param The type of entity that this gateway will work with. + */ +public interface GenericGateway { + + /** + * Save an entity to the data source. + * + * @param entity The entity to be saved. + */ + void save(T entity); + + /** + * Get an entity by its ID. + * + * @param id The ID of the entity to retrieve. + * @return The entity associated with the provided ID. + */ + Object get(int id); + + /** + * Check if an entity with a given ID exists. + * + * @param id The ID of the entity to check. + * @return True if an entity with the provided ID exists, false otherwise. + */ + boolean existsById(int id); + + /** + * Delete an entity by its ID. + * + * @param id The ID of the entity to delete. + */ + void delete(int id); + + /** + * Delete all entities. + */ + void deleteAll(); + + /** + * Get a list of all entities. + * + * @return A list containing all entities. + */ + List getAll(); +} \ No newline at end of file diff --git a/src/main/java/usecases/gateway/PostGateway.java b/src/main/java/usecases/gateway/PostGateway.java new file mode 100644 index 00000000..4a519b8a --- /dev/null +++ b/src/main/java/usecases/gateway/PostGateway.java @@ -0,0 +1,19 @@ +package usecases.gateway; + +import entities.Post; + +/** + * This interface provides a method for retrieving a post by its ID. + */ +public interface PostGateway extends GenericGateway { + + /** + * Get a post by its ID. + * + * @param id The ID of the post to retrieve. + * @return The post object associated with the provided ID. + */ + @Override + Post get(int id); + +} \ No newline at end of file diff --git a/src/main/java/usecases/gateway/QuestionGateway.java b/src/main/java/usecases/gateway/QuestionGateway.java new file mode 100644 index 00000000..b084866a --- /dev/null +++ b/src/main/java/usecases/gateway/QuestionGateway.java @@ -0,0 +1,92 @@ +package usecases.gateway; + +import entities.Post; +import entities.Question; +import java.time.LocalDate; +import java.util.List; + +/** + * This interface provides methods for retrieving questions, managing question status and attributes, + * and obtaining posts. + */ +public interface QuestionGateway extends GenericGateway { + + /** + * Get a list of questions that have not been taken by an attorney. + * + * @return A list of questions not yet taken by any attorney. + */ + List getNotTakenQuestion(); + + /** + * Get a list of questions that are not closed. + * + * @return A list of questions that are still open and not closed. + */ + List getNotClosedQuestion(); + + /** + * Get all posts associated with a specific question. + * + * @param questionId The ID of the question to retrieve posts for. + * @return A list of posts associated with the specified question. + */ + List getAllPostOfQuestion(int questionId); + + /** + * Update the "isTaken" status of a question. + * + * @param questionId The ID of the question to update. + * @param isTaken The new "isTaken" status. + */ + void updateIsTaken(int questionId, boolean isTaken); + + /** + * Update the attorney who took a specific question. + * + * @param questionId The ID of the question to update. + * @param attorneyId The ID of the attorney who took the question. + */ + void updateTakenByAttorney(int questionId, int attorneyId); + + /** + * Update the "isClose" status of a question. + * + * @param questionId The ID of the question to update. + * @param isClose The new "isClose" status. + */ + void updateIsClose(int questionId, boolean isClose); + + /** + * Update the rating of a question. + * + * @param questionId The ID of the question to update. + * @param rating The new rating value. + */ + void updateRating(int questionId, int rating); + + /** + * Update the time when a question was taken. + * + * @param questionId The ID of the question to update. + * @param time The new time when the question was taken. + */ + void updateTakenAt(int questionId, LocalDate time); + + /** + * Update the posts associated with a question. + * + * @param id The ID of the question to update. + * @param post The post to add to the question's posts. + */ + void updatePosts(int id, Post post); + + /** + * Get a question by its ID. + * + * @param id The ID of the question to retrieve. + * @return The question object associated with the provided ID. + */ + @Override + Question get(int id); +} diff --git a/src/main/java/usecases/gateway/UserGateway.java b/src/main/java/usecases/gateway/UserGateway.java new file mode 100644 index 00000000..84b57da9 --- /dev/null +++ b/src/main/java/usecases/gateway/UserGateway.java @@ -0,0 +1,38 @@ +package usecases.gateway; + +import entities.Question; +import entities.user.User; + +/** + * This interface provides methods for checking if a user exists by name, updating a user's question list, + * and retrieving a user by their ID. + * + * @param The type of user entity. + */ +public interface UserGateway extends GenericGateway { + + /** + * Check if a user with the specified name exists. + * + * @param inputUserName The name of the user to check. + * @return True if a user with the provided name exists, false otherwise. + */ + boolean existsByName(String inputUserName); + + /** + * Update the question list of a user. + * + * @param id The ID of the user to update. + * @param question The question to add to the user's question list. + */ + void updateQuestionList(int id, Question question); + + /** + * Get a user by their ID. + * + * @param id The ID of the user to retrieve. + * @return The user object associated with the provided ID. + */ + @Override + User get(int id); +} \ No newline at end of file diff --git a/src/main/java/usecases/inputboundary/CloseInputBoundary.java b/src/main/java/usecases/inputboundary/CloseInputBoundary.java new file mode 100644 index 00000000..ee98d492 --- /dev/null +++ b/src/main/java/usecases/inputboundary/CloseInputBoundary.java @@ -0,0 +1,18 @@ +package usecases.inputboundary; + +import usecases.requests.CloseRequestModel; +import usecases.responses.UserResponseModel; + +/** + * This interface provides a method for closing a question based on the provided close request model. + */ +public interface CloseInputBoundary { + + /** + * Close a question based on the provided close request model. + * + * @param closeRequestModel The request model containing the necessary information for closing the question. + * @return A response model indicating the result of closing the question, typically for updating the home page. + */ + UserResponseModel closeQuestion(CloseRequestModel closeRequestModel); +} diff --git a/src/main/java/usecases/inputboundary/PostInputBoundary.java b/src/main/java/usecases/inputboundary/PostInputBoundary.java new file mode 100644 index 00000000..1fc4d80b --- /dev/null +++ b/src/main/java/usecases/inputboundary/PostInputBoundary.java @@ -0,0 +1,17 @@ +package usecases.inputboundary; + +import usecases.requests.PostRequestModel; +import usecases.responses.TheQuestionResponseModel; + +/** + * This interface provides a method for creating a post based on the provided post request model. + */ +public interface PostInputBoundary { + /** + * Create a post based on the provided post request model. + * + * @param postRequestModel The request model containing the necessary information for post creation. + * @return A response model indicating the result of post creation, typically for updating the question UI. + */ + TheQuestionResponseModel createPost(PostRequestModel postRequestModel); +} diff --git a/src/main/java/usecases/inputboundary/QuestionInputBoundary.java b/src/main/java/usecases/inputboundary/QuestionInputBoundary.java new file mode 100644 index 00000000..f7ff794c --- /dev/null +++ b/src/main/java/usecases/inputboundary/QuestionInputBoundary.java @@ -0,0 +1,18 @@ +package usecases.inputboundary; + +import usecases.responses.TheQuestionResponseModel; +import usecases.requests.QuestionRequestModel; + +/** + * This interface provides a method for creating a question based on the provided question request model. + */ +public interface QuestionInputBoundary { + + /** + * Create a question based on the provided question request model. + * + * @param questionRequestModel The request model containing the necessary information for question creation. + * @return A response model containing the information about the created question. + */ + TheQuestionResponseModel createQuestion(QuestionRequestModel questionRequestModel); +} \ No newline at end of file diff --git a/src/main/java/usecases/inputboundary/RateInputBoundary.java b/src/main/java/usecases/inputboundary/RateInputBoundary.java new file mode 100644 index 00000000..1b6ee217 --- /dev/null +++ b/src/main/java/usecases/inputboundary/RateInputBoundary.java @@ -0,0 +1,19 @@ +package usecases.inputboundary; + +import usecases.requests.RateRequestModel; +import usecases.responses.UserResponseModel; + +/** + * This interface provides a method for rating an answer based on the provided rate request model. + */ +public interface RateInputBoundary { + + /** + * Rate an answer based on the provided rate request model. + * + * @param rateRequestModel The request model containing the necessary information for rating an answer. + * @return A response model indicating the result of rating an answer, typically for updating the home page. + */ + UserResponseModel rateAnswer(RateRequestModel rateRequestModel); + +} diff --git a/src/main/java/usecases/inputboundary/SelectInputBoundary.java b/src/main/java/usecases/inputboundary/SelectInputBoundary.java new file mode 100644 index 00000000..88ef32d6 --- /dev/null +++ b/src/main/java/usecases/inputboundary/SelectInputBoundary.java @@ -0,0 +1,18 @@ +package usecases.inputboundary; + +import usecases.responses.TheQuestionResponseModel; +import usecases.requests.SelectRequestModel; + +/** + * This interface provides a method for selecting a question based on the provided select request model. + */ +public interface SelectInputBoundary { + + /** + * Select a question based on the provided select request model. + * + * @param selectRequestModel The request model containing the necessary information for selecting a question. + * @return A response model containing the information about the selected question. + */ + TheQuestionResponseModel selectQuestion(SelectRequestModel selectRequestModel); +} \ No newline at end of file diff --git a/src/main/java/usecases/inputboundary/UserLoginInputBoundary.java b/src/main/java/usecases/inputboundary/UserLoginInputBoundary.java new file mode 100644 index 00000000..533480cb --- /dev/null +++ b/src/main/java/usecases/inputboundary/UserLoginInputBoundary.java @@ -0,0 +1,18 @@ +package usecases.inputboundary; + +import usecases.requests.UserLoginRequestModel; +import usecases.responses.UserResponseModel; + +/** + * This interface provides a method for authenticating user credentials based on the provided login request model. + */ +public interface UserLoginInputBoundary { + + /** + * Authenticate user credentials based on the provided login request model. + * + * @param requestModel The request model containing the user's login credentials. + * @return A response model indicating the result of the login attempt, typically for updating the home page. + */ + UserResponseModel login(UserLoginRequestModel requestModel); +} diff --git a/src/main/java/usecases/inputboundary/UserRegisterInputBoundary.java b/src/main/java/usecases/inputboundary/UserRegisterInputBoundary.java new file mode 100644 index 00000000..9b94cb8c --- /dev/null +++ b/src/main/java/usecases/inputboundary/UserRegisterInputBoundary.java @@ -0,0 +1,18 @@ +package usecases.inputboundary; + +import usecases.requests.RegistrationData; +import usecases.responses.BaseResponseModel; + +/** + * This interface represents the input boundary for user registration use case. + */ +public interface UserRegisterInputBoundary { + + /** + * Creates a user registration using the provided registration data. + * + * @param requestModel The registration data for the user. + * @return The response model indicating the result of the registration process. + */ + BaseResponseModel create(RegistrationData requestModel); +} diff --git a/src/main/java/usecases/inputboundary/ViewInputBoundary.java b/src/main/java/usecases/inputboundary/ViewInputBoundary.java new file mode 100644 index 00000000..2515aa0f --- /dev/null +++ b/src/main/java/usecases/inputboundary/ViewInputBoundary.java @@ -0,0 +1,16 @@ +package usecases.inputboundary; + +import usecases.responses.ViewResponseModel; + +/** + * This interface provides a method for viewing a question based on the provided view request model. + */ +public interface ViewInputBoundary { + + /** + * View a question based on the provided view request model. + * + * @return A response model containing the information about the viewed question. + */ + ViewResponseModel viewQuestion(); +} diff --git a/src/main/java/businessrule/usecase/AskQuestionInteractor.java b/src/main/java/usecases/interactors/AskQuestionInteractor.java similarity index 67% rename from src/main/java/businessrule/usecase/AskQuestionInteractor.java rename to src/main/java/usecases/interactors/AskQuestionInteractor.java index 903fe5c2..911943aa 100644 --- a/src/main/java/businessrule/usecase/AskQuestionInteractor.java +++ b/src/main/java/usecases/interactors/AskQuestionInteractor.java @@ -1,25 +1,29 @@ -package businessrule.usecase; +package usecases.interactors; -import businessrule.SessionManager; -import businessrule.UserSession; -import businessrule.outputboundary.TheQuestionOutputBoundary; -import businessrule.outputboundary.UserOutputBoundary; -import businessrule.responsemodel.TheQuestionResponseModel; -import businessrule.responsemodel.UserResponseModel; -import businessrule.usecase.util.BuilderService; -import businessrule.usecase.util.PostDisplayFormatter; -import businessrule.usecase.util.RandomNumberGenerator; -import entity.*; -import businessrule.inputboundary.QuestionInputBoundary; -import businessrule.requestmodel.QuestionRequestModel; -import businessrule.gateway.ClientGateway; -import businessrule.gateway.QuestionGateway; -import entity.factory.QuestionFactory; +import usecases.session.SessionManager; +import usecases.session.UserSession; +import usecases.outputboundary.TheQuestionOutputBoundary; +import usecases.responses.TheQuestionResponseModel; +import usecases.responses.UserResponseModel; +import usecases.utils.BuilderService; +import usecases.dto.PostDisplay; +import usecases.utils.RandomNumberGenerator; +import entities.*; +import usecases.inputboundary.QuestionInputBoundary; +import usecases.requests.QuestionRequestModel; +import usecases.gateway.ClientGateway; +import usecases.gateway.QuestionGateway; +import entities.factories.QuestionFactory; import java.time.LocalDate; import java.util.HashMap; import java.util.Map; import java.util.Objects; +/** + * This class represents the interactor responsible for asking a new question. + * This interactor validates input, generates a unique question ID, creates a question entity, + * updates relevant gateways, and constructs the response model. + */ public class AskQuestionInteractor implements QuestionInputBoundary { final QuestionGateway questionGateway; final TheQuestionOutputBoundary outputBoundary; @@ -27,6 +31,14 @@ public class AskQuestionInteractor implements QuestionInputBoundary { final ClientGateway clientGateway; final static String EMPTY_TITLE = ""; + /** + * Constructor for AskQuestionInteractor. + * + * @param questionGateway The gateway for managing question entities. + * @param outputBoundary The output boundary for preparing question response models. + * @param questionFactory The factory for creating question entities. + * @param clientGateway The gateway for managing client entities. + */ public AskQuestionInteractor(QuestionGateway questionGateway, TheQuestionOutputBoundary outputBoundary, QuestionFactory questionFactory, ClientGateway clientGateway) { this.questionGateway = questionGateway; this.outputBoundary = outputBoundary; @@ -34,6 +46,12 @@ public AskQuestionInteractor(QuestionGateway questionGateway, TheQuestionOutputB this.clientGateway = clientGateway; } + /** + * Create a new question based on the provided request model. + * + * @param questionRequestModel The request model containing question details. + * @return The response model for the newly created question. + */ public TheQuestionResponseModel createQuestion(QuestionRequestModel questionRequestModel){ try {checkValidateInput(questionRequestModel);} catch (ApplicationException e) { @@ -49,7 +67,7 @@ public TheQuestionResponseModel createQuestion(QuestionRequestModel questionRequ Question question = createQuestionEntity(questionRequestModel, askedByClient); // construct response model - Map postMap = new HashMap<>(); + Map postMap = new HashMap<>(); return outputBoundary.prepareSuccess(BuilderService.getInstance().constructTheQuestionResponse(question, response, postMap)); } diff --git a/src/main/java/usecases/interactors/AttorneyRecommendInteractor.java b/src/main/java/usecases/interactors/AttorneyRecommendInteractor.java new file mode 100644 index 00000000..1d6692bb --- /dev/null +++ b/src/main/java/usecases/interactors/AttorneyRecommendInteractor.java @@ -0,0 +1,50 @@ +package usecases.interactors; + +import usecases.gateway.AttorneyGateway; +import usecases.gateway.QuestionGateway; +import usecases.outputboundary.ViewOutputBoundary; +import entities.user.Attorney; +import entities.Question; +import entities.user.User; +import java.util.List; + +/** + * This class represents the interactor responsible for recommending questions to an attorney. + * This interactor fetches recommended questions for an attorney based on their profile and preferences. + */ +public class AttorneyRecommendInteractor extends ViewQuestionInteractorBase { + private final AttorneyGateway attorneyGateway; + + /** + * Constructor for AttorneyRecommendInteractor. + * + * @param outputBoundary The output boundary for preparing view response models. + * @param questionGateway The gateway for managing question entities. + * @param attorneyGateway The gateway for managing attorney entities. + */ + public AttorneyRecommendInteractor(ViewOutputBoundary outputBoundary, QuestionGateway questionGateway, AttorneyGateway attorneyGateway) { + super(outputBoundary, questionGateway); + this.attorneyGateway = attorneyGateway; + } + + /** + * Fetch recommended questions for an attorney. + * + * @return The list of recommended questions. + */ + @Override + protected List fetchQuestions(int userId) { + Attorney attorney = (Attorney) fetchUser(userId); + return attorney.getRecommendations(); + } + + /** + * Fetch the attorney based on the provided user ID. + * + * @return The fetched attorney entity. + */ + @Override + protected User fetchUser(int userId) { + return attorneyGateway.get(userId); + } +} diff --git a/src/main/java/usecases/interactors/AttorneyRegisterInteractor.java b/src/main/java/usecases/interactors/AttorneyRegisterInteractor.java new file mode 100644 index 00000000..610afcb1 --- /dev/null +++ b/src/main/java/usecases/interactors/AttorneyRegisterInteractor.java @@ -0,0 +1,14 @@ +package usecases.interactors; + +import entities.factories.AttorneyFactory; +import entities.factories.UserFactory; +import entities.user.Attorney; +import usecases.gateway.AttorneyGateway; +import usecases.gateway.UserGateway; +import usecases.outputboundary.BaseOutputBoundary; + +public class AttorneyRegisterInteractor extends UserRegisterInteractor { + public AttorneyRegisterInteractor(AttorneyGateway userGateway, BaseOutputBoundary outputBoundary, AttorneyFactory userFactory) { + super(userGateway, outputBoundary, userFactory); + } +} diff --git a/src/main/java/businessrule/usecase/BrowseQuestionInteractor.java b/src/main/java/usecases/interactors/BrowseQuestionInteractor.java similarity index 51% rename from src/main/java/businessrule/usecase/BrowseQuestionInteractor.java rename to src/main/java/usecases/interactors/BrowseQuestionInteractor.java index d2615a92..0e042024 100644 --- a/src/main/java/businessrule/usecase/BrowseQuestionInteractor.java +++ b/src/main/java/usecases/interactors/BrowseQuestionInteractor.java @@ -1,24 +1,39 @@ -package businessrule.usecase; +package usecases.interactors; -import businessrule.gateway.AttorneyGateway; -import businessrule.outputboundary.TheQuestionOutputBoundary; -import businessrule.gateway.QuestionGateway; -import businessrule.outputboundary.ViewOutputBoundary; -import entity.Question; -import entity.User; +import usecases.gateway.AttorneyGateway; +import usecases.gateway.QuestionGateway; +import usecases.outputboundary.ViewOutputBoundary; +import entities.Question; +import entities.user.User; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; +/** + * This class represents the interactor responsible for browsing available questions. + * This interactor fetches a list of available questions that have not been taken and are not closed. + */ public class BrowseQuestionInteractor extends ViewQuestionInteractorBase { final AttorneyGateway attorneyGateway; + /** + * Constructor for BrowseQuestionInteractor. + * + * @param outputBoundary The output boundary for preparing view response models. + * @param questionGateway The gateway for managing question entities. + * @param attorneyGateway The gateway for managing attorney entities. + */ public BrowseQuestionInteractor(ViewOutputBoundary outputBoundary, QuestionGateway questionGateway, AttorneyGateway attorneyGateway) { super(outputBoundary, questionGateway); this.attorneyGateway = attorneyGateway; } + /** + * Fetch available questions that have not been taken and are not closed. + * + * @return The list of available questions. + */ @Override protected List fetchQuestions(int userId) { List notTakenList = questionGateway.getNotTakenQuestion(); @@ -29,6 +44,11 @@ protected List fetchQuestions(int userId) { return new ArrayList<>(notCloseSet); } + /** + * Fetch the user based on the provided user ID. + * + * @return The fetched user entity. + */ @Override protected User fetchUser(int userId) { return attorneyGateway.get(userId); diff --git a/src/main/java/usecases/interactors/ClientRegisterInteractor.java b/src/main/java/usecases/interactors/ClientRegisterInteractor.java new file mode 100644 index 00000000..09a806a5 --- /dev/null +++ b/src/main/java/usecases/interactors/ClientRegisterInteractor.java @@ -0,0 +1,24 @@ +package usecases.interactors; + + +import usecases.gateway.ClientGateway; +import usecases.outputboundary.BaseOutputBoundary; +import entities.user.Client; +import entities.factories.ClientFactory; + +/** + * This class represents interactor responsible for client registration use case. + */ +public class ClientRegisterInteractor extends UserRegisterInteractor { + + /** + * Constructs a ClientRegisterInteractor. + * + * @param userGateway The gateway for user-related operations. + * @param outputBoundary The output boundary for the registration process. + * @param clientFactory The factory to create client entity + */ + public ClientRegisterInteractor(ClientGateway userGateway, BaseOutputBoundary outputBoundary, ClientFactory clientFactory) { + super(userGateway, outputBoundary, clientFactory); + } +} \ No newline at end of file diff --git a/src/main/java/usecases/interactors/CloseQuestionInteractor.java b/src/main/java/usecases/interactors/CloseQuestionInteractor.java new file mode 100644 index 00000000..99fe0457 --- /dev/null +++ b/src/main/java/usecases/interactors/CloseQuestionInteractor.java @@ -0,0 +1,60 @@ +package usecases.interactors; + +import usecases.session.SessionManager; +import usecases.session.UserSession; +import usecases.gateway.ClientGateway; +import usecases.inputboundary.CloseInputBoundary; +import usecases.outputboundary.UserOutputBoundary; +import usecases.requests.CloseRequestModel; +import usecases.responses.UserResponseModel; +import entities.Question; +import usecases.gateway.QuestionGateway; + +/** + * This class represents a use case interactor for closing a question. + * This class is responsible for handling the business logic related to closing a question in the system. + */ +public class CloseQuestionInteractor implements CloseInputBoundary { + + final QuestionGateway questionGateway; + final UserOutputBoundary outputBoundary; + final ClientGateway clientGateway; + + /** + * Constructs an instance of CloseQuestionInteractor with the necessary dependencies. + * + * @param questionGateway The gateway for question-related operations. + * @param outputBoundary The output boundary for preparing responses to the home page. + * @param clientGateway The gateway of client-specific operations + */ + public CloseQuestionInteractor(QuestionGateway questionGateway, UserOutputBoundary outputBoundary, ClientGateway clientGateway) { + this.questionGateway = questionGateway; + this.outputBoundary = outputBoundary; + this.clientGateway = clientGateway; + } + + /** + * Closes a question based on the provided CloseRequestModel and returns a response model for the home page. + * + * @param closeRequestModel The model containing information to close a question. + * @return The response model for the home page indicating the outcome of the close attempt. + */ + @Override + public UserResponseModel closeQuestion(CloseRequestModel closeRequestModel) { + // get input data + UserSession session = SessionManager.getSession(); + UserResponseModel response = session.getUserResponseModel(); + int questionId = closeRequestModel.getQuestionId(); + Question question = questionGateway.get(questionId); + + + // handle close logic and prepare response model + if(question.isTaken()){ + questionGateway.updateIsClose(questionId, true); + return outputBoundary.prepareSuccess(response); + } + else{ + return outputBoundary.prepareFail("You can only close this question once it was taken by attorney."); + } + } +} diff --git a/src/main/java/businessrule/usecase/RateInteractor.java b/src/main/java/usecases/interactors/RateInteractor.java similarity index 69% rename from src/main/java/businessrule/usecase/RateInteractor.java rename to src/main/java/usecases/interactors/RateInteractor.java index ac348e69..00df85d9 100644 --- a/src/main/java/businessrule/usecase/RateInteractor.java +++ b/src/main/java/usecases/interactors/RateInteractor.java @@ -1,33 +1,46 @@ -package businessrule.usecase; +package usecases.interactors; -import businessrule.SessionManager; -import businessrule.UserSession; -import businessrule.gateway.ClientGateway; -import businessrule.gateway.*; -import businessrule.inputboundary.RateInputBoundary; -import businessrule.outputboundary.UserOutputBoundary; -import businessrule.requestmodel.RateRequestModel; -import businessrule.gateway.QuestionGateway; -import businessrule.responsemodel.UserResponseModel; -import businessrule.usecase.util.EmailNotificationSender; -import entity.Attorney; -import entity.Client; -import entity.Question; +import usecases.session.SessionManager; +import usecases.session.UserSession; +import usecases.gateway.*; +import usecases.inputboundary.RateInputBoundary; +import usecases.outputboundary.UserOutputBoundary; +import usecases.requests.RateRequestModel; +import usecases.gateway.QuestionGateway; +import usecases.responses.UserResponseModel; +import usecases.utils.EmailNotificationSender; +import entities.user.Attorney; +import entities.Question; +/** + * This class represents the interactor responsible for rating an answer. + * This interactor handles the process of rating an answer given by a client, updating the rating of the associated question. + */ public class RateInteractor implements RateInputBoundary { private final QuestionGateway questionGateway; private final UserOutputBoundary outputBoundary; - private final ClientGateway clientGateway; private final AttorneyGateway attorneyGateway; - public RateInteractor(QuestionGateway questionGateway, UserOutputBoundary outputBoundary, ClientGateway clientGateway, AttorneyGateway attorneyGateway) { + /** + * Constructor for RateInteractor. + * + * @param questionGateway The gateway for managing question entities. + * @param outputBoundary The output boundary for preparing user response models. + * @param attorneyGateway The gateway for managing attorney entities + */ + public RateInteractor(QuestionGateway questionGateway, UserOutputBoundary outputBoundary, AttorneyGateway attorneyGateway) { this.questionGateway = questionGateway; this.outputBoundary = outputBoundary; - this.clientGateway = clientGateway; this.attorneyGateway = attorneyGateway; } + /** + * Rate an answer and update the associated question's rating. + * + * @param rateRequestModel The request model containing rating details. + * @return The response model for the home page. + */ @Override public UserResponseModel rateAnswer(RateRequestModel rateRequestModel) { // get input @@ -35,7 +48,6 @@ public UserResponseModel rateAnswer(RateRequestModel rateRequestModel) { int answerId = rateRequestModel.getAnswerId(); UserSession session = SessionManager.getSession(); UserResponseModel response = session.getUserResponseModel(); - Client client = clientGateway.get(response.getUserId()); Question answer = questionGateway.get(answerId); diff --git a/src/main/java/businessrule/usecase/ReplyInteractor.java b/src/main/java/usecases/interactors/ReplyInteractor.java similarity index 85% rename from src/main/java/businessrule/usecase/ReplyInteractor.java rename to src/main/java/usecases/interactors/ReplyInteractor.java index 62edee41..03a955b2 100644 --- a/src/main/java/businessrule/usecase/ReplyInteractor.java +++ b/src/main/java/usecases/interactors/ReplyInteractor.java @@ -1,25 +1,29 @@ -package businessrule.usecase; - -import businessrule.SessionManager; -import businessrule.UserSession; -import businessrule.inputboundary.PostInputBoundary; -import businessrule.outputboundary.TheQuestionOutputBoundary; -import businessrule.outputboundary.UserOutputBoundary; -import businessrule.requestmodel.PostRequestModel; -import businessrule.gateway.PostGateway; -import businessrule.gateway.QuestionGateway; -import businessrule.responsemodel.TheQuestionResponseModel; -import businessrule.responsemodel.UserResponseModel; -import businessrule.usecase.util.*; -import businessrule.gateway.UserGateway; -import businessrule.gateway.UserGatewayFactory; -import entity.*; -import entity.factory.PostFactory; +package usecases.interactors; + +import entities.user.Attorney; +import entities.user.User; +import usecases.session.SessionManager; +import usecases.session.UserSession; +import usecases.dto.PostDisplay; +import usecases.inputboundary.PostInputBoundary; +import usecases.outputboundary.TheQuestionOutputBoundary; +import usecases.requests.PostRequestModel; +import usecases.gateway.PostGateway; +import usecases.gateway.QuestionGateway; +import usecases.responses.TheQuestionResponseModel; +import usecases.responses.UserResponseModel; +import usecases.gateway.UserGateway; +import infrastructure.database.UserGatewayFactory; +import entities.*; +import entities.factories.PostFactory; +import usecases.utils.*; import java.time.LocalDate; import java.util.Map; - +/** + * This class represents the use case for creating a new post (reply) in response to a question. + */ public class ReplyInteractor implements PostInputBoundary { final QuestionGateway questionGateway; @@ -37,6 +41,12 @@ public ReplyInteractor(QuestionGateway questionGateway, PostGateway postGateway, this.userGatewayFactory = userGatewayFactory; } + /** + * Creates a new post (reply) in response to a question. + * + * @param postRequestModel The request model containing the details for the new post. + * @return A response model containing information about the updating question UI. + */ @Override public TheQuestionResponseModel createPost(PostRequestModel postRequestModel) { // get input data @@ -74,7 +84,7 @@ public TheQuestionResponseModel createPost(PostRequestModel postRequestModel) { } PostMapConstructor postMapConstructor = new PostMapConstructor(userGatewayFactory); - Map postMap = postMapConstructor.constructPostMap(question); + Map postMap = postMapConstructor.constructPostMap(question); TheQuestionResponseModel theQuestionResponseModel = BuilderService.getInstance().constructTheQuestionResponse(question, response, postMap); return outputBoundary.prepareSuccess(theQuestionResponseModel); } diff --git a/src/main/java/businessrule/usecase/SelectQuestionInteractor.java b/src/main/java/usecases/interactors/SelectQuestionInteractor.java similarity index 54% rename from src/main/java/businessrule/usecase/SelectQuestionInteractor.java rename to src/main/java/usecases/interactors/SelectQuestionInteractor.java index 2fee0ed9..c81a3728 100644 --- a/src/main/java/businessrule/usecase/SelectQuestionInteractor.java +++ b/src/main/java/usecases/interactors/SelectQuestionInteractor.java @@ -1,33 +1,51 @@ -package businessrule.usecase; - -import businessrule.SessionManager; -import businessrule.UserSession; -import businessrule.inputboundary.SelectInputBoundary; -import businessrule.outputboundary.TheQuestionOutputBoundary; -import businessrule.requestmodel.SelectRequestModel; -import businessrule.responsemodel.UserResponseModel; -import businessrule.usecase.util.BuilderService; -import businessrule.usecase.util.PostDisplayFormatter; -import businessrule.usecase.util.PostMapConstructor; -import entity.Question; -import businessrule.gateway.QuestionGateway; -import businessrule.responsemodel.TheQuestionResponseModel; -import entity.User; -import businessrule.gateway.UserGateway; -import businessrule.gateway.UserGatewayFactory; +package usecases.interactors; + +import usecases.session.SessionManager; +import usecases.session.UserSession; +import usecases.inputboundary.SelectInputBoundary; +import usecases.outputboundary.TheQuestionOutputBoundary; +import usecases.requests.SelectRequestModel; +import usecases.responses.UserResponseModel; +import usecases.utils.BuilderService; +import usecases.dto.PostDisplay; +import usecases.utils.PostMapConstructor; +import entities.Question; +import usecases.gateway.QuestionGateway; +import usecases.responses.TheQuestionResponseModel; +import entities.user.User; +import usecases.gateway.UserGateway; +import infrastructure.database.UserGatewayFactory; import java.util.Map; +/** + * This class represents the interactor responsible for selecting a question. + * This interactor handles the process of selecting a question based on user input and + * preparing and formatting the response model. + */ public class SelectQuestionInteractor implements SelectInputBoundary { final QuestionGateway questionGateway; final TheQuestionOutputBoundary outputBoundary; final UserGatewayFactory userGatewayFactory; + /** + * Constructor for SelectQuestionInteractor. + * + * @param questionGateway The gateway for managing question entities. + * @param outputBoundary The output boundary for preparing question response models. + * @param userGatewayFactory The factory for creating user gateways. + */ public SelectQuestionInteractor(QuestionGateway questionGateway, TheQuestionOutputBoundary outputBoundary, UserGatewayFactory userGatewayFactory) { this.questionGateway = questionGateway; this.outputBoundary = outputBoundary; this.userGatewayFactory = userGatewayFactory; } + /** + * Select a question and prepare the response model. + * + * @param selectRequestModel The request model containing selection details. + * @return The response model for the selected question. + */ @Override public TheQuestionResponseModel selectQuestion(SelectRequestModel selectRequestModel) { // get input data @@ -46,7 +64,7 @@ public TheQuestionResponseModel selectQuestion(SelectRequestModel selectRequestM boolean isQuestionSelectable = user.isQuestionSelectable(question); if (isQuestionSelectable) { PostMapConstructor postMapConstructor = new PostMapConstructor(userGatewayFactory); - Map postMap = postMapConstructor.constructPostMap(question); + Map postMap = postMapConstructor.constructPostMap(question); TheQuestionResponseModel theQuestionResponseModel = BuilderService.getInstance().constructTheQuestionResponse(question, response, postMap); return outputBoundary.prepareSuccess(theQuestionResponseModel); } else { diff --git a/src/main/java/businessrule/usecase/UserLoginInteractor.java b/src/main/java/usecases/interactors/UserLoginInteractor.java similarity index 59% rename from src/main/java/businessrule/usecase/UserLoginInteractor.java rename to src/main/java/usecases/interactors/UserLoginInteractor.java index d20e86e2..ebb4e337 100644 --- a/src/main/java/businessrule/usecase/UserLoginInteractor.java +++ b/src/main/java/usecases/interactors/UserLoginInteractor.java @@ -1,32 +1,47 @@ -package businessrule.usecase; +package usecases.interactors; -import businessrule.SessionManager; -import businessrule.UserSession; -import businessrule.gateway.UserGatewayFactory; -import businessrule.inputboundary.UserLoginInputBoundary; -import businessrule.outputboundary.UserOutputBoundary; -import businessrule.requestmodel.UserLoginRequestModel; -import businessrule.gateway.UserGateway; -import businessrule.responsemodel.UserResponseModel; -import businessrule.responsemodel.ViewResponseModel; -import businessrule.usecase.util.BuilderService; -import businessrule.usecase.util.QuestionDisplayFormatter; -import businessrule.usecase.util.QuestionMapConstructor; -import entity.User; -import entity.ApplicationException; +import usecases.session.SessionManager; +import usecases.session.UserSession; +import infrastructure.database.UserGatewayFactory; +import usecases.inputboundary.UserLoginInputBoundary; +import usecases.outputboundary.UserOutputBoundary; +import usecases.requests.UserLoginRequestModel; +import usecases.gateway.UserGateway; +import usecases.responses.UserResponseModel; +import usecases.utils.BuilderService; +import usecases.dto.QuestionDisplay; +import usecases.utils.QuestionMapConstructor; +import entities.user.User; +import entities.ApplicationException; -import javax.swing.text.View; import java.util.Map; +/** + * This class represents the interactor responsible for handling user login. + * This interactor handles the process of authenticating user login credentials,retrieving user information, + * and preparing the appropriate response model. + */ public class UserLoginInteractor implements UserLoginInputBoundary{ final UserGatewayFactory userGatewayFactory; final UserOutputBoundary outputBoundary; + /** + * Constructor for UserLoginInteractor. + * + * @param userGatewayFactory The factory for creating user gateways. + * @param outputBoundary The output boundary for preparing home page response models. + */ public UserLoginInteractor(UserGatewayFactory userGatewayFactory, UserOutputBoundary outputBoundary) { this.userGatewayFactory = userGatewayFactory; this.outputBoundary = outputBoundary; } + /** + * Authenticate user login and prepare the response model. + * + * @param requestModel The request model containing user login details. + * @return The response model for the home page. + */ @Override public UserResponseModel login(UserLoginRequestModel requestModel) { int userId = requestModel.getUserId(); @@ -42,7 +57,7 @@ public UserResponseModel login(UserLoginRequestModel requestModel) { setUserSession(responseModel); - Map questionMap = new QuestionMapConstructor().constructQuestionMap(user.getQuestionsList()); + Map questionMap = new QuestionMapConstructor().constructQuestionMap(user.getQuestionsList()); BuilderService.getInstance().constructViewResponse(responseModel, questionMap); return outputBoundary.prepareSuccess(responseModel); } diff --git a/src/main/java/usecases/interactors/UserRegisterInteractor.java b/src/main/java/usecases/interactors/UserRegisterInteractor.java new file mode 100644 index 00000000..ee279728 --- /dev/null +++ b/src/main/java/usecases/interactors/UserRegisterInteractor.java @@ -0,0 +1,87 @@ +package usecases.interactors; + +import entities.ApplicationException; +import usecases.gateway.UserGateway; +import usecases.inputboundary.UserRegisterInputBoundary; +import usecases.outputboundary.BaseOutputBoundary; +import usecases.responses.BaseResponseModel; +import usecases.utils.CredentialChecker; +import usecases.utils.RandomNumberGenerator; +import entities.user.User; +import usecases.requests.RegistrationData; +import entities.factories.UserFactory; + +/** + * This class represents the user registration interactors. + * + * @param The type of UserGateway used for interacting with users. + * @param The type of UserFactory used for creating user instances. + * @param The type of User entity. + */ +public abstract class UserRegisterInteractor, F extends UserFactory, U extends User> implements UserRegisterInputBoundary { + protected final T userGateway; + protected final F userFactory; + + protected final BaseOutputBoundary outputBoundary; + + /** + * Constructor for UserRegisterInteractor. + * + * @param userGateway The gateway for user-related operations. + * @param outputBoundary The output boundary for registration results. + * @param userFactory The user factory to create corresponding user entity. + */ + public UserRegisterInteractor(T userGateway, BaseOutputBoundary outputBoundary, F userFactory) { + this.userGateway = userGateway; + this.outputBoundary = outputBoundary; + this.userFactory = userFactory; + } + + public BaseResponseModel create(RegistrationData requestModel) { + try {checkCredential(requestModel);} + catch (ApplicationException e) { + return outputBoundary.prepareFail(e.getMessage()); + } + int userId = generateId(); + requestModel.setUserId(userId); + U user = userFactory.createUser(requestModel); + userGateway.save(user); + return outputBoundary.prepareSuccess(String.valueOf(userId)); + } + + protected int generateId() { + RandomNumberGenerator generator = new RandomNumberGenerator(); + int randomUserId = generator.generateClientId(8); + boolean exists = userGateway.existsById(randomUserId); + while (exists) { + randomUserId = generator.generateClientId(8); + exists = userGateway.existsById(randomUserId); + } return randomUserId; + } + + protected void checkCredential(RegistrationData requestModel) throws ApplicationException { + // prepare input data + String inputUserName = requestModel.userName; + String inputEmail = requestModel.email; + String inputPassword1 = requestModel.password; + String inputPassword2 = requestModel.password2; + String inputPostalCode = requestModel.postalCode; + int inputAge = requestModel.age; + + // validate input data + CredentialChecker checker = new CredentialChecker(); + if (userGateway.existsByName(inputUserName)) { + throw new ApplicationException("User name already exists"); + } else if (!inputPassword1.equals(inputPassword2)) { + throw new ApplicationException("Passwords does not match"); + } else if (inputPassword1.length() < 8) { + throw new ApplicationException("Password is less than 8 characters"); + } else if (!checker.checkEmail(inputEmail)) { + throw new ApplicationException("Email is invalid"); + } else if (!checker.checkAge(inputAge)) { + throw new ApplicationException("Age is invalid"); + } else if (!checker.checkPostalCode(inputPostalCode)) { + throw new ApplicationException("Postal Code is invalid"); + } + } +} diff --git a/src/main/java/usecases/interactors/ViewQuestionInteractor.java b/src/main/java/usecases/interactors/ViewQuestionInteractor.java new file mode 100644 index 00000000..c905a779 --- /dev/null +++ b/src/main/java/usecases/interactors/ViewQuestionInteractor.java @@ -0,0 +1,54 @@ +package usecases.interactors; + + +import usecases.gateway.QuestionGateway; +import usecases.gateway.UserGateway; +import infrastructure.database.UserGatewayFactory; +import usecases.outputboundary.ViewOutputBoundary; +import entities.Question; +import entities.user.User; +import java.util.List; + +/** + * This class represents the interactor responsible for viewing questions. + * This interactor handles the process of fetching and displaying questions based on user input + * and preparing the appropriate response model. + */ +public class ViewQuestionInteractor extends ViewQuestionInteractorBase{ + final UserGatewayFactory userGatewayFactory; + + /** + * Constructor for ViewQuestionInteractor. + * + * @param questionGateway The gateway for managing question entities. + * @param outputBoundary The output boundary for preparing view response models. + * @param userGatewayFactory The factory for creating user gateways. + */ + public ViewQuestionInteractor(ViewOutputBoundary outputBoundary, QuestionGateway questionGateway, UserGatewayFactory userGatewayFactory) { + super(outputBoundary, questionGateway); + this.userGatewayFactory = userGatewayFactory; + } + + /** + * Fetch questions for the user. + * + * @return The list of questions for the user. + */ + @Override + protected List fetchQuestions(int userId) { + User user = fetchUser(userId); + return user.getQuestionsList(); + } + + /** + * Fetch the user based on the request model. + * + * @return The user corresponding to the request. + */ + @Override + protected User fetchUser(int userId) { + UserGateway userGateway = userGatewayFactory.createUserGateway(userId); + return userGateway.get(userId); + } + +} \ No newline at end of file diff --git a/src/main/java/usecases/interactors/ViewQuestionInteractorBase.java b/src/main/java/usecases/interactors/ViewQuestionInteractorBase.java new file mode 100644 index 00000000..5a838af8 --- /dev/null +++ b/src/main/java/usecases/interactors/ViewQuestionInteractorBase.java @@ -0,0 +1,65 @@ +package usecases.interactors; + +import usecases.session.SessionManager; +import usecases.session.UserSession; +import usecases.gateway.QuestionGateway; +import usecases.inputboundary.ViewInputBoundary; +import usecases.outputboundary.ViewOutputBoundary; +import usecases.responses.UserResponseModel; +import usecases.responses.ViewResponseModel; +import usecases.utils.BuilderService; +import usecases.dto.QuestionDisplay; +import usecases.utils.QuestionMapConstructor; +import entities.Question; +import entities.user.User; + +import java.util.List; +import java.util.Map; + +/** + * This class represents the use case to view questions. + */ +public abstract class ViewQuestionInteractorBase implements ViewInputBoundary { + protected final ViewOutputBoundary outputBoundary; + protected final QuestionGateway questionGateway; + + /** + * Constructs a ViewQuestionInteractorBase instance. + * + * @param outputBoundary The output boundary for communicating results. + * @param questionGateway The gateway for accessing question data. + */ + public ViewQuestionInteractorBase(ViewOutputBoundary outputBoundary, QuestionGateway questionGateway) { + this.outputBoundary = outputBoundary; + this.questionGateway = questionGateway; + } + + @Override + public ViewResponseModel viewQuestion() { + // get input + UserSession session = SessionManager.getSession(); + UserResponseModel response = session.getUserResponseModel(); + int userId = response.getUserId(); + + // strategy + List questionList = fetchQuestions(userId); + + QuestionMapConstructor questionMapConstructor = new QuestionMapConstructor(); + Map questionMap = questionMapConstructor.constructQuestionMap(questionList); + return outputBoundary.prepareSuccess(BuilderService.getInstance().constructViewResponse(response, questionMap)); + } + + /** + * Fetches a list of questions based on the provided view request model. + * + * @return A list of questions based on the provided criteria. + */ + protected abstract List fetchQuestions(int userId); + + /** + * Fetches user information based on the provided view request model. + * + * @return The user associated with the provided criteria. + */ + protected abstract User fetchUser(int userId); +} diff --git a/src/main/java/businessrule/outputboundary/BaseOutputBoundary.java b/src/main/java/usecases/outputboundary/BaseOutputBoundary.java similarity index 50% rename from src/main/java/businessrule/outputboundary/BaseOutputBoundary.java rename to src/main/java/usecases/outputboundary/BaseOutputBoundary.java index 5ed70001..e510601a 100644 --- a/src/main/java/businessrule/outputboundary/BaseOutputBoundary.java +++ b/src/main/java/usecases/outputboundary/BaseOutputBoundary.java @@ -1,7 +1,6 @@ -package businessrule.outputboundary; +package usecases.outputboundary; -import adapter.controller.ControlContainer; -import businessrule.responsemodel.BaseResponseModel; +import usecases.responses.BaseResponseModel; public interface BaseOutputBoundary { BaseResponseModel prepareSuccess(String msg); diff --git a/src/main/java/usecases/outputboundary/TheQuestionOutputBoundary.java b/src/main/java/usecases/outputboundary/TheQuestionOutputBoundary.java new file mode 100644 index 00000000..832df65e --- /dev/null +++ b/src/main/java/usecases/outputboundary/TheQuestionOutputBoundary.java @@ -0,0 +1,25 @@ +package usecases.outputboundary; + +import usecases.responses.TheQuestionResponseModel; + +/** + * This interface provides methods for setting the control container, preparing failure responses, + * and preparing success responses. + */ +public interface TheQuestionOutputBoundary { + /** + * Prepare a failure response for a question. + * + * @param msg The message indicating the reason for the failure. + * @return A prepared question response model indicating the failure. + */ + TheQuestionResponseModel prepareFail(String msg); + + /** + * Prepare a success response for a question. + * + * @param response The response model to prepare as a success response. + * @return A prepared question response model indicating the success. + */ + TheQuestionResponseModel prepareSuccess(TheQuestionResponseModel response); +} diff --git a/src/main/java/businessrule/outputboundary/UserOutputBoundary.java b/src/main/java/usecases/outputboundary/UserOutputBoundary.java similarity index 53% rename from src/main/java/businessrule/outputboundary/UserOutputBoundary.java rename to src/main/java/usecases/outputboundary/UserOutputBoundary.java index d667397e..d28ed449 100644 --- a/src/main/java/businessrule/outputboundary/UserOutputBoundary.java +++ b/src/main/java/usecases/outputboundary/UserOutputBoundary.java @@ -1,7 +1,6 @@ -package businessrule.outputboundary; +package usecases.outputboundary; -import adapter.controller.ControlContainer; -import businessrule.responsemodel.UserResponseModel; +import usecases.responses.UserResponseModel; public interface UserOutputBoundary { UserResponseModel prepareFail(String msg); diff --git a/src/main/java/usecases/outputboundary/ViewOutputBoundary.java b/src/main/java/usecases/outputboundary/ViewOutputBoundary.java new file mode 100644 index 00000000..3b099a46 --- /dev/null +++ b/src/main/java/usecases/outputboundary/ViewOutputBoundary.java @@ -0,0 +1,26 @@ +package usecases.outputboundary; + +import usecases.responses.ViewResponseModel; + +/** + * This interface provides methods for setting the control container, preparing failure responses, + * and preparing success responses. + */ +public interface ViewOutputBoundary { + + /** + * Prepare a failure response for a view. + * + * @param msg The message indicating the reason for the failure. + * @return A prepared view response model indicating the failure. + */ + ViewResponseModel prepareFail(String msg); + + /** + * Prepare a success response for a view. + * + * @param response The response model to prepare as a success response. + * @return A prepared view response model indicating the success. + */ + ViewResponseModel prepareSuccess(ViewResponseModel response); +} diff --git a/src/main/java/usecases/requests/AttorneyRegistrationData.java b/src/main/java/usecases/requests/AttorneyRegistrationData.java new file mode 100644 index 00000000..8e18a905 --- /dev/null +++ b/src/main/java/usecases/requests/AttorneyRegistrationData.java @@ -0,0 +1,38 @@ +package usecases.requests; + +import java.util.Set; + +public class AttorneyRegistrationData extends RegistrationData { + + public AttorneyRegistrationData(Builder builder) { + super(builder.userName, builder.email, builder.password, builder.password2, builder.stateAbb, builder.postalCode); + this.professionals = builder.professionals; + + } + + public static class Builder { + private final String userName; + private final String email; + private final String password; + private final String stateAbb; + private final String postalCode; + private final String password2; + private Set professionals; + + public Builder(RegistrationData registrationData) { + this.userName = registrationData.userName; + this.email = registrationData.email; + this.password = registrationData.password; + this.password2 = registrationData.password2; + this.stateAbb = registrationData.stateAbb; + this.postalCode = registrationData.postalCode; + } + + public AttorneyRegistrationData.Builder professionals(Set professionals) { + this.professionals = professionals; + return this; + } + + public AttorneyRegistrationData build() {return new AttorneyRegistrationData(this);} + } +} diff --git a/src/main/java/usecases/requests/ClientRegistrationData.java b/src/main/java/usecases/requests/ClientRegistrationData.java new file mode 100644 index 00000000..58693ef1 --- /dev/null +++ b/src/main/java/usecases/requests/ClientRegistrationData.java @@ -0,0 +1,69 @@ +package usecases.requests; + +public class ClientRegistrationData extends RegistrationData { + public ClientRegistrationData(Builder builder) { + super(builder.userName, builder.email, builder.password, builder.password2, builder.stateAbb, builder.postalCode); + this.ethnicity = builder.ethnicity; + this.age = builder.age; + this.gender = builder.gender; + this.maritalStatus = builder.maritalStatus; + this.numberOfHousehold = builder.numberOfHousehold; + this.annualIncome = builder.annualIncome; + } + + public static class Builder { + private final String userName; + private final String email; + private final String password; + private final String stateAbb; + private final String postalCode; + private String ethnicity; + private int age; + private String gender; + private String maritalStatus; + private int numberOfHousehold; + private float annualIncome; + private final String password2; + + public Builder(RegistrationData registrationData) { + this.userName = registrationData.userName; + this.email = registrationData.email; + this.password = registrationData.password; + this.password2 = registrationData.password2; + this.stateAbb = registrationData.stateAbb; + this.postalCode = registrationData.postalCode; + } + + public ClientRegistrationData.Builder ethnicity(String ethnicity) { + this.ethnicity = ethnicity; + return this; + } + + public ClientRegistrationData.Builder age(int age) { + this.age = age; + return this; + } + + public ClientRegistrationData.Builder gender(String gender) { + this.gender = gender; + return this; + } + + public ClientRegistrationData.Builder maritalStatus(String maritalStatus) { + this.maritalStatus = maritalStatus; + return this; + } + + public ClientRegistrationData.Builder numberOfHousehold(int numberOfHousehold) { + this.numberOfHousehold = numberOfHousehold; + return this; + } + + public ClientRegistrationData.Builder annualIncome(float annualIncome) { + this.annualIncome = annualIncome; + return this; + } + + public ClientRegistrationData build() {return new ClientRegistrationData(this);} + } +} diff --git a/src/main/java/usecases/requests/CloseRequestModel.java b/src/main/java/usecases/requests/CloseRequestModel.java new file mode 100644 index 00000000..29d67d89 --- /dev/null +++ b/src/main/java/usecases/requests/CloseRequestModel.java @@ -0,0 +1,26 @@ +package usecases.requests; + +/** + * This class represents a request model to close a question made by a client. + */ +public class CloseRequestModel { + private final int questionId; + + /** + * Constructs a CloseRequestModel object with the provided question ID and user ID. + * + * @param questionId The ID of the question to be closed. + */ + public CloseRequestModel(int questionId) { + this.questionId = questionId; + } + + /** + * Gets the ID of the question to be closed. + * + * @return The ID of the question. + */ + public int getQuestionId() { + return questionId; + } +} diff --git a/src/main/java/usecases/requests/PostRequestModel.java b/src/main/java/usecases/requests/PostRequestModel.java new file mode 100644 index 00000000..43793b26 --- /dev/null +++ b/src/main/java/usecases/requests/PostRequestModel.java @@ -0,0 +1,38 @@ +package usecases.requests; + +/** + * This class represents a post request model for creating a new post. + */ +public class PostRequestModel { + private final int questionId; + private final String postText; + + /** + * Constructs a new PostRequestModel instance. + * + * @param questionId The ID of the question associated with the post. + * @param postText The text content of the post. + */ + public PostRequestModel(int questionId, String postText){ + this.questionId = questionId; + this.postText = postText; + } + + /** + * Get the ID of the question associated with the post. + * + * @return The question ID. + */ + public int getQuestionId() { + return questionId; + } + + /** + * Get the text content of the post. + * + * @return The post text. + */ + public String getPostText() { + return postText; + } +} diff --git a/src/main/java/usecases/requests/QuestionRequestModel.java b/src/main/java/usecases/requests/QuestionRequestModel.java new file mode 100644 index 00000000..561bf9ca --- /dev/null +++ b/src/main/java/usecases/requests/QuestionRequestModel.java @@ -0,0 +1,64 @@ +package usecases.requests; + +import java.time.LocalDate; + +/** + * This class represents a post request model for creating a new question. + */ +public class QuestionRequestModel { + private final String questionCategory; + private final String title; + private final LocalDate createAt; + private final LocalDate legalDeadline; + + /** + * Constructs a new QuestionRequestModel with the specified parameters. + * + * @param questionCategory The category of the question. + * @param title The title of the question. + * @param createAt The creation date of the question. + * @param legalDeadline The legal deadline for the question. + */ + public QuestionRequestModel(String questionCategory, String title, LocalDate createAt, LocalDate legalDeadline) { + this.questionCategory = questionCategory; + this.title = title; + this.createAt = createAt; + this.legalDeadline = legalDeadline; + } + + /** + * Get the category of the question. + * + * @return The question category. + */ + public String getQuestionCategory() { + return questionCategory; + } + + /** + * Get the title of the question. + * + * @return The question title. + */ + public String getTitle() { + return title; + } + + /** + * Get the creation date of the question. + * + * @return The creation date. + */ + public LocalDate getCreateAt() { + return createAt; + } + + /** + * Get the legal deadline for the question. + * + * @return The legal deadline. + */ + public LocalDate getLegalDeadline() { + return legalDeadline; + } +} diff --git a/src/main/java/usecases/requests/RateRequestModel.java b/src/main/java/usecases/requests/RateRequestModel.java new file mode 100644 index 00000000..649118a6 --- /dev/null +++ b/src/main/java/usecases/requests/RateRequestModel.java @@ -0,0 +1,39 @@ +package usecases.requests; + +/** + * This class represents a request model for rating an answer. + */ +public class RateRequestModel { + + private final int rating; + private final int answerId; + + /** + * Constructs a RateRequestModel with the specified parameters. + * + * @param rating The rating value for the answer. + * @param answerId The ID of the answer being rated. + */ + public RateRequestModel(int rating, int answerId) { + this.rating = rating; + this.answerId = answerId; + } + + /** + * Retrieves the rating value for the answer. + * + * @return The rating value. + */ + public int getRating() { + return rating; + } + + /** + * Retrieves the ID of the answer being rated. + * + * @return The answer ID. + */ + public int getAnswerId() { + return answerId; + } +} diff --git a/src/main/java/usecases/requests/RegistrationData.java b/src/main/java/usecases/requests/RegistrationData.java new file mode 100644 index 00000000..025f01bf --- /dev/null +++ b/src/main/java/usecases/requests/RegistrationData.java @@ -0,0 +1,92 @@ +package usecases.requests; + +import java.util.Set; + +/** + * This class represents registration data for a user, client, or attorney. + */ +public class RegistrationData { + // common fields for user registration + public int userId; + public String userName; + public String email; + public String password; + public String stateAbb; + public String postalCode; + + // fields for client registration + public String ethnicity; + public int age; + public String gender; + public String maritalStatus; + public int numberOfHousehold; + public float annualIncome; + public String password2; + + // fields for attorney registration + public Set professionals; + + public RegistrationData() { + } + + public RegistrationData(String userName, String email, String password, String password2, String stateAbb, String postalCode) { + this.userName = userName; + this.email = email; + this.password = password; + this.password2 = password2; + this.stateAbb = stateAbb; + this.postalCode = postalCode; + } + + public RegistrationData(Builder builder) { + this.userName = builder.userName; + this.email = builder.email; + this.password = builder.password; + this.password2 = builder.password2; + this.stateAbb = builder.stateAbb; + this.postalCode = builder.postalCode; + } + + public void setUserId(int userId) {this.userId = userId;} + + public static class Builder { + private String userName; + private String email; + private String password; + private String stateAbb; + private String postalCode; + private String password2; + + public Builder userName(String userName) { + this.userName = userName; + return this; + } + + public Builder email(String email) { + this.email = email; + return this; + } + + public Builder password(String password) { + this.password = password; + return this; + } + + public Builder stateAbb(String stateAbb) { + this.stateAbb = stateAbb; + return this; + } + + public Builder postalCode(String postalCode) { + this.postalCode = postalCode; + return this; + } + + public Builder password2(String password) { + this.password2 = password; + return this; + } + + public RegistrationData build() {return new RegistrationData(this);} + } +} diff --git a/src/main/java/usecases/requests/SelectRequestModel.java b/src/main/java/usecases/requests/SelectRequestModel.java new file mode 100644 index 00000000..c88027b7 --- /dev/null +++ b/src/main/java/usecases/requests/SelectRequestModel.java @@ -0,0 +1,28 @@ +package usecases.requests; + +/** + * This class represents a request model for selecting a question. + * It encapsulates the necessary information for selecting a question, including the question ID + * and the user ID requesting the selection. + */ +public class SelectRequestModel { + private final int questionId; + + /** + * Constructs a SelectRequestModel with the provided question ID and user ID. + * + * @param questionId The ID of the question to be selected. + */ + public SelectRequestModel(int questionId) { + this.questionId = questionId; + } + + /** + * Gets the question ID from the request model. + * + * @return The ID of the question. + */ + public int getQuestionId() { + return questionId; + } +} diff --git a/src/main/java/usecases/requests/UserLoginRequestModel.java b/src/main/java/usecases/requests/UserLoginRequestModel.java new file mode 100644 index 00000000..cfad5a60 --- /dev/null +++ b/src/main/java/usecases/requests/UserLoginRequestModel.java @@ -0,0 +1,39 @@ +package usecases.requests; + +/** + * This class represents a user login request model. + * It encapsulates the information needed for a user to perform a login operation. + */ +public class UserLoginRequestModel { + private final int userId; + private final String password; + + /** + * Constructs a new UserLoginRequestModel with the provided user ID and password. + * + * @param userId The unique identifier of the user. + * @param password The user's password. + */ + public UserLoginRequestModel(int userId, String password) { + this.userId = userId; + this.password = password; + } + + /** + * Retrieves the user ID from the login request. + * + * @return The user's unique identifier. + */ + public int getUserId() { + return userId; + } + + /** + * Retrieves the password from the login request. + * + * @return The user's password. + */ + public String getPassword() { + return password; + } +} diff --git a/src/main/java/usecases/responses/BaseResponseModel.java b/src/main/java/usecases/responses/BaseResponseModel.java new file mode 100644 index 00000000..80934674 --- /dev/null +++ b/src/main/java/usecases/responses/BaseResponseModel.java @@ -0,0 +1,8 @@ +package usecases.responses; + +/** + * This class represents a simple and convenient an empty response. + */ + +public class BaseResponseModel { +} diff --git a/src/main/java/businessrule/responsemodel/TheQuestionResponseModel.java b/src/main/java/usecases/responses/TheQuestionResponseModel.java similarity index 80% rename from src/main/java/businessrule/responsemodel/TheQuestionResponseModel.java rename to src/main/java/usecases/responses/TheQuestionResponseModel.java index ace4a0b2..fa06fcdb 100644 --- a/src/main/java/businessrule/responsemodel/TheQuestionResponseModel.java +++ b/src/main/java/usecases/responses/TheQuestionResponseModel.java @@ -1,16 +1,20 @@ -package businessrule.responsemodel; +package usecases.responses; -import businessrule.usecase.util.PostDisplayFormatter; +import usecases.dto.PostDisplay; import java.time.LocalDate; import java.util.Map; +/** + * This class represents a question with detailed information. + * It is including its user, title, type, deadline, closure status, and associated posts. + */ public class TheQuestionResponseModel extends UserResponseModel{ private final int questionId; private final String title; private final String type; private final LocalDate deadline; private final boolean isClose; - private final Map postMap; + private final Map postMap; private TheQuestionResponseModel(Builder builder) { super(builder.userId, builder.userName, builder.userType); @@ -22,7 +26,7 @@ private TheQuestionResponseModel(Builder builder) { this.postMap = builder.postMap; } - public TheQuestionResponseModel(int userId, String userName, String userType, int questionId, String title, String type, LocalDate deadline, boolean isClose, Map postMap) { + public TheQuestionResponseModel(int userId, String userName, String userType, int questionId, String title, String type, LocalDate deadline, boolean isClose, Map postMap) { super(userId, userName, userType); this.questionId = questionId; this.title = title; @@ -33,15 +37,15 @@ public TheQuestionResponseModel(int userId, String userName, String userType, in } public static class Builder { - private int userId; - private String userName; - private String userType; + private final int userId; + private final String userName; + private final String userType; private int questionId; private String questionTitle; private String questionType; private LocalDate deadline; private boolean isClose; - private Map postMap; + private Map postMap; public Builder(UserResponseModel userModel) { this.userId = userModel.getUserId(); @@ -74,7 +78,7 @@ public Builder isClose(boolean isClose) { return this; } - public Builder postMap(Map postMap) { + public Builder postMap(Map postMap) { this.postMap = postMap; return this; } @@ -100,7 +104,7 @@ public LocalDate getDeadline() { public boolean isClose() {return isClose;} - public Map getPostMap() { + public Map getPostMap() { return postMap; } } diff --git a/src/main/java/businessrule/responsemodel/UserResponseModel.java b/src/main/java/usecases/responses/UserResponseModel.java similarity index 68% rename from src/main/java/businessrule/responsemodel/UserResponseModel.java rename to src/main/java/usecases/responses/UserResponseModel.java index 017fbdb1..a715f651 100644 --- a/src/main/java/businessrule/responsemodel/UserResponseModel.java +++ b/src/main/java/usecases/responses/UserResponseModel.java @@ -1,10 +1,20 @@ -package businessrule.responsemodel; +package usecases.responses; +/** + * This class represents a response model to store the user specific output. + */ public class UserResponseModel extends BaseResponseModel{ private final int userId; private final String userName; private final String userType; + /** + * Constructs a new UserResponseModel object with the specified user information. + * + * @param userId The unique identifier of the user. + * @param userName The name of the user. + * @param userType The type of the user. + */ public UserResponseModel(int userId, String userName, String userType) { this.userId = userId; this.userName = userName; diff --git a/src/main/java/businessrule/responsemodel/ViewResponseModel.java b/src/main/java/usecases/responses/ViewResponseModel.java similarity index 64% rename from src/main/java/businessrule/responsemodel/ViewResponseModel.java rename to src/main/java/usecases/responses/ViewResponseModel.java index 8aaf994b..e74550c7 100644 --- a/src/main/java/businessrule/responsemodel/ViewResponseModel.java +++ b/src/main/java/usecases/responses/ViewResponseModel.java @@ -1,35 +1,38 @@ -package businessrule.responsemodel; +package usecases.responses; -import businessrule.usecase.util.QuestionDisplayFormatter; +import usecases.dto.QuestionDisplay; import java.util.Map; +/** + * This class represents response data related to the question list shown to the user. + */ public class ViewResponseModel extends UserResponseModel{ - private final Map questionMap; + private final Map questionMap; public ViewResponseModel(Builder builder) { super(builder.userId, builder.userName, builder.userType); this.questionMap = builder.questionMap; } - public ViewResponseModel(int userId, String userName, String userType, Map questionMap) { + public ViewResponseModel(int userId, String userName, String userType, Map questionMap) { super(userId, userName, userType); this.questionMap = questionMap; } public static class Builder { - private int userId; - private String userName; - private String userType; - private Map questionMap; + private final int userId; + private final String userName; + private final String userType; + private Map questionMap; public Builder(UserResponseModel userModel) { this.userId = userModel.getUserId(); this.userName = userModel.getUserName(); this.userType = userModel.getUserType(); } - public Builder questionMap(Map questionMap) { + public Builder questionMap(Map questionMap) { this.questionMap = questionMap; return this; } @@ -39,7 +42,7 @@ public ViewResponseModel build() { } } - public Map getQuestionMap() { + public Map getQuestionMap() { return questionMap; } } diff --git a/src/main/java/businessrule/SessionManager.java b/src/main/java/usecases/session/SessionManager.java similarity index 80% rename from src/main/java/businessrule/SessionManager.java rename to src/main/java/usecases/session/SessionManager.java index 59077327..90d87e32 100644 --- a/src/main/java/businessrule/SessionManager.java +++ b/src/main/java/usecases/session/SessionManager.java @@ -1,5 +1,8 @@ -package businessrule; +package usecases.session; +/** + * This class manages the life-cycle of the user session. + */ public class SessionManager { // The current active user session. diff --git a/src/main/java/businessrule/UserSession.java b/src/main/java/usecases/session/UserSession.java similarity index 58% rename from src/main/java/businessrule/UserSession.java rename to src/main/java/usecases/session/UserSession.java index 5f76dd81..30576c5b 100644 --- a/src/main/java/businessrule/UserSession.java +++ b/src/main/java/usecases/session/UserSession.java @@ -1,9 +1,11 @@ -package businessrule; +package usecases.session; -import businessrule.responsemodel.UserResponseModel; -import driver.screen.UIManager; -import driver.screen.UserUI; +import usecases.responses.UserResponseModel; +/** + * This class represents a session for a specific user. + * It stores key user information and carries over it once the user login. + */ public class UserSession { private final UserResponseModel userResponseModel; diff --git a/src/main/java/businessrule/usecase/util/BuilderService.java b/src/main/java/usecases/utils/BuilderService.java similarity index 66% rename from src/main/java/businessrule/usecase/util/BuilderService.java rename to src/main/java/usecases/utils/BuilderService.java index ab59f0c3..98576ba1 100644 --- a/src/main/java/businessrule/usecase/util/BuilderService.java +++ b/src/main/java/usecases/utils/BuilderService.java @@ -1,13 +1,16 @@ -package businessrule.usecase.util; +package usecases.utils; -import businessrule.responsemodel.TheQuestionResponseModel; -import businessrule.responsemodel.UserResponseModel; -import businessrule.responsemodel.ViewResponseModel; -import entity.Question; - -import java.util.HashMap; +import usecases.dto.PostDisplay; +import usecases.dto.QuestionDisplay; +import usecases.responses.TheQuestionResponseModel; +import usecases.responses.UserResponseModel; +import usecases.responses.ViewResponseModel; +import entities.Question; import java.util.Map; +/** + * This class provides builder service to construct the question response model and the view response model from base. + */ public class BuilderService { public static final BuilderService INSTANCE = new BuilderService(); @@ -17,7 +20,7 @@ public BuilderService() { public static BuilderService getInstance() { return INSTANCE; } - public TheQuestionResponseModel constructTheQuestionResponse(Question question, UserResponseModel response, Map postMap) { + public TheQuestionResponseModel constructTheQuestionResponse(Question question, UserResponseModel response, Map postMap) { return response.toQuestionResponseBuilder() .questionId(question.getQuestionId()) .questionTitle(question.getTitle()) @@ -28,7 +31,7 @@ public TheQuestionResponseModel constructTheQuestionResponse(Question question, .build(); } - public ViewResponseModel constructViewResponse(UserResponseModel response, Map questionMap) { + public ViewResponseModel constructViewResponse(UserResponseModel response, Map questionMap) { return response.toViewResponseBuilder() .questionMap(questionMap) .build(); diff --git a/src/main/java/businessrule/usecase/util/CredentialChecker.java b/src/main/java/usecases/utils/CredentialChecker.java similarity index 63% rename from src/main/java/businessrule/usecase/util/CredentialChecker.java rename to src/main/java/usecases/utils/CredentialChecker.java index cc63bc32..a062d8ae 100644 --- a/src/main/java/businessrule/usecase/util/CredentialChecker.java +++ b/src/main/java/usecases/utils/CredentialChecker.java @@ -1,6 +1,12 @@ -package businessrule.usecase.util; +package usecases.utils; import org.apache.commons.validator.routines.EmailValidator; -import java.util.regex.*; + +/** + * This is a utility class for checking various credentials and data formats. + * The "CredentialChecker" class provides methods to validate different types of credentials, + * ensuring that they meet specific criteria or follow certain patterns. +*/ + public class CredentialChecker { diff --git a/src/main/java/businessrule/usecase/util/EmailNotificationSender.java b/src/main/java/usecases/utils/EmailNotificationSender.java similarity index 82% rename from src/main/java/businessrule/usecase/util/EmailNotificationSender.java rename to src/main/java/usecases/utils/EmailNotificationSender.java index 40990af2..66648ee9 100644 --- a/src/main/java/businessrule/usecase/util/EmailNotificationSender.java +++ b/src/main/java/usecases/utils/EmailNotificationSender.java @@ -1,4 +1,4 @@ -package businessrule.usecase.util; +package usecases.utils; import javax.mail.Message; import javax.mail.Session; @@ -7,6 +7,9 @@ import javax.mail.internet.MimeMessage; import java.util.Properties; +/** + * This class provides emailing service for other usecase. + */ public class EmailNotificationSender{ private static final String address ="2066465318@qq.com"; @@ -17,7 +20,13 @@ public static void main(String[] args) throws Exception { sendEmail("josephpc0612@gmail.com","test title", "test content"); } - public static void sendEmail(String email, String title, String content) throws Exception{ + /** + * + * @param email The email address of the user, should be in valid format. + * @param title The tile of the email. + * @param content The specific content of the email. + */ + public static void sendEmail(String email, String title, String content) throws Exception { Properties properties = new Properties(); properties.put("mail.transport.protocol", "smtp"); properties.put("mail.smtp.host", "smtp.qq.com"); @@ -33,7 +42,6 @@ public static void sendEmail(String email, String title, String content) throws message.setFrom(new InternetAddress(address)); message.setRecipients(Message.RecipientType.TO, new InternetAddress[]{new InternetAddress(email)}); - //message.setRecipient(Message.RecipientType.TO, new InternetAddress("xxx@qq.com")); message.setSubject(title); diff --git a/src/main/java/businessrule/usecase/util/MatchingHandler.java b/src/main/java/usecases/utils/MatchingHandler.java similarity index 82% rename from src/main/java/businessrule/usecase/util/MatchingHandler.java rename to src/main/java/usecases/utils/MatchingHandler.java index 57b7489d..762a3616 100644 --- a/src/main/java/businessrule/usecase/util/MatchingHandler.java +++ b/src/main/java/usecases/utils/MatchingHandler.java @@ -1,27 +1,28 @@ -package businessrule.usecase.util; +package usecases.utils; -import businessrule.gateway.AttorneyGateway; -import businessrule.gateway.ClientGateway; -import businessrule.gateway.QuestionGateway; -import businessrule.requestmodel.RegistrationData; +import usecases.dto.Matching; +import usecases.dto.MatchingResult; +import usecases.gateway.AttorneyGateway; +import usecases.gateway.ClientGateway; +import usecases.gateway.QuestionGateway; import com.fasterxml.jackson.core.JsonProcessingException; import java.io.*; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; -import driver.database.AttorneyRepository; -import driver.database.ClientRepository; -import driver.database.QuestionRepo; -import entity.Post; -import entity.Question; -import entity.Attorney; -import entity.Client; +import entities.Question; +import entities.user.Attorney; +import entities.user.Client; import java.nio.file.Files; import java.nio.file.Path; import java.util.*; import com.fasterxml.jackson.databind.ObjectMapper; -import entity.factory.ClientFactory; + import java.nio.charset.StandardCharsets; +/** + * This class represents the process of matching questions with attorneys. + * It uses various gateways and utility classes to reach this process. + */ public class MatchingHandler { final AttorneyGateway attorneyGateway; final ClientGateway clientGateway; @@ -29,12 +30,22 @@ public class MatchingHandler { final ObjectMapper objectMapper = new ObjectMapper(); final PythonReader pythonReader = new PythonReader(); + /** + * Constructs a `MatchingHandler` object with the given gateways. + * + * @param attorneyGateway The gateway for accessing attorney information. + * @param clientGateway The gateway for accessing client information. + * @param questionGateway The gateway for accessing question information. + */ public MatchingHandler(AttorneyGateway attorneyGateway, ClientGateway clientGateway, QuestionGateway questionGateway) { this.attorneyGateway = attorneyGateway; this.clientGateway = clientGateway; this.questionGateway = questionGateway; } + /** + * Initiates the matching process by obtaining matching results and updating recommendations. + */ public void match() { try { MatchingResult matchingResult = getMatching(); @@ -44,6 +55,11 @@ public void match() { } } + /** + * Updates attorney recommendations based on the matching results. + * + * @param matchingResult The result of the matching process. + */ public void updateMatching(MatchingResult matchingResult) { // clear all matching attorneyGateway.clearAllRecommendations(); @@ -55,6 +71,12 @@ public void updateMatching(MatchingResult matchingResult) { } } + /** + * Retrieves matching results by processing questions and attorneys. + * + * @return The matching results. + * @throws IOException If there's an I/O error during the process. + */ public MatchingResult getMatching() throws IOException { List questionList = questionGateway.getNotTakenQuestion(); List attorneyList = attorneyGateway.getAll(); @@ -68,6 +90,7 @@ public MatchingResult getMatching() throws IOException { return new MatchingResult(matchingList); } + private List pythonMatching(List questions, List attorneys, Map weights) throws IOException{ Map stringWeights = new HashMap<>(); for (Map.Entry entry : weights.entrySet()) { diff --git a/src/main/java/usecases/utils/PostMapConstructor.java b/src/main/java/usecases/utils/PostMapConstructor.java new file mode 100644 index 00000000..884c279e --- /dev/null +++ b/src/main/java/usecases/utils/PostMapConstructor.java @@ -0,0 +1,59 @@ +package usecases.utils; + +import usecases.dto.PostDisplay; +import usecases.gateway.UserGateway; +import infrastructure.database.UserGatewayFactory; +import entities.Post; +import entities.Question; +import entities.user.User; + +import java.time.LocalDate; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +/** + * This class represents a map of post IDs to formatted post display information. + * It uses a `UserGatewayFactory` to create user gateways for retrieving user information. + */ +public class PostMapConstructor { + private final UserGatewayFactory userGatewayFactory; + + /** + * Constructs a `PostMapConstructor` object with the given `UserGatewayFactory`. + * + * @param userGatewayFactory The factory for creating user gateways. + */ + public PostMapConstructor(UserGatewayFactory userGatewayFactory) {this.userGatewayFactory = userGatewayFactory;} + + /** + * Constructs a map of post IDs to formatted post display information based on the given question. + * + * @param question The question containing posts to be mapped. + * @return A map of post IDs to formatted post display information. + */ + public Map constructPostMap(Question question) { + List postList = question.getPosts(); + + // initialize post map + Map postMap = new LinkedHashMap<>(); + + // handle the empty post list + if (postList.isEmpty()) {return postMap;} + + // for each post list, find the data needed + for (Post post: postList) { + int postId = post.getPostId(); + UserGateway userGateway = userGatewayFactory.createUserGateway(post.getBelongsTo()); + User user = userGateway.get(post.getBelongsTo()); + String userType = user.getUserType(); + String name = user.getUserName(); + String postText = post.getPostText(); + LocalDate createAt = post.getCreateAt(); + PostDisplay postDisplayFormatter = new PostDisplay(postText, userType, name, createAt); + // put every data needed to the post map + postMap.put(postId, postDisplayFormatter); + } + return postMap; + } +} diff --git a/src/main/java/businessrule/usecase/util/PythonReader.java b/src/main/java/usecases/utils/PythonReader.java similarity index 71% rename from src/main/java/businessrule/usecase/util/PythonReader.java rename to src/main/java/usecases/utils/PythonReader.java index 89b8155f..df0ae22e 100644 --- a/src/main/java/businessrule/usecase/util/PythonReader.java +++ b/src/main/java/usecases/utils/PythonReader.java @@ -1,12 +1,22 @@ -package businessrule.usecase.util; +package usecases.utils; import org.apache.commons.io.IOUtils; + import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; +/** + * This class represents a utility for executing Python scripts and reading their output. + */ public class PythonReader { + /** + * Executes a Python command and reads the output. + * + * @param command The Python command to execute. + * @return The output generated by the Python script. + */ public String readPythonExec(String command) { Process proc; try { @@ -21,6 +31,7 @@ public String readPythonExec(String command) { return IOUtils.toString(inputStream, StandardCharsets.UTF_8); } catch (IOException | InterruptedException e) { e.printStackTrace(); - } return null; + } + return null; } } diff --git a/src/main/java/usecases/utils/QuestionMapConstructor.java b/src/main/java/usecases/utils/QuestionMapConstructor.java new file mode 100644 index 00000000..2be4996c --- /dev/null +++ b/src/main/java/usecases/utils/QuestionMapConstructor.java @@ -0,0 +1,38 @@ +package usecases.utils; + +import entities.Question; +import usecases.dto.QuestionDisplay; + +import java.time.LocalDate; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +/** + * This class constructs a map of question IDs to formatted display objects using `QuestionDisplayFormatter`. + */ +public class QuestionMapConstructor { + + /** + * Constructs a map of question IDs to formatted display objects. + * + * @param questionList The list of questions to be processed. + * @return A map where each question's ID is associated with a corresponding formatted display object. + */ + public Map constructQuestionMap(List questionList) { + Map questionMap = new LinkedHashMap<>(); + if (questionList.isEmpty()) { + return questionMap; + } + for (Question question: questionList) { + int questionId = question.getQuestionId(); + String title = question.getTitle(); + String type = question.getType(); + LocalDate legalDeadline = question.getLegalDeadline(); + boolean isClose = question.isClose(); + QuestionDisplay questionDisplayFormatter = new QuestionDisplay(title, type, legalDeadline, isClose); + questionMap.put(questionId, questionDisplayFormatter); + } + return questionMap; + } +} diff --git a/src/main/java/usecases/utils/RandomNumberGenerator.java b/src/main/java/usecases/utils/RandomNumberGenerator.java new file mode 100644 index 00000000..724ebd7c --- /dev/null +++ b/src/main/java/usecases/utils/RandomNumberGenerator.java @@ -0,0 +1,77 @@ +package usecases.utils; + +import java.util.Random; + +/** + * This class provides utility methods to generate random numbers and create unique IDs for different entities. + */ +public class RandomNumberGenerator { + + /** + * Generates a random integer with the specified number of digits. + * + * @param digit The number of digits the generated random number should have. + * @return A random integer with the specified number of digits. + */ + public static int generate(int digit) { + Random random = new Random(); + int min = (int) Math.pow(10, digit - 1); + int max = (int) Math.pow(10, digit) - 1; + + return random.nextInt(max - min + 1) + min; + } + + /** + * Adds a digit to the front of an existing number. + * + * @param number The existing number. + * @param digit The digit to be added to the front. + * @return The resulting number after adding the digit to the front. + */ + public static int addDigitToFront(int number, int digit) { + String numAsStr = String.valueOf(number); + String digAsStr = String.valueOf(digit); + String res = digAsStr + numAsStr; + return Integer.parseInt(res); + } + + /** + * Generates a unique attorney ID with the specified number of digits. + * + * @param digit The number of digits the generated attorney ID should have. + * @return A unique attorney ID with the specified number of digits. + */ + public int generateAttorneyId(int digit) { + return addDigitToFront(generate(digit - 1), 1); + } + + /** + * Generates a unique client ID with the specified number of digits. + * + * @param digit The number of digits the generated client ID should have. + * @return A unique client ID with the specified number of digits. + */ + public int generateClientId(int digit) { + return addDigitToFront(generate(digit - 1), 2); + } + + /** + * Generates a unique question ID with the specified number of digits. + * + * @param digit The number of digits the generated question ID should have. + * @return A unique question ID with the specified number of digits. + */ + public int generateQuestionId(int digit) { + return addDigitToFront(generate(digit - 1), 3); + } + + /** + * Generates a unique post ID with the specified number of digits. + * + * @param digit The number of digits the generated post ID should have. + * @return A unique post ID with the specified number of digits. + */ + public int generatePostId(int digit) { + return addDigitToFront(generate(digit - 1), 4); + } +} \ No newline at end of file diff --git a/src/test/java/controllertesting/CloseQuestionControlTest.java b/src/test/java/controllertesting/CloseQuestionControlTest.java index 8d4cb770..d0047952 100644 --- a/src/test/java/controllertesting/CloseQuestionControlTest.java +++ b/src/test/java/controllertesting/CloseQuestionControlTest.java @@ -1,13 +1,16 @@ package controllertesting; -import adapter.controller.CloseQuestionControl; -import businessrule.inputboundary.CloseInputBoundary; -import businessrule.requestmodel.CloseRequestModel; -import businessrule.responsemodel.UserResponseModel; +import adapters.controllers.CloseQuestionControl; +import usecases.inputboundary.CloseInputBoundary; +import usecases.requests.CloseRequestModel; +import usecases.responses.UserResponseModel; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.*; +/** + * This class contains unit tests for the CloseQuestionControl class. + */ public class CloseQuestionControlTest { private static final int QUESTION_ID = 333333333; private static final int USER_ID = 11234567; @@ -15,10 +18,16 @@ public class CloseQuestionControlTest { private static final String USER_TYPE = "Client"; private static UserResponseModel expectedResponse; + /** + * Sets up the necessary resources for testing CloseQuestionControl. + */ public void setUpCloseQuestionControl() { expectedResponse = new UserResponseModel(USER_ID, USERNAME, USER_TYPE); } + /** + * Tests the closeQuestion method of CloseQuestionControl. + */ @Test public void testCloseQuestion() { setUpCloseQuestionControl(); diff --git a/src/test/java/controllertesting/ControlContainerTest.java b/src/test/java/controllertesting/ControlContainerTest.java index 86482152..c077b8bf 100644 --- a/src/test/java/controllertesting/ControlContainerTest.java +++ b/src/test/java/controllertesting/ControlContainerTest.java @@ -1,80 +1,107 @@ package controllertesting; -import adapter.controller.*; +import adapters.controllers.*; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.*; +/** + * This class contains unit tests for the ControlContainer class. + */ public class ControlContainerTest { + /** + * Tests the getClientRegisterControl method of ControlContainer. + */ @Test public void testGetClientRegisterControl() { - ClientRegisterControl mockClientRegisterControl = mock(ClientRegisterControl.class); - ControlContainer controlContainer = new ControlContainer(mockClientRegisterControl, null, null, null, null, null, null, null, null, null); - assertEquals(mockClientRegisterControl, controlContainer.getClientRegisterControl()); + RegisterControl mockRegisterControl = mock(RegisterControl.class); + ControlContainer controlContainer = new ControlContainer(mockRegisterControl, null, null, null, null, null, null, null, null, null, null); + assertEquals(mockRegisterControl, controlContainer.getClientRegisterControl()); } + /** + * Tests the getCloseQuestionControl method of ControlContainer. + */ @Test public void testGetCloseQuestionControl() { CloseQuestionControl mockCloseQuestionControl = mock(CloseQuestionControl.class); - ControlContainer controlContainer = new ControlContainer(null, mockCloseQuestionControl, null, null, null, null, null, null, null, null); + ControlContainer controlContainer = new ControlContainer(null, mockCloseQuestionControl, null, null, null, null, null, null, null, null, null); assertEquals(mockCloseQuestionControl, controlContainer.getCloseQuestionControl()); } + /** + * Tests the getPostControl method of ControlContainer. + */ @Test public void testGetPostControl() { PostControl mockPostControl = mock(PostControl.class); - ControlContainer controlContainer = new ControlContainer(null, null, mockPostControl, null, null, null, null, null, null, null); + ControlContainer controlContainer = new ControlContainer(null, null, mockPostControl, null, null, null, null, null, null, null, null); assertEquals(mockPostControl, controlContainer.getPostControl()); } + /** + * Tests the getQuestionControl method of ControlContainer. + */ @Test public void testGetQuestionControl() { QuestionControl mockQuestionControl = mock(QuestionControl.class); - ControlContainer controlContainer = new ControlContainer(null, null, null, mockQuestionControl, null, null, null, null, null, null); + ControlContainer controlContainer = new ControlContainer(null, null, null, mockQuestionControl, null, null, null, null, null, null, null); assertEquals(mockQuestionControl, controlContainer.getQuestionControl()); } + /** + * Tests the getRateControl method of ControlContainer. + */ @Test public void testGetRateControl() { RateControl mockRateControl = mock(RateControl.class); - ControlContainer controlContainer = new ControlContainer(null, null, null, null, mockRateControl, null, null, null, null, null); + ControlContainer controlContainer = new ControlContainer(null, null, null, null, mockRateControl, null, null, null, null, null, null); assertEquals(mockRateControl, controlContainer.getRateControl()); } + /** + * Tests the getSelectQuestionControl method of ControlContainer. + */ @Test public void testGetSelectQuestionControl() { SelectQuestionControl mockSelectQuestionControl = mock(SelectQuestionControl.class); - ControlContainer controlContainer = new ControlContainer(null, null, null, null, null, mockSelectQuestionControl, null, null, null, null); + ControlContainer controlContainer = new ControlContainer(null, null, null, null, null, mockSelectQuestionControl, null, null, null, null, null); assertEquals(mockSelectQuestionControl, controlContainer.getSelectQuestionControl()); } + /** + * Tests the getUserLoginControl method of ControlContainer. + */ @Test public void testGetUserLoginControl() { UserLoginControl mockUserLoginControl = mock(UserLoginControl.class); - ControlContainer controlContainer = new ControlContainer(null, null, null, null, null, null, mockUserLoginControl, null, null, null); + ControlContainer controlContainer = new ControlContainer(null, null, null, null, null, null, mockUserLoginControl, null, null, null, null); assertEquals(mockUserLoginControl, controlContainer.getUserLoginControl()); } + /** + * Tests the getViewQuestionControl method of ControlContainer. + */ @Test public void testGetViewQuestionControl() { ViewQuestionControl mockViewQuestionControl = mock(ViewQuestionControl.class); - ControlContainer controlContainer = new ControlContainer(null, null, null, null, null, null, null, mockViewQuestionControl, null, null); + ControlContainer controlContainer = new ControlContainer(null, null, null, null, null, null, null, mockViewQuestionControl, null, null, null); assertEquals(mockViewQuestionControl, controlContainer.getViewQuestionControl()); } @Test public void testGetBrowseQuestionControl() { ViewQuestionControl mockBrowseQuestionControl = mock(ViewQuestionControl.class); - ControlContainer controlContainer = new ControlContainer(null, null, null, null, null, null, null, null, mockBrowseQuestionControl, null); + ControlContainer controlContainer = new ControlContainer(null, null, null, null, null, null, null, null, mockBrowseQuestionControl, null, null); assertEquals(mockBrowseQuestionControl, controlContainer.getBrowseQuestionControl()); } @Test public void testGetRecommendationControl() { ViewQuestionControl mockRecommendationControl = mock(ViewQuestionControl.class); - ControlContainer controlContainer = new ControlContainer(null, null, null, null, null, null, null, null, null, mockRecommendationControl); + ControlContainer controlContainer = new ControlContainer(null, null, null, null, null, null, null, null, null, mockRecommendationControl, null); assertEquals(mockRecommendationControl, controlContainer.getRecommendationControl()); } } diff --git a/src/test/java/controllertesting/PostControlTest.java b/src/test/java/controllertesting/PostControlTest.java index 8a8c3c98..a498cca7 100644 --- a/src/test/java/controllertesting/PostControlTest.java +++ b/src/test/java/controllertesting/PostControlTest.java @@ -1,16 +1,19 @@ package controllertesting; -import adapter.controller.PostControl; -import businessrule.SessionManager; -import businessrule.UserSession; -import businessrule.inputboundary.PostInputBoundary; -import businessrule.requestmodel.PostRequestModel; -import businessrule.responsemodel.TheQuestionResponseModel; -import businessrule.responsemodel.UserResponseModel; +import adapters.controllers.PostControl; +import usecases.session.SessionManager; +import usecases.session.UserSession; +import usecases.inputboundary.PostInputBoundary; +import usecases.requests.PostRequestModel; +import usecases.responses.TheQuestionResponseModel; +import usecases.responses.UserResponseModel; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.*; +/** + * This class contains unit tests for the PostControl class. + */ public class PostControlTest { private static final int USER_ID = 11234567; private static final String USER_NAME = "SampleUser"; @@ -22,11 +25,17 @@ public class PostControlTest { private static TheQuestionResponseModel expectedResponse; + /** + * Sets up the necessary resources for testing PostControl. + */ public void setUpPostControl() { // Arrange expectedResponse = new TheQuestionResponseModel(USER_ID, USER_NAME, USER_TYPE, QUESTION_ID, QUESTION_TITLE, QUESTION_TYPE, null, false, null); } + /** + * Tests the createPost method of PostControl. + */ @Test public void testCreatePost() { setUpPostControl(); diff --git a/src/test/java/controllertesting/QuestionControlTest.java b/src/test/java/controllertesting/QuestionControlTest.java index aab26322..44b61e06 100644 --- a/src/test/java/controllertesting/QuestionControlTest.java +++ b/src/test/java/controllertesting/QuestionControlTest.java @@ -1,27 +1,32 @@ package controllertesting; -import adapter.controller.QuestionControl; -import businessrule.SessionManager; -import businessrule.UserSession; -import businessrule.inputboundary.QuestionInputBoundary; -import businessrule.requestmodel.QuestionRequestModel; -import businessrule.responsemodel.TheQuestionResponseModel; -import businessrule.responsemodel.UserResponseModel; +import adapters.controllers.QuestionControl; +import usecases.session.SessionManager; +import usecases.session.UserSession; +import usecases.inputboundary.QuestionInputBoundary; +import usecases.requests.QuestionRequestModel; +import usecases.responses.TheQuestionResponseModel; +import usecases.responses.UserResponseModel; import org.junit.jupiter.api.Test; import java.time.LocalDate; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.*; +/** + * This class contains unit tests for the QuestionControl class. + */ public class QuestionControlTest { private static final int USER_ID = 11234567; private static final int QUESTION_ID = 333333333; private static final String USER_NAME = "SampleUser"; private static final String TITLE = "SampleTitle"; private static final String TYPE = "SampleType"; - private static TheQuestionResponseModel expectedUserResponse; - public void setUpQuestionControl() { + /** + * Sets up the necessary resources for testing QuestionControl. + */ + public void setUpQuestionControl(){ // Arrange UserResponseModel userResponseModel = new UserResponseModel(USER_ID, USER_NAME, TYPE); UserSession userSession = new UserSession(userResponseModel); @@ -31,12 +36,16 @@ public void setUpQuestionControl() { } + /** + * Tests the createQuestion method of QuestionControl. + */ @Test public void testCreateQuestion() { setUpQuestionControl(); QuestionInputBoundary mockInputBoundary = mock(QuestionInputBoundary.class); when(mockInputBoundary.createQuestion(any(QuestionRequestModel.class))).thenReturn(expectedUserResponse); + when(mockInputBoundary.createQuestion(any(QuestionRequestModel.class))).thenReturn(expectedUserResponse); QuestionControl control = new QuestionControl(mockInputBoundary); // Sample input parameters diff --git a/src/test/java/controllertesting/RateControlTest.java b/src/test/java/controllertesting/RateControlTest.java index 331458e0..c9ef7504 100644 --- a/src/test/java/controllertesting/RateControlTest.java +++ b/src/test/java/controllertesting/RateControlTest.java @@ -1,23 +1,28 @@ package controllertesting; -import adapter.controller.RateControl; -import businessrule.SessionManager; -import businessrule.UserSession; -import businessrule.inputboundary.RateInputBoundary; -import businessrule.requestmodel.RateRequestModel; - -import businessrule.responsemodel.UserResponseModel; +import adapters.controllers.RateControl; +import usecases.session.SessionManager; +import usecases.session.UserSession; +import usecases.inputboundary.RateInputBoundary; +import usecases.requests.RateRequestModel; +import usecases.responses.UserResponseModel; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.*; +/** + * This class contains unit tests for the RateControl class. + */ public class RateControlTest { private static final int RATE = 5; private static final int USER_ID = 11123456; private static final int QUESTION_ID = 333333333; private static UserResponseModel expectedResponse; + /** + * Sets up the necessary resources for testing RateControl. + */ public void setUpRateControl() { // Arrange expectedResponse = new UserResponseModel(USER_ID, "SampleUser", "SampleType"); @@ -28,6 +33,9 @@ public void setUpRateControl() { SessionManager.setSession(userSession); } + /** + * Tests the rateAnswer method of RateControl. + */ @Test public void testRateAnswer() { setUpRateControl(); diff --git a/src/test/java/controllertesting/ClientRegisterControlTest.java b/src/test/java/controllertesting/RegisterControlTest.java similarity index 57% rename from src/test/java/controllertesting/ClientRegisterControlTest.java rename to src/test/java/controllertesting/RegisterControlTest.java index 943d5d77..4ffc74e3 100644 --- a/src/test/java/controllertesting/ClientRegisterControlTest.java +++ b/src/test/java/controllertesting/RegisterControlTest.java @@ -1,16 +1,20 @@ package controllertesting; -import adapter.controller.ClientRegisterControl; -import businessrule.inputboundary.UserRegisterInputBoundary; -import businessrule.requestmodel.RegistrationData; -import businessrule.responsemodel.BaseResponseModel; +import adapters.controllers.RegisterControl; +import usecases.inputboundary.UserRegisterInputBoundary; +import usecases.requests.ClientRegistrationData; +import usecases.requests.RegistrationData; +import usecases.responses.BaseResponseModel; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.mockito.Mockito.*; -public class ClientRegisterControlTest { +/** + * This class contains unit tests for the ClientRegisterControl class. + */ +public class RegisterControlTest { private static final String USER_NAME = "SampleUser"; private static final String EMAIL = "sample@example.com"; private static final String PASSWORD1 = "test password"; @@ -26,11 +30,17 @@ public class ClientRegisterControlTest { private static BaseResponseModel expectedResponse; + /** + * Sets up the needed resources for testing ClientRegisterControl. + */ public void setUpClientRegisterControl() { // Arrange expectedResponse = new BaseResponseModel(); } + /** + * Tests the create method of ClientRegisterControl. + */ @Test public void testCreateClientRegistration() { setUpClientRegisterControl(); @@ -38,11 +48,20 @@ public void testCreateClientRegistration() { UserRegisterInputBoundary mockInputBoundary = mock(UserRegisterInputBoundary.class); when(mockInputBoundary.create(any(RegistrationData.class))).thenReturn(expectedResponse); - ClientRegisterControl control = new ClientRegisterControl(mockInputBoundary); + RegisterControl control = new RegisterControl(mockInputBoundary); + + RegistrationData data = new RegistrationData(USER_NAME, EMAIL, PASSWORD1, PASSWORD2, STATE_ABB, POSTAL_CODE); + ClientRegistrationData inputData = new ClientRegistrationData.Builder(data) + .age(AGE) + .annualIncome(ANNUAL_INCOME) + .gender(GENDER) + .maritalStatus(MARITAL_STATUS) + .numberOfHousehold(NUMBER_OF_HOUSEHOLD) + .ethnicity(ETHNICITY) + .build(); // Act - BaseResponseModel actualResponse = control.create(USER_NAME, EMAIL, PASSWORD1, PASSWORD2, STATE_ABB, - POSTAL_CODE, ETHNICITY, AGE, GENDER, MARITAL_STATUS, NUMBER_OF_HOUSEHOLD, ANNUAL_INCOME); + BaseResponseModel actualResponse = control.create(inputData); // Assert assertNotNull(actualResponse); diff --git a/src/test/java/controllertesting/SelectQuestionControlTest.java b/src/test/java/controllertesting/SelectQuestionControlTest.java index 42a9397c..7b2ec584 100644 --- a/src/test/java/controllertesting/SelectQuestionControlTest.java +++ b/src/test/java/controllertesting/SelectQuestionControlTest.java @@ -1,18 +1,21 @@ package controllertesting; -import adapter.controller.SelectQuestionControl; -import businessrule.SessionManager; -import businessrule.UserSession; -import businessrule.inputboundary.SelectInputBoundary; -import businessrule.requestmodel.SelectRequestModel; -import businessrule.responsemodel.TheQuestionResponseModel; - -import businessrule.responsemodel.UserResponseModel; +import adapters.controllers.SelectQuestionControl; +import usecases.session.SessionManager; +import usecases.session.UserSession; +import usecases.inputboundary.SelectInputBoundary; +import usecases.requests.SelectRequestModel; +import usecases.responses.TheQuestionResponseModel; + +import usecases.responses.UserResponseModel; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.*; +/** + * This class contains unit tests for the SelectQuestionControl class. + */ public class SelectQuestionControlTest { private static final int QUESTION_ID = 323456783; private static final int USER_ID = 11233456; @@ -22,6 +25,9 @@ public class SelectQuestionControlTest { private static final String QUESTION_TYPE = "sample question type"; private static TheQuestionResponseModel expectedResponse; + /** + * Sets up the necessary resources for testing SelectQuestionControl. + */ public void setUpSelectQuestionControl() { // Arrange expectedResponse = new TheQuestionResponseModel(USER_ID, USER_NAME, USER_TYPE, QUESTION_ID, QUESTION_TITLE, QUESTION_TYPE, null, false, null); @@ -32,6 +38,9 @@ public void setUpSelectQuestionControl() { SessionManager.setSession(userSession); } + /** + * Tests the selectQuestion method of SelectQuestionControl. + */ @Test public void testSelectQuestion() { setUpSelectQuestionControl(); diff --git a/src/test/java/controllertesting/UserLoginControlTest.java b/src/test/java/controllertesting/UserLoginControlTest.java index 4db13995..7c67a35e 100644 --- a/src/test/java/controllertesting/UserLoginControlTest.java +++ b/src/test/java/controllertesting/UserLoginControlTest.java @@ -1,25 +1,34 @@ package controllertesting; -import adapter.controller.UserLoginControl; -import businessrule.inputboundary.UserLoginInputBoundary; -import businessrule.requestmodel.UserLoginRequestModel; +import adapters.controllers.UserLoginControl; +import usecases.inputboundary.UserLoginInputBoundary; +import usecases.requests.UserLoginRequestModel; -import businessrule.responsemodel.UserResponseModel; +import usecases.responses.UserResponseModel; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.*; +/** + * This class contains unit tests for the UserLoginControl class. + */ public class UserLoginControlTest { private static final int USER_ID = 11234567; private static final String PASSWORD = "test password"; private static UserResponseModel expectedResponse; + /** + * Sets up the necessary resources for testing UserLoginControl. + */ public void setUpUserLoginControl() { // Arrange expectedResponse = new UserResponseModel(USER_ID, "SampleUser", "SampleType"); } + /** + * Tests the login method of UserLoginControl. + */ @Test public void testLogin() { setUpUserLoginControl(); diff --git a/src/test/java/controllertesting/ViewQuestionControlTest.java b/src/test/java/controllertesting/ViewQuestionControlTest.java index aa707232..f0c5ef40 100644 --- a/src/test/java/controllertesting/ViewQuestionControlTest.java +++ b/src/test/java/controllertesting/ViewQuestionControlTest.java @@ -1,23 +1,32 @@ package controllertesting; -import adapter.controller.ViewQuestionControl; -import businessrule.inputboundary.ViewInputBoundary; -import businessrule.responsemodel.ViewResponseModel; +import adapters.controllers.ViewQuestionControl; +import usecases.inputboundary.ViewInputBoundary; +import usecases.responses.ViewResponseModel; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.*; +/** + * This class contains unit tests for the ViewQuestionControl class. + */ public class ViewQuestionControlTest { private static final int USER_ID = 11234567; private static final String USER_NAME = "SampleUser"; private static final String USER_TYPE = "SampleType"; private static ViewResponseModel expectedResponse; + /** + * Sets up the necessary resources for testing ViewQuestionControl. + */ public void setUpViewQuestionControl() { // Arrange expectedResponse = new ViewResponseModel(USER_ID, USER_NAME, USER_TYPE, null); } + /** + * Tests the viewQuestion method of ViewQuestionControl. + */ @Test public void testViewQuestion() { setUpViewQuestionControl(); diff --git a/src/test/java/entitytesting/AttorneyTest.java b/src/test/java/entitytesting/AttorneyTest.java index 8eb378de..7fd926c9 100644 --- a/src/test/java/entitytesting/AttorneyTest.java +++ b/src/test/java/entitytesting/AttorneyTest.java @@ -1,12 +1,16 @@ package entitytesting; -import businessrule.requestmodel.RegistrationData; -import entity.Attorney; -import entity.Question; -import entity.factory.AttorneyFactory; +import usecases.requests.RegistrationData; +import entities.user.Attorney; +import entities.Question; +import entities.factories.AttorneyFactory; import org.junit.jupiter.api.Test; import java.util.List; import static org.junit.jupiter.api.Assertions.*; +/** + * This class contains unit tests for the Attorney class. + */ + class AttorneyTest { int expectedUserId = 1000000; String expectedName = "Xingfu Wu"; @@ -16,6 +20,9 @@ class AttorneyTest { String expectedStateAbb = "CA"; String expectedPostalCode = "12345"; + /** + * Test the constructor and getter methods of the Attorney class. + */ @Test void testConstructorAndGetter() { RegistrationData registrationData = new RegistrationData(expectedName, expectedEmail, expectedPassword, expectedPassword2, expectedStateAbb, @@ -32,6 +39,9 @@ void testConstructorAndGetter() { assertEquals(expectedPostalCode, attorney.getPostalCode(), "Postal code is not set correctly in the constructor."); } + /** + * Test the setter methods of the Attorney class. + */ @Test void testSetters() { @@ -58,6 +68,10 @@ void testSetters() { assertEquals(expectedStateAbb, attorney.getStateAbb(), "The state abbreviation should be updated."); assertEquals(expectedPostalCode, attorney.getPostalCode(), "The postal code should be updated."); } + + /** + * Test adding questions to the Attorney's question list. + */ @Test void testAddQuestion() { RegistrationData registrationData = new RegistrationData(expectedName, expectedEmail, expectedPassword, expectedPassword2, expectedStateAbb, @@ -79,6 +93,10 @@ void testAddQuestion() { attorney.addQuestion(question1); assertEquals(2, questions.size(), "After adding a duplicate question, the number of questions in the list should remain at 2."); } + + /** + * Test if a question is closeable when it's not taken. + */ @Test void testCloseableIfNotTaken() { RegistrationData registrationData = new RegistrationData(expectedName, expectedEmail, expectedPassword, expectedPassword2, expectedStateAbb, @@ -94,6 +112,10 @@ void testCloseableIfNotTaken() { boolean actual = attorney.isQuestionCloseable(question); assertEquals(expected, actual, "The question should not be closeable when it is not taken."); } + + /** + * Test if a question is not closeable when taken by another attorney. + */ @Test void testCloseableIfTakenByOthers() { RegistrationData registrationData1 = new RegistrationData(expectedName, expectedEmail, expectedPassword, expectedPassword2, expectedStateAbb, @@ -125,6 +147,10 @@ void testUserType() { String actual = attorney.getUserType(); assertEquals(expected, actual); } + + /** + * Test if a question is not selectable when it's closed. + */ @Test void testSelectableIfClosed() { RegistrationData registrationData = new RegistrationData(expectedName, expectedEmail, expectedPassword, expectedPassword2, expectedStateAbb, @@ -139,6 +165,10 @@ void testSelectableIfClosed() { boolean actual = attorney.isQuestionSelectable(question); assertEquals(expected, actual, "The question should not be selectable when it is closed."); } + + /** + * Test if a question is selectable when it's not taken. + */ @Test void testSelectableIfNotTaken() { RegistrationData registrationData = new RegistrationData(expectedName, expectedEmail, expectedPassword, expectedPassword2, expectedStateAbb, @@ -153,6 +183,10 @@ void testSelectableIfNotTaken() { boolean actual = attorney.isQuestionSelectable(question); assertEquals(expected, actual, "The question should be selectable when it is not taken."); } + + /** + * Test if a question is not selectable when taken by another attorney. + */ @Test void testSelectableIfTakenByOthers() { RegistrationData registrationData1 = new RegistrationData(expectedName, expectedEmail, expectedPassword, expectedPassword2, expectedStateAbb, @@ -175,6 +209,9 @@ void testSelectableIfTakenByOthers() { assertEquals(expected, actual, "The question should not be selectable when it is taken by a different attorney."); } + /** + * Test if a question is selectable when taken by the current attorney. + */ @Test void testSelectableIfTakenBySelf() { RegistrationData registrationData = new RegistrationData(expectedName, expectedEmail, expectedPassword, expectedPassword2, expectedStateAbb, @@ -191,6 +228,9 @@ void testSelectableIfTakenBySelf() { assertEquals(expected, actual, "The question should be selectable when it is taken by the current attorney."); } + /** + * Test if a question is not replyable when it is closed. + */ @Test void testReplyableIfClosed() { RegistrationData registrationData = new RegistrationData(expectedName, expectedEmail, expectedPassword, expectedPassword2, expectedStateAbb, @@ -205,6 +245,10 @@ void testReplyableIfClosed() { boolean actual = attorney.isQuestionReplyable(question); assertEquals(expected, actual, "The question should not be replyable when it is closed."); } + + /** + * Test if a question is not replyable when it is taken by a different attorney.". + */ @Test void testReplyableIfTakenByOthers() { RegistrationData registrationData1 = new RegistrationData(expectedName, expectedEmail, expectedPassword, expectedPassword2, expectedStateAbb, @@ -226,6 +270,9 @@ void testReplyableIfTakenByOthers() { assertEquals(expected, actual, "The question should not be replyable when it is taken by a different attorney."); } + /** + * Test if a question is replyable when it is taken by the current attorney. + */ @Test void testReplyableIfTakenBySelf() { RegistrationData registrationData = new RegistrationData(expectedName, expectedEmail, expectedPassword, expectedPassword2, expectedStateAbb, @@ -242,6 +289,9 @@ void testReplyableIfTakenBySelf() { assertEquals(expected, actual, "The question should be replyable when it is taken by the current attorney."); } + /** + * Test if a question is replyable when it is not taken by any user. + */ @Test void testReplyableIfNotTaken() { RegistrationData registrationData = new RegistrationData(expectedName, expectedEmail, expectedPassword, expectedPassword2, expectedStateAbb, @@ -271,7 +321,9 @@ void testEqualsFailByNotEqual(){ assertEquals(attorney1, attorney2, "The equal method is wrong"); } - + /** + * Test if a question is not rateable by an Attorney. + */ @Test void testIsQuestionRateable() { RegistrationData registrationData = new RegistrationData(expectedName, expectedEmail, expectedPassword, expectedPassword2, expectedStateAbb, diff --git a/src/test/java/entitytesting/ClientTest.java b/src/test/java/entitytesting/ClientTest.java index e4314cca..6b00d269 100644 --- a/src/test/java/entitytesting/ClientTest.java +++ b/src/test/java/entitytesting/ClientTest.java @@ -1,16 +1,19 @@ package entitytesting; -import businessrule.requestmodel.RegistrationData; -import entity.Client; -import entity.Question; -import entity.factory.ClientFactory; +import usecases.requests.ClientRegistrationData; +import usecases.requests.RegistrationData; +import entities.user.Client; +import entities.Question; +import entities.factories.ClientFactory; import org.junit.jupiter.api.Test; import java.time.LocalDate; import java.util.Objects; import static org.junit.jupiter.api.Assertions.*; - +/** + * This class contains unit tests for the Client class. + */ class ClientTest { int expectedUserId = 1000000; @@ -27,12 +30,22 @@ class ClientTest { int expectedNumberOfHousehold = 1; float expectedAnnualIncome = 60000.0f; + /** + * Tests the constructor and getter methods of the Client class. + */ @Test void testConstructorAndGetter() { // constructor - RegistrationData registrationData = new RegistrationData(expectedName, expectedEmail, expectedPassword, expectedPassword2, expectedStateAbb, - expectedPostalCode, expectedEthnicity, expectedAge, expectedGender, expectedMaritalStatus, - expectedNumberOfHousehold, expectedAnnualIncome); + RegistrationData data = new RegistrationData(expectedName, expectedEmail, expectedPassword, expectedPassword2, expectedStateAbb, + expectedPostalCode); + ClientRegistrationData registrationData = new ClientRegistrationData.Builder(data) + .age(expectedAge) + .annualIncome(expectedAnnualIncome) + .gender(expectedGender) + .maritalStatus(expectedMaritalStatus) + .numberOfHousehold(expectedNumberOfHousehold) + .ethnicity(expectedEthnicity) + .build(); ClientFactory clientFactory = new ClientFactory(); Client client = clientFactory.createUser(registrationData); client.setUserId(expectedUserId); @@ -54,6 +67,9 @@ void testConstructorAndGetter() { assertEquals("Client", client.getUserType(), "isClient should be true for Client."); } + /** + * Tests the setter methods of the Client class. + */ @Test void testSetters() { // no-arg constructor @@ -88,11 +104,21 @@ void testSetters() { assertEquals(expectedAnnualIncome, client.getAnnualIncome(), "Annual income is incorrect."); } + /** + * Tests whether a question can be closed successfully for a client. + */ @Test void testClientIsQuestionCloseableSucceed() { - RegistrationData registrationData = new RegistrationData(expectedName, expectedEmail, expectedPassword, expectedPassword2, expectedStateAbb, - expectedPostalCode, expectedEthnicity, expectedAge, expectedGender, expectedMaritalStatus, - expectedNumberOfHousehold, expectedAnnualIncome); + RegistrationData data = new RegistrationData(expectedName, expectedEmail, expectedPassword, expectedPassword2, expectedStateAbb, + expectedPostalCode); + ClientRegistrationData registrationData = new ClientRegistrationData.Builder(data) + .age(expectedAge) + .annualIncome(expectedAnnualIncome) + .gender(expectedGender) + .maritalStatus(expectedMaritalStatus) + .numberOfHousehold(expectedNumberOfHousehold) + .ethnicity(expectedEthnicity) + .build(); ClientFactory clientFactory = new ClientFactory(); Client client = clientFactory.createUser(registrationData); @@ -102,11 +128,21 @@ void testClientIsQuestionCloseableSucceed() { assertTrue(client.isQuestionCloseable(question)); } + /** + * Tests whether a closed question is not closeable for a client. + */ @Test void testClientIsQuestionCloseableFail() { - RegistrationData registrationData = new RegistrationData(expectedName, expectedEmail, expectedPassword, expectedPassword2, expectedStateAbb, - expectedPostalCode, expectedEthnicity, expectedAge, expectedGender, expectedMaritalStatus, - expectedNumberOfHousehold, expectedAnnualIncome); + RegistrationData data = new RegistrationData(expectedName, expectedEmail, expectedPassword, expectedPassword2, expectedStateAbb, + expectedPostalCode); + ClientRegistrationData registrationData = new ClientRegistrationData.Builder(data) + .age(expectedAge) + .annualIncome(expectedAnnualIncome) + .gender(expectedGender) + .maritalStatus(expectedMaritalStatus) + .numberOfHousehold(expectedNumberOfHousehold) + .ethnicity(expectedEthnicity) + .build(); ClientFactory clientFactory = new ClientFactory(); Client client = clientFactory.createUser(registrationData); @@ -116,11 +152,21 @@ void testClientIsQuestionCloseableFail() { assertFalse(client.isQuestionCloseable(question)); } + /** + * Tests whether a question is selectable for a client. + */ @Test void testClientIsQuestionSelectable() { - RegistrationData registrationData = new RegistrationData(expectedName, expectedEmail, expectedPassword, expectedPassword2, expectedStateAbb, - expectedPostalCode, expectedEthnicity, expectedAge, expectedGender, expectedMaritalStatus, - expectedNumberOfHousehold, expectedAnnualIncome); + RegistrationData data = new RegistrationData(expectedName, expectedEmail, expectedPassword, expectedPassword2, expectedStateAbb, + expectedPostalCode); + ClientRegistrationData registrationData = new ClientRegistrationData.Builder(data) + .age(expectedAge) + .annualIncome(expectedAnnualIncome) + .gender(expectedGender) + .maritalStatus(expectedMaritalStatus) + .numberOfHousehold(expectedNumberOfHousehold) + .ethnicity(expectedEthnicity) + .build(); ClientFactory clientFactory = new ClientFactory(); Client client = clientFactory.createUser(registrationData); Question question = new Question(); @@ -129,11 +175,21 @@ void testClientIsQuestionSelectable() { } + /** + * Tests whether a question can be replied to successfully for a client. + */ @Test void testClientIsQuestionReplyableSucceed() { - RegistrationData registrationData = new RegistrationData(expectedName, expectedEmail, expectedPassword, expectedPassword2, expectedStateAbb, - expectedPostalCode, expectedEthnicity, expectedAge, expectedGender, expectedMaritalStatus, - expectedNumberOfHousehold, expectedAnnualIncome); + RegistrationData data = new RegistrationData(expectedName, expectedEmail, expectedPassword, expectedPassword2, expectedStateAbb, + expectedPostalCode); + ClientRegistrationData registrationData = new ClientRegistrationData.Builder(data) + .age(expectedAge) + .annualIncome(expectedAnnualIncome) + .gender(expectedGender) + .maritalStatus(expectedMaritalStatus) + .numberOfHousehold(expectedNumberOfHousehold) + .ethnicity(expectedEthnicity) + .build(); ClientFactory clientFactory = new ClientFactory(); Client client = clientFactory.createUser(registrationData); @@ -144,11 +200,21 @@ void testClientIsQuestionReplyableSucceed() { } + /** + * Tests whether a closed question is not replyable for a client. + */ @Test void testClientIsQuestionReplyableFail() { - RegistrationData registrationData = new RegistrationData(expectedName, expectedEmail, expectedPassword, expectedPassword2, expectedStateAbb, - expectedPostalCode, expectedEthnicity, expectedAge, expectedGender, expectedMaritalStatus, - expectedNumberOfHousehold, expectedAnnualIncome); + RegistrationData data = new RegistrationData(expectedName, expectedEmail, expectedPassword, expectedPassword2, expectedStateAbb, + expectedPostalCode); + ClientRegistrationData registrationData = new ClientRegistrationData.Builder(data) + .age(expectedAge) + .annualIncome(expectedAnnualIncome) + .gender(expectedGender) + .maritalStatus(expectedMaritalStatus) + .numberOfHousehold(expectedNumberOfHousehold) + .ethnicity(expectedEthnicity) + .build(); ClientFactory clientFactory = new ClientFactory(); Client client = clientFactory.createUser(registrationData); Question question = new Question(); @@ -158,11 +224,21 @@ void testClientIsQuestionReplyableFail() { } + /** + * Tests the addition of a question to the client's question list. + */ @Test void testAddQuestion() { - RegistrationData registrationData = new RegistrationData(expectedName, expectedEmail, expectedPassword, expectedPassword2, expectedStateAbb, - expectedPostalCode, expectedEthnicity, expectedAge, expectedGender, expectedMaritalStatus, - expectedNumberOfHousehold, expectedAnnualIncome); + RegistrationData data = new RegistrationData(expectedName, expectedEmail, expectedPassword, expectedPassword2, expectedStateAbb, + expectedPostalCode); + ClientRegistrationData registrationData = new ClientRegistrationData.Builder(data) + .age(expectedAge) + .annualIncome(expectedAnnualIncome) + .gender(expectedGender) + .maritalStatus(expectedMaritalStatus) + .numberOfHousehold(expectedNumberOfHousehold) + .ethnicity(expectedEthnicity) + .build(); ClientFactory clientFactory = new ClientFactory(); Client client = clientFactory.createUser(registrationData); @@ -173,11 +249,21 @@ void testAddQuestion() { assertTrue(client.getQuestionsList().contains(question), "The question is not added in the list."); } + /** + * Tests the hash code generation of the Client class. + */ @Test void testHashCodeSucceed() { - RegistrationData registrationData = new RegistrationData(expectedName, expectedEmail, expectedPassword, expectedPassword2, expectedStateAbb, - expectedPostalCode, expectedEthnicity, expectedAge, expectedGender, expectedMaritalStatus, - expectedNumberOfHousehold, expectedAnnualIncome); + RegistrationData data = new RegistrationData(expectedName, expectedEmail, expectedPassword, expectedPassword2, expectedStateAbb, + expectedPostalCode); + ClientRegistrationData registrationData = new ClientRegistrationData.Builder(data) + .age(expectedAge) + .annualIncome(expectedAnnualIncome) + .gender(expectedGender) + .maritalStatus(expectedMaritalStatus) + .numberOfHousehold(expectedNumberOfHousehold) + .ethnicity(expectedEthnicity) + .build(); ClientFactory clientFactory = new ClientFactory(); Client client = clientFactory.createUser(registrationData); client.setUserId(expectedUserId); @@ -185,25 +271,54 @@ void testHashCodeSucceed() { assertEquals(client.hashCode(), Objects.hashCode(1000000), "The hashcode is wrong"); } + /** + * Tests whether the generated hash code differs when expected and actual user IDs do not match. + */ @Test void testHashCodeFail() { - RegistrationData registrationData = new RegistrationData(expectedName, expectedEmail, expectedPassword, expectedPassword2, expectedStateAbb, - expectedPostalCode, expectedEthnicity, expectedAge, expectedGender, expectedMaritalStatus, - expectedNumberOfHousehold, expectedAnnualIncome); + RegistrationData data = new RegistrationData(expectedName, expectedEmail, expectedPassword, expectedPassword2, expectedStateAbb, + expectedPostalCode); + ClientRegistrationData registrationData = new ClientRegistrationData.Builder(data) + .age(expectedAge) + .annualIncome(expectedAnnualIncome) + .gender(expectedGender) + .maritalStatus(expectedMaritalStatus) + .numberOfHousehold(expectedNumberOfHousehold) + .ethnicity(expectedEthnicity) + .build(); ClientFactory clientFactory = new ClientFactory(); Client client = clientFactory.createUser(registrationData); assertNotEquals(client.hashCode(), Objects.hashCode(100000), "The hashcode is wrong"); } + /** + * Tests inequality between two Client objects with non-matching data. + */ @Test void testEqualsFailByNotEqual() { - RegistrationData registrationData1 = new RegistrationData(expectedName, expectedEmail, expectedPassword, expectedPassword2, expectedStateAbb, - expectedPostalCode, expectedEthnicity, expectedAge, expectedGender, expectedMaritalStatus, - expectedNumberOfHousehold, expectedAnnualIncome); - RegistrationData registrationData2 = new RegistrationData("joseph", expectedEmail, expectedPassword, expectedPassword2, expectedStateAbb, - expectedPostalCode, expectedEthnicity, expectedAge, expectedGender, expectedMaritalStatus, - expectedNumberOfHousehold, expectedAnnualIncome); + RegistrationData data1 = new RegistrationData(expectedName, expectedEmail, expectedPassword, expectedPassword2, expectedStateAbb, + expectedPostalCode); + ClientRegistrationData registrationData1 = new ClientRegistrationData.Builder(data1) + .age(expectedAge) + .annualIncome(expectedAnnualIncome) + .gender(expectedGender) + .maritalStatus(expectedMaritalStatus) + .numberOfHousehold(expectedNumberOfHousehold) + .ethnicity(expectedEthnicity) + .build(); + + RegistrationData data2 = new RegistrationData("test", expectedEmail, expectedPassword, expectedPassword2, expectedStateAbb, + expectedPostalCode); + ClientRegistrationData registrationData2 = new ClientRegistrationData.Builder(data2) + .age(expectedAge) + .annualIncome(expectedAnnualIncome) + .gender(expectedGender) + .maritalStatus(expectedMaritalStatus) + .numberOfHousehold(expectedNumberOfHousehold) + .ethnicity(expectedEthnicity) + .build(); + ClientFactory clientFactory = new ClientFactory(); Client client1 = clientFactory.createUser(registrationData1); client1.setUserId(1); @@ -213,11 +328,21 @@ void testEqualsFailByNotEqual() { assertNotEquals(client1, client2, "The equal method is wrong"); } + /** + * Test the toString method of the Client class when it succeeds. + */ @Test void testToStringSucceed() { - RegistrationData registrationData = new RegistrationData(expectedName, expectedEmail, expectedPassword, expectedPassword2, expectedStateAbb, - expectedPostalCode, expectedEthnicity, expectedAge, expectedGender, expectedMaritalStatus, - expectedNumberOfHousehold, expectedAnnualIncome); + RegistrationData data = new RegistrationData(expectedName, expectedEmail, expectedPassword, expectedPassword2, expectedStateAbb, + expectedPostalCode); + ClientRegistrationData registrationData = new ClientRegistrationData.Builder(data) + .age(expectedAge) + .annualIncome(expectedAnnualIncome) + .gender(expectedGender) + .maritalStatus(expectedMaritalStatus) + .numberOfHousehold(expectedNumberOfHousehold) + .ethnicity(expectedEthnicity) + .build(); ClientFactory clientFactory = new ClientFactory(); Client client = clientFactory.createUser(registrationData); @@ -225,11 +350,21 @@ void testToStringSucceed() { assertEquals(expectedToString, client.toString(), "The toString method is wrong"); } + /** + * Test the toString method of the Client class when it fails. + */ @Test void testToStringFail() { - RegistrationData registrationData = new RegistrationData(expectedName, expectedEmail, expectedPassword, expectedPassword2, expectedStateAbb, - expectedPostalCode, expectedEthnicity, expectedAge, expectedGender, expectedMaritalStatus, - expectedNumberOfHousehold, expectedAnnualIncome); + RegistrationData data = new RegistrationData(expectedName, expectedEmail, expectedPassword, expectedPassword2, expectedStateAbb, + expectedPostalCode); + ClientRegistrationData registrationData = new ClientRegistrationData.Builder(data) + .age(expectedAge) + .annualIncome(expectedAnnualIncome) + .gender(expectedGender) + .maritalStatus(expectedMaritalStatus) + .numberOfHousehold(expectedNumberOfHousehold) + .ethnicity(expectedEthnicity) + .build(); ClientFactory clientFactory = new ClientFactory(); Client client = clientFactory.createUser(registrationData); @@ -237,11 +372,21 @@ void testToStringFail() { assertNotSame(expectedToString, client.toString(), "The toString method is wrong"); } + /** + * Tests whether a closed question is rateable for a client. + */ @Test void testIsQuestionRateableSucceed() { - RegistrationData registrationData = new RegistrationData(expectedName, expectedEmail, expectedPassword, expectedPassword2, expectedStateAbb, - expectedPostalCode, expectedEthnicity, expectedAge, expectedGender, expectedMaritalStatus, - expectedNumberOfHousehold, expectedAnnualIncome); + RegistrationData data = new RegistrationData(expectedName, expectedEmail, expectedPassword, expectedPassword2, expectedStateAbb, + expectedPostalCode); + ClientRegistrationData registrationData = new ClientRegistrationData.Builder(data) + .age(expectedAge) + .annualIncome(expectedAnnualIncome) + .gender(expectedGender) + .maritalStatus(expectedMaritalStatus) + .numberOfHousehold(expectedNumberOfHousehold) + .ethnicity(expectedEthnicity) + .build(); ClientFactory clientFactory = new ClientFactory(); Client client = clientFactory.createUser(registrationData); Question question = new Question(); @@ -249,11 +394,21 @@ void testIsQuestionRateableSucceed() { assertTrue(client.isQuestionRateable(question), "IsQuestionRateable is wrong."); } + /** + * Tests whether a non-closed question is not rateable for a client. + */ @Test void testIsQuestionRateableFail() { - RegistrationData registrationData = new RegistrationData(expectedName, expectedEmail, expectedPassword, expectedPassword2, expectedStateAbb, - expectedPostalCode, expectedEthnicity, expectedAge, expectedGender, expectedMaritalStatus, - expectedNumberOfHousehold, expectedAnnualIncome); + RegistrationData data = new RegistrationData(expectedName, expectedEmail, expectedPassword, expectedPassword2, expectedStateAbb, + expectedPostalCode); + ClientRegistrationData registrationData = new ClientRegistrationData.Builder(data) + .age(expectedAge) + .annualIncome(expectedAnnualIncome) + .gender(expectedGender) + .maritalStatus(expectedMaritalStatus) + .numberOfHousehold(expectedNumberOfHousehold) + .ethnicity(expectedEthnicity) + .build(); ClientFactory clientFactory = new ClientFactory(); Client client = clientFactory.createUser(registrationData); Question question = new Question(); diff --git a/src/test/java/entitytesting/PostTest.java b/src/test/java/entitytesting/PostTest.java index 2803ecc4..20ff3070 100644 --- a/src/test/java/entitytesting/PostTest.java +++ b/src/test/java/entitytesting/PostTest.java @@ -1,12 +1,18 @@ package entitytesting; import org.junit.jupiter.api.Test; -import entity.Post; +import entities.Post; import java.time.LocalDate; import static org.junit.jupiter.api.Assertions.*; +/** + * This class contains unit tests for the Post class. + */ class PostTest { + /** + * Test the constructor and getter methods of the Post class. + */ @Test void testConstructorAndGetter() { // expected values @@ -27,6 +33,9 @@ void testConstructorAndGetter() { assertEquals(expectedBelongsTo, post.getBelongsTo(), "BelongsTo is wrong."); } + /** + * Test the setter methods of the Post class. + */ @Test void testSetters() { // expected values @@ -54,6 +63,9 @@ void testSetters() { assertEquals(expectedBelongsTo, post.getBelongsTo(), "BelongsTo is wrong."); } + /** + * Test the equals method of the Post class when objects are the same. + */ @Test void testEqualsFailByDifferentPostId() { Post post1 = new Post(1000000, 2000000, LocalDate.now(), "This is a post.", 3000000); @@ -61,6 +73,9 @@ void testEqualsFailByDifferentPostId() { assertNotEquals(post1, post2, "The equal method is wrong"); } + /** + * Test the toString method of the Post class when it succeeds. + */ @Test void testToStringSucceed() { int expectedQuestionId = 1000000; @@ -70,6 +85,9 @@ void testToStringSucceed() { assertEquals(expectedToString, post.toString(), "The toString method is wrong"); } + /** + * Test the toString method of the Post class when it fails. + */ @Test void testToStringFail() { int expectedQuestionId = 1000000; diff --git a/src/test/java/entitytesting/QuestionTest.java b/src/test/java/entitytesting/QuestionTest.java index 0c075393..d703bfbf 100644 --- a/src/test/java/entitytesting/QuestionTest.java +++ b/src/test/java/entitytesting/QuestionTest.java @@ -1,14 +1,19 @@ package entitytesting; import org.junit.jupiter.api.Test; -import entity.Question; +import entities.Question; import java.time.LocalDate; import java.util.Objects; - import static org.junit.jupiter.api.Assertions.*; +/** + * This class contains unit tests for the Question class. + */ class QuestionTest { + /** + * Test the constructor and getter methods of the Question class. + */ @Test void testConstructorAndGetter() { int expectedQuestionId = 1000000; @@ -34,6 +39,9 @@ void testConstructorAndGetter() { assertTrue(question.getPosts().isEmpty(), "Posts should be empty initially."); } + /** + * Test the setter methods of the Question class. + */ @Test void testSetters() { int expectedQuestionId = 1000000; @@ -61,6 +69,9 @@ void testSetters() { assertEquals(expectedLegalDeadline, question.getLegalDeadline(), "LegalDeadline is wrong."); } + /** + * Test the equals method of the Question class with objects that are not equal. + */ @Test void testEqualsFailByNotEqual() { Question question1 = new Question(1, "fraud", "Question Title", LocalDate.now(), 2000000, LocalDate.now()); @@ -68,18 +79,27 @@ void testEqualsFailByNotEqual() { assertNotEquals(question1, question2, "The equal method is wrong"); } + /** + * Test the hashCode method of the Question class when it fails. + */ @Test void testHashCodeFail() { Question question1 = new Question(1, "fraud", "Question Title", LocalDate.now(), 2000000, LocalDate.now()); assertNotEquals(question1.hashCode(), Objects.hashCode(2), "The hashCode is wrong"); } + /** + * Test the hashCode method of the Question class when it succeeds. + */ @Test void testHashCodeSucceed() { Question question1 = new Question(1, "fraud", "Question Title", LocalDate.now(), 2000000, LocalDate.now()); assertEquals(question1.hashCode(), Objects.hashCode(1), "The hashCode is wrong"); } + /** + * Test the toString method of the Question class when it succeeds. + */ @Test void testToStringSucceed() { String expectedType = "fraud"; @@ -89,6 +109,9 @@ void testToStringSucceed() { assertEquals(expectedToString, question1.toString(), "The toString method is wrong"); } + /** + * Test the toString method of the Question class when it fails. + */ @Test void testToStringFail() { String unexpectedType = "criminal"; diff --git a/src/test/java/factorytesting/ClientFactoryTest.java b/src/test/java/factorytesting/ClientFactoryTest.java index 9a39f811..f1f1f5a7 100644 --- a/src/test/java/factorytesting/ClientFactoryTest.java +++ b/src/test/java/factorytesting/ClientFactoryTest.java @@ -1,13 +1,20 @@ package factorytesting; -import businessrule.requestmodel.RegistrationData; -import entity.Client; -import entity.factory.ClientFactory; +import usecases.requests.ClientRegistrationData; +import usecases.requests.RegistrationData; +import entities.user.Client; +import entities.factories.ClientFactory; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; +/** + * This class contains unit tests for the ClientFactory class. + */ public class ClientFactoryTest { + /** + * Test the create method of ClientFactory. + */ @Test public void testCreate() { ClientFactory factory = new ClientFactory(); @@ -26,8 +33,15 @@ public void testCreate() { int numberOfHousehold = 1; float annualIncome = 50000.0f; - RegistrationData inputData = new RegistrationData(userName, email, password, password2, stateAbb, - postalCode, ethnicity, age, gender, maritalStatus, numberOfHousehold, annualIncome); + RegistrationData registrationData = new RegistrationData(userName, email, password, password2, stateAbb, + postalCode); + ClientRegistrationData inputData= new ClientRegistrationData.Builder(registrationData).age(age) + .annualIncome(annualIncome) + .ethnicity(ethnicity) + .gender(gender) + .maritalStatus(maritalStatus) + .numberOfHousehold(numberOfHousehold) + .build(); Client client = factory.createUser(inputData); client.setUserId(userId); diff --git a/src/test/java/factorytesting/CredentialCheckerTest.java b/src/test/java/factorytesting/CredentialCheckerTest.java index e597e195..4c953f50 100644 --- a/src/test/java/factorytesting/CredentialCheckerTest.java +++ b/src/test/java/factorytesting/CredentialCheckerTest.java @@ -2,19 +2,28 @@ import static org.junit.jupiter.api.Assertions.*; -import businessrule.usecase.util.CredentialChecker; +import usecases.utils.CredentialChecker; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +/** + * This class contains unit tests for CredentialChecker class. + */ class CredentialCheckerTest { private CredentialChecker checker; + /** + * Set up the CredentialChecker instance before each test. + */ @BeforeEach void setUpCredentialChecker() { checker = new CredentialChecker(); } + /** + * Test the checkPostalCode method of CredentialChecker. + */ @Test void testCheckPostalCode() { assertTrue(checker.checkPostalCode("12345"), "Postal code validation failed."); @@ -23,7 +32,9 @@ void testCheckPostalCode() { assertFalse(checker.checkPostalCode(""), "Empty postal code passed validation."); } - + /** + * Test the checkEmail method of CredentialChecker. + */ @Test void testCheckEmail() { assertTrue(checker.checkEmail("test@example.com"), "Expected valid email to pass validation."); @@ -33,7 +44,9 @@ void testCheckEmail() { assertFalse(checker.checkEmail(" "), "Expected email with only spaces to fail validation."); } - + /** + * Test the checkAge method of CredentialChecker. + */ @Test void testCheckAge() { assertTrue(checker.checkAge(50), "Age validation failed."); diff --git a/src/test/java/factorytesting/PostFactoryTest.java b/src/test/java/factorytesting/PostFactoryTest.java index 808bb69d..a304fee0 100644 --- a/src/test/java/factorytesting/PostFactoryTest.java +++ b/src/test/java/factorytesting/PostFactoryTest.java @@ -2,13 +2,16 @@ import static org.junit.jupiter.api.Assertions.*; -import entity.Post; -import entity.factory.PostFactory; +import entities.Post; +import entities.factories.PostFactory; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.time.LocalDate; +/** + * This class contains unit tests for PostFactory class. + */ class PostFactoryTest { private PostFactory postFactory; @@ -18,6 +21,9 @@ class PostFactoryTest { private String postText; private int belongsTo; + /** + * Set up the PostFactory instance before each test. + */ @BeforeEach void setUpPostFactory() { postFactory = new PostFactory(); @@ -28,6 +34,9 @@ void setUpPostFactory() { belongsTo = 3; } + /** + * Test the create method of PostFactory. + */ @Test void testCreate() { Post post = postFactory.create(postId, questionId, createAt, postText, belongsTo); diff --git a/src/test/java/factorytesting/QuestionFactoryTest.java b/src/test/java/factorytesting/QuestionFactoryTest.java index 4bf3116f..7b37404a 100644 --- a/src/test/java/factorytesting/QuestionFactoryTest.java +++ b/src/test/java/factorytesting/QuestionFactoryTest.java @@ -2,13 +2,16 @@ import static org.junit.jupiter.api.Assertions.*; -import entity.Question; -import entity.factory.QuestionFactory; +import entities.Question; +import entities.factories.QuestionFactory; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.time.LocalDate; +/** + * This class contains unit tests for the QuestionFactory class. + */ class QuestionFactoryTest { private QuestionFactory questionFactory; @@ -19,6 +22,9 @@ class QuestionFactoryTest { private int askedByClient; private LocalDate legalDeadline; + /** + * Set up the QuestionFactory instance and test data before tests. + */ @BeforeEach void setUpQuestionFactory() { questionFactory = new QuestionFactory(); @@ -30,6 +36,9 @@ void setUpQuestionFactory() { legalDeadline = LocalDate.now(); } + /** + * Test the create method of QuestionFactory. + */ @Test void testCreate() { Question question = questionFactory.create(questionId, type, title, createAt, askedByClient, legalDeadline); diff --git a/src/test/java/factorytesting/RandomNumberGeneratorTest.java b/src/test/java/factorytesting/RandomNumberGeneratorTest.java index 5bd0115f..9f83fc2d 100644 --- a/src/test/java/factorytesting/RandomNumberGeneratorTest.java +++ b/src/test/java/factorytesting/RandomNumberGeneratorTest.java @@ -2,42 +2,60 @@ import static org.junit.jupiter.api.Assertions.*; -import businessrule.usecase.util.RandomNumberGenerator; +import usecases.utils.RandomNumberGenerator; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +/** + * This class contains unit tests for the RandomNumberGenerator class. + */ public class RandomNumberGeneratorTest { private static final int PASSWORD_LENGTH = 8; private RandomNumberGenerator generator; + /** + * Set up the test environment by initializing the RandomNumberGenerator instance. + */ @BeforeEach void setUp() { generator = new RandomNumberGenerator(); } + /** + * Test the generation of a client ID and check if it starts with "2". + */ @Test void testGenerateClientId() { int clientId = generator.generateClientId(PASSWORD_LENGTH); assertTrue(String.valueOf(clientId).startsWith("2"), "ClientId should start with 2."); } + /** + * Test the generation of an attorney ID and check if it starts with "1". + */ @Test void testGenerateAttorneyId() { int attorneyId = generator.generateAttorneyId(PASSWORD_LENGTH); assertTrue(String.valueOf(attorneyId).startsWith("1"), "AttorneyId should start with 1."); } + /** + * Test the generation of a question ID and check if it starts with "3". + */ @Test void testGenerateQuestionId() { int questionId = generator.generateQuestionId(PASSWORD_LENGTH); assertTrue(String.valueOf(questionId).startsWith("3"), "QuestionId should start with 3."); } + /** + * Test the generation of a post ID and check if it starts with "4". + */ @Test void testGeneratePostId() { int postId = generator.generatePostId(PASSWORD_LENGTH); assertTrue(String.valueOf(postId).startsWith("4"), "PostId should start with 4."); } -} +} \ No newline at end of file diff --git a/src/test/java/factorytesting/UIFactoryTest.java b/src/test/java/factorytesting/UIFactoryTest.java new file mode 100644 index 00000000..21d627ad --- /dev/null +++ b/src/test/java/factorytesting/UIFactoryTest.java @@ -0,0 +1,105 @@ +package factorytesting; + +import infrastructure.screens.UIFactory; +import usecases.responses.BaseResponseModel; +import usecases.responses.TheQuestionResponseModel; +import usecases.responses.UserResponseModel; +import usecases.responses.ViewResponseModel; +import usecases.utils.BuilderService; +import usecases.dto.PostDisplay; +import usecases.dto.QuestionDisplay; +import infrastructure.screens.*; +import infrastructure.screens.utils.UIManager; +import entities.Question; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import javax.swing.*; +import java.awt.*; +import java.time.LocalDate; +import java.util.HashMap; +import java.util.Map; +import static org.junit.jupiter.api.Assertions.assertEquals; + +class UIFactoryTest { + + private final int USER_ID = 24567893; + private final int QUESTION_ID = 324724768; + private final String USER_NAME = "test client"; + private final String TITLE = "test title"; + private final String QUESTION_TYPE = "test type"; + private UserResponseModel userResponseModel; + private BaseResponseModel baseResponseModel; + private TheQuestionResponseModel theQuestionResponseModel; + private ViewResponseModel viewResponseModel; + private UIManager uiManager; + private Map postMap; + private Map questionMap; + + @BeforeEach + void setUpUIFactory() { + baseResponseModel = new BaseResponseModel(); + String USER_TYPE = "Client"; + userResponseModel = new UserResponseModel(USER_ID, USER_NAME, USER_TYPE); + JPanel testScreen = new JPanel(); + CardLayout testCardLayout = new CardLayout(); + uiManager = new UIManager(testScreen, testCardLayout); + Question question = new Question(QUESTION_ID, QUESTION_TYPE, TITLE, LocalDate.now(), USER_ID, LocalDate.now()); + String POST_TEXT = "test text"; + PostDisplay post = new PostDisplay(POST_TEXT, USER_TYPE, USER_NAME, LocalDate.now()); + + postMap = new HashMap<>(); + postMap.put(1, post); + questionMap = new HashMap<>(); + theQuestionResponseModel = BuilderService.getInstance().constructTheQuestionResponse(question, userResponseModel, postMap); + viewResponseModel = BuilderService.getInstance().constructViewResponse(userResponseModel, questionMap); + + + } + + @Test + void testGetWelcomeUI() { + setUpUIFactory(); + WelcomeUI expectedUI = new WelcomeUI(uiManager); + assertEquals(expectedUI.getClass(), UIFactory.getUI(UIFactory.UIType.WELCOME_UI, uiManager, baseResponseModel).getClass()); + } + @Test + void testGetRegisterUI() { + setUpUIFactory(); + RegisterUI expectedUI = new RegisterUI(uiManager); + assertEquals(expectedUI.getClass(), UIFactory.getUI(UIFactory.UIType.REGISTER_UI, uiManager, baseResponseModel).getClass()); + } + @Test + void testGetLoginUI() { + setUpUIFactory(); + LoginUI expectedUI = new LoginUI(uiManager); + assertEquals(expectedUI.getClass(), UIFactory.getUI(UIFactory.UIType.LOGIN_UI, uiManager, baseResponseModel).getClass()); + } + + @Test + void testGetHomePageUI() { + setUpUIFactory(); + HomePageUI expectedUI = new ClientHomePageUI(USER_NAME, USER_ID, uiManager); + assertEquals(expectedUI.getClass(), UIFactory.getUI(UIFactory.UIType.HOME_PAGE, uiManager, userResponseModel).getClass()); + } + + @Test + void testAskQuestionUI() { + setUpUIFactory(); + AskQuestionUI expectedUI = new AskQuestionUI(USER_NAME, USER_ID, uiManager); + assertEquals(expectedUI.getClass(), UIFactory.getUI(UIFactory.UIType.ASK_QUESTION_UI, uiManager, theQuestionResponseModel).getClass()); + } + + @Test + void testQuestionListUI() { + setUpUIFactory(); + QuestionListUI expectedUI = new QuestionListUI(USER_NAME, USER_ID, uiManager, questionMap); + assertEquals(expectedUI.getClass(), UIFactory.getUI(UIFactory.UIType.QUESTION_LIST_UI, uiManager, viewResponseModel).getClass()); + } + + @Test + void testQuestionUI() { + setUpUIFactory(); + QuestionOpenClientUI expectedUI = new QuestionOpenClientUI(USER_NAME, USER_ID, uiManager, QUESTION_ID, TITLE, QUESTION_TYPE, LocalDate.now(), postMap); + assertEquals(expectedUI.getClass(), UIFactory.getUI(UIFactory.UIType.QUESTION_UI, uiManager, theQuestionResponseModel).getClass()); + } +} \ No newline at end of file diff --git a/src/test/java/gatewaytesting/AttorneyRepositoryTest.java b/src/test/java/gatewaytesting/AttorneyRepositoryTest.java index e9706eb8..23327c1d 100644 --- a/src/test/java/gatewaytesting/AttorneyRepositoryTest.java +++ b/src/test/java/gatewaytesting/AttorneyRepositoryTest.java @@ -1,15 +1,16 @@ package gatewaytesting; -import businessrule.requestmodel.RegistrationData; -import driver.database.*; -import entity.Question; -import entity.factory.AttorneyFactory; -import entity.factory.ClientFactory; +import usecases.requests.ClientRegistrationData; +import usecases.requests.RegistrationData; +import infrastructure.database.*; +import entities.Question; +import entities.factories.AttorneyFactory; +import entities.factories.ClientFactory; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -import entity.Attorney; -import entity.Client; +import entities.user.Attorney; +import entities.user.Client; import javax.persistence.EntityManager; @@ -18,6 +19,9 @@ import static org.junit.jupiter.api.Assertions.*; +/** + * This class contains unit tests for the AttorneyRepository class. + */ public class AttorneyRepositoryTest { //Attorney @@ -32,6 +36,9 @@ public class AttorneyRepositoryTest { final static int ASKED_BY_CLIENT1 = 20; final static int ASKED_BY_CLIENT2 = 30; + /** + * Set up the test environment by initializing the AttorneyRepository instance. + */ @BeforeAll public static void setUp() { //Attorney @@ -65,7 +72,15 @@ public static void setUp() { int clientNumHouseHold = 1; float clientAnnualIncome = 100; - RegistrationData registrationData2 = new RegistrationData(clientUsername, clientEmail, clientPassword, clientPassword2, clientState, clientPostalCode, clientEthnicity, clientAge, clientGender, clientMaritalStatus, clientNumHouseHold, clientAnnualIncome); + RegistrationData data = new RegistrationData(clientUsername, clientEmail, clientPassword, clientPassword2, clientState, clientPostalCode); + ClientRegistrationData registrationData2 = new ClientRegistrationData.Builder(data) + .age(clientAge) + .annualIncome(clientAnnualIncome) + .gender(clientGender) + .maritalStatus(clientMaritalStatus) + .numberOfHousehold(clientNumHouseHold) + .ethnicity(clientEthnicity) + .build(); ClientFactory clientFactory = new ClientFactory(); Client c = clientFactory.createUser(registrationData2); @@ -94,6 +109,9 @@ public static void setUp() { qRepo.save(q2); } + /** + * Test whether an attorney ID exists in the repository. + */ @Test public void testExistsById(){ AttorneyRepository repo = new AttorneyRepository(); @@ -103,6 +121,9 @@ public void testExistsById(){ assertFalse(repo.existsById(75), "The id exists!"); } + /** + * Test whether an attorney with a given username exists in the repository. + */ @Test public void testExistsByName() { AttorneyRepository repo = new AttorneyRepository(); @@ -112,6 +133,9 @@ public void testExistsByName() { assertFalse(repo.existsByName("John"), "The username exists!"); } + /** + * Test updating an attorney's question list. + */ @Test public void testUpdateQuestionList() { AttorneyRepository repo = new AttorneyRepository(); @@ -124,6 +148,9 @@ public void testUpdateQuestionList() { assert expectedList1.equals(repo.get(ATTORNEY_ID).getQuestionsList()); } + /** + * Delete all data in AttorneyRepository, ClientRepository and QuestionRepo. + */ @AfterAll public static void tearDown() { AttorneyRepository repo = new AttorneyRepository(); @@ -134,6 +161,9 @@ public static void tearDown() { qRepo.deleteAll(); } + /** + * Test getting an attorney from the repository. + */ @Test public void testGetUser() { String attorneyUsername = "yao"; @@ -158,6 +188,9 @@ public void testGetUser() { assertEquals(a, repo.get(ATTORNEY_ID), "That is not the correct client!"); } + /** + * Test adding an attorney to the repository. + */ @Test public void testAddUser() { int attorneyId = 50; @@ -183,6 +216,9 @@ public void testAddUser() { assertTrue(repo.existsById(attorneyId), "The attorney is not added!"); } + /** + * Test deleting an attorney in the repository. + */ @Test public void testDeleteUser() { int attorneyId = 50; @@ -213,6 +249,9 @@ public void testDeleteUser() { repo.save(a); } + /** + * Test deleting all attorneys in the repository. + */ @Test public void testDeleteAllUser() { int attorneyId = 50; @@ -234,8 +273,15 @@ public void testDeleteAllUser() { Attorney a1 = attorneyFactory.createUser(registrationData2); a.setUserId(attorneyId2); - RegistrationData registrationData3 = new RegistrationData("bob", "bob.bob@gmail.com", "bob123321", "bob123321", "ON", - "M1MA6A", "asian", 20, "Male", "Single", 1, 1000.0f); + RegistrationData data = new RegistrationData("bob", "bob.bob@gmail.com", "bob123321", "bob123321", "ON", "M1MA6A"); + ClientRegistrationData registrationData3 = new ClientRegistrationData.Builder(data) + .age(20) + .annualIncome(1000.0f) + .gender("Male") + .maritalStatus("Single") + .numberOfHousehold(1) + .ethnicity("asian") + .build(); ClientFactory clientFactory = new ClientFactory(); Client c = clientFactory.createUser(registrationData3); diff --git a/src/test/java/gatewaytesting/ClientRepositoryTest.java b/src/test/java/gatewaytesting/ClientRepositoryTest.java index 7b2c98b8..0d7df38a 100644 --- a/src/test/java/gatewaytesting/ClientRepositoryTest.java +++ b/src/test/java/gatewaytesting/ClientRepositoryTest.java @@ -1,18 +1,19 @@ package gatewaytesting; -import businessrule.requestmodel.RegistrationData; -import driver.database.AttorneyRepository; -import driver.database.ClientRepository; -import driver.database.DatabaseConnection; -import driver.database.QuestionRepo; -import entity.Question; -import entity.factory.AttorneyFactory; -import entity.factory.ClientFactory; +import usecases.requests.ClientRegistrationData; +import usecases.requests.RegistrationData; +import infrastructure.database.AttorneyRepository; +import infrastructure.database.ClientRepository; +import infrastructure.database.DatabaseConnection; +import infrastructure.database.QuestionRepo; +import entities.Question; +import entities.factories.AttorneyFactory; +import entities.factories.ClientFactory; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -import entity.Attorney; -import entity.Client; +import entities.user.Attorney; +import entities.user.Client; import javax.persistence.EntityManager; @@ -21,6 +22,9 @@ import static org.junit.jupiter.api.Assertions.*; +/** + * This class contains unit tests for the ClientRepository class. + */ public class ClientRepositoryTest { //Client @@ -32,6 +36,9 @@ public class ClientRepositoryTest { final static int QUESTION_ID1 = 25; final static int QUESTION_ID2 = 35; + /** + * Set up the test environment by initializing the ClientRepository instance. + */ @BeforeAll public static void setUp() { //Client @@ -48,7 +55,16 @@ public static void setUp() { int clientNumHouseHold = 1; float clientAnnualIncome = 100; - RegistrationData registrationData = new RegistrationData(clientName, clientEmail, clientPassword, clientPassword2, clientState, clientPostalCode, clientEthnicity, clientAge, clientGender, clientMaritalStatus, clientNumHouseHold, clientAnnualIncome); + RegistrationData data = new RegistrationData(clientName, clientEmail, clientPassword, clientPassword2, clientState, clientPostalCode); + ClientRegistrationData registrationData = new ClientRegistrationData.Builder(data) + .age(clientAge) + .annualIncome(clientAnnualIncome) + .gender(clientGender) + .maritalStatus(clientMaritalStatus) + .numberOfHousehold(clientNumHouseHold) + .ethnicity(clientEthnicity) + .build(); + ClientFactory clientFactory = new ClientFactory(); Client c = clientFactory.createUser(registrationData); @@ -91,6 +107,9 @@ public static void setUp() { qRepo.save(q2); } + /** + * Test whether a client ID exists in the repository. + */ @Test public void testExistsById(){ ClientRepository repo = new ClientRepository(); @@ -100,6 +119,9 @@ public void testExistsById(){ assertFalse(repo.existsById(200), "The id exists!"); } + /** + * Test whether a client with a given username exists in the repository. + */ @Test public void testExistsByName() { ClientRepository repo = new ClientRepository(); @@ -109,6 +131,9 @@ public void testExistsByName() { assertFalse(repo.existsByName("John"), "The username exists!"); } + /** + * Test updating a client's question list. + */ @Test public void testUpdateQuestionList() { ClientRepository repo = new ClientRepository(); @@ -120,6 +145,9 @@ public void testUpdateQuestionList() { assert expectedList.equals(repo.get(CLIENT_ID).getQuestionsList()); } + /** + * Delete all data in AttorneyRepository, ClientRepository and QuestionRepo. + */ @AfterAll public static void tearDown() { ClientRepository repo = new ClientRepository(); @@ -130,6 +158,9 @@ public static void tearDown() { qRepo.deleteAll(); } + /** + * Test getting a client from the repository. + */ @Test public void testGetUser() { String clientUsername = "bob"; @@ -146,7 +177,15 @@ public void testGetUser() { float clientAnnualIncome = 100; //constructors - RegistrationData registrationData = new RegistrationData(clientUsername, clientEmail, clientPassword, clientPassword2, clientState, clientPostalCode, clientEthnicity, clientAge, clientGender, clientMaritalStatus, clientNumHouseHold, clientAnnualIncome); + RegistrationData data = new RegistrationData(clientUsername, clientEmail, clientPassword, clientPassword2, clientState, clientPostalCode); + ClientRegistrationData registrationData = new ClientRegistrationData.Builder(data) + .age(clientAge) + .annualIncome(clientAnnualIncome) + .gender(clientGender) + .maritalStatus(clientMaritalStatus) + .numberOfHousehold(clientNumHouseHold) + .ethnicity(clientEthnicity) + .build(); ClientFactory clientFactory = new ClientFactory(); Client c = clientFactory.createUser(registrationData); @@ -160,6 +199,9 @@ public void testGetUser() { assertEquals(c, repo.get(100), "That is not the correct client!"); } + /** + * Test adding a client to the repository. + */ @Test public void testAddUser() { String clientUsername = "bob"; @@ -176,7 +218,15 @@ public void testAddUser() { float clientAnnualIncome = 100; //constructors - RegistrationData registrationData = new RegistrationData(clientUsername, clientEmail, clientPassword, clientPassword2, clientState, clientPostalCode, clientEthnicity, clientAge, clientGender, clientMaritalStatus, clientNumHouseHold, clientAnnualIncome); + RegistrationData data = new RegistrationData(clientUsername, clientEmail, clientPassword, clientPassword2, clientState, clientPostalCode); + ClientRegistrationData registrationData = new ClientRegistrationData.Builder(data) + .age(clientAge) + .annualIncome(clientAnnualIncome) + .gender(clientGender) + .maritalStatus(clientMaritalStatus) + .numberOfHousehold(clientNumHouseHold) + .ethnicity(clientEthnicity) + .build(); ClientFactory clientFactory = new ClientFactory(); Client c = clientFactory.createUser(registrationData); @@ -190,6 +240,9 @@ public void testAddUser() { assertTrue(repo.existsById(100), "The client is not added!"); } + /** + * Test deleting a client in the repository. + */ @Test public void testDeleteUser() { int clientId = 100; @@ -207,7 +260,15 @@ public void testDeleteUser() { float clientAnnualIncome = 100; //constructors - RegistrationData registrationData = new RegistrationData(clientUsername, clientEmail, clientPassword, clientPassword2, clientState, clientPostalCode, clientEthnicity, clientAge, clientGender, clientMaritalStatus, clientNumHouseHold, clientAnnualIncome); + RegistrationData data = new RegistrationData(clientUsername, clientEmail, clientPassword, clientPassword2, clientState, clientPostalCode); + ClientRegistrationData registrationData = new ClientRegistrationData.Builder(data) + .age(clientAge) + .annualIncome(clientAnnualIncome) + .gender(clientGender) + .maritalStatus(clientMaritalStatus) + .numberOfHousehold(clientNumHouseHold) + .ethnicity(clientEthnicity) + .build(); ClientFactory clientFactory = new ClientFactory(); Client c = clientFactory.createUser(registrationData); @@ -226,6 +287,9 @@ public void testDeleteUser() { repo.save(c); } + /** + * Test deleting all client in the repository. + */ @Test public void testDeleteAllUser() { int clientId = 100; @@ -244,12 +308,28 @@ public void testDeleteAllUser() { float clientAnnualIncome = 100; //constructors - RegistrationData registrationData = new RegistrationData(clientUsername, clientEmail, clientPassword, clientPassword2, clientState, clientPostalCode, clientEthnicity, clientAge, clientGender, clientMaritalStatus, clientNumHouseHold, clientAnnualIncome); + RegistrationData data = new RegistrationData(clientUsername, clientEmail, clientPassword, clientPassword2, clientState, clientPostalCode); + ClientRegistrationData registrationData = new ClientRegistrationData.Builder(data) + .age(clientAge) + .annualIncome(clientAnnualIncome) + .gender(clientGender) + .maritalStatus(clientMaritalStatus) + .numberOfHousehold(clientNumHouseHold) + .ethnicity(clientEthnicity) + .build(); ClientFactory clientFactory = new ClientFactory(); Client c = clientFactory.createUser(registrationData); c.setUserId(clientId); - RegistrationData registrationData2 = new RegistrationData(clientUsername, clientEmail, clientPassword, clientPassword2, clientState, clientPostalCode, clientEthnicity, clientAge, clientGender, clientMaritalStatus, clientNumHouseHold, clientAnnualIncome); + + ClientRegistrationData registrationData2 = new ClientRegistrationData.Builder(data) + .age(clientAge) + .annualIncome(clientAnnualIncome) + .gender(clientGender) + .maritalStatus(clientMaritalStatus) + .numberOfHousehold(clientNumHouseHold) + .ethnicity(clientEthnicity) + .build(); Client c1 = clientFactory.createUser(registrationData2); c1.setUserId(clientId2); diff --git a/src/test/java/gatewaytesting/PostRepoTest.java b/src/test/java/gatewaytesting/PostRepoTest.java index cf13ed97..1c0c62c5 100644 --- a/src/test/java/gatewaytesting/PostRepoTest.java +++ b/src/test/java/gatewaytesting/PostRepoTest.java @@ -1,15 +1,18 @@ package gatewaytesting; -import driver.database.DatabaseConnection; -import driver.database.PostRepo; +import infrastructure.database.DatabaseConnection; +import infrastructure.database.PostRepo; import org.junit.jupiter.api.*; -import entity.Post; +import entities.Post; import javax.persistence.EntityManager; import java.time.LocalDate; import static org.junit.jupiter.api.Assertions.*; +/** + * This class contains unit tests for the PostRepo class. + */ public class PostRepoTest { //Post @@ -18,6 +21,9 @@ public class PostRepoTest { //Question final static int QUESTION_ID = 10; + /** + * Set up the test environment by initializing the PostRepo instance. + */ @BeforeEach public void setUp() { LocalDate createdAt = LocalDate.now(); @@ -33,6 +39,9 @@ public void setUp() { repo.save(p); } + /** + * Test whether a post ID exists in the repository. + */ @Test public void testCheckExistsById() { PostRepo repo = new PostRepo(); @@ -42,7 +51,9 @@ public void testCheckExistsById() { assertFalse(repo.existsById(15), "The id exists!"); } - + /** + * Test saving a post into the repository. + */ @Test public void testSavePost() { int questionId = 15; @@ -63,6 +74,9 @@ public void testSavePost() { assertTrue(repo.existsById(postId), "Post is not saved into the database!"); } + /** + * Test retrieving a post from the repository. + */ @Test public void testGetPost() { int questionId = 15; @@ -83,6 +97,9 @@ public void testGetPost() { assertEquals(p, repo.get(postId), "That is not the correct post!"); } + /** + * Test deleting a post from the repository. + */ @Test public void testDeletePost() { int questionId = 15; @@ -99,12 +116,15 @@ public void testDeletePost() { repo.deleteAll(); repo.save(p); - //test deleting an existing client from the database + //test deleting an existing post from the database assertTrue(repo.existsById(postId), "The post was not added!"); repo.delete(postId); assertFalse(repo.existsById(postId), "the post was not deleted!"); } + /** + * Test deleting all posts from the repository. + */ @Test public void testDeleteAllPost() { int questionId = 15; diff --git a/src/test/java/gatewaytesting/QuestionRepoTest.java b/src/test/java/gatewaytesting/QuestionRepoTest.java index 431489e0..75d6b253 100644 --- a/src/test/java/gatewaytesting/QuestionRepoTest.java +++ b/src/test/java/gatewaytesting/QuestionRepoTest.java @@ -1,14 +1,14 @@ package gatewaytesting; -import businessrule.requestmodel.RegistrationData; -import driver.database.*; -import entity.Post; -import entity.factory.AttorneyFactory; +import usecases.requests.RegistrationData; +import infrastructure.database.*; +import entities.Post; +import entities.factories.AttorneyFactory; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -import entity.Question; -import entity.Attorney; +import entities.Question; +import entities.user.Attorney; import javax.persistence.EntityManager; import java.time.LocalDate; @@ -17,6 +17,9 @@ import static org.junit.jupiter.api.Assertions.*; +/** + * This class contains unit tests for the QuestionRepo class. + */ public class QuestionRepoTest { //Question @@ -33,6 +36,9 @@ public class QuestionRepoTest { final static int ATTORNEY_ID = 150; final static int ATTORNEY_ID1 = 151; + /** + * Set up the test environment by initializing the QuestionRepo instance. + */ @BeforeAll public static void setUp() { //Question @@ -90,6 +96,9 @@ public static void setUp() { aRepo.save(a1); } + /** + * Tests the existsById method of the QuestionRepo class. + */ @Test public void testExistsById() { QuestionRepo repo = new QuestionRepo(); @@ -99,6 +108,9 @@ public void testExistsById() { assertFalse(repo.existsById(7), "The question exists!"); } + /** + * Test for the getAll method of QuestionRepo. + */ @Test public void testGetAllQuestion() { QuestionRepo repo = new QuestionRepo(); @@ -120,6 +132,9 @@ public void testGetAllQuestion() { repo.save(q1); } + /** + * Test for the getNotTakenQuestion method of QuestionRepo. + */ @Test public void testGetNotTakenQuestion() { QuestionRepo repo = new QuestionRepo(); @@ -129,6 +144,9 @@ public void testGetNotTakenQuestion() { assert expectedList.equals(repo.getNotTakenQuestion()); } + /** + * Test for the getNotClosedQuestion method of QuestionRepo. + */ @Test public void testGetNotClosedQuestion() { QuestionRepo repo = new QuestionRepo(); @@ -138,6 +156,9 @@ public void testGetNotClosedQuestion() { assert expectedList.equals(repo.getNotClosedQuestion()); } + /** + * Test for the getAllPostOfQuestion method of QuestionRepo. + */ @Test public void testGetAllPostByQuestion() { QuestionRepo repo = new QuestionRepo(); @@ -149,6 +170,9 @@ public void testGetAllPostByQuestion() { assert expectedList.equals(repo.getAllPostOfQuestion(QUESTION_ID)); } + /** + * Test for the updateIsTaken method of QuestionRepo. + */ @Test public void testUpdateIsTaken() { QuestionRepo repo = new QuestionRepo(); @@ -167,6 +191,9 @@ public void testUpdateIsTaken() { repo.updateIsTaken(QUESTION_ID1, false); } + /** + * Tests the updateTakenByAttorney method of the QuestionRepo class. + */ @Test public void testUpdateTakenByAttorney() { QuestionRepo repo = new QuestionRepo(); @@ -179,6 +206,9 @@ public void testUpdateTakenByAttorney() { assertNotEquals(ATTORNEY_ID, repo.get(QUESTION_ID).getTakenByAttorney(), "q is taken by a!"); } + /** + * Tests the updateIsClose method of the QuestionRepo class. + */ @Test public void testUpdateIsClose() { QuestionRepo repo = new QuestionRepo(); @@ -193,6 +223,9 @@ public void testUpdateIsClose() { assertFalse(repo.get(QUESTION_ID).isClose(), "This question was updated!"); } + /** + * Tests the updateRating method of the QuestionRepo class. + */ @Test public void testUpdateRating() { QuestionRepo repo = new QuestionRepo(); @@ -207,6 +240,9 @@ public void testUpdateRating() { assertEquals(0, repo.get(QUESTION_ID).getRating(), "This question was updated!"); } + /** + * Tests the updateTakenAt method of the QuestionRepo class. + */ @Test public void testUpdateTakenAt() { LocalDate changeDate = LocalDate.now().minusDays(1); @@ -222,6 +258,9 @@ public void testUpdateTakenAt() { assertEquals(CREATE_AT, repo.get(QUESTION_ID).getTakenAt(), "This question was updated!"); } + /** + * Tests the updatePosts method of the QuestionRepo class. + */ @Test public void testUpdatePosts() { QuestionRepo repo = new QuestionRepo(); @@ -233,6 +272,9 @@ public void testUpdatePosts() { assert expectedList.equals(repo.get(QUESTION_ID).getPosts()); } + /** + * Delete all data in AttorneyRepository, ClientRepository and QuestionRepo. + */ @AfterAll public static void tearDown() { QuestionRepo repo = new QuestionRepo(); @@ -243,6 +285,9 @@ public static void tearDown() { aRepo.deleteAll(); } + /** + * Tests saving a question using the save method of the QuestionRepo class. + */ @Test public void testSaveQuestion() { int questionId = 6; @@ -265,6 +310,9 @@ public void testSaveQuestion() { assertTrue(repo.existsById(questionId), "Question is not saved into the database!"); } + /** + * Tests retrieving a question using the get method of the QuestionRepo class. + */ @Test public void testGetQuestion() { int questionId = 6; @@ -287,6 +335,9 @@ public void testGetQuestion() { assertEquals(q, repo.get(questionId), "That is not the correct question!"); } + /** + * Tests deleting a question in the QuestionRepo. + */ @Test public void testDeleteQuestion() { int questionId = 6; @@ -318,6 +369,9 @@ public void testDeleteQuestion() { repo.save(q1); } + /** + * Tests deleting all question in QuestionRepo. + */ @Test public void testDeleteAllQuestion() { int questionId = 6; diff --git a/src/test/java/usecasetesting/AskQuestionUseCaseTest.java b/src/test/java/usecasetesting/AskQuestionUseCaseTest.java index 890cec66..3d205d2c 100644 --- a/src/test/java/usecasetesting/AskQuestionUseCaseTest.java +++ b/src/test/java/usecasetesting/AskQuestionUseCaseTest.java @@ -1,23 +1,29 @@ package usecasetesting; -import businessrule.SessionManager; -import businessrule.UserSession; -import businessrule.gateway.AttorneyGateway; -import businessrule.gateway.ClientGateway; -import businessrule.gateway.QuestionGateway; -import businessrule.inputboundary.QuestionInputBoundary; -import businessrule.outputboundary.TheQuestionOutputBoundary; -import businessrule.requestmodel.QuestionRequestModel; -import businessrule.responsemodel.TheQuestionResponseModel; -import businessrule.responsemodel.UserResponseModel; -import businessrule.usecase.AskQuestionInteractor; -import driver.database.*; -import entity.*; -import entity.factory.QuestionFactory; +import entities.user.Attorney; +import entities.user.Client; +import entities.user.User; +import usecases.session.SessionManager; +import usecases.session.UserSession; +import usecases.gateway.AttorneyGateway; +import usecases.gateway.ClientGateway; +import usecases.gateway.QuestionGateway; +import usecases.inputboundary.QuestionInputBoundary; +import usecases.outputboundary.TheQuestionOutputBoundary; +import usecases.requests.QuestionRequestModel; +import usecases.responses.TheQuestionResponseModel; +import usecases.responses.UserResponseModel; +import usecases.interactors.AskQuestionInteractor; +import infrastructure.database.*; +import entities.factories.QuestionFactory; import org.junit.jupiter.api.Test; import java.time.LocalDate; import static org.junit.jupiter.api.Assertions.*; + +/** + * This class contains unit tests for the AskQuestionUseCase class. + */ public class AskQuestionUseCaseTest { final static int CLIENT_ID = 21345678; final static String CLIENT_USERNAME = "test client"; @@ -29,6 +35,9 @@ public class AskQuestionUseCaseTest { private AttorneyGateway attorneyGateway; private QuestionInputBoundary questionInputBoundary; + /** + * Set up the test environment by initializing the AskQuestionUseCase instance. + */ public void setUpAskQuestionUseCase(){ questionGateway = new QuestionRepo(); QuestionFactory questionFactory = new QuestionFactory(); @@ -69,6 +78,9 @@ public TheQuestionResponseModel prepareSuccess(TheQuestionResponseModel response SessionManager.setSession(session); } + /** + * Test the AskQuestionUseCase when the question is created successfully. + */ @Test public void TestAskQuestionPassed(){ setUpAskQuestionUseCase(); @@ -79,9 +91,12 @@ public void TestAskQuestionPassed(){ User user = clientGateway.get(CLIENT_ID); assertEquals(1, user.getQuestionsList().size(), "The ask question use case failed."); - ClearAllRepository(); + clearAllRepository(); } + /** + * Test the AskQuestionUseCase when the question creation fails due to an empty category. + */ @Test public void TestAskQuestionFailByEmptyCategory(){ setUpAskQuestionUseCase(); @@ -92,10 +107,13 @@ public void TestAskQuestionFailByEmptyCategory(){ User user = clientGateway.get(CLIENT_ID); assertEquals(0, user.getQuestionsList().size(), "The ask question use case failed."); - ClearAllRepository(); + clearAllRepository(); } - public void ClearAllRepository(){ + /** + * Delete all data in questionGateway, clientGateway and attorneyGateway. + */ + public void clearAllRepository(){ questionGateway = new QuestionRepo(); clientGateway = new ClientRepository(); attorneyGateway = new AttorneyRepository(); diff --git a/src/test/java/usecasetesting/BrowseQuestionUseCaseTest.java b/src/test/java/usecasetesting/BrowseQuestionUseCaseTest.java index f39ac770..27e3e237 100644 --- a/src/test/java/usecasetesting/BrowseQuestionUseCaseTest.java +++ b/src/test/java/usecasetesting/BrowseQuestionUseCaseTest.java @@ -1,20 +1,25 @@ package usecasetesting; -import businessrule.SessionManager; -import businessrule.UserSession; -import businessrule.gateway.AttorneyGateway; -import businessrule.gateway.ClientGateway; -import businessrule.gateway.QuestionGateway; -import businessrule.inputboundary.ViewInputBoundary; -import businessrule.outputboundary.ViewOutputBoundary; -import businessrule.responsemodel.UserResponseModel; -import businessrule.responsemodel.ViewResponseModel; -import businessrule.usecase.BrowseQuestionInteractor; -import driver.database.*; -import entity.*; +import entities.user.Attorney; +import entities.user.Client; +import usecases.session.SessionManager; +import usecases.session.UserSession; +import usecases.gateway.AttorneyGateway; +import usecases.gateway.ClientGateway; +import usecases.gateway.QuestionGateway; +import usecases.inputboundary.ViewInputBoundary; +import usecases.outputboundary.ViewOutputBoundary; +import usecases.responses.UserResponseModel; +import usecases.responses.ViewResponseModel; +import usecases.interactors.BrowseQuestionInteractor; +import infrastructure.database.*; +import entities.*; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; +/** + * This class contains unit tests for the BrowseQuestionUseCase class. + */ public class BrowseQuestionUseCaseTest { final static int CLIENT_ID = 21345678; final static int ATTORNEY_ID = 11345678; @@ -42,6 +47,9 @@ public ViewResponseModel prepareSuccess(ViewResponseModel response) { }; private ViewInputBoundary viewInputBoundary; + /** + * Set up the test environment by initializing the BrowseQuestionUseCase instance. + */ public void setUpBrowseUseCase(){ questionGateway = new QuestionRepo(); clientGateway = new ClientRepository(); @@ -77,6 +85,9 @@ public void setUpBrowseUseCase(){ SessionManager.setSession(session); } + /** + * Test the AttorneyBrowseQuestionUseCase when the question is created successfully. + */ @Test public void TestAttorneyBrowseQuestionUseCase(){ setUpBrowseUseCase(); @@ -84,6 +95,9 @@ public void TestAttorneyBrowseQuestionUseCase(){ ClearAllRepository(); } + /** + * Delete all data in questionGateway, clientGateway and attorneyGateway. + */ public void ClearAllRepository(){ questionGateway = new QuestionRepo(); clientGateway = new ClientRepository(); diff --git a/src/test/java/usecasetesting/ClientRegisterUseCaseTest.java b/src/test/java/usecasetesting/ClientRegisterUseCaseTest.java index 7417cf8b..5de41192 100644 --- a/src/test/java/usecasetesting/ClientRegisterUseCaseTest.java +++ b/src/test/java/usecasetesting/ClientRegisterUseCaseTest.java @@ -1,15 +1,19 @@ package usecasetesting; -import businessrule.gateway.ClientGateway; -import businessrule.inputboundary.UserRegisterInputBoundary; -import businessrule.outputboundary.BaseOutputBoundary; -import businessrule.requestmodel.RegistrationData; -import businessrule.responsemodel.BaseResponseModel; -import businessrule.usecase.ClientRegisterInteractor; -import driver.database.ClientRepository; -import entity.factory.ClientFactory; +import usecases.gateway.ClientGateway; +import usecases.inputboundary.UserRegisterInputBoundary; +import usecases.outputboundary.BaseOutputBoundary; +import usecases.requests.ClientRegistrationData; +import usecases.requests.RegistrationData; +import usecases.responses.BaseResponseModel; +import usecases.interactors.ClientRegisterInteractor; +import infrastructure.database.ClientRepository; +import entities.factories.ClientFactory; import org.junit.jupiter.api.Test; +/** + * This class contains unit tests for the ClientRegisterUseCase class. + */ public class ClientRegisterUseCaseTest { final static String PASSWORD = "abcdefghi"; final static String USER_NAME = "joseph"; @@ -26,6 +30,10 @@ public class ClientRegisterUseCaseTest { private ClientGateway clientGateway; private UserRegisterInputBoundary clientRegisterInputBoundary; + + /** + * Set up the test environment by initializing the ClientRegisterUseCase instance. + */ public void setUpClientRegisterUseCase(){ clientGateway = new ClientRepository(); ClientFactory clientFactory = new ClientFactory(); @@ -45,78 +53,159 @@ public BaseResponseModel prepareFail(String msg) { } }; - clientRegisterInputBoundary = new ClientRegisterInteractor(clientGateway, clientFactory, baseOutputBoundary); + clientRegisterInputBoundary = new ClientRegisterInteractor(clientGateway, baseOutputBoundary, clientFactory); } + + /** + * Test the successful registration scenario. + */ @Test public void TestSuccessfulRegistration(){ setUpClientRegisterUseCase(); - RegistrationData inputData = new RegistrationData(USER_NAME, EMAIL, PASSWORD, PASSWORD2, STATE_ABB, POSTALCODE, ETHNICITY, AGE, GENDER, MARITAL_STATUS, NUMBER_OF_HOUSEHOLD, ANNUAL_INCOME); + RegistrationData data = new RegistrationData(USER_NAME, EMAIL, PASSWORD, PASSWORD2, STATE_ABB, POSTALCODE); + ClientRegistrationData inputData = new ClientRegistrationData.Builder(data) + .age(AGE) + .annualIncome(ANNUAL_INCOME) + .gender(GENDER) + .maritalStatus(MARITAL_STATUS) + .numberOfHousehold(NUMBER_OF_HOUSEHOLD) + .ethnicity(ETHNICITY) + .build(); clientRegisterInputBoundary.create(inputData); ClearAllRepository(); } + /** + * Test the registration failure scenario due to an existing user. + */ @Test public void TestRegistrationFailByAlreadyExists(){ setUpClientRegisterUseCase(); - RegistrationData inputData = new RegistrationData(USER_NAME, EMAIL, PASSWORD, PASSWORD2, STATE_ABB, POSTALCODE, ETHNICITY, AGE, GENDER, MARITAL_STATUS, NUMBER_OF_HOUSEHOLD, ANNUAL_INCOME); + RegistrationData data = new RegistrationData(USER_NAME, EMAIL, PASSWORD, PASSWORD2, STATE_ABB, POSTALCODE); + ClientRegistrationData inputData = new ClientRegistrationData.Builder(data) + .age(AGE) + .annualIncome(ANNUAL_INCOME) + .gender(GENDER) + .maritalStatus(MARITAL_STATUS) + .numberOfHousehold(NUMBER_OF_HOUSEHOLD) + .ethnicity(ETHNICITY) + .build(); clientRegisterInputBoundary.create(inputData); clientRegisterInputBoundary.create(inputData); ClearAllRepository(); } + /** + * Test registration failure due to password not matching. + */ @Test public void TestRegistrationFailByPasswordDoesNotMatch(){ setUpClientRegisterUseCase(); - RegistrationData inputData = new RegistrationData(USER_NAME, EMAIL, PASSWORD, "a", STATE_ABB, POSTALCODE, ETHNICITY, AGE, GENDER, MARITAL_STATUS, NUMBER_OF_HOUSEHOLD, ANNUAL_INCOME); + RegistrationData data = new RegistrationData(USER_NAME, EMAIL, PASSWORD, "a", STATE_ABB, POSTALCODE); + ClientRegistrationData inputData = new ClientRegistrationData.Builder(data) + .age(AGE) + .annualIncome(ANNUAL_INCOME) + .gender(GENDER) + .maritalStatus(MARITAL_STATUS) + .numberOfHousehold(NUMBER_OF_HOUSEHOLD) + .ethnicity(ETHNICITY) + .build(); clientRegisterInputBoundary.create(inputData); ClearAllRepository(); } + + /** + * Test registration failure due to password length being too small. + */ @Test public void TestRegistrationFailByPasswordLengthTooSmall(){ setUpClientRegisterUseCase(); - RegistrationData inputData = new RegistrationData(USER_NAME, EMAIL, "a", "a", STATE_ABB, POSTALCODE, ETHNICITY, AGE, GENDER, MARITAL_STATUS, NUMBER_OF_HOUSEHOLD, ANNUAL_INCOME); + RegistrationData data = new RegistrationData(USER_NAME, EMAIL, "a", "a", STATE_ABB, POSTALCODE); + ClientRegistrationData inputData = new ClientRegistrationData.Builder(data) + .age(AGE) + .annualIncome(ANNUAL_INCOME) + .gender(GENDER) + .maritalStatus(MARITAL_STATUS) + .numberOfHousehold(NUMBER_OF_HOUSEHOLD) + .ethnicity(ETHNICITY) + .build(); clientRegisterInputBoundary.create(inputData); ClearAllRepository(); } + /** + * Test registration failure due to invalid email format. + */ @Test public void TestRegistrationFailByInvalidEmail(){ setUpClientRegisterUseCase(); - RegistrationData inputData = new RegistrationData(USER_NAME, "abcd", PASSWORD, PASSWORD2, STATE_ABB, POSTALCODE, ETHNICITY, AGE, GENDER, MARITAL_STATUS, NUMBER_OF_HOUSEHOLD, ANNUAL_INCOME); - + RegistrationData data = new RegistrationData(USER_NAME, "abcd", PASSWORD, PASSWORD2, STATE_ABB, POSTALCODE); + ClientRegistrationData inputData = new ClientRegistrationData.Builder(data) + .age(AGE) + .annualIncome(ANNUAL_INCOME) + .gender(GENDER) + .maritalStatus(MARITAL_STATUS) + .numberOfHousehold(NUMBER_OF_HOUSEHOLD) + .ethnicity(ETHNICITY) + .build(); clientRegisterInputBoundary.create(inputData); ClearAllRepository(); } + /** + * Test registration failure due to invalid age. + */ @Test public void TestRegistrationFailByInvalidAge(){ setUpClientRegisterUseCase(); - RegistrationData inputData = new RegistrationData(USER_NAME, EMAIL, PASSWORD, PASSWORD2, STATE_ABB, POSTALCODE, ETHNICITY, 300, GENDER, MARITAL_STATUS, NUMBER_OF_HOUSEHOLD, ANNUAL_INCOME); + RegistrationData data = new RegistrationData(USER_NAME, EMAIL, PASSWORD, PASSWORD2, STATE_ABB, POSTALCODE); + ClientRegistrationData inputData = new ClientRegistrationData.Builder(data) + .age(300) + .annualIncome(ANNUAL_INCOME) + .gender(GENDER) + .maritalStatus(MARITAL_STATUS) + .numberOfHousehold(NUMBER_OF_HOUSEHOLD) + .ethnicity(ETHNICITY) + .build(); clientRegisterInputBoundary.create(inputData); ClearAllRepository(); } + /** + * Test registration failure due to invalid postal code. + */ @Test public void TestRegistrationFailByInvalidPostalCode(){ setUpClientRegisterUseCase(); - RegistrationData inputData = new RegistrationData(USER_NAME, EMAIL, PASSWORD, PASSWORD2, STATE_ABB, "test code", ETHNICITY, AGE, GENDER, MARITAL_STATUS, NUMBER_OF_HOUSEHOLD, ANNUAL_INCOME); + RegistrationData data = new RegistrationData(USER_NAME, EMAIL, PASSWORD, PASSWORD2, STATE_ABB, "test code"); + ClientRegistrationData inputData = new ClientRegistrationData.Builder(data) + .age(AGE) + .annualIncome(ANNUAL_INCOME) + .gender(GENDER) + .maritalStatus(MARITAL_STATUS) + .numberOfHousehold(NUMBER_OF_HOUSEHOLD) + .ethnicity(ETHNICITY) + .build(); clientRegisterInputBoundary.create(inputData); ClearAllRepository(); } + /** + * Delete all data in clientGateway. + */ public void ClearAllRepository(){ clientGateway = new ClientRepository(); clientGateway.deleteAll(); diff --git a/src/test/java/usecasetesting/CloseUseCaseTest.java b/src/test/java/usecasetesting/CloseUseCaseTest.java index 64501b23..2bcd4105 100644 --- a/src/test/java/usecasetesting/CloseUseCaseTest.java +++ b/src/test/java/usecasetesting/CloseUseCaseTest.java @@ -1,22 +1,25 @@ package usecasetesting; -import businessrule.SessionManager; -import businessrule.UserSession; -import businessrule.gateway.*; -import businessrule.inputboundary.CloseInputBoundary; -import businessrule.outputboundary.UserOutputBoundary; -import businessrule.requestmodel.CloseRequestModel; -import businessrule.responsemodel.UserResponseModel; -import businessrule.usecase.CloseQuestionInteractor; -import driver.database.*; -import entity.Attorney; -import entity.Client; -import entity.Question; +import usecases.session.SessionManager; +import usecases.session.UserSession; +import usecases.gateway.*; +import usecases.inputboundary.CloseInputBoundary; +import usecases.outputboundary.UserOutputBoundary; +import usecases.requests.CloseRequestModel; +import usecases.responses.UserResponseModel; +import usecases.interactors.CloseQuestionInteractor; +import infrastructure.database.*; +import entities.user.Attorney; +import entities.user.Client; +import entities.Question; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; +/** + * This class contains test cases for the CloseUseCase class. + */ public class CloseUseCaseTest { final static int CLIENT_ID = 21345678; final static String CLIENT_USERNAME = "test client"; @@ -31,6 +34,9 @@ public class CloseUseCaseTest { private AttorneyGateway attorneyGateway; private CloseInputBoundary closeInputBoundary; + /** + * Set up the test environment by initializing the CloseUseUseCase instance. + */ public void setUpCloseUseCase(){ questionGateway = new QuestionRepo(); @@ -81,6 +87,10 @@ public UserResponseModel prepareSuccess(UserResponseModel userResponseModel) { UserSession session = new UserSession(userResponseModel); SessionManager.setSession(session); } + + /** + * Test case for closing a question by a client. + */ @Test public void TestClientCloseableQuestion(){ setUpCloseUseCase(); @@ -92,6 +102,10 @@ public void TestClientCloseableQuestion(){ assertTrue(question.isClose()); ClearAllRepository(); } + + /** + * Test case for attempting to close an already closed question by a client. + */ @Test public void TestClientUnclosableQuestion(){ setUpCloseUseCase(); @@ -102,7 +116,10 @@ public void TestClientUnclosableQuestion(){ ClearAllRepository(); } - public void ClearAllRepository(){ + /** + * Delete all data in clientGateway. + */ + public void ClearAllRepository() { questionGateway = new QuestionRepo(); clientGateway = new ClientRepository(); attorneyGateway = new AttorneyRepository(); diff --git a/src/test/java/usecasetesting/RateAnswerUseCaseTest.java b/src/test/java/usecasetesting/RateAnswerUseCaseTest.java index 8d86f41b..32b9546a 100644 --- a/src/test/java/usecasetesting/RateAnswerUseCaseTest.java +++ b/src/test/java/usecasetesting/RateAnswerUseCaseTest.java @@ -1,23 +1,24 @@ package usecasetesting; -import businessrule.SessionManager; -import businessrule.UserSession; -import businessrule.gateway.*; -import businessrule.inputboundary.RateInputBoundary; -import businessrule.outputboundary.UserOutputBoundary; -import businessrule.requestmodel.RateRequestModel; -import businessrule.responsemodel.UserResponseModel; -import businessrule.usecase.RateInteractor; -import driver.database.*; -import entity.Attorney; -import entity.Client; -import entity.Question; +import usecases.session.SessionManager; +import usecases.session.UserSession; +import usecases.gateway.*; +import usecases.inputboundary.RateInputBoundary; +import usecases.outputboundary.UserOutputBoundary; +import usecases.requests.RateRequestModel; +import usecases.responses.UserResponseModel; +import usecases.interactors.RateInteractor; +import infrastructure.database.*; +import entities.user.Attorney; +import entities.user.Client; +import entities.Question; import org.junit.jupiter.api.Test; - - import static org.junit.jupiter.api.Assertions.*; +/** + * This class contains unit tests for the RateAnswerUseCase class. + */ public class RateAnswerUseCaseTest { final static int CLIENT_ID = 21345678; @@ -32,6 +33,9 @@ public class RateAnswerUseCaseTest { private AttorneyGateway attorneyGateway; private RateInputBoundary rateInputBoundary; + /** + * Set up the test environment by initializing the RateAnswerUseCase instance. + */ public void setUpRateAnswerUseCase(){ questionGateway = new QuestionRepo(); @@ -55,7 +59,7 @@ public UserResponseModel prepareSuccess(UserResponseModel userResponseModel) { return null; } }; - rateInputBoundary = new RateInteractor(questionGateway, userOutputBoundary, clientGateway, attorneyGateway); + rateInputBoundary = new RateInteractor(questionGateway, userOutputBoundary, attorneyGateway); Question question = new Question(); question.setQuestionId(QUESTION_ID); @@ -87,6 +91,10 @@ public UserResponseModel prepareSuccess(UserResponseModel userResponseModel) { UserSession session = new UserSession(userResponseModel); SessionManager.setSession(session); } + + /** + * Test for the scenario when a client rates a closed question. + */ @Test public void TestClientRateClosedQuestion(){ setUpRateAnswerUseCase(); @@ -96,6 +104,10 @@ public void TestClientRateClosedQuestion(){ assertEquals(10, questionGateway.get(CLOSED_QUESTION_ID).getRating()); ClearAllRepository(); } + + /** + * Test for the scenario when a client rates an unclosed question. + */ @Test public void TestClientRateUnClosedQuestion(){ setUpRateAnswerUseCase(); @@ -104,6 +116,10 @@ public void TestClientRateUnClosedQuestion(){ rateInputBoundary.rateAnswer(inputData); ClearAllRepository(); } + + /** + * Delete all data in clientGateway, questionGateway, attorneyGateway, postGateway. + */ public void ClearAllRepository(){ questionGateway = new QuestionRepo(); clientGateway = new ClientRepository(); diff --git a/src/test/java/usecasetesting/ReplyUseCaseTest.java b/src/test/java/usecasetesting/ReplyUseCaseTest.java index cc71106d..5c3a95d8 100644 --- a/src/test/java/usecasetesting/ReplyUseCaseTest.java +++ b/src/test/java/usecasetesting/ReplyUseCaseTest.java @@ -1,22 +1,27 @@ package usecasetesting; -import businessrule.SessionManager; -import businessrule.UserSession; -import businessrule.gateway.*; -import businessrule.inputboundary.PostInputBoundary; -import businessrule.outputboundary.TheQuestionOutputBoundary; -import businessrule.requestmodel.PostRequestModel; -import businessrule.responsemodel.TheQuestionResponseModel; -import businessrule.responsemodel.UserResponseModel; -import businessrule.usecase.ReplyInteractor; -import driver.database.*; -import entity.*; -import entity.factory.PostFactory; +import entities.user.Attorney; +import entities.user.Client; +import entities.user.User; +import usecases.session.SessionManager; +import usecases.session.UserSession; +import usecases.gateway.*; +import usecases.inputboundary.PostInputBoundary; +import usecases.outputboundary.TheQuestionOutputBoundary; +import usecases.requests.PostRequestModel; +import usecases.responses.TheQuestionResponseModel; +import usecases.responses.UserResponseModel; +import usecases.interactors.ReplyInteractor; +import infrastructure.database.*; +import entities.*; +import entities.factories.PostFactory; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; - +/** + * This class contains test cases for the ReplyUseCase. + */ public class ReplyUseCaseTest { final static int CLIENT_ID = 21345678; final static String CLIENT_USERNAME = "test client"; @@ -32,6 +37,10 @@ public class ReplyUseCaseTest { private ClientGateway clientGateway; private AttorneyGateway attorneyGateway; private PostInputBoundary postInputBoundary; + + /** + * Set up the test environment by initializing the ReplyUseCase instance. + */ public void setUpReplyUseCase(){ questionGateway = new QuestionRepo(); @@ -58,10 +67,12 @@ public TheQuestionResponseModel prepareSuccess(TheQuestionResponseModel response Question question = new Question(); question.setQuestionId(QUESTION_ID); + question.setAskedByClient(CLIENT_ID); questionGateway.save(question); Question closedQuestion = new Question(); closedQuestion.setQuestionId(CLOSED_QUESTION_ID); + closedQuestion.setAskedByClient(CLIENT_ID); questionGateway.save(closedQuestion); Client client = new Client(); @@ -80,6 +91,9 @@ public TheQuestionResponseModel prepareSuccess(TheQuestionResponseModel response attorneyGateway.save(secondAttorney); } + /** + * Test client's reply to a question. + */ @Test public void testClientReply(){ setUpReplyUseCase(); @@ -95,6 +109,9 @@ public void testClientReply(){ ClearAllRepository(); } + /** + * Test attorney's first reply to a question. + */ @Test public void testAttorneyFirstReply(){ setUpReplyUseCase(); @@ -116,6 +133,9 @@ public void testAttorneyFirstReply(){ assertTrue(attorneyQuestion.isTaken()); ClearAllRepository(); } + /** + * Test attorney's follow-up reply to a question. + */ @Test public void testAttorneyFollowUp(){ setUpReplyUseCase(); @@ -143,6 +163,10 @@ public void testAttorneyFollowUp(){ ClearAllRepository(); } + + /** + * Test failure to reply to a closed question. + */ @Test public void testFailToReplyQuestionClosed(){ setUpReplyUseCase(); @@ -154,6 +178,9 @@ public void testFailToReplyQuestionClosed(){ ClearAllRepository(); } + /** + * Test failure to reply to a question that was taken by other attorney. + */ @Test public void testAttorneyFailToReplyQuestionTakenByOther(){ setUpReplyUseCase(); @@ -177,6 +204,9 @@ public void testAttorneyFailToReplyQuestionTakenByOther(){ ClearAllRepository(); } + /** + * Delete all data in clientGateway, questionGateway, attorneyGateway, postGateway. + */ public void ClearAllRepository(){ questionGateway = new QuestionRepo(); clientGateway = new ClientRepository(); diff --git a/src/test/java/usecasetesting/SelectQuestionUseCaseTest.java b/src/test/java/usecasetesting/SelectQuestionUseCaseTest.java index b0cbcfa5..19b368ab 100644 --- a/src/test/java/usecasetesting/SelectQuestionUseCaseTest.java +++ b/src/test/java/usecasetesting/SelectQuestionUseCaseTest.java @@ -1,25 +1,29 @@ package usecasetesting; -import businessrule.SessionManager; -import businessrule.UserSession; -import businessrule.gateway.*; -import businessrule.inputboundary.SelectInputBoundary; -import businessrule.outputboundary.TheQuestionOutputBoundary; -import businessrule.requestmodel.SelectRequestModel; -import businessrule.responsemodel.TheQuestionResponseModel; -import businessrule.responsemodel.UserResponseModel; -import businessrule.usecase.util.PostDisplayFormatter; -import businessrule.usecase.SelectQuestionInteractor; -import driver.database.*; -import entity.*; +import entities.user.Attorney; +import entities.user.Client; +import usecases.session.SessionManager; +import usecases.session.UserSession; +import usecases.gateway.*; +import usecases.inputboundary.SelectInputBoundary; +import usecases.outputboundary.TheQuestionOutputBoundary; +import usecases.requests.SelectRequestModel; +import usecases.responses.TheQuestionResponseModel; +import usecases.responses.UserResponseModel; +import usecases.dto.PostDisplay; +import usecases.interactors.SelectQuestionInteractor; +import infrastructure.database.*; +import entities.*; import org.junit.jupiter.api.Test; import java.util.ArrayList; import java.util.List; import static org.junit.jupiter.api.Assertions.*; - +/** + * This class contains test cases for the SelectQuestionUseCase. + */ public class SelectQuestionUseCaseTest { final static int CLIENT_ID = 21345678; final static String CLIENT_USERNAME = "test client"; @@ -39,6 +43,9 @@ public class SelectQuestionUseCaseTest { private AttorneyGateway attorneyGateway; private SelectInputBoundary selectInputBoundary; + /** + * Set up the test environment by initializing the SelectQuestionUseCase instance. + */ public void setUpSelectUseCase(){ questionGateway = new QuestionRepo(); UserGatewayFactory userGatewayFactory = new UserGatewayFactory(); @@ -56,7 +63,7 @@ public TheQuestionResponseModel prepareFail(String msg) { @Override public TheQuestionResponseModel prepareSuccess(TheQuestionResponseModel response) { assertEquals(2, response.getPostMap().size(), "The post map is not correct."); - List arrayList; + List arrayList; arrayList = new ArrayList<>(response.getPostMap().values()); assertEquals("test text", arrayList.get(0).getPostText()); return null; @@ -109,6 +116,9 @@ public TheQuestionResponseModel prepareSuccess(TheQuestionResponseModel response questionGateway.save(question3); } + /** + * Test the scenario where a client selects a question. + */ @Test public void TestClientSelectQuestionUseCase(){ setUpSelectUseCase(); @@ -122,6 +132,9 @@ public void TestClientSelectQuestionUseCase(){ ClearAllRepository(); } + /** + * Test the scenario where an attorney selects a non-taken question. + */ @Test public void TestAttorneySelectNonTakenQuestionUseCase(){ setUpSelectUseCase(); @@ -134,6 +147,9 @@ public void TestAttorneySelectNonTakenQuestionUseCase(){ ClearAllRepository(); } + /** + * Test the scenario where an attorney selects a question taken by himself. + */ @Test public void TestAttorneySelectQuestionTakenByHimselfUseCase(){ setUpSelectUseCase(); @@ -146,6 +162,9 @@ public void TestAttorneySelectQuestionTakenByHimselfUseCase(){ ClearAllRepository(); } + /** + * Test the scenario where an attorney's attempt to select a closed question fails. + */ @Test public void TestAttorneySelectQuestionFailByClosedQuestion(){ setUpSelectUseCase(); @@ -158,6 +177,9 @@ public void TestAttorneySelectQuestionFailByClosedQuestion(){ ClearAllRepository(); } + /** + * Test the scenario where an attorney's attempt to select a question taken by another attorney fails. + */ @Test public void TestAttorneySelectQuestionFailByTakenByOther(){ setUpSelectUseCase(); @@ -170,6 +192,9 @@ public void TestAttorneySelectQuestionFailByTakenByOther(){ ClearAllRepository(); } + /** + * Delete all data in clientGateway, questionGateway, attorneyGateway, postGateway. + */ public void ClearAllRepository(){ questionGateway = new QuestionRepo(); clientGateway = new ClientRepository(); diff --git a/src/test/java/usecasetesting/UserLoginUseCaseTest.java b/src/test/java/usecasetesting/UserLoginUseCaseTest.java index 51f9f4b6..4de29f3f 100644 --- a/src/test/java/usecasetesting/UserLoginUseCaseTest.java +++ b/src/test/java/usecasetesting/UserLoginUseCaseTest.java @@ -1,18 +1,21 @@ package usecasetesting; -import businessrule.SessionManager; -import businessrule.gateway.ClientGateway; -import businessrule.gateway.UserGatewayFactory; -import businessrule.inputboundary.UserLoginInputBoundary; -import businessrule.outputboundary.UserOutputBoundary; -import businessrule.requestmodel.UserLoginRequestModel; -import businessrule.responsemodel.UserResponseModel; -import businessrule.usecase.UserLoginInteractor; -import driver.database.*; -import entity.Client; +import usecases.session.SessionManager; +import usecases.gateway.ClientGateway; +import infrastructure.database.UserGatewayFactory; +import usecases.inputboundary.UserLoginInputBoundary; +import usecases.outputboundary.UserOutputBoundary; +import usecases.requests.UserLoginRequestModel; +import usecases.responses.UserResponseModel; +import usecases.interactors.UserLoginInteractor; +import infrastructure.database.*; +import entities.user.Client; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; +/** + * This class contains test cases for the UserLoginUseCase. + */ public class UserLoginUseCaseTest { final static int CLIENT_ID = 21345678; @@ -20,6 +23,9 @@ public class UserLoginUseCaseTest { private ClientGateway clientGateway; private UserLoginInputBoundary userLoginInputBoundary; + /** + * Set up the test environment by initializing the UserLoginUseCase instance. + */ public void setUpUserLoginUseCase(){ UserGatewayFactory userGatewayFactory = new UserGatewayFactory(); clientGateway = new ClientRepository(); @@ -45,6 +51,9 @@ public UserResponseModel prepareSuccess(UserResponseModel userResponseModel) { client.setPassword(PASSWORD); clientGateway.save(client); } + /** + * Test a successful user login scenario. + */ @Test public void TestLoginPass(){ setUpUserLoginUseCase(); @@ -52,6 +61,9 @@ public void TestLoginPass(){ userLoginInputBoundary.login(inputData); ClearAllRepository(); } + /** + * Test user login fails because the provided ID does not exist. + */ @Test public void TestLoginFailIdDNE(){ setUpUserLoginUseCase(); @@ -59,6 +71,9 @@ public void TestLoginFailIdDNE(){ userLoginInputBoundary.login(inputData); ClearAllRepository(); } + /** + * Test the scenario where user login fails because the provided password is incorrect. + */ @Test public void TestLoginFailWrongPassword(){ setUpUserLoginUseCase(); @@ -67,6 +82,9 @@ public void TestLoginFailWrongPassword(){ ClearAllRepository(); } + /** + * Delete all data in clientGateway. + */ public void ClearAllRepository(){ clientGateway = new ClientRepository(); clientGateway.deleteAll(); diff --git a/src/test/java/usecasetesting/UserViewHistoryUseCaseTest.java b/src/test/java/usecasetesting/UserViewHistoryUseCaseTest.java index 3377f0e9..ae7b314c 100644 --- a/src/test/java/usecasetesting/UserViewHistoryUseCaseTest.java +++ b/src/test/java/usecasetesting/UserViewHistoryUseCaseTest.java @@ -1,29 +1,32 @@ package usecasetesting; -import businessrule.SessionManager; -import businessrule.UserSession; -import businessrule.gateway.AttorneyGateway; -import businessrule.gateway.ClientGateway; -import businessrule.gateway.QuestionGateway; -import businessrule.gateway.UserGatewayFactory; -import businessrule.inputboundary.ViewInputBoundary; -import businessrule.outputboundary.ViewOutputBoundary; -import businessrule.responsemodel.UserResponseModel; -import businessrule.responsemodel.ViewResponseModel; -import businessrule.usecase.util.QuestionDisplayFormatter; -import businessrule.usecase.ViewQuestionInteractor; -import driver.database.AttorneyRepository; -import driver.database.ClientRepository; -import driver.database.QuestionRepo; -import entity.Attorney; -import entity.Client; -import entity.Question; +import usecases.session.SessionManager; +import usecases.session.UserSession; +import usecases.gateway.AttorneyGateway; +import usecases.gateway.ClientGateway; +import usecases.gateway.QuestionGateway; +import infrastructure.database.UserGatewayFactory; +import usecases.inputboundary.ViewInputBoundary; +import usecases.outputboundary.ViewOutputBoundary; +import usecases.responses.UserResponseModel; +import usecases.responses.ViewResponseModel; +import usecases.dto.QuestionDisplay; +import usecases.interactors.ViewQuestionInteractor; +import infrastructure.database.AttorneyRepository; +import infrastructure.database.ClientRepository; +import infrastructure.database.QuestionRepo; +import entities.user.Attorney; +import entities.user.Client; +import entities.Question; import org.junit.jupiter.api.Test; import java.util.ArrayList; import java.util.List; import static org.junit.jupiter.api.Assertions.*; +/** + * This class contains test cases for the ViewHistoryUseCase. + */ public class UserViewHistoryUseCaseTest { final static int CLIENT_ID = 21345678; final static String CLIENT_USERNAME = "test client"; @@ -37,6 +40,9 @@ public class UserViewHistoryUseCaseTest { private AttorneyGateway attorneyGateway; private ViewInputBoundary viewInputBoundary; + /** + * Set up the test environment by initializing the ViewQuestionUseCase instance. + */ public void setUpViewQuestionUseCase(){ UserGatewayFactory userGatewayFactory = new UserGatewayFactory(); questionGateway = new QuestionRepo(); @@ -53,7 +59,7 @@ public ViewResponseModel prepareFail(String msg) { @Override public ViewResponseModel prepareSuccess(ViewResponseModel response) { assertEquals(1, response.getQuestionMap().size(), "The Question Map is not correct."); - List arrayList; + List arrayList; arrayList = new ArrayList<>(response.getQuestionMap().values()); assertEquals("test title", arrayList.get(0).getTitle()); return null; @@ -78,6 +84,9 @@ public ViewResponseModel prepareSuccess(ViewResponseModel response) { attorneyGateway.save(attorney); } + /** + * Test the client's view question use case. + */ @Test public void TestClientViewUseCase(){ setUpViewQuestionUseCase(); @@ -89,6 +98,9 @@ public void TestClientViewUseCase(){ ClearAllRepository(); } + /** + * Test the attorney's view question use case. + */ @Test public void TestAttorneyViewUseCase(){ setUpViewQuestionUseCase(); @@ -100,6 +112,9 @@ public void TestAttorneyViewUseCase(){ ClearAllRepository(); } + /** + * Delete all data in clientGateway, questionGateway, attorneyGateway. + */ public void ClearAllRepository(){ questionGateway = new QuestionRepo(); clientGateway = new ClientRepository(); diff --git a/src/test/java/usecasetesting/ViewRecommendationUseCaseTest.java b/src/test/java/usecasetesting/ViewRecommendationUseCaseTest.java index 66bd644d..3a87ebd3 100644 --- a/src/test/java/usecasetesting/ViewRecommendationUseCaseTest.java +++ b/src/test/java/usecasetesting/ViewRecommendationUseCaseTest.java @@ -1,17 +1,19 @@ package usecasetesting; -import businessrule.SessionManager; -import businessrule.UserSession; -import businessrule.gateway.AttorneyGateway; -import businessrule.gateway.ClientGateway; -import businessrule.gateway.QuestionGateway; -import businessrule.inputboundary.ViewInputBoundary; -import businessrule.outputboundary.ViewOutputBoundary; -import businessrule.responsemodel.UserResponseModel; -import businessrule.responsemodel.ViewResponseModel; -import businessrule.usecase.AttorneyRecommendInteractor; -import driver.database.*; -import entity.*; +import entities.user.Attorney; +import entities.user.Client; +import usecases.session.SessionManager; +import usecases.session.UserSession; +import usecases.gateway.AttorneyGateway; +import usecases.gateway.ClientGateway; +import usecases.gateway.QuestionGateway; +import usecases.inputboundary.ViewInputBoundary; +import usecases.outputboundary.ViewOutputBoundary; +import usecases.responses.UserResponseModel; +import usecases.responses.ViewResponseModel; +import usecases.interactors.AttorneyRecommendInteractor; +import infrastructure.database.*; +import entities.*; import org.junit.jupiter.api.Test; import java.time.LocalDate; import static org.junit.jupiter.api.Assertions.*;