Skip to content

Commit

Permalink
Create citizen dashboard controller (view applications controller) (#35)
Browse files Browse the repository at this point in the history
* fix: Make payment amount a double in the db

* feat: Add application statuses to string constants

* refactor: Use new strategy to populate JTable in dashboard

* feat: Add cancel handler for apply view

* Add controller to routing

* feat: Add session ender convenience method

* feat: Add citizen dashboard controller

* fix: Change return type of application IDs

* refactor: Cache application objects in ApplicationFactory

* fix: Rename resource
  • Loading branch information
Vishwas Adiga authored Nov 7, 2021
1 parent d8bae4a commit a15398e
Show file tree
Hide file tree
Showing 14 changed files with 352 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public ApplyForPassportController(ApplicationFormView view) {
initSubmitButtonHandler();
initDocumentUploadButtonsHandlers();
initFindCitizenHandler();
initButtonHandlers();

view.getEmergencyNameTextField().setEnabled(false);
view.getEmergencyPhoneTextField().setEnabled(false);
Expand All @@ -72,29 +73,6 @@ public View getApplicationFormView() {
public void initButtons() {
view.getBookSlotButton().setEnabled(false);
view.getPayButton().setEnabled(false);

view.getBookSlotButton().addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
// TODO: Find better conversion strategy between LocalDate and Date
application.setDateOfAppointment(
new Date(view.getAppointmentDatePicker().getDate().toEpochDay() * 24 * 60 * 60 * 1000));
application.advanceApplicationStatus();
showInfo("Slot booked!");
}
});

view.getPayButton().addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
application.getPayment().fulfil();
application.advanceApplicationStatus();
showInfo("Payment successful.\nApplication successfully submitted!");
RouteController.getController().routeTo(Route.CITIZEN_DASHBOARD);
}
});
}

private void initSubmitButtonHandler() {
Expand Down Expand Up @@ -127,13 +105,14 @@ public void actionPerformed(ActionEvent event) {
return;
}

CitizenSession session = (CitizenSession) SessionController.getController().getCurrentSession();
if (application == null) {
int selection = view.getApplicationTypeButtonGroup().getSelection().getMnemonic();
ApplicationType type = (selection == 0 ? ApplicationType.FRESH : ApplicationType.RE_ISSUE);
application = ApplicationFactory.getInstance().getApplication(type);
application.setApplicant(session.getCitizen());
}
CitizenSession session = (CitizenSession) SessionController.getController().getCurrentSession();
application.setApplicant(session.getCitizen());

view.getPayerNameLabel().setText(application.getApplicant().getName());
view.getPaymentAmountLabel().setText("Rs." + Application.APPLICATION_PAYMENT_AMOUNT);

Expand Down Expand Up @@ -163,10 +142,10 @@ public void actionPerformed(ActionEvent event) {
}
documentPaths.clear();

if (!application.hasRequiredDocuments()) {
showError("Please upload all required documents!");
return;
}
// if (!application.hasRequiredDocuments()) {
// showError("Please upload all required documents!");
// return;
// }

application.advanceApplicationStatus();

Expand Down Expand Up @@ -222,6 +201,39 @@ public boolean isDateAllowed(LocalDate requestedSlot) {
}
});
}

private void initButtonHandlers() {
view.getBookSlotButton().addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
// TODO: Find better conversion strategy between LocalDate and Date
application.setDateOfAppointment(
new Date(view.getAppointmentDatePicker().getDate().toEpochDay() * 24 * 60 * 60 * 1000));
application.advanceApplicationStatus();
showInfo("Slot booked!");
}
});

view.getPayButton().addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
application.getPayment().fulfil();
application.advanceApplicationStatus();
showInfo("Payment successful.\nApplication successfully submitted!");
RouteController.getController().routeTo(Route.CITIZEN_DASHBOARD);
}
});

view.getCancelButton().addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
RouteController.getController().routeTo(Route.CITIZEN_DASHBOARD);
}
});
}

