org.teama.UserListDA.java Source code

Java tutorial

Introduction

Here is the source code for org.teama.UserListDA.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 UserListDA {
    static private ArrayList<User> userList = null;

    static public ArrayList<User> getUserList() {
        if (userList == null)
            load();
        return userList;
    }

    static public User getUser(final int id) {
        if (userList == null)
            load();

        if (userList != null) {
            /* Use collectionUtils to find the matching App from the list */
            return (User) CollectionUtils.find(userList, new Predicate() {
                @Override
                public boolean evaluate(Object o) {
                    User u = (User) o;
                    return u.getIdUser() == id;
                }
            });
        } else {
            return null;
        }
    }

    static public User getUser(final String email) {
        if (userList == null)
            load();

        if (userList != null) {
            /* Use collectionUtils to find the matching App from the list */
            return (User) CollectionUtils.find(userList, new Predicate() {
                @Override
                public boolean evaluate(Object o) {
                    User u = (User) o;
                    return u.getEmailAddress().equals(email);
                }
            });
        } 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'.
        */
        userList = new ArrayList<User>();

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

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

            if (rs != null) {
                while (rs.next()) {
                    User u = new User(rs.getInt("idUser"), rs.getString("emailAddress"), rs.getString("lastName"),
                            rs.getString("firstName"), rs.getInt("sortPreference"));
                    userList.add(u);
                }
            }
            connection.close();
            statement.close();
            res = true;

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

        return res;
    }
}