Package com.darkhonor.rage.libs.dataaccess

Examples of com.darkhonor.rage.libs.dataaccess.GradedEventDAO


            // TODO: RAGE-24 - Migrate to the QuestionDAO class
            Question question = em.find(Question.class, selectedQuestion.getId());
            if (question != null)
            {

                GradedEventDAO gradedEventDAO =
                    new GradedEventDAO(emf.createEntityManager());
                try
                {
                    // Remove the Question from any GradedEvents it's current part of
                    int count = gradedEventDAO.removeQuestionFromAllGradedEvents(question);
                    LOGGER.info("Question (" + question.getName() + ") removed from "
                        + count + " Graded Events");
                } catch (Exception ex)
                {
                    LOGGER.warn("(btnRemoveQuestionActionPerformed) Error removing "
                        + "Question (" + question.getName() + ") from all Graded"
                        + " Events");
                } finally
                {
                    gradedEventDAO.closeConnection();
                }
           
                try
                {
                    tx.begin();
View Full Code Here


        if (emf.isOpen())
        {
            LOGGER.debug("Loading GradedEvents");
            //EntityManager em = emf.createEntityManager();
            GradedEventDAO gradedEventDAO =
                    new GradedEventDAO(emf.createEntityManager());
            try
            {
                modGradedEvents.removeAll();
                List<GradedEvent> events =
                        gradedEventDAO.getGradedEventsByCourseAndAssignment();
                LOGGER.debug("Found " + events.size() + " Graded Events");
                int count = 0;
                for (int i = 0; i < events.size(); i++)
                {
                    modGradedEvents.add(events.get(i));
                    count++;
                }
                LOGGER.debug("Loaded " + count + " Graded Events to the model");
            } catch (NoResultException ex)
            {
                JOptionPane.showMessageDialog(this, "WARNING: No Graded Events Loaded",
                        "ERROR", JOptionPane.ERROR_MESSAGE);
                LOGGER.warn("No Graded Events loaded");
            } catch (Exception ex)
            {
                LOGGER.error("Exception Caught: " + ex.getLocalizedMessage());
            } finally
            {
                gradedEventDAO.closeConnection();
            }
            //if (em.isOpen())
            //    em.close();
            btnAddGradedEvent.setEnabled(true);
            btnRemoveGradedEvent.setEnabled(true);
View Full Code Here

    private void lstGradedEventsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lstGradedEventsMouseClicked

        //EntityManager em = emf.createEntityManager();
        CourseDAO courseDAO = new CourseDAO(emf.createEntityManager());
        GradedEventDAO gradedEventDAO = new GradedEventDAO(emf.createEntityManager());
        //EntityTransaction tx = em.getTransaction();

        GradedEvent event = (GradedEvent) lstGradedEvents.getSelectedValue();
        if (event != null)
        {
            txtAssignment.setText(event.getAssignment());
            txtTermYear.setText(event.getTerm());
            txtVersion.setText(event.getVersion());
            chkPartialCredit.setSelected(event.getPartialCredit());
            txtDueDate.setText(event.getDueDate());

            // Get the course for this GradedEvent
            cboCourse.removeAllItems();
            //CriteriaBuilder cb = em.getCriteriaBuilder();
            //CriteriaQuery cq = cb.createQuery(Course.class);
            //Root<Course> courseRoot = cq.from(Course.class);
            //cq.orderBy(cb.asc(courseRoot.get("name")));
            //TypedQuery<Course> courseQuery = em.createQuery(cq);
            //List<Course> courses = courseQuery.getResultList();
            List<Course> courses = courseDAO.getAllCoursesByName();
            for (int i = 0; i < courses.size(); i++)
            {
                cboCourse.addItem(courses.get(i).getName());
                if (courses.get(i).equals(event.getCourse()))
                {
                    cboCourse.setSelectedIndex(i);
                }
            }

            // Get the list of questions for this event
            //GradedEvent gevent = em.find(GradedEvent.class, event.getId());
            GradedEvent gevent = gradedEventDAO.find(event.getId());
           
            modGradedEventQuestions.removeAll();
            for (int i = 0; i < gevent.getQuestions().size(); i++)
            {
                modGradedEventQuestions.add(gevent.getQuestions().get(i));
            }
        }
        //em.close();
        courseDAO.closeConnection();
        gradedEventDAO.closeConnection();
    }//GEN-LAST:event_lstGradedEventsMouseClicked
View Full Code Here

        {
            //EntityManager em = emf.createEntityManager();
            //EntityTransaction tx = em.getTransaction();

            GradedEvent selectedEvent = (GradedEvent) lstGradedEvents.getSelectedValue();
            GradedEventDAO gradedEventDAO = new GradedEventDAO(emf.createEntityManager());
            GradedEvent gevent = gradedEventDAO.find(selectedEvent.getId());
            //GradedEvent gevent = em.find(GradedEvent.class, selectedEvent.getId());

            // We were able to pull the GradedEvent object from the DB...now
            // we need to pull it's list of questions and then remove the selected
            // one from it's list and re-persist
            if (gevent != null)
            {
                Question quest = (Question) lstGradedEventQuestions.getSelectedValue();
                if (gevent.removeQuestion(quest))
                {
                    JOptionPane.showMessageDialog(this, "Question " + quest.getName()
                            + " removed from Graded Event " + gevent.getAssignment(),
                            "Success", JOptionPane.INFORMATION_MESSAGE);
                    modGradedEventQuestions.remove(lstGradedEventQuestions.getSelectedIndex());
                    LOGGER.info("Question " + quest.getName() + " removed from "
                            + "Graded Event.");
                } else
                {
                    JOptionPane.showMessageDialog(this, "ERROR: Question not removed",
                            "Error", JOptionPane.ERROR_MESSAGE);
                    LOGGER.warn("Question not removed.");
                }
                //tx.begin();
                //gevent = em.merge(gevent);
                gevent = gradedEventDAO.update(gevent);
                //tx.commit();
                modGradedEvents.update(gevent);
            }
            gradedEventDAO.closeConnection();
            //em.close();
        } else
        {
            LOGGER.debug("No question or event selected.  Selected Indices:");
            LOGGER.debug(" - lstGradedEvents: " + lstGradedEvents.getSelectedIndex());
View Full Code Here

                LOGGER.debug("Updating list of questions for the GradedEvent");
                GradedEvent gradedEvent = (GradedEvent) lstGradedEvents.getSelectedValue();
                //EntityManager em = emf.createEntityManager();
                //EntityTransaction tx = em.getTransaction();
                LOGGER.debug("Merging GradedEvent with Persistence Context");
                GradedEventDAO gradedEventDAO = new GradedEventDAO(emf.createEntityManager());
                //tx.begin();
                //gradedEvent = em.find(GradedEvent.class, gradedEvent.getId());
                //tx.commit();
                gradedEvent = gradedEventDAO.find(gradedEvent.getId());
                LOGGER.debug("Adding " + gradedEvent.getQuestions().size()
                        + " Questions to GradedEvent Question List Model");
                int actual = 0;
                modGradedEventQuestions.removeAll();
                for (int i = 0; i < gradedEvent.getQuestions().size(); i++)
                {
                    modGradedEventQuestions.add(gradedEvent.getQuestions().get(i));
                    actual++;
                }
                LOGGER.debug("Added " + actual + " Questions to List Model");
                // Update the Graded Event List Model
                modGradedEvents.update(gradedEvent);
                //em.close();
                gradedEventDAO.closeConnection();
            } else
            {
                LOGGER.debug("Questions not added");
            }
        } else
View Full Code Here

            {
                JOptionPane.showMessageDialog(this, "Graded Event Added Successfully",
                        "Success", JOptionPane.INFORMATION_MESSAGE);
                LOGGER.debug("Clearing GradedEvent model to reload values");
                modGradedEvents.removeAll();
                GradedEventDAO gradedEventDAO =
                        new GradedEventDAO(emf.createEntityManager());
                List<GradedEvent> gradedEvents =
                        gradedEventDAO.getGradedEventsByCourseAndAssignment();

                int count = 0;
                for (int i = 0; i < gradedEvents.size(); i++)
                {
                    modGradedEvents.add(gradedEvents.get(i));
                    count++;
                }
                LOGGER.debug("Added " + count + " Graded Events to the model");
                gradedEventDAO.closeConnection();
                LOGGER.info("Graded Event added");
            } else
            {
                LOGGER.info("Graded Event not added");
            }
View Full Code Here

        // Remove GradedEvent from the database
        if (emf != null && emf.isOpen() && lstGradedEvents.getSelectedIndex() >= 0)
        {
            //EntityManager em = emf.createEntityManager();
            GradedEvent selectedEvent = (GradedEvent) lstGradedEvents.getSelectedValue();
            GradedEventDAO gradedEventDAO = new GradedEventDAO(emf.createEntityManager());
            //GradedEvent gradedEvent = em.find(GradedEvent.class, selectedEvent.getId());
            GradedEvent gradedEvent = gradedEventDAO.find(selectedEvent.getId());
            gradedEvent.clearQuestions();
            //EntityTransaction tx = em.getTransaction();
            //tx.begin();
            gradedEvent = gradedEventDAO.update(gradedEvent);
            //gradedEvent = em.merge(gradedEvent);
            gradedEventDAO.delete(gradedEvent.getId());
            //em.remove(gradedEvent);
            //tx.commit();
            //em.close();
            gradedEventDAO.closeConnection();
        }
    }//GEN-LAST:event_btnRemoveGradedEventActionPerformed
