Skip to content

Commit

Permalink
Police Officer Dashboard Controller (#39)
Browse files Browse the repository at this point in the history
* fix : Typo in getMethod

* feat: Created Police Officer Model

* feat: Added Police Officer Session

* feat: Added Police Session

* feat : Added PoliceOfficer Login Handler

* feat: Added PoliceDashboard Controller(Work in progress)

* fix :formatting

* feat: Finish police verification controller

Co-authored-by: Vishwas-Adiga <[email protected]>
  • Loading branch information
mehsheed and Vishwas-Adiga authored Nov 8, 2021
1 parent e632461 commit 33e384a
Show file tree
Hide file tree
Showing 13 changed files with 416 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

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

import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import edu.manipal.icas.simple.models.Citizen;
import edu.manipal.icas.simple.models.PassportOfficer;
import edu.manipal.icas.simple.models.PassportOfficerRole;
import edu.manipal.icas.simple.models.PoliceOfficer;
import edu.manipal.icas.simple.session.Session;
import edu.manipal.icas.simple.session.SessionFactory;
import edu.manipal.icas.simple.session.SessionType;
Expand Down Expand Up @@ -69,20 +71,33 @@ public void actionPerformed(ActionEvent e) {
temppr = PassportOfficerRole.GRANTING;
st = SessionType.GRANTING_OFFICER;
break;

default:
break;
}

if (PassportOfficer.authenticate(officerId, temppr)) {
Session session = SessionFactory.getFactory().getSession(st, idString);
if (SessionController.getController().startSession(session))
RouteController.getController().routeTo(session.getDefaultRoute());
else
showError("An internal error has occurred. Please try again later.");
if (selectedOfficerType == "Police Officer") {
if (PoliceOfficer.authenticate(officerId)) {
st = SessionType.POLICE;
Session session = SessionFactory.getFactory().getSession(st, idString);
if (SessionController.getController().startSession(session))
RouteController.getController().routeTo(session.getDefaultRoute());
else
showError("An internal error has occurred. Please try again later.");

} else
showError("Officer doesn't exist");
} else
showError("Officer doesn't exist");
} else {
if (PassportOfficer.authenticate(officerId, temppr)) {
Session session = SessionFactory.getFactory().getSession(st, idString);
if (SessionController.getController().startSession(session))
RouteController.getController().routeTo(session.getDefaultRoute());
else
showError("An internal error has occurred. Please try again later.");

} else
showError("Officer doesn't exist");
}
}

});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
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 java.util.List;
import java.util.ResourceBundle;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.table.DefaultTableModel;

import org.icepdf.ri.common.SwingController;
import org.icepdf.ri.common.SwingViewBuilder;
import org.icepdf.ri.util.PropertiesManager;

import edu.manipal.icas.simple.models.PoliceOfficer;
import edu.manipal.icas.simple.models.application.AcceptedDocumentType;
import edu.manipal.icas.simple.models.application.Application;
import edu.manipal.icas.simple.models.application.ApplicationStatus;
import edu.manipal.icas.simple.session.PoliceOfficerSession;
import edu.manipal.icas.simple.session.Session;
import edu.manipal.icas.simple.views.PoliceDashboardView;
import edu.manipal.icas.simple.views.View;

public class PoliceDashboardViewController {

private PoliceDashboardView poView;
private Session session;
private Application application;
private PoliceOfficer policeOfficer;

public PoliceDashboardViewController(PoliceDashboardView poView) {
this.poView = poView;
application = null;

poView.getFrame().addWindowListener(new WindowAdapter() {
@Override
public void windowOpened(WindowEvent e) {
session = SessionController.getController().getCurrentSession();
policeOfficer = ((PoliceOfficerSession) session).getOfficer();
initPoliceOfficerFields();
}
});

initLogOutHandlers();
initAppplicationNavigationButtons();

}

private void initPoliceOfficerFields() {
final List<Application> applications = policeOfficer.getApplications();
poView.getOfficerIdLabel().setText("" + policeOfficer.getBadgeId());
poView.getApplicationIdComboBox().removeAllItems();
for (Application application : applications) {
poView.getApplicationIdComboBox().addItem(application.getApplicationId());
}

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

@Override
public void actionPerformed(ActionEvent e) {
application = applications.get(poView.getApplicationIdComboBox().getSelectedIndex());
boolean editable = application.getStatus() == ApplicationStatus.PENDING_ADDRESS_VERIFICATION;
poView.getIncompleteButton().setEnabled(editable);
poView.getAdverseButton().setEnabled(editable);
poView.getClearButton().setEnabled(editable);
}
});

try {
poView.getApplicationIdComboBox().setSelectedIndex(0);
} catch (Exception e) {
// officer has no applications to process
}

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

for (Application app : applications) {
model.addRow(
new Object[] { app.getApplicationId(), app.getApplicant().getName(), app.getPresentAddress() });
}

poView.getAdverseButton().addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
policeOfficer.verifyAddress(application.getApplicant(),
application.getDocument(AcceptedDocumentType.ADDRESS_PROOF));
application.setStatus(ApplicationStatus.ADDRESS_VERIFICATION_ADVERSE);
application.setStatus(ApplicationStatus.REJECTED);
disableButtons();
}
});

poView.getClearButton().addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
policeOfficer.verifyAddress(application.getApplicant(),
application.getDocument(AcceptedDocumentType.ADDRESS_PROOF));
application.setStatus(ApplicationStatus.ADDRESS_VERIFICATION_CLEAR);
application.setStatus(ApplicationStatus.COMPLETED_AND_SUCCESSFUL);
disableButtons();
}
});

poView.getIncompleteButton().addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
policeOfficer.verifyAddress(application.getApplicant(),
application.getDocument(AcceptedDocumentType.ADDRESS_PROOF));
application.setStatus(ApplicationStatus.ADDRESS_VERIFICATION_INCOMPLETE);
application.setStatus(ApplicationStatus.REJECTED);
disableButtons();
}
});

poView.getViewDocumentButton().addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
PropertiesManager properties = new PropertiesManager(System.getProperties(),
ResourceBundle.getBundle(PropertiesManager.DEFAULT_MESSAGE_BUNDLE));
properties.setBoolean("application.viewerpreferences.hidetoolbar", Boolean.TRUE);
SwingController pdfController = new SwingController();
pdfController.openDocument(application.getDocument(AcceptedDocumentType.ADDRESS_PROOF).getPath());

SwingViewBuilder factory = new SwingViewBuilder(pdfController, properties);
JFrame frame = new JFrame();
frame.add(factory.buildViewerPanel());
frame.setSize(1000, 1000);
frame.setVisible(true);
}
});
}

public View getPoliceOfficerView() {
return poView;
}

private void initLogOutHandlers() {
ActionListener logOutListener = new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
SessionController.getController().endCurrentSession();
RouteController.getController().routeTo(Route.OFFICER_LOGIN);
}
};
poView.getLogOutButton().addActionListener(logOutListener);
}

private void initAppplicationNavigationButtons() {
poView.getNextApplicationIdButton()
.addActionListener(new NavigationListener(poView.getApplicationIdComboBox(), true));
poView.getPreviousApplicationIdButton()
.addActionListener(new NavigationListener(poView.getApplicationIdComboBox(), false));

}

private void showError(String message) {
JOptionPane.showMessageDialog(null, message, "Error", JOptionPane.ERROR_MESSAGE);
}

private class NavigationListener implements ActionListener {
private JComboBox<?> comboBox;
private boolean isEventForNavigatingToNext;

NavigationListener(JComboBox<?> comboBox, boolean flag) {
this.comboBox = comboBox;
isEventForNavigatingToNext = flag;
}

@Override
public void actionPerformed(ActionEvent event) {
int newIndex = comboBox.getSelectedIndex() + (isEventForNavigatingToNext ? 1 : -1);
if (newIndex < 0 || newIndex >= comboBox.getItemCount()) {
showError("You've reached the end of the list!");
return;
}
comboBox.setSelectedIndex(newIndex);
}

}

