Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.kmc.inventory.actions;
- import java.util.*;
- import javax.persistence.*;
- import javax.swing.*;
- import org.openxava.actions.*;
- import org.openxava.jpa.*;
- import com.kmc.inventory.lists.*;
- import com.kmc.inventory.models.*;
- public class ItemScanAction extends ViewBaseAction {
- public void execute() throws Exception {
- Inventory inventory = null;
- Employee employee = null;
- String scan = getView().getValueString("scanId");
- String isIssued = getView().getValueString("toIssue");
- /*
- * Item and Employee was not found
- */
- if ( !isItemExists(scan) && !isEmployee(scan) ) {
- addError("not_found", scan);
- }
- /*
- * Item existence
- */
- else if ( isItemExists(scan) ) {
- /*
- * Employee must be scanned first before Item code
- */
- if ( getView().getValueString("employeeId").isEmpty() ) {
- addError("employee_scan_first");
- }
- else if ( !getView().getValueString("employeeId").isEmpty() ){
- /*
- * Scanned item was {available}
- * If the check box was checked, item will be issued to the employee
- * Otherwise, item will be borrowed
- */
- if ( getInventoryStatus(scan) == 0 ) {
- inventory = getInventoryById(scan);
- employee = getEmployeeById(getView().getValueString("employeeId"));
- inventory.setDateBorrowed(new Date());
- inventory.setDateReturned(null);
- if ( isIssued.equalsIgnoreCase("true") ) {
- if ( JOptionPane.showConfirmDialog(null, "Are you sure?", "Issuing to " + getFullName(getView().getValueString("employeeId")), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION ) {
- inventory.setStatus(Status.Issued);
- inventory.setEmployee(employee);
- getView().setValue("itemCategory", inventory.getItemCategory().getDescription());
- getView().setValue("brand", inventory.getBrand().getDescription());
- getView().setValue("itemPhoto", inventory.getItemPhoto());
- getView().setValue("department", employee.getDepartment().getDescription());
- addMessage("item_issued", inventory.getItemName(), getFullName(getView().getValueString("employeeId")));
- }
- }
- else if ( isIssued.equalsIgnoreCase("false") ) {
- inventory.setStatus(Status.Borrowed);
- inventory.setEmployee(employee);
- getView().setValue("itemCategory", inventory.getItemCategory().getDescription());
- getView().setValue("brand", inventory.getBrand().getDescription());
- getView().setValue("itemPhoto", inventory.getItemPhoto());
- getView().setValue("department", employee.getDepartment().getDescription());
- addMessage("item_borrowed", inventory.getItemName(), getFullName(getView().getValueString("employeeId")));
- }
- }
- /*
- * Scanned item was {borrowed}
- * Setting the scanned item back to its available status
- */
- else if ( getInventoryStatus(scan) == 1 ) {
- inventory = getInventoryById(scan);
- employee = getEmployeeById(getView().getValueString("employeeId"));
- inventory.setEmployee(null);
- inventory.setStatus(Status.Available);
- inventory.setDateBorrowed(null);
- inventory.setDateReturned(new Date());
- getView().setValue("itemCategory", inventory.getItemCategory().getDescription());
- getView().setValue("brand", inventory.getBrand().getDescription());
- getView().setValue("itemPhoto", inventory.getItemPhoto());
- getView().setValue("department", employee.getDepartment().getDescription());
- addMessage("item_available", inventory.getItemName(), employee.getFirstName() + " " + employee.getLastName());
- }
- /*
- * Scanned item was {issued}
- * Item was already issued, no transaction will commit
- */
- else if ( getInventoryStatus(scan) == 2 ) {
- inventory = getInventoryById(scan);
- addError("item_issued", inventory.getItemName(), getFullName(getView().getValueString("employeeId")));
- }
- XPersistence.commit();
- }
- }
- else if ( isEmployee(scan) ) {
- getView().setValue("employeeId", scan);
- getView().setValue("borrowedBy", (getEmployeeById(scan).getFirstName() + " " + getEmployeeById(scan).getLastName()));
- }
- }
- /*
- * Check item existence
- * scan = value from scan field
- *
- * Returns true if an item was found, false if item was not present in database
- */
- public static boolean isItemExists( String scan ) {
- Query query = XPersistence.getManager().createQuery(
- "FROM Inventory i " +
- "WHERE i.serviceTag = :service OR i.assetTag = :asset");
- query.setParameter("service", scan);
- query.setParameter("asset", scan);
- try {
- query.getSingleResult();
- return true;
- } catch( NoResultException nre ) {
- System.out.println(nre.toString());
- return false;
- }
- }
- /*
- * Check employee existence
- * scan = value from scan field
- *
- * Returns true if an employee was found, false if employee was not present in database
- */
- private static boolean isEmployee( String scan ) {
- Query query = XPersistence.getManager().createQuery(
- "FROM Employee e " +
- "WHERE e.employeeId = :id");
- query.setParameter("id", scan);
- try {
- query.getSingleResult();
- return true;
- } catch( NoResultException nre ) {
- System.out.println(nre.toString());
- return false;
- }
- }
- /*
- * Accessing Inventory record
- * scan = value from scan field
- *
- * Returns mapped Inventory if Id was found, null if Id was not found
- */
- private static Inventory getInventoryById( String scan ) {
- Query query = XPersistence.getManager().createQuery(
- "FROM Inventory i WHERE " +
- "i.assetTag = :asset OR i.serviceTag = :service");
- query.setParameter("asset", scan);
- query.setParameter("service", scan);
- try {
- return (Inventory) query.getSingleResult();
- } catch( NoResultException nre ) {
- System.out.println(nre.toString());
- nre.printStackTrace();
- return null;
- }
- }
- /*
- * Accessing Employee record
- * scan = value from scan field
- *
- * Returns mapped Employee if Id was found, null if Id was not found
- */
- private static Employee getEmployeeById( String scan ) {
- Query query = XPersistence.getManager().createQuery(
- "FROM Employee e " +
- "WHERE e.employeeId = :id");
- query.setParameter("id", scan);
- try {
- return (Employee) query.getSingleResult();
- } catch( NoResultException nre ) {
- System.out.println(nre.toString());
- return null;
- }
- }
- /*
- * Inventory status
- * id = value from scan field
- *
- * Returns status flag
- * 0 = Available
- * 1 = Borrowed
- * 2 = Issued
- * 3 = Transferred
- */
- private static int getInventoryStatus( String id ) {
- try {
- Query query = XPersistence.getManager().createQuery(
- "SELECT i.status FROM Inventory i " +
- "WHERE i.assetTag = :asset OR i.serviceTag = :service");
- query.setParameter("asset", id);
- query.setParameter("service", id);
- Status status = (Status) query.getSingleResult();
- if ( status.toString().equals("Available") ) {
- return 0;
- }
- else if ( status.toString().equals("Borrowed") ) {
- return 1;
- }
- else if ( status.toString().equals("Issued") ) {
- return 2;
- }
- else if ( status.toString().equals("Transferred") ) {
- return 3;
- }
- else {
- return 0;
- }
- } catch( NoResultException nre ) {
- return 0;
- } catch( Exception e ) {
- return 0;
- }
- }
- /*
- * Getting Employee full name
- * id = value from scan field
- *
- * Returns the full name of an employee, with a space as separator
- */
- private static String getFullName( String id ) {
- Query query = XPersistence.getManager().createQuery(
- "FROM Employee e " +
- "WHERE e.employeeId = :id");
- query.setParameter("id", id);
- try {
- Employee employee = (Employee) query.getSingleResult();
- return employee.getFirstName() + " " + employee.getLastName();
- } catch( NoResultException nre ) {
- System.out.println(nre.toString());
- return null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment