org.teama.StatusListDA.java Source code

Java tutorial

Introduction

Here is the source code for org.teama.StatusListDA.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.logging.Level;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;

/**
 *
 * @author bamidei
 */
public class StatusListDA {
    static private ArrayList<Status> statusList = null;

    static public ArrayList<Status> getStatusList() {
        if (statusList == null)
            load();
        return statusList;
    }

    static public Status getStatus(final int id) {
        if (statusList == null)
            load();

        if (statusList != null) {
            /* Use collectionUtils to find the matching App from the list */
            return (Status) CollectionUtils.find(statusList, new Predicate() {
                @Override
                public boolean evaluate(Object o) {
                    Status s = (Status) o;
                    return s.getStatusId() == 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'.
        */
        statusList = new ArrayList<Status>();

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

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

            if (rs != null) {
                while (rs.next()) {
                    Status s;

                    s = new Status(rs.getInt("status_id"), rs.getString("status"));
                    statusList.add(s);
                }
            }
            connection.close();
            statement.close();
            res = true;

        } catch (SQLException ex) {
            DBconnect.getLogger().log(Level.SEVERE, "Error Reading status list from database", ex);
            res = false;
        }

        return res;
    }
}