private void disableButtons() {
poView.getAdverseButton().setEnabled(false);
poView.getClearButton().setEnabled(false);
poView.getIncompleteButton().setEnabled(false);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public final class RouteController {
private ApplyForPassportController applyForPassportController;
private ViewApplicationsController viewApplicationsController;
private ProcessApplicationController processApplicationController;
private PoliceDashboardViewController addressVerificationController;

private RouteController() {
currentView = null;
Expand All @@ -40,6 +41,7 @@ private RouteController() {
viewApplicationsController = new ViewApplicationsController(new CitizenDashboardViewImpl());
processApplicationController = new ProcessApplicationController(new BiometricOfficerViewImpl(),
new VerificationOfficerDashboardViewImpl(), new PassportGrantingOfficerDashboardViewImpl());
addressVerificationController = new PoliceDashboardViewController(new PoliceDashboardViewImpl());
}

/**
Expand Down Expand Up @@ -90,7 +92,7 @@ public void routeTo(Route route) {
displayView(processApplicationController.getBiometricOfficerView());
break;
case POLICE_DASHBOARD:
displayView(new PoliceDashboardViewImpl());
displayView(addressVerificationController.getPoliceOfficerView());
break;
case VERIFICATION_DASHBOARD:
displayView(processApplicationController.getVerificationOfficerView());
Expand Down
13 changes: 13 additions & 0 deletions SIMPLE/src/edu/manipal/icas/simple/databases/PoliceDatabase.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package edu.manipal.icas.simple.databases;

import java.io.IOException;
import java.util.List;

/**
* Interface that defines operations to be implemented in an API that persists
* data about police officers in the system.
Expand All @@ -21,4 +24,14 @@ public interface PoliceDatabase {
* @return {@code true} if the entry was found, {@code false} otherwise
*/
Boolean policeExists(Integer badgeId);

/**
* Fetches a list of applications that a given officer is responsible for
* handling.
*
* @param officerId ID of the officer
* @return list of applications
* @throws IOException if no passport officer was found for the passed ID
*/
List<Integer> fetchApplications(Integer officerId) throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.util.ArrayList;
import java.util.List;

import com.healthmarketscience.jackcess.CursorBuilder;
import com.healthmarketscience.jackcess.Row;
import com.healthmarketscience.jackcess.Table;

Expand Down Expand Up @@ -43,9 +42,9 @@ public PassportOfficerRole fetchOfficerRole(Integer officerId) throws IOExceptio
public List<Integer> fetchApplications(Integer officerId) throws IOException {
List<Integer> applicationIds = new ArrayList<>();

Table citizensTable = MsAccessApplicationDatabase.getDatabase().table;
Table applicationsTable = MsAccessApplicationDatabase.getDatabase().table;
Row row = null;
while((row = citizensTable.getNextRow()) != null) {
while((row = applicationsTable.getNextRow()) != null) {
try {
String applicantId = row.getString(MsAccessApplicationDatabase.FIELD_APPLICANT_ID);
Integer officeId = MsAccessCitizenDatabase.getDatabase().fetchPassportOfficeId(applicantId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package edu.manipal.icas.simple.impl.databases;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.healthmarketscience.jackcess.Row;
import com.healthmarketscience.jackcess.Table;

import edu.manipal.icas.simple.databases.PoliceDatabase;

/**
* Concrete class that implements persistence API operations from
* {@link PoliceDatabase}
*
*
* @author Vishwas Adiga ([email protected])
*
*/
Expand All @@ -16,7 +23,7 @@ private MsAccessPoliceDatabase() {
super("Police");
}

public static MsAccessPoliceDatabase geDatabase() {
public static MsAccessPoliceDatabase getDatabase() {
return DATABASE;
}

Expand All @@ -25,4 +32,25 @@ public Boolean policeExists(Integer badgeId) {
return rowExists(badgeId);
}

@Override
public List<Integer> fetchApplications(Integer badgeId) throws IOException {
List<Integer> applicationIds = new ArrayList<>();

Table applicationsTable = MsAccessApplicationDatabase.getDatabase().table;
Row row = null;
while ((row = applicationsTable.getNextRow()) != null) {
try {
String applicantId = row.getString(MsAccessApplicationDatabase.FIELD_APPLICANT_ID);
Integer rowBadgeId = MsAccessCitizenDatabase.getDatabase().fetchPoliceBadgeId(applicantId);
if (rowBadgeId == badgeId) {
applicationIds.add(row.getInt(MsAccessApplicationDatabase.FIELD_ID));
}
} catch (Exception e) {
e.printStackTrace();
continue;
}
}
return applicationIds;
}

}
Loading

0 comments on commit 33e384a

Please sign in to comment.