eriezelagera

Inventory System Scan

Aug 1st, 2013
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.17 KB | None | 0 0
  1. package com.kmc.inventory.actions;
  2.  
  3. import java.util.*;
  4.  
  5. import javax.persistence.*;
  6. import javax.swing.*;
  7.  
  8. import org.openxava.actions.*;
  9. import org.openxava.jpa.*;
  10.  
  11. import com.kmc.inventory.lists.*;
  12. import com.kmc.inventory.models.*;
  13.  
  14. public class ItemScanAction extends ViewBaseAction {
  15.  
  16.     public void execute() throws Exception {
  17.        
  18.         Inventory inventory = null;
  19.         Employee employee = null;
  20.        
  21.         String scan = getView().getValueString("scanId");
  22.         String isIssued = getView().getValueString("toIssue");
  23.        
  24.         /*
  25.          *   Item and Employee was not found
  26.          */
  27.         if ( !isItemExists(scan) && !isEmployee(scan) ) {
  28.             addError("not_found", scan);
  29.         }
  30.        
  31.         /*
  32.          *   Item existence
  33.          */
  34.         else if ( isItemExists(scan) ) {
  35.            
  36.             /*
  37.              *   Employee must be scanned first before Item code
  38.              */
  39.             if ( getView().getValueString("employeeId").isEmpty() ) {
  40.                 addError("employee_scan_first");
  41.             }
  42.             else if ( !getView().getValueString("employeeId").isEmpty() ){
  43.                 /*
  44.                  *   Scanned item was {available}
  45.                  *      If the check box was checked, item will be issued to the employee
  46.                  *      Otherwise, item will be borrowed
  47.                  */
  48.                 if ( getInventoryStatus(scan) == 0 ) {
  49.                     inventory = getInventoryById(scan);
  50.                     employee = getEmployeeById(getView().getValueString("employeeId"));
  51.                    
  52.                     inventory.setDateBorrowed(new Date());
  53.                     inventory.setDateReturned(null);
  54.                    
  55.                     if ( isIssued.equalsIgnoreCase("true") ) {
  56.                         if ( JOptionPane.showConfirmDialog(null, "Are you sure?", "Issuing to " + getFullName(getView().getValueString("employeeId")), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION ) {
  57.                             inventory.setStatus(Status.Issued);
  58.                            
  59.                             inventory.setEmployee(employee);
  60.                             getView().setValue("itemCategory", inventory.getItemCategory().getDescription());
  61.                             getView().setValue("brand", inventory.getBrand().getDescription());
  62.                             getView().setValue("itemPhoto", inventory.getItemPhoto());
  63.                             getView().setValue("department", employee.getDepartment().getDescription());
  64.                            
  65.                             addMessage("item_issued", inventory.getItemName(), getFullName(getView().getValueString("employeeId")));
  66.                         }
  67.                     }
  68.                     else if ( isIssued.equalsIgnoreCase("false") ) {
  69.                         inventory.setStatus(Status.Borrowed);
  70.                        
  71.                         inventory.setEmployee(employee);
  72.                         getView().setValue("itemCategory", inventory.getItemCategory().getDescription());
  73.                         getView().setValue("brand", inventory.getBrand().getDescription());
  74.                         getView().setValue("itemPhoto", inventory.getItemPhoto());
  75.                         getView().setValue("department", employee.getDepartment().getDescription());
  76.                        
  77.                         addMessage("item_borrowed", inventory.getItemName(), getFullName(getView().getValueString("employeeId")));
  78.                     }
  79.                 }
  80.                 /*  
  81.                  *   Scanned item was {borrowed}
  82.                  *      Setting the scanned item back to its available status
  83.                  */
  84.                 else if ( getInventoryStatus(scan) == 1 ) {
  85.                     inventory = getInventoryById(scan);
  86.                     employee = getEmployeeById(getView().getValueString("employeeId"));
  87.                    
  88.                     inventory.setEmployee(null);
  89.                     inventory.setStatus(Status.Available);
  90.                     inventory.setDateBorrowed(null);
  91.                     inventory.setDateReturned(new Date());
  92.                    
  93.                     getView().setValue("itemCategory", inventory.getItemCategory().getDescription());
  94.                     getView().setValue("brand", inventory.getBrand().getDescription());
  95.                     getView().setValue("itemPhoto", inventory.getItemPhoto());
  96.                     getView().setValue("department", employee.getDepartment().getDescription());
  97.                    
  98.                     addMessage("item_available", inventory.getItemName(), employee.getFirstName() + " " + employee.getLastName());
  99.                 }
  100.                 /*
  101.                  *   Scanned item was {issued}
  102.                  *      Item was already issued, no transaction will commit
  103.                  */
  104.                 else if ( getInventoryStatus(scan) == 2 ) {
  105.                     inventory = getInventoryById(scan);
  106.                    
  107.                     addError("item_issued", inventory.getItemName(), getFullName(getView().getValueString("employeeId")));
  108.                 }
  109.                 XPersistence.commit();
  110.             }
  111.         }
  112.         else if ( isEmployee(scan) ) {
  113.             getView().setValue("employeeId", scan);
  114.             getView().setValue("borrowedBy", (getEmployeeById(scan).getFirstName() + " " + getEmployeeById(scan).getLastName()));
  115.         }
  116.     }
  117.    
  118.     /*
  119.      *   Check item existence
  120.      *      scan = value from scan field
  121.      *      
  122.      *   Returns true if an item was found, false if item was not present in database
  123.      */
  124.     public static boolean isItemExists( String scan ) {
  125.        
  126.         Query query = XPersistence.getManager().createQuery(
  127.                 "FROM Inventory i " +
  128.                 "WHERE i.serviceTag = :service OR i.assetTag = :asset");
  129.         query.setParameter("service", scan);
  130.         query.setParameter("asset", scan);
  131.        
  132.         try {
  133.             query.getSingleResult();
  134.             return true;
  135.         } catch( NoResultException nre ) {
  136.             System.out.println(nre.toString());
  137.             return false;
  138.         }
  139.     }
  140.    
  141.     /*
  142.      *   Check employee existence
  143.      *      scan = value from scan field
  144.      *      
  145.      *   Returns true if an employee was found, false if employee was not present in database
  146.      */
  147.     private static boolean isEmployee( String scan ) {
  148.        
  149.         Query query = XPersistence.getManager().createQuery(
  150.                 "FROM Employee e " +
  151.                 "WHERE e.employeeId = :id");
  152.         query.setParameter("id", scan);
  153.        
  154.         try {
  155.             query.getSingleResult();
  156.             return true;
  157.         } catch( NoResultException nre ) {
  158.             System.out.println(nre.toString());
  159.             return false;
  160.         }
  161.     }
  162.    
  163.     /*
  164.      *   Accessing Inventory record
  165.      *      scan = value from scan field
  166.      *      
  167.      *   Returns mapped Inventory if Id was found, null if Id was not found
  168.      */
  169.     private static Inventory getInventoryById( String scan ) {
  170.         Query query = XPersistence.getManager().createQuery(
  171.                 "FROM Inventory i WHERE " +
  172.                 "i.assetTag = :asset OR i.serviceTag = :service");
  173.         query.setParameter("asset", scan);
  174.         query.setParameter("service", scan);
  175.        
  176.         try {
  177.             return (Inventory) query.getSingleResult();
  178.         } catch( NoResultException nre ) {
  179.             System.out.println(nre.toString());
  180.             nre.printStackTrace();
  181.             return null;
  182.         }
  183.        
  184.        
  185.     }
  186.    
  187.     /*
  188.      *   Accessing Employee record
  189.      *      scan = value from scan field
  190.      *      
  191.      *   Returns mapped Employee if Id was found, null if Id was not found
  192.      */
  193.     private static Employee getEmployeeById( String scan ) {
  194.        
  195.         Query query = XPersistence.getManager().createQuery(
  196.                 "FROM Employee e " +
  197.                 "WHERE e.employeeId = :id");
  198.         query.setParameter("id", scan);
  199.        
  200.         try {
  201.             return (Employee) query.getSingleResult();
  202.         } catch( NoResultException nre ) {
  203.             System.out.println(nre.toString());
  204.             return null;
  205.         }
  206.     }
  207.    
  208.     /*
  209.      *   Inventory status
  210.      *      id = value from scan field
  211.      *      
  212.      *   Returns status flag
  213.      *      0 = Available
  214.      *      1 = Borrowed
  215.      *      2 = Issued
  216.      *      3 = Transferred
  217.      */
  218.     private static int getInventoryStatus( String id ) {
  219.        
  220.         try {
  221.             Query query = XPersistence.getManager().createQuery(
  222.                     "SELECT i.status FROM Inventory i " +
  223.                     "WHERE i.assetTag = :asset OR i.serviceTag = :service");
  224.             query.setParameter("asset", id);
  225.             query.setParameter("service", id);
  226.            
  227.             Status status = (Status) query.getSingleResult();
  228.             if ( status.toString().equals("Available") ) {
  229.                 return 0;
  230.             }
  231.             else if ( status.toString().equals("Borrowed") ) {
  232.                 return 1;
  233.             }
  234.             else if ( status.toString().equals("Issued") ) {
  235.                 return 2;
  236.             }
  237.             else if ( status.toString().equals("Transferred") ) {
  238.                 return 3;
  239.             }
  240.             else {
  241.                 return 0;
  242.             }
  243.         } catch( NoResultException nre ) {
  244.             return 0;
  245.         } catch( Exception e ) {
  246.             return 0;
  247.         }
  248.     }
  249.    
  250.     /*
  251.      *   Getting Employee full name
  252.      *      id = value from scan field
  253.      *      
  254.      *   Returns the full name of an employee, with a space as separator
  255.      */
  256.     private static String getFullName( String id ) {
  257.         Query query = XPersistence.getManager().createQuery(
  258.                 "FROM Employee e " +
  259.                 "WHERE e.employeeId = :id");
  260.         query.setParameter("id", id);
  261.        
  262.         try {
  263.             Employee employee = (Employee) query.getSingleResult();
  264.             return employee.getFirstName() + " " + employee.getLastName();
  265.         } catch( NoResultException nre ) {
  266.             System.out.println(nre.toString());
  267.             return null;
  268.         }
  269.     }
  270.    
  271. }
Advertisement
Add Comment
Please, Sign In to add comment