View Full Code Here

            throw new IllegalArgumentException("Connection to database is not active");
        } else
        {
            this.emf = emf;
            EntityManager em = this.emf.createEntityManager();
            GradedEventDAO gradedEventDAO = new GradedEventDAO(emf.createEntityManager());
            gradedEvent = gradedEventDAO.find(gradedEvent.getId());
            //this.gradedEvent = em.find(GradedEvent.class, gradedEvent.getId());

            lblGradedEvent.setText(gradedEvent.getCourse().getName() + " ("
                    + gradedEvent.getTerm() + ") " + gradedEvent.getAssignment()
                    + ", Version: " + gradedEvent.getVersion());

            // Generate list of Question Id's to pass to Query
            ArrayList<Long> questionIds = new ArrayList<Long>();
            for (int i = 0; i < this.gradedEvent.getQuestions().size(); i++)
            {
                questionIds.add(this.gradedEvent.getQuestions().get(i).getId());
            }
            LOGGER.debug("Adding Questions to QuestionListModel");
            // Build the Database Query
            // TODO: RAGE-24 - Migrate to the QuestionDAO class
            CriteriaBuilder cb = em.getCriteriaBuilder();
            CriteriaQuery<Question> cq = cb.createQuery(Question.class);
            Root<Question> questionRoot = cq.from(Question.class);
            // Restrict result to those not found in the list of Question Id's
            cq.where(cb.not(questionRoot.get("id").in(questionIds)));
            cq.orderBy(cb.asc(questionRoot.get("name")));
            TypedQuery<Question> questionQuery = em.createQuery(cq);
            try
            {
                LOGGER.debug("Getting list of Questions");
                List<Question> result = questionQuery.getResultList();
                LOGGER.debug("Found " + result.size() + " Questions");

                // Fill the QuestionListModel on the page
                modQuestionList.removeAll();
                int actual = 0;
                for (int i = 0; i < result.size(); i++)
                {
                    Question question = result.get(i);
                    LOGGER.debug("Adding Question: " + question.getName());
                    modQuestionList.add(question);
                    actual++;
                }
                LOGGER.debug("Added " + actual + " Questions");
                okButton.setEnabled(true);
            } catch (NoResultException ex)
            {
                JOptionPane.showMessageDialog(this, "ERROR: No Questions Found",
                        "ERROR", JOptionPane.ERROR_MESSAGE);
                LOGGER.error("No Questions found");
                okButton.setEnabled(false);
            } catch (Exception ex)
            {
                JOptionPane.showMessageDialog(this, "ERROR: There is an error "
                        + "loading questions. ", "ERROR", JOptionPane.ERROR_MESSAGE);
                LOGGER.error("Error adding questions.  Exception: "
                        + ex.getLocalizedMessage());
                okButton.setEnabled(false);
            } finally
            {
                em.close();
                gradedEventDAO.closeConnection();
            }
        }
    }
