org.teama.IssueListDA.java Source code

Java tutorial

Introduction

Here is the source code for org.teama.IssueListDA.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package org.teama;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.logging.Level;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;

/**
 *
 * @author bamidei
 */
public class IssueListDA {
    private static ArrayList<Issue> issueList;

    static public ArrayList<Issue> getIssueList() {
        if (issueList == null)
            load();
        return issueList;
    }

    static public ArrayList<Issue> getIssueList(final int sortBy) {
        Collections.sort(getIssueList(), new Comparator<Issue>() {
            @Override
            public int compare(Issue o1, Issue o2) {
                int result;
                switch (sortBy) {
                /*Severity Sort */
                case 1:
                    result = o1.getPriorityLabel().compareTo(o2.getPriorityLabel());
                    break;
                /*Assignee Sort */
                case 2:
                    result = o1.getAssignToName().compareTo(o2.getAssignToName());
                    break;

                /*Submitter Sort */
                case 3:
                    result = o1.getSubmitByName().compareTo(o2.getSubmitByName());
                    break;

                /*Default sort by statsu */
                default:
                    result = o1.getStatus().compareTo(o2.getStatus());
                    break;
                }
                return result;
            }
        });
        return issueList;
    }

    static public Issue getIssue(final int id) {
        if (issueList != null) {
            /* Use collectionUtils to find the matching App from the list */
            return (Issue) CollectionUtils.find(issueList, new Predicate() {
                @Override
                public boolean evaluate(Object o) {
                    Issue i = (Issue) o;
                    return i.getIssueId() == id;
                }
            });
        } else {
            return null;
        }
    }

    static public boolean load() {
        boolean res;

        /* Get a new list to populate - even if we had already populated, this 
        will throw away the old and replace with a new 'load'.
        */
        issueList = new ArrayList<Issue>();

        /*Simple get all of the defects query.  We'll sort and filter as
            necessary using java collection operations. */
        String query = "SELECT * FROM BugTracker.defects";

        try {
            Connection connection = DBconnect.getConnection();
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery(query);

            if (rs != null) {
                while (rs.next()) {
                    Issue i;

                    i = new Issue(rs.getInt("request_id"), rs.getInt("app_no"), rs.getString("summary"),
                            rs.getString("descrp"), rs.getInt("priority_no"), rs.getInt("submitter"),
                            rs.getString("created"), rs.getInt("assignee"), rs.getInt("status_no"));
                    issueList.add(i);
                }
            }
            connection.close();
            statement.close();
            res = true;

        } catch (SQLException ex) {
            DBconnect.getLogger().log(Level.SEVERE, "Error reading issue list from DB", ex);
            res = false;
        }

        return res;
    }
}