private boolean formFieldsEmpty() {
JTextComponent[] fields = new JTextComponent[] { view.getPlaceOfBirthTextField(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import edu.manipal.icas.simple.impl.views.PassportGrantingOfficerDashboardViewImpl;
import edu.manipal.icas.simple.impl.views.PoliceDashboardViewImpl;
import edu.manipal.icas.simple.impl.views.ProfileCreationViewImpl;
import edu.manipal.icas.simple.impl.views.ApplicationFormViewImpl;
import edu.manipal.icas.simple.impl.views.VerificationOfficerDashboardViewImpl;
import edu.manipal.icas.simple.views.View;

Expand All @@ -23,15 +24,19 @@ public final class RouteController {
private final SessionController sessions = SessionController.getController();

private View currentView;

private LoginController loginController;
private CreateProfileController createProfileController;
private ApplyForPassportController applyForPassportController;
private ViewApplicationsController viewApplicationsController;

private RouteController() {
currentView = null;

loginController = new LoginController(new CitizenLoginViewImpl(), new OfficerLoginViewImpl());
createProfileController = new CreateProfileController(new ProfileCreationViewImpl());
applyForPassportController = new ApplyForPassportController(new ApplicationFormViewImpl());
currentView = null;
viewApplicationsController = new ViewApplicationsController(new CitizenDashboardViewImpl());
}

/**
Expand Down Expand Up @@ -77,7 +82,7 @@ public void routeTo(Route route) {
displayView(applyForPassportController.getApplicationFormView());
break;
case CITIZEN_DASHBOARD:
displayView(new CitizenDashboardViewImpl());
displayView(viewApplicationsController.getDashboardView());
break;
case BIOMETRICS_DASHBOARD:
displayView(new BiometricOfficerViewImpl());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public Session getCurrentSession() {
return db.getCurrentSession();
}

public boolean startSession(Session session) {
public Boolean startSession(Session session) {
try {
db.startSession(session);
return true;
Expand All @@ -32,4 +32,15 @@ public boolean startSession(Session session) {
}
return false;
}

public Boolean endCurrentSession() {
try {
db.endCurrentSession();
return true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package edu.manipal.icas.simple.controllers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.table.DefaultTableModel;

import edu.manipal.icas.simple.models.application.Application;
import edu.manipal.icas.simple.models.application.ApplicationQuestion;
import edu.manipal.icas.simple.session.CitizenSession;
import edu.manipal.icas.simple.utils.StringConstants;
import edu.manipal.icas.simple.views.CitizenDashboardView;
import edu.manipal.icas.simple.views.View;

public class ViewApplicationsController {
private CitizenDashboardView view;
private CitizenSession session;

public ViewApplicationsController(CitizenDashboardView dashboardView) {
this.view = dashboardView;
initLogoutHandler();
initNewApplicationHandler();
initFaqHandler();

dashboardView.getFrame().addWindowListener(new WindowAdapter() {
@Override
public void windowActivated(WindowEvent e) {
session = (CitizenSession) SessionController.getController().getCurrentSession();
initCitizenDetailFields();
initTrackApplicationFields();
}
});
}

public View getDashboardView() {
return view;
}

private void initLogoutHandler() {
view.getLogoutButton().addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
SessionController.getController().endCurrentSession();
RouteController.getController().routeTo(Route.CITIZEN_LOGIN);
}
});
}

private void initNewApplicationHandler() {
view.getApplicationForNewPassportButton().addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
RouteController.getController().routeTo(Route.APPLICATION_FORM);
}
});
}

private void initCitizenDetailFields() {
view.getEmailAddressTextField().setText(session.getCitizen().getEmailAddress());
}

private void initTrackApplicationFields() {
view.getApplicationIdComboBox().removeAllItems();
for (Application application : session.getCitizen().getApplications()) {
view.getApplicationIdComboBox().addItem(application.getApplicationId());
}

view.getApplicationStatus().setText("-");

view.getApplicationIdComboBox().addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
Integer selection = (Integer) view.getApplicationIdComboBox().getSelectedItem();
if (selection == null)
return;

for (Application application : session.getCitizen().getApplications()) {
if (application.getApplicationId() == selection) {
showTrackingForApplication(application);
break;
}
}
}
});
}

private void initFaqHandler() {
view.getRedirectToAppStepsButton().addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
// TODO (mehsheed): Add redirect to FAQ website
}
});
}

private void showTrackingForApplication(Application application) {
view.getApplicationStatus().setText(StringConstants.APPLICATION_STATUS_STRINGS.get(application.getStatus()));
DefaultTableModel model = (DefaultTableModel) view.getApplicationTable().getModel();

for (int i = model.getRowCount() - 1; i >= 0; --i) {
model.removeRow(i);
}

model.addRow(new Object[] { "Application ID", application.getApplicationId() });
model.addRow(new Object[] { "Applicant's Name", application.getApplicant().getName() });
model.addRow(new Object[] { "Application Created On", application.getDateCreated() });
model.addRow(new Object[] { "Appointment Scheduled On", application.getDateOfAppointment() });
model.addRow(new Object[] { "Application Type", application.getType() });
model.addRow(new Object[] { "Birth Address", application.getBirthAddress() });
model.addRow(new Object[] { "Permanent Address", application.getPermanentAddress() });
model.addRow(new Object[] { "Present Address", application.getPresentAddress() });
model.addRow(new Object[] { "Father's Name", application.getNameOfFather() });
model.addRow(new Object[] { "Mother's Name", application.getNameOfMother() });

for (ApplicationQuestion question : ApplicationQuestion.values()) {
model.addRow(new Object[] { StringConstants.APPLICATION_QUESTIONS.get(question),
application.getAnswerForQuestion(question) ? "Yes" : "No" });
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public interface CitizenDatabase {
* application
* @throws IOException if no citizen entry was found for the email address
*/
void saveApplicationIds(String emailAddress, List<String> applicationIds) throws IOException;
void saveApplicationIds(String emailAddress, List<Integer> applicationIds) throws IOException;

/**
* Fetches a list of application IDs belonging to the citizen from the database.
Expand All @@ -243,5 +243,5 @@ public interface CitizenDatabase {
* @return list of unique application IDs
* @throws IOException if no citizen entry was found for the email address
*/
List<String> fetchApplicationIds(String emailAddress) throws IOException;
List<Integer> fetchApplicationIds(String emailAddress) throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,10 @@ public void savePassportIds(String emailAddress, List<Integer> passportIds) thro

@Override
public List<Integer> fetchPassportIds(String emailAddress) throws IOException {
List<String> deserialisedStrings = Arrays.asList(getRow(emailAddress).getString(FIELD_PASSPORT_IDS).split(","));
String rawContent = getRow(emailAddress).getString(FIELD_PASSPORT_IDS);
if (rawContent == null)
return new ArrayList<>();
List<String> deserialisedStrings = Arrays.asList(rawContent.split(","));
List<Integer> pIds = new ArrayList<>();
for (String string : deserialisedStrings) {
pIds.add(Integer.parseInt(string));
Expand All @@ -155,15 +158,27 @@ public List<Integer> fetchPassportIds(String emailAddress) throws IOException {
}

@Override
public void saveApplicationIds(String emailAddress, List<String> applicationIds) throws IOException {
public void saveApplicationIds(String emailAddress, List<Integer> applicationIds) throws IOException {
List<String> serialisedStrings = new ArrayList<>();
for (Integer aId : applicationIds) {
serialisedStrings.add(aId + "");
}

Row row = getRow(emailAddress);
row.put(FIELD_PASSPORT_IDS, String.join(",", applicationIds));
row.put(FIELD_APPLICATION_IDS, String.join(",", serialisedStrings));
table.updateRow(row);

}

@Override
public List<String> fetchApplicationIds(String emailAddress) throws IOException {
return Arrays.asList(getRow(emailAddress).getString(FIELD_APPLICATION_IDS).split(","));
public List<Integer> fetchApplicationIds(String emailAddress) throws IOException {
String rawContent = getRow(emailAddress).getString(FIELD_APPLICATION_IDS);
if (rawContent == null)
return new ArrayList<>();
List<String> deserialisedStrings = Arrays.asList(rawContent.split(","));
List<Integer> aIds = new ArrayList<>();
for (String string : deserialisedStrings) {
aIds.add(Integer.parseInt(string));
}
return aIds;
}
}
Loading

0 comments on commit a15398e

Please sign in to comment.