View Full Code Here

                }
            }
            LOGGER.debug("Added " + actual + " Questions to GradedEvent");
            LOGGER.debug("Saving GradedEvent");
            // TODO: RAGE-69 - Migrate to the GradedEventDAO class
            GradedEventDAO gradedEventDAO = new GradedEventDAO(emf.createEntityManager());
//            EntityTransaction tx = em.getTransaction();
            try
            {
                gradedEventDAO.update(gradedEvent);
//                tx.begin();
//                em.merge(gradedEvent);
//                tx.commit();
            } catch (IllegalStateException ex)
            {
                LOGGER.error("Error saving Graded Event to database.  Exception: "
                        + ex.getLocalizedMessage());
//                tx.rollback();
            } catch (NullPointerException ex)
            {
                LOGGER.error("Error saving Graded Event to database.  Exception: "
                        + ex.getLocalizedMessage());
//                tx.rollback();
            } catch (IllegalArgumentException ex)
            {
                LOGGER.error("Error saving Graded Event to database.  Exception: "
                        + ex.getLocalizedMessage());
//                tx.rollback();
            } finally
            {
                gradedEventDAO.closeConnection();
            }
            em.close();
        }

        doClose(RET_OK);
View Full Code Here

            //Course course = em.find(Course.class, selectedCourse.getId());
            LOGGER.debug("Course Name: " + course.getName());

            LOGGER.debug("Querying the data source to see if a GradedEvent "
                    + "already exists");
            GradedEventDAO gradedEventDAO =
                    new GradedEventDAO(emf.createEntityManager());
