Blackthorn

Report.java

Aug 16th, 2016
718
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.22 KB | None | 0 0
  1. package model;
  2.  
  3. import java.sql.Date;
  4. import java.text.SimpleDateFormat;
  5. import java.util.List;
  6.  
  7. import javax.persistence.EntityManager;
  8. import javax.persistence.EntityManagerFactory;
  9. import javax.persistence.Persistence;
  10.  
  11. public class Report {
  12.  
  13.     private static final int SLOW_LOG_DEFAULT_DURATION = 10;
  14.     private static final String DATE_FORMAT_PATTERN = "d/MM/yyyy";
  15.  
  16.     public static void main(String[] args) throws InterruptedException {
  17.         EntityManagerFactory emf = null;
  18.         EntityManager em = null;
  19.         String format = null;
  20.  
  21.         try {
  22.             emf = Persistence.createEntityManagerFactory("Northwoods");
  23.             em = emf.createEntityManager();
  24.            
  25.             /////////////////////////////////
  26.             //              7a            //
  27.             ///////////////////////////////
  28.             // In the first section of NorthwoodsReport.java, add a new Enrolment entry to the database. Initiate a
  29.             // transaction and instantiate a new Enrolment member.
  30.             // • For the Course Section (CourseSection): write a Hibernate JPA query that retrieves the
  31.             // CourseSection instance where SEC_NUM = 2 and F_ID = 3. Set the course section for the new
  32.             // enrolment to be the CourseSection instance.
  33.             // • Persist the new enrolment member using the appropriate JPA method and commit the transaction.
  34.            
  35.             // Returns something like:
  36.             //
  37.             // -----7a-----
  38.             // Persisted JS570!
  39.             // ------------
  40.             //
  41.            
  42.             {
  43.                 slowLog("\n-----7a-----");
  44.                 CourseSection cs = em.createQuery(""
  45.                         + "SELECT cs FROM CourseSection cs "
  46.                         + "WHERE cs.secNum = :SEC_NUM "
  47.                         + " AND  cs.faculty.fId = :F_ID "
  48.                         , CourseSection.class)
  49.                         .setParameter("SEC_NUM", 2)
  50.                         .setParameter("F_ID", 3)
  51.                         .setMaxResults(1).getSingleResult();
  52.                
  53.                 Student me = new Student();
  54.                 me.setSId("JS" + (int)(Math.random() * 1000));
  55.                
  56.                 me.setSFirst("John");
  57.                 me.setSLast("Steel");
  58.            
  59.                 em.getTransaction().begin();
  60.                 Enrollment myEnrollment = new Enrollment();
  61.                 myEnrollment.setCourseSection(cs);
  62.                 myEnrollment.setStudent(me);
  63.                
  64.                 em.persist(myEnrollment);
  65.                 em.getTransaction().commit();
  66.                 slowLog("Persisted %s!", me.getSId());
  67.                 slowLog("------------\n");
  68.             }
  69.  
  70.             /////////////////////////////////
  71.             //              7b            //
  72.             ///////////////////////////////
  73.             // In the next section of NorthwoodsReport.java, write the JPQL query that retrieves all of the faculties
  74.             // being supervised by Supervisor 4. Print their first name, last name, location room, building code, and
  75.             // building location capacity.
  76.            
  77.             // Returns something like:
  78.             //
  79.             // -----7b-----
  80.             // ID   First      Last       Location   Capacity   Building Code
  81.             // 1    Teresa     Marx       424        1          BUS
  82.             // 3    Colin      Langley    217        2          LIB
  83.             // ------------
  84.             //
  85.            
  86.             {
  87.                 slowLog("\n-----7b-----");
  88.                 List<Faculty> faculty = em.createQuery(""
  89.                         + "SELECT f "
  90.                         + "FROM Faculty f "
  91.                         + "WHERE f.fSuper = :SUPER"
  92.                         , Faculty.class)
  93.                         .setParameter("SUPER", 4)
  94.                         .getResultList();
  95.                
  96.                 format = "%-4s %-10s %-10s %-10s %-10s %s";
  97.                 slowLog(format, "ID", "First", "Last", "Location", "Capacity", "Building Code");
  98.                 for(Faculty f : faculty) {
  99.                     slowLog(format,
  100.                             f.getFId(),
  101.                             f.getFFirst(),
  102.                             f.getFLast(),
  103.                             f.getLocation().getRoom(),
  104.                             f.getLocation().getCapacity(),
  105.                             f.getLocation().getBldgCode());
  106.                 }
  107.                 slowLog("------------\n");
  108.             }
  109.            
  110.             /////////////////////////////////
  111.             //              7c            //
  112.             ///////////////////////////////
  113.             // In the next section of NorthwoodsReport.java, write the JPQL query that retrieves all of the students
  114.             // that have achieved any grade of “A” in any of their courses.
  115.             // • Ensure that no student appears in the list more than once.
  116.             // • To receive full marks you must write a single JPQL join query with your Java application.
  117.             // • Order the list of students in ascending order by surname. In your output, include the student
  118.             // ID, first name, last name, birth date, and the name and rank of their faculty advisor.
  119.            
  120.             // Returns something like:
  121.             //
  122.             // -----7c-----
  123.             // ID         First      Last       Birthday           Advisor              Advisor Rank
  124.             // JO100      Tammy      Jones      14/07/1985         Marx, Teresa         Associate
  125.             // PE100      Jorge      Perez      19/08/1985         Marx, Teresa         Associate
  126.             // ------------
  127.             //
  128.            
  129.             {
  130.                 slowLog("\n-----7c-----");
  131.                 List<Student> students = em.createQuery(""
  132.                         + "SELECT DISTINCT s "
  133.                         + "FROM Enrollment e "
  134.                         + "JOIN e.student s "
  135.                         + "WHERE e.grade = :GRADE "
  136.                         + "ORDER BY s.sLast"
  137.                         , Student.class)
  138.                         .setParameter("GRADE", "A")
  139.                         .getResultList();
  140.                 format = "%-10s %-10s %-10s %-18s %-20s %s";
  141.                
  142.                 slowLog(format, "ID", "First", "Last", "Birthday", "Advisor", "Advisor Rank");
  143.                 for(Student s : students) {
  144.                     slowLog(format,
  145.                             s.getSId(),
  146.                             s.getSFirst(),
  147.                             s.getSLast(),
  148.                             s.getSDob(),
  149.                             s.getFaculty().getFLast() + ", " + s.getFaculty().getFFirst(),
  150.                             s.getFaculty().getFRank());
  151.                 }
  152.                 slowLog("------------\n");
  153.             }
  154.            
  155.             /////////////////////////////////
  156.             //              7d            //
  157.             ///////////////////////////////
  158.             // In the third section of NorthwoodsReport.java, write a JPQL query that lists the Students who reside in
  159.             // Florida State (“FL”) and who are supervised by the faculty member with the surname of “Marx”.
  160.             // • You must use query parameters for the conditions involving state and faculty surname.
  161.             // • Order the list of students first by first name and then by last name.
  162.             // • Output the following attributes for each student: Student ID, first name, last name, and birth
  163.             // date.
  164.            
  165.             // Returns something like:
  166.             //
  167.             // -----7d-----
  168.             // ID         First      Last       Birthday
  169.             // JO100      Tammy      Jones      14/07/1985
  170.             // MA100      John       Marsh      10/10/1982
  171.             // ------------
  172.             //
  173.            
  174.             {
  175.                 slowLog("\n-----7d-----");
  176.                 List<Student> students = em.createQuery(""
  177.                         + "SELECT s "
  178.                         + "FROM Student s "
  179.                         + "JOIN s.faculty f "
  180.                         + "WHERE s.sState = :STATE "
  181.                         + "  AND f.fLast = :NAME "
  182.                         + "ORDER BY s.sLast, s.sFirst"
  183.                         , Student.class)
  184.                         .setParameter("NAME", "Marx")
  185.                         .setParameter("STATE", "FL")
  186.                         .getResultList();
  187.                 format = "%-10s %-10s %-10s %s";
  188.                
  189.                 slowLog(format, "ID", "First", "Last", "Birthday");
  190.                 for (Student s : students) {
  191.                     slowLog(format,
  192.                             s.getSId(),
  193.                             s.getSFirst(),
  194.                             s.getSLast(),
  195.                             s.getSDob());
  196.                 }
  197.                 slowLog("------------\n");
  198.             }
  199.            
  200.         } catch (Exception e) {
  201.             e.printStackTrace();
  202.         } finally {
  203.             if (emf != null) {
  204.                 emf.close();
  205.             }
  206.         }
  207.     }
  208.  
  209.     /**
  210.      * Slow Logger
  211.      * @param message The message you want to print
  212.      * @throws InterruptedException
  213.      */
  214.     private static void slowLog(String message) throws InterruptedException {
  215.         slowLog(message, SLOW_LOG_DEFAULT_DURATION);
  216.     }
  217.    
  218.     /**
  219.      * Slow Logger with Formatting
  220.      * @param format The format string
  221.      * @param args The format objects
  222.      * @throws InterruptedException
  223.      */
  224.     private static void slowLog(String format, Object... args) throws InterruptedException {
  225.         formatDates(args);
  226.         slowLog(String.format(format, args), SLOW_LOG_DEFAULT_DURATION);
  227.     }
  228.    
  229.     /**
  230.      * Slow Logger with duration
  231.      * @param message The message you want printed
  232.      * @param duration The total time you want to take to print it
  233.      * @throws InterruptedException
  234.      */
  235.     public static void slowLog(String message, int duration) throws InterruptedException {
  236.         Thread.sleep(duration/2);
  237.         System.out.println(message);
  238.         Thread.sleep(duration/2);
  239.     }
  240.    
  241.     /**
  242.      * Format Dates
  243.      * Loops over some objects, formating all dates.
  244.      * @param args An array of objects that might include a date
  245.      */
  246.     public static void formatDates(Object... args) {
  247.         SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT_PATTERN);
  248.         for (int i = 0; i < args.length; i++) {
  249.             if (Date.class.isAssignableFrom(args[i].getClass())) {
  250.                 args[i] = dateFormat.format(args[i]);
  251.             }
  252.         }
  253.     }
  254. }
Advertisement
Add Comment
Please, Sign In to add comment