//            CriteriaBuilder cb = em.getCriteriaBuilder();
//            CriteriaQuery cq = cb.createQuery(GradedEvent.class);
//            Root<GradedEvent> gradedEventRoot = cq.from(GradedEvent.class);
//            cq.where(cb.and(cb.equal(gradedEventRoot.get("assignment"), txtAssignment.getText().trim()),
//                    cb.equal(gradedEventRoot.get("term"), txtTerm.getText().trim()),
//                    cb.equal(gradedEventRoot.get("course"), course),
//                    cb.equal(gradedEventRoot.get("version"), txtVersion.getText().trim())));
//
//            TypedQuery<GradedEvent> gradedEventQuery = em.createQuery(cq);
//            LOGGER.debug("Query built");
            try
            {
                //gradedEvent = gradedEventQuery.getSingleResult();
                gradedEvent = gradedEventDAO.find(course,
                        txtAssignment.getText().trim(), txtTerm.getText().trim(),
                        txtVersion.getText().trim());
            } catch (IllegalArgumentException ex)
            {
                // Because of the check at the beginning of this method, we should
                // never reach this point.  But just in case...
                LOGGER.warn("Invalid value provided.  Ensure Course, Term, "
                        + "Assignment, and Version are not blank.");
            }
            if (gradedEvent != null)
            {
                JOptionPane.showMessageDialog(this, "ERROR: This Graded "
                        + "Event already exists in the system.", "ERROR",
                        JOptionPane.ERROR_MESSAGE);
                LOGGER.error("This GradedEvent already exists in the database");
                doClose(RET_CANCEL);
            } else
            {
                LOGGER.debug("Creating a new GradedEvent");
                EntityTransaction tx = em.getTransaction();
                gradedEvent = new GradedEvent();
                gradedEvent.setCourse(course);
                gradedEvent.setAssignment(txtAssignment.getText().trim());
                gradedEvent.setTerm(txtTerm.getText().trim());
                gradedEvent.setVersion(txtVersion.getText().trim());
                gradedEvent.setPartialCredit(chkPartialCredit.isSelected());
                try
                {
                    gradedEvent.setDueDate(txtDueDate.getText().trim());
                } catch (IllegalArgumentException ex)
                {
                    LOGGER.warn("Invalid Date Specified.  No Due Date Set");
                }
                LOGGER.debug("Graded Event: " + gradedEvent.toString());
                List<Question> objects = lstQuestions.getSelectedValuesList();
                LOGGER.debug("Adding " + objects.size() + " Questions to GradedEvent");
                int count = 0;
                for (int i = 0; i < objects.size(); i++)
                {
                    // TODO: RAGE-24 - Migrate to the QuestionDAO class. 
                    // Action: Update the Question in the current Persistence Context
                    Question question = objects.get(i);
                    try
                    {
                        question = em.merge(question);
                    } catch (Exception ex)
                    {
                        LOGGER.error("Exception: " + ex.getLocalizedMessage());
                        tx.rollback();
                    }
                    gradedEvent.addQuestion(question);
                    count++;
                }
                LOGGER.debug(count + " Questions added.  Saving to database");
                Long newId;
                try
                {
                    newId = gradedEventDAO.create(gradedEvent);
                    LOGGER.info("Graded Event created with Id: " + newId);
//                tx.begin();
//                em.persist(gradedEvent);
//                tx.commit();
//            } catch (EntityExistsException ex)
//            {
//                LOGGER.error("Graded Event exists in the database previously");
//                LOGGER.error("Exception: " + ex.getLocalizedMessage());
//                em.close();
//                doClose(RET_CANCEL);
//            } catch (IllegalStateException ex)
//            {
//                LOGGER.error("EntityManager has been closed.");
//                LOGGER.error("Exception: " + ex.getLocalizedMessage());
//                em.close();
//                doClose(RET_CANCEL);
//            } catch (IllegalArgumentException ex)
//            {
//                LOGGER.error("Not saving a mapped Entity");
//                LOGGER.error("Exception: " + ex.getLocalizedMessage());
//                em.close();
//                doClose(RET_CANCEL);
//            } catch (TransactionRequiredException ex)
//            {
//                LOGGER.error("Exception: " + ex.getLocalizedMessage());
//                em.close();
//                doClose(RET_CANCEL);
                } catch (Exception ex)
                {
                    LOGGER.error("Exception: " + ex.getLocalizedMessage());
                    em.close();
                    doClose(RET_CANCEL);
                }
               
                LOGGER.debug("Graded Event saved to database");
                txtTerm.setText("");
                txtAssignment.setText("");
                txtVersion.setText("");
                txtDueDate.setText("");
                lstQuestions.setSelectedIndex(-1);
                doClose(RET_OK);
            }

            em.close();
            courseDAO.closeConnection();
            gradedEventDAO.closeConnection();
        }
    }
View Full Code Here

TOP

Related Classes of com.darkhonor.rage.libs.dataaccess.GradedEventDAO

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.